Pagure - Support for branch creation/deletion

Thanks to:
- https://pagure.io/pagure/pull-request/4625 event branch creation
- https://pagure.io/pagure/pull-request/4626 oldrev for branch update

This patch:
- Handles git.branch.creation and git.branch.deletion events.
- Adds support for old_commit event attribute.

It also removes unexisting stop_commit event atribute handling,
it is only end_commit.

Change-Id: I10423b654df48436bb0ad11c2043c2d67cac7780
This commit is contained in:
Fabien Boucher 2019-09-26 15:46:09 +02:00
parent 970b2ecd0e
commit f1cc190e70
3 changed files with 84 additions and 38 deletions

View File

@ -1453,7 +1453,8 @@ class FakePagureConnection(pagureconnection.PagureConnection):
'msg': {
'project_fullname': project,
'branch': 'master',
'stop_commit': headsha,
'end_commit': headsha,
'old_commit': '1' * 40,
},
'msg_id': str(uuid.uuid4()),
'timestamp': 1427459070,
@ -1475,6 +1476,20 @@ class FakePagureConnection(pagureconnection.PagureConnection):
}
return (name, data)
def getGitBranchEvent(self, project, branch, type, rev):
name = 'pg_push'
data = {
'msg': {
'project_fullname': project,
'branch': branch,
'rev': rev,
},
'msg_id': str(uuid.uuid4()),
'timestamp': 1427459070,
'topic': 'git.branch.%s' % type,
}
return (name, data)
def setZuulWebPort(self, port):
self.zuul_web_port = port

View File

@ -184,7 +184,8 @@ class TestPagureDriver(ZuulTestCase):
def test_ref_updated(self):
event = self.fake_pagure.getGitReceiveEvent('org/project')
expected_newrev = event[1]['msg']['stop_commit']
expected_newrev = event[1]['msg']['end_commit']
expected_oldrev = event[1]['msg']['old_commit']
self.fake_pagure.emitEvent(event)
self.waitUntilSettled()
self.assertEqual(1, len(self.history))
@ -202,6 +203,43 @@ class TestPagureDriver(ZuulTestCase):
'https://pagure/org/project/c/%s' % zuulvars['newrev'],
zuulvars['change_url'])
self.assertEqual(expected_newrev, zuulvars['newrev'])
self.assertEqual(expected_oldrev, zuulvars['oldrev'])
@simple_layout('layouts/basic-pagure.yaml', driver='pagure')
def test_ref_created(self):
self.create_branch('org/project', 'stable-1.0')
path = os.path.join(self.upstream_root, 'org/project')
repo = git.Repo(path)
newrev = repo.commit('refs/heads/stable-1.0').hexsha
event = self.fake_pagure.getGitBranchEvent(
'org/project', 'stable-1.0', 'creation', newrev)
old = self.sched.tenant_last_reconfigured.get('tenant-one', 0)
self.fake_pagure.emitEvent(event)
self.waitUntilSettled()
new = self.sched.tenant_last_reconfigured.get('tenant-one', 0)
# New timestamp should be greater than the old timestamp
self.assertLess(old, new)
self.assertEqual(1, len(self.history))
self.assertEqual(
'SUCCESS',
self.getJobFromHistory('project-post-job').result)
job = self.getJobFromHistory('project-post-job')
zuulvars = job.parameters['zuul']
self.assertEqual('refs/heads/stable-1.0', zuulvars['ref'])
self.assertEqual('post', zuulvars['pipeline'])
self.assertEqual('project-post-job', zuulvars['job'])
self.assertEqual('stable-1.0', zuulvars['branch'])
self.assertEqual(newrev, zuulvars['newrev'])
@simple_layout('layouts/basic-pagure.yaml', driver='pagure')
def test_ref_deleted(self):
event = self.fake_pagure.getGitBranchEvent(
'org/project', 'stable-1.0', 'deletion', '0' * 40)
self.fake_pagure.emitEvent(event)
self.waitUntilSettled()
self.assertEqual(0, len(self.history))
@simple_layout('layouts/basic-pagure.yaml', driver='pagure')
def test_ref_updated_and_tenant_reconfigure(self):

