-
Notifications
You must be signed in to change notification settings - Fork 1
Add workflow to validate source repos definitions and synchronise existing definitions #508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MoteHue
wants to merge
4
commits into
main
Choose a base branch
from
source-repos-sanity-checks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| --- | ||
| name: Validate source repositories | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| pull_request: | ||
| jobs: | ||
| validate-source-repos: | ||
| name: Validate source repositories | ||
|
|
||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 | ||
|
|
||
| # Python version must be pinned because of issue with Ubuntu permissions | ||
| # See https://github.com/actions/runner-images/issues/11499 | ||
| - name: Set up Python | ||
| uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 | ||
| with: | ||
| python-version: '3.12' | ||
|
|
||
| - name: Install pip dependencies | ||
| run: | | ||
| pip install -r requirements.txt | ||
|
|
||
| - name: Validate source repositories | ||
| run: python3 scripts/validate-source-repos.py | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import json | ||
| import sys | ||
| import yaml | ||
|
MoteHue marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def read_repos_data(): | ||
| terraform_repositories = {} | ||
| ansible_repositories = {} | ||
|
|
||
| with open('terraform/github/terraform.tfvars.json', 'r') as file: | ||
| data = json.load(file) | ||
| terraform_repositories = data['repositories'] | ||
|
|
||
| with open('ansible/inventory/group_vars/all/source-repositories', | ||
| 'r') as file: | ||
| data = yaml.safe_load(file) | ||
| ansible_repositories = data['source_repositories'] | ||
|
|
||
| return terraform_repositories, ansible_repositories | ||
|
|
||
|
|
||
| def get_repos_diff(tf_repos, ans_repos): | ||
| ans_repos_list = list(ans_repos.keys()) | ||
|
|
||
| tf_repos_list = [] | ||
| for team in tf_repos: | ||
| tf_repos_list += tf_repos[team] | ||
|
|
||
| diff = list(set(tf_repos_list).symmetric_difference(ans_repos_list)) | ||
|
|
||
| return diff | ||
|
|
||
|
|
||
| def _extract_group_from_community_files(files): | ||
| for file in files: | ||
| if 'codeowners' in file: | ||
| content = file['codeowners']['content'] | ||
| return content.strip('{ }').replace('_', '').split('.')[-1] | ||
|
MoteHue marked this conversation as resolved.
MoteHue marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def _group_ans_repos_by_group(ans_repos): | ||
| ans_repos_by_group = {} | ||
| for repo in ans_repos: | ||
| group = _extract_group_from_community_files( | ||
| ans_repos[repo]['community_files']) | ||
| if group in ans_repos_by_group: | ||
| ans_repos_by_group[group].append(repo) | ||
| else: | ||
| ans_repos_by_group[group] = [repo] | ||
| return ans_repos_by_group | ||
|
|
||
|
|
||
| def get_mismatched_repos(tf_repos, ans_repos, repos_missing): | ||
| ans_repos_new = _group_ans_repos_by_group(ans_repos) | ||
| tf_repos_new = {k.lower(): v for k, v in tf_repos.items()} | ||
|
|
||
| mismatched_repos = [] | ||
| for group in tf_repos_new: | ||
| if len(tf_repos_new[group]) == 0 or group not in ans_repos_new: | ||
| continue | ||
| else: | ||
| diff = list(set(tf_repos_new[group]).symmetric_difference( | ||
| set(ans_repos_new[group]))) | ||
| mismatched_repos.extend(diff) | ||
| return list(set(mismatched_repos).difference(set(repos_missing))) | ||
|
|
||
|
|
||
| def main(): | ||
| terraform_repos, ansible_repos = read_repos_data() | ||
|
|
||
| repos_missing = get_repos_diff(terraform_repos, ansible_repos) | ||
|
|
||
| print('The following repos are only present in one of the Ansible ' | ||
| f'source-repositories and the Terraform tfvars: {repos_missing}') | ||
|
|
||
|
MoteHue marked this conversation as resolved.
|
||
| mismatched_repos = get_mismatched_repos(terraform_repos, ansible_repos, | ||
| repos_missing) | ||
|
|
||
| print('The following repos are assigned to different codeowner groups in ' | ||
| 'the Ansible source-repositories and the Terraform tfvars: ' | ||
| f'{mismatched_repos}') | ||
|
|
||
| return len(repos_missing) > 0 or len(mismatched_repos) > 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.