From 7bde3fe55bb1149a82ab465a90bf2c606adea4d8 Mon Sep 17 00:00:00 2001 From: Bryan Mutai Date: Wed, 16 Apr 2025 03:05:02 +0300 Subject: [PATCH] fix(extend-awards): add check for existing branch with pending awards.csv extension (#2093) --- .github/workflows/extend-awards.yml | 24 +++++++++++++++++++++++- docs/dev/extend-awards.md | 16 +++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/.github/workflows/extend-awards.yml b/.github/workflows/extend-awards.yml index 345ce354..6d5d9a3f 100644 --- a/.github/workflows/extend-awards.yml +++ b/.github/workflows/extend-awards.yml @@ -22,14 +22,36 @@ jobs: with: python-version: '3.13' - run: pip install requests + - name: Check if branch exists + id: check_branch + run: | + git fetch origin extend-awards/patch || echo "Branch does not exist" + if git show-ref --verify --quiet refs/remotes/origin/extend-awards/patch; then + echo "exists=true" >> $GITHUB_ENV + else + echo "exists=false" >> $GITHUB_ENV + fi + - name: Checkout to existing branch + if: env.exists == 'true' + run: | + git checkout extend-awards/patch + git config user.name 'github-actions[bot]' + git config user.email 'github-actions[bot]@users.noreply.github.com' - run: python extend-awards.py env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_CONTEXT: ${{ toJson(github) }} + - name: Commit changes and push to existing branch + if: env.exists == 'true' + run: | + git commit -am "Extending awards.csv" + git push origin extend-awards/patch - uses: peter-evans/create-pull-request@v7 + if: env.exists == 'false' with: add-paths: awards.csv branch: extend-awards/patch commit-message: Extending awards.csv title: Extending awards.csv - body: A PR was merged that solves an issue and awards.csv should be extended. + body: One or more PR's were merged that solve an issue(s) and awards.csv should be extended. Remembere to delete the branch after merging. + delete-branch: true diff --git a/docs/dev/extend-awards.md b/docs/dev/extend-awards.md index 51caf563..b1381075 100644 --- a/docs/dev/extend-awards.md +++ b/docs/dev/extend-awards.md @@ -21,6 +21,20 @@ The primary job consists of several steps: - a script (see below) is executed, which appends lines to [awards.csv](awards.csv) if needed - [create-pull-request](https://github.com/peter-evans/create-pull-request) looks for modified files and creates (or updates) a PR +### Branch Existence Check + +The workflow includes functionality to check if the branch `extend-awards/patch` already exists. This ensures that if a PR is already open for extending `awards.csv`, the workflow will add a commit to the existing branch instead of creating a new PR. The steps are as follows: + +1. **Check if the branch exists**: + - The workflow fetches the branch `extend-awards/patch` from the remote repository. + - If the branch exists, an environment variable `exists=true` is set; otherwise, `exists=false`. + +2. **Handle existing branch**: + - If the branch exists (`exists=true`), the workflow checks out the branch, configures the Github bot user, and commits the changes directly to the branch. + - If the branch does not exist (`exists=false`), the workflow creates a new branch and opens a new PR using the `create-pull-request` action. + +This ensures that changes are consolidated into a single PR when possible. + ## Script The script is [extend-awards.py](extend-awards.py). @@ -41,7 +55,7 @@ Finally, it appends zero, one, or two lines to the awards.csv file. ## Diagnostics -In the GitHub web interface under 'Actions' each invokation of the action can be viewed, including environment and [output and errors](https://en.wikipedia.org/wiki/Standard_streams) of the script. First, the specific invokation is selected, then the job 'if_merged', then the step 'Run python extend-awards.py'. The environment is found by expanding the inner 'Run python extended-awards.py' on the first line. +In the GitHub web interface under 'Actions' each invocation of the action can be viewed, including environment and [output and errors](https://en.wikipedia.org/wiki/Standard_streams) of the script. First, the specific invocation is selected, then the job 'if_merged', then the step 'Run python extend-awards.py'. The environment is found by expanding the inner 'Run python extended-awards.py' on the first line. The normal output includes details about the issue number found, the amount calculation, or the reason for not appending lines.