Skip to content

Commit 7bde3fe

Browse files
authored
fix(extend-awards): add check for existing branch with pending awards.csv extension (#2093)
1 parent 66dbf24 commit 7bde3fe

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

.github/workflows/extend-awards.yml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,36 @@ jobs:
2222
with:
2323
python-version: '3.13'
2424
- run: pip install requests
25+
- name: Check if branch exists
26+
id: check_branch
27+
run: |
28+
git fetch origin extend-awards/patch || echo "Branch does not exist"
29+
if git show-ref --verify --quiet refs/remotes/origin/extend-awards/patch; then
30+
echo "exists=true" >> $GITHUB_ENV
31+
else
32+
echo "exists=false" >> $GITHUB_ENV
33+
fi
34+
- name: Checkout to existing branch
35+
if: env.exists == 'true'
36+
run: |
37+
git checkout extend-awards/patch
38+
git config user.name 'github-actions[bot]'
39+
git config user.email 'github-actions[bot]@users.noreply.github.com'
2540
- run: python extend-awards.py
2641
env:
2742
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2843
GITHUB_CONTEXT: ${{ toJson(github) }}
44+
- name: Commit changes and push to existing branch
45+
if: env.exists == 'true'
46+
run: |
47+
git commit -am "Extending awards.csv"
48+
git push origin extend-awards/patch
2949
- uses: peter-evans/create-pull-request@v7
50+
if: env.exists == 'false'
3051
with:
3152
add-paths: awards.csv
3253
branch: extend-awards/patch
3354
commit-message: Extending awards.csv
3455
title: Extending awards.csv
35-
body: A PR was merged that solves an issue and awards.csv should be extended.
56+
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.
57+
delete-branch: true

docs/dev/extend-awards.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ The primary job consists of several steps:
2121
- a script (see below) is executed, which appends lines to [awards.csv](awards.csv) if needed
2222
- [create-pull-request](https://github.com/peter-evans/create-pull-request) looks for modified files and creates (or updates) a PR
2323

24+
### Branch Existence Check
25+
26+
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:
27+
28+
1. **Check if the branch exists**:
29+
- The workflow fetches the branch `extend-awards/patch` from the remote repository.
30+
- If the branch exists, an environment variable `exists=true` is set; otherwise, `exists=false`.
31+
32+
2. **Handle existing branch**:
33+
- 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.
34+
- 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.
35+
36+
This ensures that changes are consolidated into a single PR when possible.
37+
2438
## Script
2539

2640
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.
4155

4256
## Diagnostics
4357

44-
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.
58+
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.
4559

4660
The normal output includes details about the issue number found, the amount calculation, or the reason for not appending lines.
4761

0 commit comments

Comments
 (0)