-
Notifications
You must be signed in to change notification settings - Fork 27
120 lines (100 loc) · 3.74 KB
/
Copy pathrequirments-sync.yml
File metadata and controls
120 lines (100 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Netlify requires requirements.txt (humans do not)
name: Sync requirements.txt with pyproject.toml
on:
pull_request:
paths: ['poetry.lock']
types: [opened, synchronize, reopened]
permissions:
contents: write
jobs:
detect-requirements-delta:
runs-on: ubuntu-latest
outputs:
has_change: ${{ steps.detect.outputs.has_change }}
# Skip if the last commit was from the bot (prevent infinite loops)
if: github.event.head_commit.author.name != 'github-actions[bot]'
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref || github.ref_name }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version-file: 'pyproject.toml'
- name: Install Poetry
run: pip install poetry
- name: Install Dependencies via Poetry
run: poetry install --only main --no-root
- name: Detect whether requirements.txt has change
id: detect
run: |
output=$(make requirements.txt 2>&1)
echo "$output"
# Check whether Make output suggests file is up to date
if echo "$output" | grep -qi "up to date\|up-to-date\|already up to date"; then
echo "has_change=false" >> $GITHUB_OUTPUT
echo "::notice::requirements.txt seems up to date"
else
echo "has_change=true" >> $GITHUB_OUTPUT
fi
prevent-bot-commit-if-human-edit:
runs-on: ubuntu-latest
needs: detect-requirements-delta
if: needs.detect-requirements-delta.outputs.has_change == 'true'
outputs:
is_human_edit: ${{ steps.check.outputs.is_human_edit }}
offender_author: ${{ steps.check.outputs.offender_author }}
offender_subject: ${{ steps.check.outputs.offender_subject }}
env:
ALLOWED_BOTS: 'github-actions[bot],dependabot[bot]'
COMMIT_MSG_FILE: .github/commit-messages/requirements_update.txt
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref || github.ref_name }}
fetch-depth: 0
- name: Check for human edits
id: check
env:
MODE: detect
run: bash ./.github/actions/validate-requirements/check.sh
commit-requirements-delta:
runs-on: ubuntu-latest
needs: [detect-requirements-delta, prevent-bot-commit-if-human-edit]
if: needs.detect-requirements-delta.outputs.has_change == 'true' && needs.prevent-bot-commit-if-human-edit.outputs.is_human_edit == 'false'
env:
COMMIT_MSG_FILE: .github/commit-messages/requirements_update.txt
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref || github.ref_name }}
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version-file: 'pyproject.toml'
- name: Install Poetry
run: pip install poetry
- name: Generate requirements.txt
run: make requirements.txt
- name: Configure Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Commit requirements.txt if changed
run: |
git add -f requirements.txt
if git diff --staged --quiet; then
echo "No changes to requirements.txt"
else
commit_msg=$(sed -n '1p' "$COMMIT_MSG_FILE" 2>/dev/null | tr -d '\r')
if [ -z "$commit_msg" ]; then
echo "::error::Missing or empty canonical commit message file: $COMMIT_MSG_FILE"
exit 1
fi
git commit -m "$commit_msg"
git push
fi