-
Notifications
You must be signed in to change notification settings - Fork 907
[Tasks] Fix error handling for deleted repositories in GitHub move detection #3248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
saksham23467
wants to merge
2
commits into
chaoss:main
Choose a base branch
from
saksham23467:fix/repo-move-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,17 +46,32 @@ def extract_owner_and_repo_from_endpoint(key_auth, url, logger): | |
|
||
def ping_github_for_repo_move(session, key_auth, repo, logger,collection_hook='core'): | ||
|
||
owner, name = get_owner_repo(repo.repo_git) | ||
url = f"https://api.github.com/repos/{owner}/{name}" | ||
try: | ||
owner, name = get_owner_repo(repo.repo_git) | ||
url = f"https://api.github.com/repos/{owner}/{name}" | ||
except Exception as e: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where possible and appropriate, please try to catch a specific exception class instead of a bare Exception. |
||
logger.error(f"Failed to parse repo URL {repo.repo_git}: {str(e)}") | ||
raise Exception(f"Invalid repository URL format: {repo.repo_git}") | ||
|
||
attempts = 0 | ||
while attempts < 10: | ||
response_from_gh = hit_api(key_auth, url, logger) | ||
try: | ||
response_from_gh = hit_api(key_auth, url, logger) | ||
|
||
if response_from_gh and response_from_gh.status_code != 404: | ||
break | ||
|
||
if response_from_gh and response_from_gh.status_code != 404: | ||
break | ||
attempts += 1 | ||
except Exception as e: | ||
logger.warning(f"API call attempt {attempts + 1} failed for {url}: {str(e)}") | ||
attempts += 1 | ||
if attempts >= 10: | ||
raise Exception(f"Failed to get API response after {attempts} attempts: {str(e)}") | ||
|
||
attempts += 1 | ||
# Validate response | ||
if not response_from_gh: | ||
logger.error(f"No response received from GitHub API for {url}") | ||
raise Exception(f"No response from GitHub API for {url}") | ||
|
||
#Update Url and retry if 301 | ||
#301 moved permanently | ||
|
@@ -79,9 +94,10 @@ def ping_github_for_repo_move(session, key_auth, repo, logger,collection_hook='c | |
|
||
update_repo_with_dict(repo, repo_update_dict, logger) | ||
|
||
logger.info(f"Repository {repo.repo_git} has moved to {repo_update_dict['repo_git']}. Updated repository URL and resetting collection.") | ||
raise Exception("ERROR: Repo has moved! Resetting Collection!") | ||
|
||
#Mark as ignore if 404 | ||
#Mark as ignore if 404 (repository deleted) | ||
if response_from_gh.status_code == 404: | ||
repo_update_dict = { | ||
'repo_git': repo.repo_git, | ||
|
@@ -113,14 +129,15 @@ def ping_github_for_repo_move(session, key_auth, repo, logger,collection_hook='c | |
collectionRecord.ml_task_id = None | ||
collectionRecord.ml_data_last_collected = datetime.today().strftime('%Y-%m-%dT%H:%M:%SZ') | ||
|
||
|
||
session.commit() | ||
raise Exception("ERROR: Repo has moved, and there is no redirection! 404 returned, not 301. Resetting Collection!") | ||
|
||
logger.warning(f"Repository {repo.repo_git} returned 404 (deleted). Marked as IGNORE and collection stopped.") | ||
return # Return gracefully instead of raising exception | ||
|
||
|
||
if attempts >= 10: | ||
logger.error(f"Could not check if repo moved because the api timed out 10 times. Url: {url}") | ||
raise Exception(f"ERROR: Could not get api response for repo: {url}") | ||
raise Exception(f"ERROR: Could not get api response for repo: {url} after {attempts} attempts") | ||
|
||
#skip if not 404 | ||
logger.info(f"Repo found at url: {url}") | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like the Clones metric API you implemented for #3247 got included in this PR.
Feature implementations and bugfixes for existing infrastructure should be made into separate PRs. Please rebase and update the PR to only include the error handling changes.