Skip to content

Commit 3430aa7

Browse files
authored
Merge pull request #8 from qiime2/linting-action
start adding linter
2 parents b6b65f1 + c0cab9f commit 3430aa7

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

.github/scripts/lint.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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')

.github/workflows/lint-pr.yml

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: lint-pr
2+
3+
# Run on PR changing files in plugins dir against main
4+
on:
5+
pull_request:
6+
branches:
7+
- 'main'
8+
paths:
9+
- '/plugins/**'
10+
11+
jobs:
12+
lint-yml:
13+
runs-on: ubuntu-latest
14+
outputs:
15+
ENV_FILES: ${{ steps.lint.outputs.ENV_FILES }}
16+
17+
steps:
18+
# Checkout new commit and prior HEAD
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 2
23+
24+
# Get diff
25+
# This feels odd to me got it from
26+
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#multiline-strings
27+
- name: Get Diff
28+
run: |
29+
{
30+
echo 'DIFF<<EOF'
31+
git diff --name-only HEAD~ HEAD
32+
echo EOF
33+
} >> "$GITHUB_ENV"
34+
35+
# Lint all plugins/** files in diff (filtering of diff occurs in Python)
36+
# Also gets all env files in added plugins for env testing
37+
- name: Lint Yml and Get Env Files
38+
id: lint
39+
run: python .github/scripts/lint.py ${{ secrets.GITHUB_TOKEN }} $DIFF
40+
41+
# Install all envs found and at least make sure `qiime info` runs
42+
# TODO: Make this attempt to run pytest?
43+
test-envs:
44+
needs: lint-yml
45+
strategy:
46+
matrix:
47+
os: [ubuntu-latest, macos-13]
48+
url: ${{ fromJSON(needs.lint-yml.outputs.ENV_FILES) }}
49+
runs-on: ${{ matrix.os }}
50+
env:
51+
prefix: ./test
52+
53+
steps:
54+
- name: Set up Miniconda
55+
uses: conda-incubator/setup-miniconda@v3
56+
with:
57+
activate-environment: ''
58+
architecture: 'x64'
59+
auto-activate-base: true
60+
miniconda-version: 'latest'
61+
62+
# Hacky stff to make it so I can activate the conda env for testing
63+
- name: Set up Test Environment
64+
env:
65+
url: ${{ matrix.url }}
66+
run: |
67+
conda env create -y -p ${{ env.prefix }} -f $url
68+
mkdir -p ${{ env.prefix }}/etc
69+
cat <<EOF > '${{ env.prefix }}/etc/activate.sh'
70+
. "$CONDA/etc/profile.d/conda.sh"
71+
conda activate '${{ env.prefix }}'
72+
EOF
73+
chmod +x 'test/etc/activate.sh'
74+
75+
- name: Test
76+
run: |
77+
source ${{ env.prefix }}/etc/activate.sh
78+
conda activate ${{ env.prefix }}
79+
qiime info
80+
81+
# Grab the target repo and validate it?
82+
# Description?
83+
# README?
84+
# Env files?

0 commit comments

Comments
 (0)