Build GitHub URLs for tags correctly

When building the GitHub web url for a change, the sha of a tag was
treated like a commit sha, which leads in an invalid GitHub URL
(resulting in a 404).
To avoid this, we could use the tag information of the change and
build a tag url instead which points to the corresponding release/tag
page instead of the commit.

Change-Id: I4bf8aae0adcc6b14b43a8bbaa3fb9e891b511af4
This commit is contained in:
Felix Schmidt 2019-10-16 10:28:24 +02:00
parent 0c353ad9ac
commit 0f367b22a6
No known key found for this signature in database
GPG Key ID: E95717A102DD3030
2 changed files with 16 additions and 4 deletions

View File

@ -215,6 +215,9 @@ class TestGithubDriver(ZuulTestCase):
self.assertEqual('refs/tags/newtag', build_params['zuul']['ref'])
self.assertFalse('oldrev' in build_params['zuul'])
self.assertEqual(sha, build_params['zuul']['newrev'])
self.assertEqual(
'https://github.com/org/project/releases/tag/newtag',
build_params['zuul']['change_url'])
self.executor_server.hold_jobs_in_build = False
self.executor_server.release()
self.waitUntilSettled()
@ -241,6 +244,9 @@ class TestGithubDriver(ZuulTestCase):
self.assertEqual('refs/heads/master', build_params['zuul']['ref'])
self.assertFalse('oldrev' in build_params['zuul'])
self.assertEqual(new_sha, build_params['zuul']['newrev'])
self.assertEqual(
'https://github.com/org/project/commit/%s' % new_sha,
build_params['zuul']['change_url'])
self.executor_server.hold_jobs_in_build = False
self.executor_server.release()

View File

@ -1061,9 +1061,11 @@ class GithubConnection(BaseConnection):
change.is_current_patchset = (change.pr.get('head').get('sha') ==
event.patch_number)
else:
tag = None
if event.ref and event.ref.startswith('refs/tags/'):
change = Tag(project)
change.tag = event.ref[len('refs/tags/'):]
tag = event.ref[len('refs/tags/'):]
change.tag = tag
elif event.ref and event.ref.startswith('refs/heads/'):
change = Branch(project)
change.branch = event.ref[len('refs/heads/'):]
@ -1072,7 +1074,9 @@ class GithubConnection(BaseConnection):
change.ref = event.ref
change.oldrev = event.oldrev
change.newrev = event.newrev
change.url = self.getGitwebUrl(project, sha=event.newrev)
# In case we have a tag, we build the url pointing to this
# tag/release on GitHub.
change.url = self.getGitwebUrl(project, sha=event.newrev, tag=tag)
change.source_event = event
if hasattr(event, 'commits'):
change.files = self.getPushedFileNames(event)
@ -1278,9 +1282,11 @@ class GithubConnection(BaseConnection):
return 'https://%s/%s' % (self.server, project.name)
def getGitwebUrl(self, project, sha=None):
def getGitwebUrl(self, project, sha=None, tag=None):
url = 'https://%s/%s' % (self.server, project)
if sha is not None:
if tag is not None:
url += '/releases/tag/%s' % tag
elif sha is not None:
url += '/commit/%s' % sha
return url