Skip to content

Commit 3d34237

Browse files
cescginaopenshift-merge-bot[bot]
authored andcommitted
Fix refspec, url for cloning projects in reproducer
Zuul reproducer does only seem to support cloning projects from github, while trying to clone projects from gerrit it uses a wrong url and refspec. This change introduces two filters to get the right values for url and refspec for the different sources (github and gerrit).
1 parent 2b7db4e commit 3d34237

File tree

6 files changed

+294
-2
lines changed

6 files changed

+294
-2
lines changed

ansible.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
action_plugins = ./plugins/action:~/plugins/action:/usr/share/ansible/plugins/action
33
library = ./plugins/modules:~/plugins/modules:/usr/share/ansible/plugins/modules
44
roles_path = ~/ci-framework-data/artifacts/roles:./roles:/usr/share/ansible/roles:/etc/ansible/roles:~/.ansible/roles
5+
filter_plugins = ./plugins/filter:~/plugins/filter:/usr/share/ansible/plugins/filter
56
log_path = ~/ansible.log
67
# We may consider ansible.builtin.junit
78
callbacks_enabled = ansible.posix.profile_tasks
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python3
2+
3+
__metaclass__ = type
4+
5+
DOCUMENTATION = """
6+
name: reproducer_gerrit_infix
7+
short_description: Maps a hostname to the infix needed for cloning a repo
8+
for that hostname.
9+
description:
10+
- Maps a hostname to the infix needed for cloning a repo from that
11+
hostname.Some gerrit instances add an infix like "gerrit", or "r"
12+
to the repo clone url.
13+
options:
14+
_input:
15+
description:
16+
- repo hostname where we want to clone from.
17+
type: str
18+
required: true
19+
"""
20+
21+
EXAMPLES = """
22+
- name: Get infix to clone repo from rdo gerrit
23+
ansible.builtin.set_fact:
24+
gerrit_infix: >-
25+
{{
26+
"review.rdoproject.org" | reproducer_gerrit_infix
27+
}}
28+
"""
29+
30+
RETURN = """
31+
_value:
32+
description: The infix to add to the url to clone the repo.
33+
type: str
34+
sample:
35+
"gerrit/"
36+
37+
"""
38+
39+
from ansible.errors import AnsibleFilterTypeError
40+
41+
42+
class FilterModule:
43+
@classmethod
44+
def __map_hostname_infix(cls, hostname):
45+
if not isinstance(hostname, str):
46+
raise AnsibleFilterTypeError(
47+
f"reproducer_gerrit_infix requires a str, got {type(hostname)}"
48+
)
49+
if "rdoproject" in hostname:
50+
return "r/"
51+
elif "code.eng" in hostname:
52+
return "gerrit/"
53+
return ""
54+
55+
def filters(self):
56+
return {
57+
"reproducer_gerrit_infix": self.__map_hostname_infix,
58+
}

plugins/filter/reproducer_refspec.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env python3
2+
3+
__metaclass__ = type
4+
5+
DOCUMENTATION = """
6+
name: reproducer_refspec
7+
short_description: Maps repo information from zuul to a refspec.
8+
description:
9+
- Maps repo information from zuul to a refspec that should be
10+
used for pulling it with git.
11+
options:
12+
_input:
13+
description:
14+
- repo information from zuul variables.
15+
type: dict
16+
required: true
17+
"""
18+
19+
EXAMPLES = """
20+
- name: Get refspec to clone repo from rdo gerrit
21+
ansible.builtin.set_fact:
22+
change_refspec: >-
23+
{{
24+
repo | reproducer_refspec
25+
}}
26+
"""
27+
28+
RETURN = """
29+
_value:
30+
description: The refspec to pull the change.
31+
type: str
32+
sample:
33+
"refs/changes/15/449415/5"
34+
35+
"""
36+
37+
from ansible.errors import AnsibleFilterError, AnsibleFilterTypeError
38+
39+
40+
class FilterModule:
41+
@classmethod
42+
def __map_repo_refspec(cls, repo):
43+
if not isinstance(repo, dict):
44+
raise AnsibleFilterTypeError(
45+
f"reproducer_refspec requires a dict, got {type(repo)}"
46+
)
47+
if "change" not in repo:
48+
# handle case pointing to main/master, e.g. a periodic job
49+
return ""
50+
change = repo["change"]
51+
if "project" not in repo:
52+
raise AnsibleFilterError(
53+
"repo information does not contain 'project' field"
54+
)
55+
if "canonical_hostname" not in repo["project"]:
56+
raise AnsibleFilterError(
57+
"repo information does not contain 'canonical_hostname' field"
58+
)
59+
hostname = repo["project"]["canonical_hostname"]
60+
if "rdoproject" in hostname or "code.eng" in hostname:
61+
if "patchset" not in repo:
62+
raise AnsibleFilterError(
63+
"repo information does not contain 'patchset' field"
64+
)
65+
patchset = repo["patchset"]
66+
# changes coming from gerrit
67+
return f"refs/changes/{change[-2:]}/{change}/{patchset}"
68+
else:
69+
# changes coming from github
70+
return f"pull/{change}/head"
71+
72+
def filters(self):
73+
return {
74+
"reproducer_refspec": self.__map_repo_refspec,
75+
}

