Merge "Codify branch matchers and refs"

This commit is contained in:
Zuul 2021-09-27 23:47:59 +00:00 committed by Gerrit Code Review
commit 82812c1bf7
7 changed files with 119 additions and 0 deletions

View File

@ -815,6 +815,13 @@ Here is an example of two job definitions:
the commit referenced by the tag. If any of those branches match a the commit referenced by the tag. If any of those branches match a
branch matcher, the matcher is considered to have matched. branch matcher, the matcher is considered to have matched.
Additionally in the case of a tag item, if the expression
matches the full name of the ref (eg, `refs/tags/foo`) then the
job is considered to match. The preceding section still
applies, so the definition must appear in a branch containing
the commit referenced by the tag to be considered, and then the
expression must also match the tag.
This example illustrates a job called *run-tests* which uses a This example illustrates a job called *run-tests* which uses a
nodeset based on the current release of an operating system to nodeset based on the current release of an operating system to
perform its tests, except when testing changes to the stable/2.0 perform its tests, except when testing changes to the stable/2.0

View File

@ -0,0 +1,16 @@
- job:
name: other-job
- job:
name: test-job-1
- job:
name: test-job-2
- project:
tag:
jobs:
- test-job-1:
branches: "^refs/tags/tag1-.*$"
- test-job-2:
branches: "^refs/tags/tag2-.*$"

View File

@ -0,0 +1 @@
test

View File

@ -0,0 +1,2 @@
- hosts: all
tasks: []

View File

@ -0,0 +1,12 @@
- pipeline:
name: tag
manager: independent
trigger:
gerrit:
- event: ref-updated
ref: ^refs/tags/.*$
- job:
name: base
parent: null
run: playbooks/base.yaml

View File

@ -0,0 +1,8 @@
- tenant:
name: tenant-one
source:
gerrit:
config-projects:
- project-config
untrusted-projects:
- org/project

View File

@ -838,6 +838,79 @@ class TestBranchMismatch(ZuulTestCase):
], ordered=False) ], ordered=False)
class TestBranchRef(ZuulTestCase):
tenant_config_file = 'config/branch-ref/main.yaml'
def test_ref_match(self):
# Test that branch matchers for explicit refs work as expected
# First, make a branch with another job so we can examine
# different branches.
self.create_branch('org/project', 'stable')
self.fake_gerrit.addEvent(
self.fake_gerrit.getFakeBranchCreatedEvent(
'org/project', 'stable'))
self.waitUntilSettled()
in_repo_conf = textwrap.dedent(
"""
- project:
tag:
jobs:
- other-job:
branches: "^refs/tags/tag1-.*$"
""")
file_dict = {'zuul.yaml': in_repo_conf}
A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A',
files=file_dict)
A.setMerged()
self.fake_gerrit.addEvent(A.getChangeMergedEvent())
self.waitUntilSettled()
# We're going to tag master, which is still at the branch
# point for stable, so the tagged commit will appear in both
# branches. This should cause test-job-1 (from the project
# config on master) and other-job (from the project config on
# stable).
event = self.fake_gerrit.addFakeTag('org/project', 'master', 'tag1-a')
self.fake_gerrit.addEvent(event)
self.waitUntilSettled()
self.assertHistory([
dict(name='other-job', result='SUCCESS', ref='refs/tags/tag1-a'),
dict(name='test-job-1', result='SUCCESS', ref='refs/tags/tag1-a')],
ordered=False)
# Next, merge a noop change to master so that we can tag a
# commit that's unique to master.
B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
B.setMerged()
self.fake_gerrit.addEvent(B.getChangeMergedEvent())
self.waitUntilSettled()
# This tag should only run test-job-1, since it doesn't appear
# in stable, it doesn't get that project config applied.
event = self.fake_gerrit.addFakeTag('org/project', 'master', 'tag1-b')
self.fake_gerrit.addEvent(event)
self.waitUntilSettled()
self.assertHistory([
dict(name='other-job', result='SUCCESS', ref='refs/tags/tag1-a'),
dict(name='test-job-1', result='SUCCESS', ref='refs/tags/tag1-a'),
dict(name='test-job-1', result='SUCCESS', ref='refs/tags/tag1-b')],
ordered=False)
# Now tag the same commit with the other format; we should get
# only test-job-2 added.
event = self.fake_gerrit.addFakeTag('org/project', 'master', 'tag2-a')
self.fake_gerrit.addEvent(event)
self.waitUntilSettled()
self.assertHistory([
dict(name='other-job', result='SUCCESS', ref='refs/tags/tag1-a'),
dict(name='test-job-1', result='SUCCESS', ref='refs/tags/tag1-a'),
dict(name='test-job-1', result='SUCCESS', ref='refs/tags/tag1-b'),
dict(name='test-job-2', result='SUCCESS', ref='refs/tags/tag2-a')],
ordered=False)
class TestAllowedProjects(ZuulTestCase): class TestAllowedProjects(ZuulTestCase):
tenant_config_file = 'config/allowed-projects/main.yaml' tenant_config_file = 'config/allowed-projects/main.yaml'