|
| 1 | +import os |
| 2 | +import re |
| 3 | +import sys |
| 4 | +import yaml |
| 5 | +import requests |
| 6 | + |
| 7 | + |
| 8 | +DIR = 'plugins' |
| 9 | + |
| 10 | +KEY_SET = set(['owner', 'name', 'branch', 'docs']) |
| 11 | +ENV_FILE_REGEX = '.*-qiime2-.*-20[0-9][0-9]\.([1-9]|1[0-2])\.yml' |
| 12 | +GITHUB_BASE_URL = "https://api.github.com" |
| 13 | + |
| 14 | +env_urls = [] |
| 15 | + |
| 16 | +GITHUB_TOKEN = sys.argv[1] |
| 17 | +GITHUB_BASE_URL = 'https://api.github.com' |
| 18 | + |
| 19 | + |
| 20 | +def lint(yml): |
| 21 | + # Assert corrrect keys |
| 22 | + assert set(yml.keys()) == KEY_SET |
| 23 | + |
| 24 | + # Get the docs URL assert 200 |
| 25 | + response = requests.get(yml['docs']) |
| 26 | + assert response.status_code == 200 |
| 27 | + |
| 28 | + # Put together the owner/name:branch |
| 29 | + url = f'{GITHUB_BASE_URL}/repos/{yml['owner']}/{yml['name']}/contents/environment-files' |
| 30 | + headers = { |
| 31 | + 'Authorization': f'token: {GITHUB_TOKEN}', |
| 32 | + 'X-GitHub-Api-Version': '2022-11-28' |
| 33 | + } |
| 34 | + query_params = { |
| 35 | + 'owner': yml['owner'], |
| 36 | + 'repo': yml['name'], |
| 37 | + 'ref': yml['branch'], |
| 38 | + 'path': '/environment-files/' |
| 39 | + } |
| 40 | + |
| 41 | + # Get all files in the /environment-files/ folder |
| 42 | + response = requests.get(url, headers=headers, params=query_params) |
| 43 | + envs = response.json() |
| 44 | + |
| 45 | + # If the file matches the regex to be a QIIME 2 environment-file then keep |
| 46 | + # track of its download URL |
| 47 | + for env in envs: |
| 48 | + if re.search(ENV_FILE_REGEX, env['name']) is not None: |
| 49 | + env_urls.append(env['download_url']) |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + GITHUB_TOKEN = sys.argv[1] |
| 54 | + files = sys.argv[2:] |
| 55 | + |
| 56 | + for file in files: |
| 57 | + head, tail = os.path.split(file) |
| 58 | + file_name, file_ext = os.path.splitext(tail) |
| 59 | + |
| 60 | + # We only care about files added to the plugins dir |
| 61 | + if head == DIR: |
| 62 | + with open(file, 'r') as fh: |
| 63 | + yml = yaml.safe_load(fh) |
| 64 | + lint(yml) |
| 65 | + |
| 66 | + with open(os.environ['GITHUB_OUTPUT'], 'a') as fh: |
| 67 | + fh.write(f'ENV_FILES={env_urls}\n') |
0 commit comments