-
Notifications
You must be signed in to change notification settings - Fork 4
Use latest version of GitHub Actions at seeding time. #33
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
Merged
+127
−24
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c912122
Use latest version of GitHub Actions at seeding time.
gouttegd e023cba
Better error handling when querying the GitHub API.
gouttegd f6d0505
Merge branch 'main' into update-checkout
gouttegd 1eaf2b6
Add comment for the mhausenblas/mkdocs-deploy-gh-pages action.
gouttegd 9dd2ec0
Cache negative results.
gouttegd ffea05c
Fix parameter name mismatch.
gouttegd 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| # odkcore - Ontology Development Kit Core | ||
| # Copyright © 2026 ODK Developers | ||
| # | ||
| # This file is part of the ODK Core project and distributed under the | ||
| # terms of a 3-clause BSD license. See the LICENSE file in that project | ||
| # for the detailed conditions. | ||
|
|
||
| from time import sleep | ||
| from typing import Any, Dict, Set, Tuple | ||
|
|
||
| import requests | ||
| from requests.exceptions import RequestException | ||
|
|
||
| from .download import RETRIABLE_HTTP_ERRORS | ||
|
|
||
|
|
||
| class GitHubHelper(object): | ||
| """Helper class to interact with the GitHub API. | ||
|
|
||
| For now, the only purpose of this class is to automatically obtain | ||
| the commit ID of the latest release of a GitHub Action. It may be | ||
| expanded for other purposes in the future. | ||
| """ | ||
|
|
||
| cache: Dict[str, Tuple[str, str]] | ||
| failure_cache: Set[str] | ||
|
|
||
| def __init__(self): | ||
| self.cache = {} | ||
| self.failure_cache = set() | ||
|
|
||
| def get_latest_release_sha(self, name: str, default: str) -> str: | ||
| """Gets the commit ID for the latest release of a GitHub project. | ||
|
|
||
| :param name: The name of a GitHub project, in `owner/repo` form. | ||
| :param default: The default tag to fallback to if we can't get the | ||
| required information from GitHub. | ||
| :returns: A string of the form `XXXX # TAG`, where `XXXX` is the | ||
| commit ID of the latest release and `TAG` is the | ||
| corresponding tag name; or just the value of the `default` | ||
| parameter if the latest commit ID could not be obtained. | ||
| """ | ||
| latest = self.get_latest_release(name) | ||
| if latest: | ||
|
gouttegd marked this conversation as resolved.
|
||
| return f"{latest[1]} # {latest[0]}" | ||
| return default | ||
|
|
||
| def get_latest_release(self, name: str) -> Tuple[str, str] | None: | ||
| """Gets the tag name and commit ID for the latest release of a GitHub project. | ||
|
|
||
| :param name: The name of a GitHub project, in `owner/repo` form. | ||
| :returns: A tuple (TAG,XXXX), where `TAG` is the tag of the | ||
| latest release and `XXXX` is the corresponding commit ID; or | ||
| None if we could not obtain the information from GitHub. | ||
| """ | ||
| cached = self.cache.get(name) | ||
| if cached or name in self.failure_cache: | ||
| return cached | ||
|
|
||
| try: | ||
| latest_release = self._query_github_api(f"repos/{name}/releases/latest") | ||
| tagname = latest_release["tag_name"] | ||
|
|
||
| release_ref = self._query_github_api(f"repos/{name}/git/ref/tags/{tagname}") | ||
| sha = release_ref["object"]["sha"] | ||
| if release_ref["object"]["type"] != "commit": | ||
| release_tag = self._query_github_api(f"repos/{name}/git/tags/{sha}") | ||
| sha = release_tag["object"]["sha"] | ||
|
|
||
| self.cache[name] = (tagname, sha) | ||
| return (tagname, sha) | ||
| except (KeyError, RequestException): | ||
| # We don't really care about what went wrong exactly (e.g. | ||
| # network issue or unexpected JSON content). | ||
| self.failure_cache.add(name) | ||
| return None | ||
|
|
||
| def _query_github_api(self, endpoint: str, max_retry: int = 4) -> Dict[str, Any]: | ||
| """Sends a query to the GitHub API and returns the JSON response.""" | ||
| headers = { | ||
| "Accept": "application/vnd.github+json", | ||
| "X-GitHub-Api-Version": "2026-03-10", | ||
| } | ||
| n_try = 0 | ||
| while True: | ||
| response = requests.get( | ||
| f"https://api.github.com/{endpoint}", timeout=5, headers=headers | ||
| ) | ||
| if response.status_code == 200: | ||
| return response.json() | ||
| elif response.status_code in RETRIABLE_HTTP_ERRORS and n_try < max_retry: | ||
| n_try += 1 | ||
| sleep(1) | ||
| else: | ||
| response.raise_for_status() | ||
| # We could get there upon receiving a non-error HTTP | ||
| # status (e.g. 203, 204) | ||
| raise RequestException(f"Unexpected status: {response.status_code}") | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.