View File

@ -68,10 +68,6 @@ from zuul.driver.pagure.paguremodel import PagureTriggerEvent, PullRequest
# api_token=QX29SXAW96C2CTLUNA5JKEEU65INGWTO2B5NHBDBRMF67S7PYZWCS0L1AKHXXXXX
#
# Current Non blocking issues:
# - Pagure does not send the oldrev info when a branch is updated/created
# https://pagure.io/pagure/issue/4401
# - Pagure does not send an event when a branch is deleted
# https://pagure.io/pagure/issue/4399 (merged so need to be used)
# - Pagure does not reset the score when a PR code is updated
# https://pagure.io/pagure/issue/3985
# - CI status flag updated field unit is second, better to have millisecond
@ -204,6 +200,8 @@ class PagureEventConnector(threading.Thread):
'pull-request.new': self._event_pull_request,
'pull-request.flag.added': self._event_flag_added,
'git.receive': self._event_ref_updated,
'git.branch.creation': self._event_ref_created,
'git.branch.deletion': self._event_ref_deleted,
'pull-request.initial_comment.edited':
self._event_issue_initial_comment,
'pull-request.tag.added':
@ -346,45 +344,40 @@ class PagureEventConnector(threading.Thread):
def _event_ref_updated(self, body):
""" Handles ref updated """
# https://fedora-fedmsg.readthedocs.io/en/latest/topics.html#git-receive
# https://fedora-fedmsg.readthedocs.io/en/latest/topics.html#pagure-git-receive
event, data = self._event_base(body)
event.project_name = data.get('project_fullname')
event.branch = data.get('branch')
event.ref = 'refs/heads/%s' % event.branch
event.newrev = data.get('end_commit', data.get('stop_commit'))
# There is no concept of old rev (that is the previous branch tip) in
# pagure. end_commit is the new tip, start_commit is the oldest
# commit on the branch merged. stop_commit is the youngest commit
# on the branch. When a PR is merged (with a merge commit) end_commit
# is the merge commit sha, start and stop commits are the boundaries
# of the branch.
# Then do not set oldrev as this information is missing
# event.oldrev = data.get('start_commit')
event.newrev = data.get('end_commit')
event.oldrev = data.get('old_commit')
event.branch_updated = True
return event
# TODO(fbo): Pagure sends an event when a branch is created but the
# old rev info is not set by pagure. A new branch will be handled
# as ref updated. https://pagure.io/pagure/issue/4401
# TODO(fbo): Pagure does not send an event when a branch is deleted
# https://pagure.io/pagure/issue/4399
# if event.oldrev == '0' * 40:
# event.branch_created = True
# if event.newrev == '0' * 40:
# event.branch_deleted = True
#
# if event.branch:
# project = self.connection.source.getProject(event.project_name)
# if event.branch_deleted:
# self.connection.project_branch_cache[project].remove(
# event.branch)
# elif event.branch_created:
# self.connection.project_branch_cache[project].append(
# event.branch)
# else:
# pass
def _event_ref_created(self, body):
""" Handles ref created """
event, data = self._event_base(body)
event.project_name = data.get('project_fullname')
event.branch = data.get('branch')
event.ref = 'refs/heads/%s' % event.branch
event.newrev = data.get('rev')
event.oldrev = '0' * 40
event.branch_created = True
self.connection.project_branch_cache[
event.project_name].append(event.branch)
return event
def _event_ref_deleted(self, body):
""" Handles ref deleted """
event, data = self._event_base(body)
event.project_name = data.get('project_fullname')
event.branch = data.get('branch')
event.ref = 'refs/heads/%s' % event.branch
event.oldrev = data.get('rev')
event.newrev = '0' * 40
event.branch_deleted = True
self.connection.project_branch_cache[
event.project_name].remove(event.branch)
return event
def run(self):