From 03a83d82782f251a35771a8b221468eeb74ea029 Mon Sep 17 00:00:00 2001 From: Dusk Banks Date: Tue, 22 Feb 2022 18:21:01 -0800 Subject: [PATCH] github: full `use_commit_name` support --- docs/usage.rst | 2 ++ nvchecker_source/github.py | 49 ++++++++++++++++++++++++++++++++------ tests/test_github.py | 8 +++++++ 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index 60f3cc4..6fb7156 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -397,6 +397,8 @@ use_commit_name Set this to ``true`` to append a plus and the commit name to the version, e.g. ``20130701.012212+e1457aadd30f53f4d50d6c4828d517355c09b8ae``. + If this isn't showing up, provide a token so it can use the v4 GraphQL API. + query When ``use_latest_tag`` is ``true``, this sets a query for the tag. The exact matching method is not documented by GitHub. diff --git a/nvchecker_source/github.py b/nvchecker_source/github.py index f8e4fa1..06e2f07 100644 --- a/nvchecker_source/github.py +++ b/nvchecker_source/github.py @@ -98,6 +98,20 @@ query latestTag( } ''' +QUERY_LATEST_RELEASE = ''' +query latestRelease( + $owner: String!, $name: String!, + $includeCommitName: Boolean = false, +) { + repository(owner: $owner, name: $name) { + latestRelease { + tagName + ... @include(if: $includeCommitName) { tagCommit { oid } } + } + } +} +''' + async def get_version_real( name: str, conf: Entry, *, cache: AsyncCache, keymanager: KeyManager, @@ -137,14 +151,35 @@ async def get_version_real( ) return ref elif conf.get('use_latest_release', False): - data = await query_rest( - cache = cache, - token = token, - url = GITHUB_LATEST_RELEASE % repo, - ) - if 'tag_name' not in data: + tag = None + if token: + owner, reponame = repo.split('/') + j = await query_graphql( + cache = cache, + token = token, + query = QUERY_LATEST_RELEASE, + variables = { + 'owner': owner, + 'name': reponame, + 'includeCommitName': use_commit_name, + }, + ) + release = j['data']['repository']['latestRelease'] + if release is not None: + tag = add_commit_name( + release['tagName'], + release['tagCommit']['oid'] if use_commit_name else None, + ) + else: + data = await query_rest( + cache = cache, + token = token, + url = GITHUB_LATEST_RELEASE % repo, + ) + if 'tag_name' in data: + tag = data['tag_name'] + if tag is None: raise GetVersionError('No release found in upstream repository.') - tag = data['tag_name'] return tag elif conf.get('use_max_tag', False): data = await query_rest( diff --git a/tests/test_github.py b/tests/test_github.py index 2c9b3e6..7b30c0b 100644 --- a/tests/test_github.py +++ b/tests/test_github.py @@ -35,6 +35,14 @@ async def test_github_latest_release(get_version): "use_latest_release": True, }) == "release3" +async def test_github_latest_release_commit_name(get_version): + assert await get_version("example", { + "source": "github", + "github": "harry-sanabria/ReleaseTestRepo", + "use_latest_release": True, + "use_commit_name": True, + }) == "release3+2b3cdf6134b07ae6ac77f11b586dc1ae6d1521db" + async def test_github_max_tag(get_version): assert await get_version("example", { "source": "github",