generated from nicebots-xyz/botkit
-
Notifications
You must be signed in to change notification settings - Fork 0
126 lines (114 loc) · 4.35 KB
/
update_dependencies.yaml
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
121
122
123
124
125
126
name: Update dependencies
on:
schedule:
- cron: '0 0 * * 0' # Runs at 00:00 every Sunday
workflow_dispatch:
inputs:
pr_branch:
description: 'Branch to push changes to (optional)'
required: false
type: string
workflow_call:
inputs:
pr_branch:
description: 'Branch to push changes to'
required: true
type: string
jobs:
update-deps:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
name: Checkout repository
with:
fetch-depth: 0 # Fetch all history for all branches
- name: Checkout target branch
if: inputs.pr_branch
run: |
BRANCH_NAME=$(echo ${{ inputs.pr_branch }} | sed 's|refs/heads/||')
git fetch origin $BRANCH_NAME
git checkout $BRANCH_NAME
git pull origin $BRANCH_NAME
- name: Setup PDM
uses: pdm-project/setup-pdm@v4
with:
cache: true
- name: Lock dependencies
run: pdm lock -G :all
- name: Export requirements
run: pdm run export
- name: Check for changes
id: git-check
run: |
git diff --exit-code --quiet requirements.txt || echo "changed=true" >> $GITHUB_OUTPUT
- name: Create required label if not exists
run: |
# Check if label exists
LABEL_EXISTS=$(gh label list | grep "automated-dependencies-update" || true)
if [ -z "$LABEL_EXISTS" ]; then
echo "Creating automated-dependencies-update label..."
gh label create "automated-dependencies-update" \
--color "2DA44E" \
--description "Automated PR for updating project dependencies"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Handle dependency updates
if: steps.git-check.outputs.changed == 'true'
run: |
git config user.name github-actions
git config user.email github-actions@github.com
# Function to commit changes
commit_changes() {
git add requirements.txt pyproject.toml pdm.lock
git commit -m "Update dependencies"
}
# Function to create PR
create_pr() {
local BRANCH=$1
gh pr create \
--title "Update dependencies" \
--body "This PR updates the project dependencies. Please review the changes and merge if everything looks good." \
--base ${{ github.ref_name }} \
--head $BRANCH \
--label "automated-dependencies-update"
}
if [ -n "${{ inputs.pr_branch }}" ]; then
# Push to existing branch
BRANCH_NAME=$(echo ${{ inputs.pr_branch }} | cut -d'/' -f 3)
git checkout $BRANCH_NAME
commit_changes
git push origin $BRANCH_NAME
else
# Check for existing PR - strict search for exact title and our specific branch pattern
EXISTING_PR=$(gh pr list --search "in:title Update dependencies is:open label:automated-dependencies-update" --json headRefName,number,author -q '.[0]')
if [ -n "$EXISTING_PR" ]; then
# Check if PR has our automation label
BRANCH_NAME=$(echo $EXISTING_PR | jq -r .headRefName)
if [[ "$BRANCH_NAME" == update-dependencies-* ]]; then
echo "Found valid automated PR with branch $BRANCH_NAME. Updating it."
git checkout -B $BRANCH_NAME
commit_changes
git push -f origin $BRANCH_NAME
else
echo "Found PR but wrong branch pattern. Creating new branch."
NEW_BRANCH="update-dependencies-${{ github.run_id }}"
git checkout -b $NEW_BRANCH
commit_changes
git push origin $NEW_BRANCH
create_pr $NEW_BRANCH
fi
else
echo "No existing PR found. Creating new branch and PR."
NEW_BRANCH="update-dependencies-${{ github.run_id }}"
git checkout -b $NEW_BRANCH
commit_changes
git push origin $NEW_BRANCH
create_pr $NEW_BRANCH
fi
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}