-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split and overhaul github.util
Introduce dedicated module for managing limits of GitHub-API. Also, create new module for GitHub-Release-specific code.
- Loading branch information
Showing
6 changed files
with
111 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,20 @@ | ||
''' | ||
limits for github-api | ||
stolen from: https://github.com/dead-claudia/github-limits | ||
limits refer to amount of codepoints (tested empirically for some samples). | ||
''' | ||
|
||
issue_body = 65536 | ||
issue_title = 256 | ||
pullrequest_body = 262144 | ||
release_body = 125000 | ||
|
||
|
||
def fits( | ||
value: str | bytes, | ||
/, | ||
limit: int, | ||
) -> bool: | ||
return len(value) <= limit |
This file contains 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,55 @@ | ||
''' | ||
utils wrapping github3.py's relase-API | ||
''' | ||
|
||
import github3.repos | ||
import github3.repos.release | ||
|
||
import github.limits | ||
|
||
|
||
def body_or_replacement( | ||
body: str, | ||
replacement: str='body was too large (limit: {limit} / actual: {actual})', | ||
limit: int=github.limits.release_body, | ||
) -> tuple[str, bool]: | ||
''' | ||
convenience function that will check whether given body is short enough to be accepted | ||
by GitHub's API. If so, passed body will be returned as first element of returned tuple, else | ||
replacement value. | ||
The second value of returned tuple will indicate whether original body was returned. Callers | ||
may use this hint to perform a mitigation. | ||
limit may be overwritten (but this is not recommended; see github.limits for more details). | ||
''' | ||
if github.limits.fits( | ||
body, | ||
limit=limit, | ||
): | ||
return body, True | ||
|
||
return replacement.format( | ||
limit=limit, | ||
actual=len(body), | ||
), False | ||
|
||
|
||
def find_draft_release( | ||
repository: github3.repos.Repository, | ||
name: str, | ||
) -> github3.repos.release.Release | None: | ||
''' | ||
finds the given draft-release. For draft-releases, lookup has to be done that way, as | ||
there is no way of directly retrieving a draft-release (as those do not yet have a tag) | ||
''' | ||
# at some point in time, github.com would return http-500 if there were more than 1020 | ||
# releases; as draft-releases are typically not too old (and such great numbers of releases | ||
# are uncommon), this should be okay to hardcode. Todo: check whether this limit is still | ||
# valid. | ||
max_releases = 1020 | ||
for release in repository.releases(number=max_releases): | ||
if not release.draft: | ||
continue | ||
if release.name == name: | ||
return release |
This file contains 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