Skip to content

Commit 24426fb

Browse files
committed
Add validation for source repos definitions
Adds a script which checks if the source repositories definitions in ansible/inventory/group_vars/all/source-repositories and terraform/github/terraform.tfvars.json are out of sync. Calls this script via a workflow on PR and pushes to main.
1 parent 14b0bdf commit 24426fb

2 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
name: Validate source repositories
3+
4+
on:
5+
push:
6+
branches:
7+
- main
8+
pull_request:
9+
jobs:
10+
validate-source-repos:
11+
name: Validate source repositories
12+
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
17+
18+
# Python version must be pinned because of issue with Ubuntu permissions
19+
# See https://github.com/actions/runner-images/issues/11499
20+
- name: Set up Python
21+
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
22+
with:
23+
python-version: '3.12'
24+
25+
- name: Install pip dependencies
26+
run: |
27+
pip install -r requirements.txt
28+
29+
- name: Validate source repositories
30+
run: python3 scripts/validate-source-repos.py

scripts/validate-source-repos.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import json
2+
import sys
3+
import yaml
4+
5+
6+
def read_repos_data():
7+
terraform_repositories = {}
8+
ansible_repositories = {}
9+
10+
with open('terraform/github/terraform.tfvars.json', 'r') as file:
11+
data = json.load(file)
12+
terraform_repositories = data['repositories']
13+
14+
with open('ansible/inventory/group_vars/all/source-repositories',
15+
'r') as file:
16+
data = yaml.safe_load(file)
17+
ansible_repositories = data['source_repositories']
18+
19+
return terraform_repositories, ansible_repositories
20+
21+
22+
def get_repos_diff(tf_repos, ans_repos):
23+
ans_repos_list = list(ans_repos.keys())
24+
25+
tf_repos_list = []
26+
for team in tf_repos:
27+
tf_repos_list += tf_repos[team]
28+
29+
diff = list(set(tf_repos_list).symmetric_difference(ans_repos_list))
30+
31+
return diff
32+
33+
34+
def _extract_group_from_community_files(files):
35+
for file in files:
36+
if 'codeowners' in file:
37+
content = file['codeowners']['content']
38+
return content.strip('{ }').replace('_', '').split('.')[-1]
39+
40+
41+
def _sort_ans_repos_by_group(ans_repos):
42+
ans_repos_by_group = {}
43+
for repo in ans_repos:
44+
files = ans_repos[repo]['community_files']
45+
group = _extract_group_from_community_files(files)
46+
if group in ans_repos_by_group:
47+
ans_repos_by_group[group].append(repo)
48+
else:
49+
ans_repos_by_group[group] = [repo]
50+
return ans_repos_by_group
51+
52+
53+
def get_mismatched_repos(tf_repos, ans_repos, repos_missing):
54+
ans_repos_new = _sort_ans_repos_by_group(ans_repos)
55+
tf_repos_new = {k.lower(): v for k, v in tf_repos.items()}
56+
57+
mismatched_repos = []
58+
for group in tf_repos_new:
59+
if len(tf_repos_new[group]) == 0 or group not in ans_repos_new:
60+
continue
61+
else:
62+
diff = list(set(tf_repos_new[group]).symmetric_difference(
63+
set(ans_repos_new[group])))
64+
mismatched_repos.extend(diff)
65+
return list(set(mismatched_repos).difference(set(repos_missing)))
66+
67+
68+
def main():
69+
terraform_repos, ansible_repos = read_repos_data()
70+
71+
repos_missing = get_repos_diff(terraform_repos, ansible_repos)
72+
73+
print('The following repos are only present in one of the Ansible '
74+
f'source-repositories and the Terraform tfvars: {repos_missing}')
75+
76+
mismatched_repos = get_mismatched_repos(terraform_repos, ansible_repos,
77+
repos_missing)
78+
79+
print('The following repos are assigned to different codeowner groups in '
80+
'the Ansible source-repositories and the Terraform tfvars: '
81+
f'{mismatched_repos}')
82+
83+
return len(repos_missing) > 0 or len(mismatched_repos) > 0
84+
85+
if __name__ == "__main__":
86+
sys.exit(main())

0 commit comments

Comments
 (0)