Skip to content

Instantly share code, notes, and snippets.

@kristianheljas
Last active March 6, 2023 11:58
Show Gist options
  • Save kristianheljas/15fd5df61ca39729f743fc19ff7602b3 to your computer and use it in GitHub Desktop.
Save kristianheljas/15fd5df61ca39729f743fc19ff7602b3 to your computer and use it in GitHub Desktop.

Usage

Launch issues at included collections (specified in ansible-build-data/7/collection-meta.yaml) and records the issue links in notified-collections.yml

# Create and acticate venv
python -m venv venv
source venv/bin/activate

# Install pip requirements
pip install pyyaml requests

# Download collection inclusion list
curl https://raw.githubusercontent.com/ansible-community/ansible-build-data/main/7/collection-meta.yaml > ansible-7-collection-meta.yml

# Create notification issue tracker file if it doesn't exist already
[ ! -f notified-collections.yml ] && (echo '{}' > notified-collections.yml)

# Set up github token for creating the issues
export GH_TOKEN="..."

# See what the script would do
python3 notify-collections.py

# Send the issue strike!
python3 notify-collections.py --go-nuclear
amazon.aws:
issue_url: https://github.com/ansible-collections/amazon.aws/issues/1041
community.aws:
issue_url: https://github.com/ansible-collections/community.aws/pull/1420
community.crypto:
issue_url: https://github.com/ansible-community/community-team/issues/60#issuecomment-1328118316
community.dns:
issue_url: https://github.com/ansible-community/community-team/issues/60#issuecomment-1328118316
community.docker:
issue_url: https://github.com/ansible-community/community-team/issues/60#issuecomment-1328118316
community.hrobot:
issue_url: https://github.com/ansible-community/community-team/issues/60#issuecomment-1328118316
community.routeros:
issue_url: https://github.com/ansible-community/community-team/issues/60#issuecomment-1328118316
community.sops:
issue_url: https://github.com/ansible-community/community-team/issues/60#issuecomment-1328118316
community.mysql:
issue_url: https://github.com/ansible-community/community-team/issues/60#issuecomment-1331869255
ansible.netcommon:
issue_url: https://github.com/ansible-community/community-team/issues/60#issuecomment-1252688300
ansible.posix:
issue_url: https://github.com/ansible-community/community-team/issues/60#issuecomment-1252688300
community.postgresql:
issue_url: https://github.com/ansible-community/community-team/issues/60#issuecomment-1252688300
import os
import re
import sys
import requests
import yaml
ISSUE_TITLE = 'Consider using true/false for all booleans in docs'
ISSUE_BODY = '''
Based on the [community decision](https://github.com/ansible-community/community-topics/discussions/120) to use `true/false` for boolean values in documentation and examples, we ask that you evaluate booleans in this collection and consider changing any that do not use `true/false` (lowercase).
See [documentation block format](https://docs.ansible.com/ansible/devel/dev_guide/developing_modules_documenting.html#documentation-block) for more info (specifically, option defaults).
If you have already implemented this or decide not to, feel free to close this issue.
---
P.S. This is auto-generated issue, please raise any concerns [here](https://github.com/ansible-community/community-team/issues/60)
'''
def print_log(msg: str, *args, **kwargs):
sys.stdout.writelines(msg.format(*args, **kwargs) + "\n")
sys.stdout.flush()
def print_debug(msg: str, *args, **kwargs):
sys.stderr.write(msg.format(*args, **kwargs) + "\n")
sys.stdout.flush()
def read_yaml_file(path: str):
with open(path, 'r') as file:
return yaml.safe_load(file)
def write_yaml_file(path: str, data):
with open(path, 'w') as file:
return file.write(yaml.dump(data))
def parse_github_repo_url(repo_url: str):
gh_url_matches = re.search(r'^https://github\.com/(.*?)/(.*?)$', repo_url)
if not gh_url_matches:
return None
gh_repo_owner, gh_repo_name = gh_url_matches.groups()
return dict(owner=gh_repo_owner, name=gh_repo_name)
def create_gh_issue(gh_repo: dict, data: dict):
assert os.getenv('GH_TOKEN')
create_url = 'https://api.github.com/repos/{}/{}/issues'.format(gh_repo['owner'], gh_repo['name'])
create_response = requests.post(
url=create_url,
headers={
'Accept': 'application/vnd.github+json',
'Authorization': 'Bearer {}'.format(os.getenv('GH_TOKEN')),
'X-GitHub-Api-Version': '2022-11-28',
},
json=data
)
create_response.raise_for_status()
return create_response.json()
# From https://github.com/ansible-community/ansible-build-data/blob/main/7/collection-meta.yaml
collections_meta = read_yaml_file('ansible-7-collection-meta.yml')
# From https://github.com/ansible-community/community-team/issues/60#issuecomment-1252688300
notified_collections = read_yaml_file('notified-collections.yml')
# Safety first!
DRY_RUN = True
if '--go-nuclear' in sys.argv:
# Extra safety!
confirm_lanuch = input("Launch issues at collections? [yes/no]: ").lower() == 'yes'
DRY_RUN = not confirm_lanuch
else:
print_debug('--go-nuclear option not specified, issues will not be launched!')
for fqcn, collection_meta in collections_meta['collections'].items():
# Check if issue is already created
notified_meta = notified_collections.get(fqcn, dict()) or dict()
if notified_meta and notified_meta.get('issue_url'):
print_debug('[{}] Issue already created at {}, skipping...', fqcn, notified_meta['issue_url'])
continue
# Check if repository is hosted at GitHub
repository_url = collections_meta['collections'][fqcn]['repository']
gh_repo = parse_github_repo_url(repository_url)
if not gh_repo:
print_debug('[{}] Couldn\'t detect github repository from {}, skipping...', fqcn, repository_url)
continue
if DRY_RUN:
print_log('[{}] Would create an issue at https://github.com/{}/{}/issues',
fqcn, gh_repo['owner'], gh_repo['name'])
continue
# Create the issue
print_log('[{}] Creating issue at https://github.com/{}/{}/issues', fqcn, gh_repo['owner'], gh_repo['name'])
create_result = create_gh_issue(gh_repo, {
'title': ISSUE_TITLE,
'body': ISSUE_BODY
})
issue_url = create_result['html_url']
print_log('[{}] Created issue at {}', fqcn, issue_url)
# Update notified list
notified_meta['issue_url'] = issue_url
notified_collections[fqcn] = notified_meta
write_yaml_file('notified-collections.yml', notified_collections)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment