From aad0532114299916fdacd472ee04351905ac8678 Mon Sep 17 00:00:00 2001 From: Snailedlt <43886029+Snailedlt@users.noreply.github.com> Date: Sat, 15 Oct 2022 15:43:52 +0200 Subject: [PATCH] Check if PR base is develop fix #1458 --- .github/scripts/build_assets/api_handler.py | 28 +++++++++++++++++++++ .github/scripts/build_assets/arg_getters.py | 10 ++++++-- .github/scripts/check_icon_pr.py | 12 ++++++++- .github/workflows/check_icon_pr.yml | 9 ++++++- 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/.github/scripts/build_assets/api_handler.py b/.github/scripts/build_assets/api_handler.py index 042edb5e..e3eb88b9 100644 --- a/.github/scripts/build_assets/api_handler.py +++ b/.github/scripts/build_assets/api_handler.py @@ -65,6 +65,34 @@ def get_merged_pull_reqs(token, page, log_output: FileIO=sys.stdout): if merged_pull_req["merged_at"] is not None] +def get_pull_req(token, pr_number): + """ + Get a PR based on the PR number + See https://docs.github.com/en/rest/pulls/pulls#get-a-pull-request for more details on the parameters. + :param token, a GitHub API token. + :param pr_number, the number of the pull request. + """ + url = f"{base_url}pulls/{pr_number}" + headers = { + "Authorization": f"token {token}" + } + response = requests.get(url, headers=headers) + if not response: + print(f"Can't query the GitHub API. Status code is {response.status_code}. Message is {response.text}") + sys.exit(1) + return response.json() + + + +def get_pr_base_branch(pull_req_data): + """ + Check whether the PR's base is develop. Meaning, if the PR is being committed to develop + :param pull_req_data - the data on a specific pull request from GitHub. + :return the base ref of the pull_req_data + """ + return pull_req_data["base"]["ref"] + + def is_feature_icon(pull_req_data): """ Check whether the pullData is a feature:icon PR. diff --git a/.github/scripts/build_assets/arg_getters.py b/.github/scripts/build_assets/arg_getters.py index d086bd33..2dbe0c5a 100644 --- a/.github/scripts/build_assets/arg_getters.py +++ b/.github/scripts/build_assets/arg_getters.py @@ -4,7 +4,7 @@ from build_assets.PathResolverAction import PathResolverAction def get_selenium_runner_args(peek_mode=False): """ - Get the commandline arguments for the icomoon_peek.py and + Get the commandline arguments for icomoon_peek.py and icomoon_build.py. """ parser = ArgumentParser(description="Upload svgs to Icomoon to create icon files.") @@ -45,13 +45,19 @@ def get_selenium_runner_args(peek_mode=False): def get_check_icon_pr_args(): """ - Get the commandline arguments for the check_icon_pr.py. + Get the commandline arguments for check_icon_pr.py. """ parser = ArgumentParser(description="Check the SVGs to ensure their attributes are correct. Run whenever a PR is opened") + parser.add_argument("token", + help="The GitHub token to access the GitHub REST API.") + parser.add_argument("pr_title", help="The title of the PR that we are peeking at") + parser.add_argument("pr_number", + help="The number of the PR that we are peeking at") + parser.add_argument("icons_folder_path", help="The path to the icons folder", action=PathResolverAction) diff --git a/.github/scripts/check_icon_pr.py b/.github/scripts/check_icon_pr.py index 40c6fe13..b2081077 100644 --- a/.github/scripts/check_icon_pr.py +++ b/.github/scripts/check_icon_pr.py @@ -5,7 +5,7 @@ from pathlib import Path # pycharm complains that build_assets is an unresolved ref # don't worry about it, the script still runs -from build_assets import filehandler, arg_getters, util +from build_assets import filehandler, arg_getters, util, api_handler def main(): @@ -16,6 +16,13 @@ def main(): """ args = arg_getters.get_check_icon_pr_args() try: + # check that the base branch of the PR is develop + pr_err_msg = "" + pr_data = api_handler.get_pull_req(args.token, args.pr_number) + pr_base_branch = api_handler.get_pr_base_branch(pr_data) + if pr_base_branch != "develop": + pr_err_msg = f"The PR's base branch is `{pr_base_branch}`, but should be `develop`, please change the PR so that it's based on, and merged into `develop`" + all_icons = filehandler.get_json_file_content(args.devicon_json_path) # get only the icon object that has the name matching the pr title @@ -40,6 +47,9 @@ def main(): svg_err_msg = check_svgs(svgs) err_msg = [] + if pr_err_msg != "": + err_msg.append(pr_err_msg) + if devicon_err_msg != "": err_msg.append(devicon_err_msg) diff --git a/.github/workflows/check_icon_pr.yml b/.github/workflows/check_icon_pr.yml index 6a57bab4..59a2edcf 100644 --- a/.github/workflows/check_icon_pr.yml +++ b/.github/workflows/check_icon_pr.yml @@ -12,10 +12,17 @@ jobs: with: python-version: 3.8 + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r ./.github/scripts/requirements.txt + - name: Run the check_svg script env: + TOKEN: ${{ secrets.GITHUB_TOKEN }} PR_TITLE: ${{ github.event.pull_request.title }} - run: python ./.github/scripts/check_icon_pr.py "$PR_TITLE" ./icons ./devicon.json + PR_NUMBER: ${{ github.event.number }} + run: python ./.github/scripts/check_icon_pr.py "$TOKEN" "$PR_TITLE" "$PR_NUMBER" ./icons ./devicon.json - name: Upload the err messages uses: actions/upload-artifact@v2