From 7b64801ed2cf842b1ef03cfc48aefdf6254e46b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=AB=20Turner?= Date: Fri, 13 Dec 2024 17:22:19 +0000 Subject: [PATCH] Added Latin e.g. to test (#181) * Added Latin e.g. to test * Deleted paths as created from another gh-action * Added requirements.txt as called in gh-action * Added Turing way pull_files test * Updated PR url from Turing Way to NHS-R * Removed Latin --- .github/workflows/no-bad-latin.yml | 2 - tests/pull_files.py | 78 ++++++++++++++++++++++++++++++ tests/requirements.txt | 3 ++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 tests/pull_files.py create mode 100644 tests/requirements.txt diff --git a/.github/workflows/no-bad-latin.yml b/.github/workflows/no-bad-latin.yml index 9dc607b..b4c74ef 100644 --- a/.github/workflows/no-bad-latin.yml +++ b/.github/workflows/no-bad-latin.yml @@ -14,8 +14,6 @@ on: pull_request: branches: - main - paths: - - "_book/" # Set up the Continuous Integration job jobs: diff --git a/tests/pull_files.py b/tests/pull_files.py new file mode 100644 index 0000000..8539a5f --- /dev/null +++ b/tests/pull_files.py @@ -0,0 +1,78 @@ +""" +Script to pull changed files in a Pull Request using a GET resquest to the +GitHub API. Copyright The Turing Way Community https://github.com/the-turing-way/the-turing-way +""" +import requests +import argparse + + +def parse_args(): + """Construct the command line interface for the script""" + DESCRIPTION = "Script to check for occurences of 'Lorem Ipsum' in Markdown files" + parser = argparse.ArgumentParser(description=DESCRIPTION) + + parser.add_argument( + "--pull-request", + type=str, + default=None, + help="If the script is be run on files changed by a pull request, parse the PR number", + ) + + return parser.parse_args() + + +def get_files_from_pr(pr_num): + """Return a list of changed files from a GitHub Pull Request + + Arguments: + pr_num {str} -- Pull Request number to get modified files from + + Returns: + {list} -- List of modified filenames + """ + files = [] + pr_url = f"https://api.github.com/repos/nhs-r-community/NHSR-way/pulls/{pr_num}/files" + resp = requests.get(pr_url) + + # Raising for status to avoid ending up with red-herring tracebacks later + resp.raise_for_status() + + for item in resp.json(): + files.append(item["filename"]) + + return files + + +def filter_files(pr_num, start_phrase="book/website", ignore_suffix=None): + """Filter modified files from a Pull Request by a start phrase + + Arguments: + pr_num {str} -- Number of the Pull Request to get modified files from + + Keyword Arguments: + start_phrase {str} -- Start phrase to filter changed files by + (default: {"book/website"}) + + ignore_suffix {str} -- File suffix or tuple of suffixes to ignore. + + + Returns: + {list} -- List of filenames that begin with the desired start phrase + """ + files = get_files_from_pr(pr_num) + filtered_files = [] + + if ignore_suffix is None: + ignore_suffix = () + + for filename in files: + if filename.startswith(start_phrase) and not filename.endswith(ignore_suffix): + filtered_files.append(filename) + + return filtered_files + + +if __name__ == "__main__": + args = parse_args() + changed_files = filter_files(args.pull_request) + print(changed_files) diff --git a/tests/requirements.txt b/tests/requirements.txt new file mode 100644 index 0000000..ea4f13e --- /dev/null +++ b/tests/requirements.txt @@ -0,0 +1,3 @@ +jsonschema +requests +ruamel.yaml \ No newline at end of file