github: full use_commit_name support

This commit is contained in:
Dusk Banks 2022-02-22 18:21:01 -08:00
parent cdd31a01e4
commit 03a83d8278
3 changed files with 52 additions and 7 deletions

View file

@ -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.

View file

@ -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(

View file

@ -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",