Fix branch name and project name for ref-updated create/delete

1.
gerritconnection: changes are using "data.change.branch" to fill
event.branch which doesn't have 'refs/heads/' prefix

branch creation and deletion were using "data.refUpdate.refName"
to fill event.branch which has the complete refname: it starts
with 'refs/heads/'

As a result, in the Scheduler event process queue, the cache was not
properly cleared when calling Abide's clearUnparsedBranchCache,
called using event.branch and not change.branch.

change.branch is already correctly set to the relative refname

When reconfiguring a tenant, it could hold data which were removed
and reconfigured tenant could still use data that had disappeared

This patch removes the prefix "refs/heads/" when setting event branch
name in case of deletion or creation

note: it's not possible to use event.branch to set change.branch in
gerritconnection.getChange as event objects doesn't have a constant base
type.
it can be either:
- GerritTriggerEvent -> TriggerEvent -> object
- DequeueEvent -> ManagementEvent -> object
the latter doesn't have a branch attribute

2.
Setting event.branch is moved to any 'ref-update' event, so it will now
be set when newrev and oldrev are both not null. This doesn't have and
should not have any impacts

Change-Id: Ie60b382b23074cc9feff0648e786ddaf0d3454aa
This commit is contained in:
Guillaume Chauvel 2020-06-30 20:38:53 +02:00
parent 438aac5b24
commit 96d002cbbe
2 changed files with 76 additions and 3 deletions

View File

@ -8365,3 +8365,71 @@ class TestSchedulerSmartReconfiguration(ZuulTestCase):
def test_smart_reconfiguration_command_socket(self):
"Test that live reconfiguration works using command socket"
self._test_smart_reconfiguration(command_socket=True)
class TestReconfigureBranch(ZuulTestCase):
def _setupTenantReconfigureTime(self):
self.old = self.scheds.first.sched.tenant_last_reconfigured\
.get('tenant-one', 0)
def _createBranch(self):
self.create_branch('org/project1', 'stable')
self.fake_gerrit.addEvent(
self.fake_gerrit.getFakeBranchCreatedEvent(
'org/project1', 'stable'))
self.waitUntilSettled()
def _deleteBranch(self):
self.delete_branch('org/project1', 'stable')
self.fake_gerrit.addEvent(
self.fake_gerrit.getFakeBranchDeletedEvent(
'org/project1', 'stable'))
self.waitUntilSettled()
def _expectReconfigure(self, doReconfigure):
new = self.scheds.first.sched.tenant_last_reconfigured\
.get('tenant-one', 0)
if doReconfigure:
self.assertLess(self.old, new)
else:
self.assertEqual(self.old, new)
self.old = new
class TestReconfigureBranchCreateDeleteSshHttp(TestReconfigureBranch):
tenant_config_file = 'config/single-tenant/main.yaml'
config_file = 'zuul-gerrit-web.conf'
def test_reconfigure_cache_branch_create_delete(self):
"Test that cache is updated clear on branch creation/deletion"
self._setupTenantReconfigureTime()
self._createBranch()
self._expectReconfigure(True)
self._deleteBranch()
self._expectReconfigure(True)
class TestReconfigureBranchCreateDeleteSsh(TestReconfigureBranch):
tenant_config_file = 'config/single-tenant/main.yaml'
def test_reconfigure_cache_branch_create_delete(self):
"Test that cache is updated clear on branch creation/deletion"
self._setupTenantReconfigureTime()
self._createBranch()
self._expectReconfigure(True)
self._deleteBranch()
self._expectReconfigure(True)
class TestReconfigureBranchCreateDeleteHttp(TestReconfigureBranch):
tenant_config_file = 'config/single-tenant/main.yaml'
config_file = 'zuul-gerrit-no-stream.conf'
def test_reconfigure_cache_branch_create_delete(self):
"Test that cache is updated clear on branch creation/deletion"
self._setupTenantReconfigureTime()
self._createBranch()
self._expectReconfigure(True)
self._deleteBranch()
self._expectReconfigure(True)

View File

@ -230,17 +230,22 @@ class GerritEventConnector(threading.Thread):
# This checks whether the event created or deleted a branch so
# that Zuul may know to perform a reconfiguration on the
# project.
branch_refs = 'refs/heads/'
if (event.type == 'ref-updated' and
((not event.ref.startswith('refs/')) or
event.ref.startswith('refs/heads'))):
event.ref.startswith(branch_refs))):
if event.ref.startswith(branch_refs):
event.branch = event.ref[len(branch_refs):]
else:
event.branch = event.ref
if event.oldrev == '0' * 40:
event.branch_created = True
event.branch = event.ref
project = self.connection.source.getProject(event.project_name)
self.connection._clearBranchCache(project)
if event.newrev == '0' * 40:
event.branch_deleted = True
event.branch = event.ref
project = self.connection.source.getProject(event.project_name)
self.connection._clearBranchCache(project)