Skip to content

Commit

Permalink
Merge pull request #367 from bellkev/master
Browse files Browse the repository at this point in the history
Handle repo-specific exceptions when checking links
  • Loading branch information
jicowan authored Aug 29, 2023
2 parents d0c4146 + 14566ae commit 34d3b94
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
39 changes: 25 additions & 14 deletions .github/actions/linkbot/linkbot/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pathlib

from github.GithubException import GithubException, RateLimitExceededException

from .checks import check_failure_for_stats
from .config import LinkBotConfig
from .links import github_links_in_files, github_links_in_strs
Expand All @@ -14,17 +16,26 @@
bot_messages = gh.get_open_issue_messages(conf.repo_url, conf.gh_user)
existing_links = github_links_in_strs(bot_messages)
for link in all_links:
if link in existing_links:
print(f'Issue already exists for {link}, skipping.')
continue
print(f'Fetching data for {link}')
stats = gh.get_repo_stats(link)
failure = check_failure_for_stats(stats, conf.max_days_old)
if not failure:
continue
print(f'Check failed for {link}: {debug_message(stats, failure)}')
if conf.dry_run:
print('Dry run, not creating issue.')
continue
issue = gh.create_issue(conf.repo_url, render_title(stats, failure), render_body(stats, failure))
print(f'Created issue {issue.number} for {link}')
try:
if link in existing_links:
print(f'Issue already exists for {link}, skipping.')
continue
print(f'Fetching data for {link}')
stats = gh.get_repo_stats(link)
failure = check_failure_for_stats(stats, conf.max_days_old)
if not failure:
continue
print(f'Check failed for {link}: {debug_message(stats, failure)}')
if conf.dry_run:
print('Dry run, not creating issue.')
continue
issue = gh.create_issue(conf.repo_url, render_title(stats, failure), render_body(stats, failure))
print(f'Created issue {issue.number} for {link}')
except RateLimitExceededException as e:
# Do not continue in the face of rate limit exceptions
raise e
except GithubException as e:
# Sometimes repo-specific exceptions occur, such as org-level IP allowlists blocking API calls
# Proceed to next issues in such cases
print('WARNING: Encountered unexpected GitHub exception:', e)
continue
3 changes: 2 additions & 1 deletion .github/actions/linkbot/linkbot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def __init__(self):
self.dry_run = os.environ.get('LINKBOT_DRY_RUN', 'false').lower() == 'true'
# GH login info used for creating issues
self.gh_user = os.environ['LINKBOT_GH_USER']
self.gh_token = os.environ['LINKBOT_GH_TOKEN']
# Allow unauthenticated use, handy for e.g. testing rate limits
self.gh_token = os.environ.get('LINKBOT_GH_TOKEN')
except KeyError as e:
print('Error: %s is a required config value' % e)
exit(1)
8 changes: 6 additions & 2 deletions .github/actions/linkbot/linkbot/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

class Client:

def __init__(self, token):
self._pygh = Github(auth=Auth.Token(token))
def __init__(self, token=None):
if token:
self._pygh = Github(auth=Auth.Token(token))
else:
print('WARNING: Making unauthenticated calls to GitHub API...')
self._pygh = Github()

@staticmethod
def repo_full_name_from_url(url):
Expand Down

0 comments on commit 34d3b94

Please sign in to comment.