Skip to content

Commit f93dbbd

Browse files
🔒 Fix update dependencies workflow to prevent force-pushing to unrelated branches (#78)
Co-authored-by: openhands <openhands@all-hands.dev>
1 parent 43d0676 commit f93dbbd

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

‎.github/workflows/update_dependencies.yaml‎

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,38 +56,60 @@ jobs:
5656
git config user.name github-actions
5757
git config user.email github-actions@github.com
5858
59+
# Function to commit changes
60+
commit_changes() {
61+
git add requirements.txt pyproject.toml pdm.lock
62+
git commit -m "Update dependencies"
63+
}
64+
65+
# Function to create PR
66+
create_pr() {
67+
local BRANCH=$1
68+
gh pr create \
69+
--title "Update dependencies" \
70+
--body "This PR updates the project dependencies. Please review the changes and merge if everything looks good." \
71+
--base ${{ github.ref_name }} \
72+
--head $BRANCH \
73+
--label "automated-dependencies-update"
74+
}
75+
5976
if [ -n "${{ inputs.pr_branch }}" ]; then
6077
# Push to existing branch
6178
BRANCH_NAME=$(echo ${{ inputs.pr_branch }} | cut -d'/' -f 3)
6279
git checkout $BRANCH_NAME
63-
git add requirements.txt pyproject.toml pdm.lock
64-
git commit -m "Update dependencies"
80+
commit_changes
6581
git push origin $BRANCH_NAME
6682
else
67-
# Check for existing PR
68-
EXISTING_PR=$(gh pr list --search "Update dependencies in:title is:open" --json headRefName,number -q '.[0]')
83+
# Check for existing PR - strict search for exact title and our specific branch pattern
84+
EXISTING_PR=$(gh pr list --search "in:title Update dependencies is:open label:automated-dependencies-update" --json headRefName,number,author -q '.[0]')
6985
7086
if [ -n "$EXISTING_PR" ]; then
71-
# Update existing PR
87+
# Multiple validation checks before using an existing branch
7288
BRANCH_NAME=$(echo $EXISTING_PR | jq -r .headRefName)
73-
git checkout -B $BRANCH_NAME
74-
git add requirements.txt pyproject.toml pdm.lock
75-
git commit -m "Update dependencies"
76-
git push -f origin $BRANCH_NAME
89+
PR_AUTHOR=$(echo $EXISTING_PR | jq -r .author.login)
90+
91+
if [[ "$PR_AUTHOR" == "github-actions[bot]" && "$BRANCH_NAME" == update-dependencies-* ]]; then
92+
echo "Found valid PR with branch $BRANCH_NAME by github-actions[bot]. Updating it."
93+
git checkout -B $BRANCH_NAME
94+
commit_changes
95+
git push -f origin $BRANCH_NAME
96+
else
97+
echo "Found PR but it's not from our bot or wrong branch pattern. Creating new branch."
98+
NEW_BRANCH="update-dependencies-${{ github.run_id }}"
99+
git checkout -b $NEW_BRANCH
100+
commit_changes
101+
git push origin $NEW_BRANCH
102+
create_pr $NEW_BRANCH
103+
fi
77104
else
78-
# Create new branch and PR
105+
echo "No existing PR found. Creating new branch and PR."
79106
NEW_BRANCH="update-dependencies-${{ github.run_id }}"
80107
git checkout -b $NEW_BRANCH
81-
git add requirements.txt pyproject.toml pdm.lock
82-
git commit -m "Update dependencies"
108+
commit_changes
83109
git push origin $NEW_BRANCH
84-
85-
gh pr create \
86-
--title "Update dependencies" \
87-
--body "This PR updates the project dependencies. Please review the changes and merge if everything looks good." \
88-
--base ${{ github.ref_name }} \
89-
--head $NEW_BRANCH
110+
create_pr $NEW_BRANCH
90111
fi
91112
fi
113+
fi
92114
env:
93115
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)