diff --git a/.github/identify_collections_to_test.py b/.github/identify_collections_to_test.py new file mode 100644 index 00000000..014d7e00 --- /dev/null +++ b/.github/identify_collections_to_test.py @@ -0,0 +1,51 @@ +import os +import json +import fnmatch +import subprocess + + +def get_changed_files(): + return subprocess.check_output([ + 'git', 'diff', + '--name-only', 'origin/main...' + ]).decode().splitlines() + + +def get_collections_from_file(file): + with open(file, 'rt') as f: + data = json.load(f) + + collections = [] + for k in data.keys(): + if 'Collection' in data[k].keys(): + collections += [data[k]['Collection']] + + # we're only running in response to changes in data_summaries/*.json files, + # so if there's such a file without a collection field we don't like that + # and should fail the test + assert len(collections) > 0 + + return collections + + +def main(): + changed_files = get_changed_files() + + collections_to_test = [] + for file in changed_files: + if not fnmatch.fnmatch(file, 'data_summaries/*.json'): + continue + + if not os.path.exists(file): # i.e., file was deleted + continue + + collections_to_test += get_collections_from_file(file) + + if collections_to_test: + print("::set-output name=collections::" + '\n'.join(collections_to_test)) + else: + print("::set-output name=collections::") + + +if __name__ == "__main__": + main() diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..56f628d8 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,40 @@ +name: CI to test new collections + +on: + pull_request: + paths: + - 'data_summaries/**.json' # Trip only when data summary changed/added + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - uses: actions/setup-python@v2 + with: + python-version: '3.11' + + - name: Cache Python dependencies + uses: actions/cache@v2 + id: cache + with: + path: ~/.cache/pip # This path can vary depending on your OS and Python version + key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} + restore-keys: | + ${{ runner.os }}-pip- + + - name: Install dependencies + if: steps.cache.outputs.cache-hit != 'true' + run: | + pip install -U pip + pip install -r requirements.txt + + - name: Identify Collections + run: python .github/identify_collections_to_test.py + + - name: Run Tests + run: | + while IFS= read -r collection; do + python src/test_new_collection.py --collection "$collection" + done <<< "${{ steps.collection.outputs.collections }}"