Skip to content

Commit 82bc612

Browse files
committed
WIP: validate source repos
1 parent 14b0bdf commit 82bc612

2 files changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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: Validate source repositories
26+
script: scripts/validate-source-repos.py

scripts/validate-source-repos.py

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

0 commit comments

Comments
 (0)