fix(extend-awards): add check for existing branch with pending awards.csv extension (#2093)

This commit is contained in:
Bryan Mutai 2025-04-16 03:05:02 +03:00 committed by GitHub
parent 66dbf2496e
commit 7bde3fe55b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 2 deletions

View File

@ -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

View File

@ -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.