roles/reproducer/tasks/push_code.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
loop: "{{ zuul['items'] | zip(repos_dir_stats.results) | list }}"
2727

2828
- name: Fetch zuul.items repositories
29+
vars:
30+
_skip_refspec: "{{ job_id is not defined or repo.change is not defined }}"
2931
ansible.builtin.git:
3032
dest: "{{ repo.project.src_dir }}"
31-
repo: "https://{{ repo.project.canonical_name }}"
32-
refspec: "{{ omit if job_id is not defined else 'pull/'~repo.change~'/head:'~job_id }}"
33+
repo: "https://{{ repo.project.canonical_hostname }}/{{ repo.project.canonical_hostname | reproducer_gerrit_infix }}{{ repo.project.name }}"
34+
refspec: "{{ omit if _skip_refspec else repo | reproducer_refspec ~ ':' ~ job_id }}"
3335
version: "{{ job_id | default(omit) }}"
3436
force: true
3537
loop: "{{ zuul['items'] }}"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
- name: Test reproducer_gerrit_infix filter
2+
ansible.builtin.assert:
3+
that:
4+
- "'github.com' | cifmw.general.reproducer_gerrit_infix == ''"
5+
- "'review.rdoproject.org' | cifmw.general.reproducer_gerrit_infix == 'r/'"
6+
- "'review.rdoprojetc.org' | cifmw.general.reproducer_gerrit_infix == ''"
7+
- "'code.engineering.com' | cifmw.general.reproducer_gerrit_infix == 'gerrit/'"
8+
- "'gitlab.cee' | cifmw.general.reproducer_gerrit_infix == ''"
9+
10+
- name: Test reproducer_gerrit_infix bad argument
11+
vars:
12+
input:
13+
- "github.com"
14+
ansible.builtin.debug:
15+
var: "input | cifmw.general.reproducer_gerrit_infix"
16+
register: _bad_reproducer_gerrit_infix_argument
17+
ignore_errors: true
18+
19+
- name: Verify reproducer_gerrit_infix showed an error message
20+
ansible.builtin.assert:
21+
that:
22+
- _bad_reproducer_gerrit_infix_argument is failed
23+
- "'reproducer_gerrit_infix requires a str' in _bad_reproducer_gerrit_infix_argument.msg"
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
- name: Test reproducer_refspec bad argument
2+
vars:
3+
input:
4+
- "github.com"
5+
ansible.builtin.debug:
6+
var: "input | cifmw.general.reproducer_refspec"
7+
register: _bad_reproducer_refspec_argument
8+
ignore_errors: true
9+
10+
- name: Verify reproducer_refspec showed an error message
11+
ansible.builtin.assert:
12+
that:
13+
- _bad_reproducer_refspec_argument is failed
14+
- "'reproducer_refspec requires a dict,' in _bad_reproducer_refspec_argument.msg"
15+
16+
- name: Test reproducer_refspec bad argument, no project
17+
vars:
18+
input:
19+
hostname: "github.com"
20+
change: "abc"
21+
ansible.builtin.debug:
22+
var: "input | cifmw.general.reproducer_refspec"
23+
register: _bad_reproducer_refspec_argument
24+
ignore_errors: true
25+
26+
- name: Verify reproducer_refspec showed an error message
27+
ansible.builtin.assert:
28+
that:
29+
- _bad_reproducer_refspec_argument is failed
30+
- "'repo information does not contain \\'project\\' field' in _bad_reproducer_refspec_argument.msg"
31+
32+
- name: Test reproducer_refspec bad argument, no hostname
33+
vars:
34+
input:
35+
project: {}
36+
change: "abc"
37+
ansible.builtin.debug:
38+
var: "input | cifmw.general.reproducer_refspec"
39+
register: _bad_reproducer_refspec_argument
40+
ignore_errors: true
41+
42+
- name: Verify reproducer_refspec showed an error message
43+
ansible.builtin.assert:
44+
that:
45+
- _bad_reproducer_refspec_argument is failed
46+
- "'repo information does not contain \\'canonical_hostname\\' field' in _bad_reproducer_refspec_argument.msg"
47+
48+
- name: Test reproducer_refspec bad argument, no patchset
49+
vars:
50+
input:
51+
project:
52+
canonical_hostname: "rdoproject.com"
53+
change: "abc"
54+
ansible.builtin.debug:
55+
var: "input | cifmw.general.reproducer_refspec"
56+
register: _bad_reproducer_refspec_argument
57+
ignore_errors: true
58+
59+
- name: Verify reproducer_refspec showed an error message
60+
ansible.builtin.assert:
61+
that:
62+
- _bad_reproducer_refspec_argument is failed
63+
- "'repo information does not contain \\'patchset\\' field' in _bad_reproducer_refspec_argument.msg"
64+
65+
- name: Test reproducer_refspec no patchset in github
66+
vars:
67+
input:
68+
project:
69+
canonical_hostname: "github.com"
70+
change: "abc"
71+
ansible.builtin.assert:
72+
that:
73+
- "input | cifmw.general.reproducer_refspec == 'pull/abc/head'"
74+
75+
- name: Test reproducer_refspec no change
76+
vars:
77+
input:
78+
project:
79+
canonical_hostname: "github.com"
80+
ansible.builtin.assert:
81+
that:
82+
- "input | cifmw.general.reproducer_refspec == ''"
83+
84+
- name: Test reproducer_refspec periodic job
85+
vars:
86+
input:
87+
project:
88+
canonical_hostname: "review.rdoproject.org"
89+
ansible.builtin.assert:
90+
that:
91+
- "input | cifmw.general.reproducer_refspec == ''"
92+
93+
- name: Test reproducer_refspec github refspec
94+
vars:
95+
input:
96+
project:
97+
canonical_hostname: "github.org"
98+
change: "1035"
99+
ansible.builtin.assert:
100+
that:
101+
- "input | cifmw.general.reproducer_refspec == 'pull/1035/head'"
102+
103+
- name: Test reproducer_refspec gitlab refspec
104+
vars:
105+
input:
106+
project:
107+
canonical_hostname: "gitlab.org"
108+
change: "1035"
109+
ansible.builtin.assert:
110+
that:
111+
- "input | cifmw.general.reproducer_refspec == 'pull/1035/head'"
112+
113+
- name: Test reproducer_refspec gerrit refspec
114+
vars:
115+
input:
116+
project:
117+
canonical_hostname: "review.rdoproject.org"
118+
change: "51319"
119+
patchset: "6"
120+
ansible.builtin.assert:
121+
that:
122+
- "input | cifmw.general.reproducer_refspec == 'refs/changes/19/51319/6'"
123+
124+
- name: Test reproducer_refspec code.eng gerrit refspec
125+
vars:
126+
input:
127+
project:
128+
canonical_hostname: "review.code.eng.org"
129+
change: "51319"
130+
patchset: "6"
131+
ansible.builtin.assert:
132+
that:
133+
- "input | cifmw.general.reproducer_refspec == 'refs/changes/19/51319/6'"

0 commit comments

Comments
 (0)