Skip to content

Commit bb9ada8

Browse files
committed
Update requirements to use PRs
1 parent b8d622d commit bb9ada8

File tree

1 file changed

+78
-8
lines changed

1 file changed

+78
-8
lines changed

.github/workflows/update-requirements.yml

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,45 @@
1-
---
21
name: Update requirements.txt
32

43
permissions:
54
contents: write
5+
pull-requests: write
66

77
on: # yamllint disable-line rule:truthy
8+
# Trigger at every push. Action will also be visible from Pull Requests to main
9+
push:
10+
branches:
11+
- main
12+
paths:
13+
- "pyproject.toml"
14+
- "poetry.lock"
815
pull_request:
916
branches:
1017
- main
1118
paths:
1219
- "pyproject.toml"
20+
- "poetry.lock"
21+
22+
env: # Comment env block if you do not want to apply fixes
23+
# Apply linter fixes configuration
24+
APPLY_FIXES: all # When active, APPLY_FIXES must also be defined as environment variable (in github/workflows/mega-linter.yml or other CI tool)
25+
APPLY_FIXES_EVENT: push # Decide which event triggers application of fixes in a commit or a PR (pull_request, push, all)
26+
# I tried around a lot to make the test workflow run on pull_request events,
27+
# but it it only worked when creating a new PR. By default, this would target the branch of the PR
28+
# and not the main branch. Instead of trying around to cherry-pick the commits from the PR
29+
# etc. I decided to just run the workflow on push events to the main branch and then create
30+
# a PR targeting the main branch.
31+
APPLY_FIXES_MODE: pull_request # If APPLY_FIXES is used, defines if the fixes are directly committed (commit) or posted in a PR (pull_request)
32+
33+
concurrency:
34+
group: ${{ github.ref }}-${{ github.workflow }}
35+
cancel-in-progress: true
1336

1437
jobs:
1538
update_requirements:
1639
runs-on: ubuntu-latest
1740
permissions:
1841
contents: write
42+
pull-requests: write
1943
steps:
2044
- name: Check out repository
2145
uses: actions/checkout@v4
@@ -41,16 +65,62 @@ jobs:
4165
run: python -m poetry export --format requirements.txt --output requirements.txt --extras healpy_support --extras pytorch_support --without-hashes
4266

4367
- name: Update requirements-dev.txt
68+
id: upreq
4469
run: python -m poetry export --with dev --format requirements.txt --output requirements-dev.txt --without-hashes
4570

4671
- name: Prepare commit
47-
run: sudo chown -Rc $UID .git/
72+
run: sudo chown -Rc "$UID" .git/
73+
74+
- name: Print git status
75+
run: git status
76+
77+
- name: Configure Git
78+
run: |
79+
git config --global user.email "[email protected]"
80+
git config --global user.name "megalinter-bot"
81+
82+
- name: Git commit all changes
83+
id: commit_changes
84+
run: |
85+
git add .
86+
if git diff-index --quiet HEAD --; then
87+
echo "No changes to commit."
88+
echo "changes_made=false" >> "$GITHUB_ENV"
89+
else
90+
git commit -a -m "Committing all changes"
91+
echo "changes_made=true" >> "$GITHUB_ENV"
92+
fi
93+
94+
# Create Pull Request step
95+
- name: Create Pull Request with applied fixes
96+
id: cpr
97+
if: github.ref == 'refs/heads/main' && env.changes_made == 'true' && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'pull_request' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && !contains(github.event.head_commit.message, 'skip fix')
98+
uses: peter-evans/create-pull-request@v5
99+
with:
100+
token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
101+
commit-message: "Update requirements"
102+
title: "Update requirements"
103+
branch: update-requirements
104+
labels: bot
105+
base: main
106+
107+
# Output PR details
108+
- name: Create PR output
109+
if: github.ref == 'refs/heads/main' && env.changes_made == 'true' && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'pull_request' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && !contains(github.event.head_commit.message, 'skip fix')
110+
run: |
111+
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
112+
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
113+
114+
# Push new commit if applicable (for now works only on PR from same repository, not from forks)
115+
- name: Prepare commit
116+
if: env.changes_made == 'true' && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'commit' && github.ref != 'refs/heads/main' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && !contains(github.event.head_commit.message, 'skip fix')
117+
run: sudo chown -Rc "$UID" .git/
48118

49-
- name: Commit changes
119+
- name: Commit and push new requirements.txt and requirements-dev.txt
120+
if: env.changes_made == 'true' && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'commit' && github.ref != 'refs/heads/main' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && !contains(github.event.head_commit.message, 'skip fix')
50121
uses: stefanzweifel/git-auto-commit-action@v4
51122
with:
52-
commit_message: "Update requirements.txt"
53-
commit_user_name: "github-actions"
54-
commit_user_email: "[email protected]"
55-
commit_author: "github-actions <[email protected]>"
56-
branch: ${{ github.head_ref }}
123+
branch: ${{ github.event.pull_request.head.ref || github.head_ref || github.ref }}
124+
commit_message: "Update requirements"
125+
commit_user_name: update-requirements-bot
126+
commit_user_email: [email protected]

0 commit comments

Comments
 (0)