From 70df11eef5399aef895e33bb7fe8d9de965a054d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Kalsnes=20Hagen?= <43886029+Snailedlt@users.noreply.github.com> Date: Wed, 26 Apr 2023 18:44:36 +0200 Subject: [PATCH] Bugfix/fix in-develop labeler (#1410) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix issue not being labeled with in-develop * restrict to only run on merge to develop * Remove Python setup Co-authored-by: Josélio Júnior <76992016+lunatic-fox@users.noreply.github.com> --------- Co-authored-by: Josélio Júnior <76992016+lunatic-fox@users.noreply.github.com> Co-authored-by: David Leal --- .github/scripts/build_assets/api_handler.py | 16 ++++++++++ .github/scripts/build_assets/arg_getters.py | 4 +-- .github/scripts/in_develop_labeler.py | 6 +++- .github/workflows/in_develop_labeler.yml | 29 ++++++++++++++----- .../in_develop_labeler_preflight.yml | 24 +++++++++++++++ 5 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/in_develop_labeler_preflight.yml diff --git a/.github/scripts/build_assets/api_handler.py b/.github/scripts/build_assets/api_handler.py index 042edb5e..b11c2dc6 100644 --- a/.github/scripts/build_assets/api_handler.py +++ b/.github/scripts/build_assets/api_handler.py @@ -180,3 +180,19 @@ def get_issues_by_labels(token: str, labels: List[str]): issues.extend(issues_only) return issues + + +def get_pr_by_number(token: str, pr_num: str): + url = base_url + "pulls/" + pr_num + headers = { + "Authorization": f"token {token}" + } + + print(f"Querying the GitHub API for requests") + 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) + + pr = response.json() + return pr diff --git a/.github/scripts/build_assets/arg_getters.py b/.github/scripts/build_assets/arg_getters.py index 084321f0..8f82f0e6 100644 --- a/.github/scripts/build_assets/arg_getters.py +++ b/.github/scripts/build_assets/arg_getters.py @@ -82,7 +82,7 @@ def get_in_develop_labeler_args(): help="The GitHub token to access the GitHub REST API.", type=str) - parser.add_argument("body", - help="The PR's initial comment by the author AKA the `body` attribute of the `pull_request` API object.", + parser.add_argument("pr_num", + help="The PR's number", type=str) return parser.parse_args() diff --git a/.github/scripts/in_develop_labeler.py b/.github/scripts/in_develop_labeler.py index c065b966..3d329c99 100644 --- a/.github/scripts/in_develop_labeler.py +++ b/.github/scripts/in_develop_labeler.py @@ -4,8 +4,12 @@ from build_assets import arg_getters, api_handler def main(): args = arg_getters.get_in_develop_labeler_args() try: + #get pr body + pr_body = api_handler.get_pr_by_number(args.token, args.pr_num)["body"] + # find the issue closing line - issue_line = [line for line in args.body.split("\n") if line.startswith("**This PR closes")][0] + print(pr_body.split("\n")) + issue_line = [line for line in pr_body.split("\n") if line.startswith("**This PR closes")][0] print("Issue Line is " + issue_line) issue_pattern = re.compile(r"\d+") diff --git a/.github/workflows/in_develop_labeler.yml b/.github/workflows/in_develop_labeler.yml index 61b7b433..d212091b 100644 --- a/.github/workflows/in_develop_labeler.yml +++ b/.github/workflows/in_develop_labeler.yml @@ -1,12 +1,14 @@ name: Label Issue In Develop -on: - pull_request: - types: [closed] +on: + workflow_run: + workflows: ['On Develop PR Merge'] + types: + - completed jobs: - label: + label_preflight: name: Label Issue In Develop runs-on: ubuntu-18.04 - if: github.event.pull_request.merged == true + if: ${{ github.event.workflow_run.conclusion == 'success' }} steps: - uses: actions/checkout@v2 @@ -19,9 +21,22 @@ jobs: run: | python -m pip install --upgrade pip pip install -r ./.github/scripts/requirements.txt + + - name: Download workflow artifact + uses: dawidd6/action-download-artifact@v2.11.0 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + workflow: peek_icons.yml + run_id: ${{ github.event.workflow_run.id }} + + - name: Read the pr_num file + id: pr_num_reader + uses: juliangruber/read-file-action@v1.0.0 + with: + path: ./pr_num/pr_num.txt - name: Run in_develop_labeler.py env: TOKEN: ${{ secrets.GITHUB_TOKEN }} - BODY: ${{ github.event.pull_request.body }} - run: python ./.github/scripts/in_develop_labeler.py $TOKEN "$BODY" + PR_NUM: ${{ steps.pr_num_reader.outputs.content }} + run: python ./.github/scripts/in_develop_labeler.py $TOKEN "$PR_NUM" diff --git a/.github/workflows/in_develop_labeler_preflight.yml b/.github/workflows/in_develop_labeler_preflight.yml new file mode 100644 index 00000000..21a5430c --- /dev/null +++ b/.github/workflows/in_develop_labeler_preflight.yml @@ -0,0 +1,24 @@ +name: On Develop PR Merge +on: + pull_request: + types: [closed] + branches: [develop] +jobs: + save_pr_num_in_artifact: + name: Preflight Label Issue In Develop + runs-on: ubuntu-18.04 + if: github.event.pull_request.merged == true + steps: + - uses: actions/checkout@v2 + + - name: Save the PR number in an artifact + shell: bash + env: + PR_NUM: ${{ github.event.number }} + run: echo $PR_NUM > pr_num.txt + + - name: Upload the PR number + uses: actions/upload-artifact@v2.2.4 + with: + name: pr_num + path: ./pr_num.txt