Skip to content

Commit bb97ae3

Browse files
committed
.github: add workflow to open downstream PR
Signed-off-by: flouthoc <[email protected]>
1 parent 921803e commit bb97ae3

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
name: 'Open downstream PRs'
2+
3+
on:
4+
pull_request_target
5+
6+
jobs:
7+
sync:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: 'Checkout Self'
11+
uses: actions/checkout@v4
12+
# This checks out the code from the PR branch itself
13+
14+
- name: 'Check for Go file changes'
15+
id: check_go_changes
16+
run: |
17+
# Get the list of changed files in the PR
18+
CHANGED_FILES=$(gh pr view ${{ github.event.pull_request.number }} --repo ${{ github.repository }} --json files --jq '.files[].path')
19+
echo "Changed files in PR:"
20+
echo "$CHANGED_FILES"
21+
22+
# Check if any .go files were changed
23+
GO_FILES_CHANGED=$(echo "$CHANGED_FILES" | grep -E '\.go$' || echo "")
24+
25+
if [ -n "$GO_FILES_CHANGED" ]; then
26+
echo "Go files were changed:"
27+
echo "$GO_FILES_CHANGED"
28+
echo "should_run=true" >> $GITHUB_OUTPUT
29+
echo "go_changes=true" >> $GITHUB_ENV
30+
else
31+
echo "No Go files were changed in this PR."
32+
echo "should_run=false" >> $GITHUB_OUTPUT
33+
echo "go_changes=false" >> $GITHUB_ENV
34+
fi
35+
env:
36+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
37+
38+
- name: 'Setup Go'
39+
if: steps.check_go_changes.outputs.should_run == 'true'
40+
uses: actions/setup-go@v4
41+
with:
42+
go-version: '1.21'
43+
44+
- name: 'Checkout forked buildah'
45+
if: steps.check_go_changes.outputs.should_run == 'true'
46+
uses: actions/checkout@v4
47+
with:
48+
repository: 'containers/buildah' # The target repository
49+
path: 'buildah' # Checkout into a sub-directory
50+
token: ${{ secrets.VENDOR_TOKEN_PODMANBOT }}
51+
52+
- name: 'Vendor Code from this repo to buildah'
53+
if: steps.check_go_changes.outputs.should_run == 'true'
54+
run: |
55+
# Get the current commit SHA from the PR
56+
COMMIT_SHA="${{ github.event.pull_request.head.sha }}"
57+
echo "Using commit SHA: $COMMIT_SHA"
58+
59+
cd buildah
60+
# Create a unique branch name based on the container-libs PR number
61+
BRANCH_NAME="sync/container-libs-${{ github.event.pull_request.number }}"
62+
git switch -c $BRANCH_NAME
63+
git remote add upstream https://github.com/containers/buildah.git
64+
git fetch upstream
65+
git rebase upstream/main
66+
67+
68+
echo "Current go.mod before update:"
69+
cat go.mod
70+
71+
# Update the go.mod file to use the specific commit
72+
# TODO: Add storage and image library here as well.
73+
go mod edit -replace go.podman.io/common=github.com/containers/container-libs/common@${COMMIT_SHA}
74+
75+
echo "After go mod edit"
76+
cat go.mod
77+
78+
# Download and verify the module
79+
GOWORK=off go mod tidy
80+
GOWORK=off go mod vendor
81+
GOWORK=off go mod verify
82+
83+
echo "Updated go.mod:"
84+
cat go.mod
85+
86+
- name: 'Commit and Push to buildah'
87+
if: steps.check_go_changes.outputs.should_run == 'true'
88+
run: |
89+
cd buildah
90+
git config user.name "github-actions[bot]"
91+
git config user.email "github-actions[bot]@users.noreply.github.com"
92+
93+
BRANCH_NAME="sync/container-libs-${{ github.event.pull_request.number }}"
94+
git switch $BRANCH_NAME
95+
96+
git add .
97+
git commit -m "feat: Vendor changes from podmanbot/container-libs#${{ github.event.pull_request.number }}"
98+
99+
# Force push to update the branch if the action re-runs on 'synchronize'
100+
git push origin $BRANCH_NAME --force
101+
102+
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
103+
104+
- name: 'Create or Update Pull Request in Buildah'
105+
if: steps.check_go_changes.outputs.should_run == 'true'
106+
id: create_pr
107+
env:
108+
GH_TOKEN: ${{ secrets.VENDOR_TOKEN_PODMANBOT }}
109+
SELF_REPO_PR_NUMBER: ${{ github.event.pull_request.number }}
110+
SELF_REPO_PR_URL: ${{ github.event.pull_request.html_url }}
111+
SELF_REPO_PR_TITLE: ${{ github.event.pull_request.title }}
112+
run: |
113+
cd buildah
114+
115+
BRANCH_NAME="sync/container-libs-${{ github.event.pull_request.number }}"
116+
PR_TITLE="Sync: ${{ env.SELF_REPO_PR_TITLE }}"
117+
PR_BODY="This PR automatically vendors changes from [repo-A#${{ env.SELF_REPO_PR_NUMBER }}](${{ env.SELF_REPO_PR_URL }})."
118+
119+
# Check if PR already exists for this branch
120+
echo "Searching for existing PR with branch: $BRANCH_NAME"
121+
122+
EXISTING_PR_URL=$(gh pr list --repo containers/buildah --head "$BRANCH_NAME" --json url --jq '.[0].url // empty' 2>/dev/null || echo "")
123+
124+
if [ -n "$EXISTING_PR_URL" ]; then
125+
echo "Found existing PR: $EXISTING_PR_URL"
126+
# Update existing PR title and body
127+
gh pr edit $EXISTING_PR_URL \
128+
--title "$PR_TITLE" \
129+
--body "$PR_BODY"
130+
echo "Updated existing PR: $EXISTING_PR_URL"
131+
echo "pr_url=$EXISTING_PR_URL" >> $GITHUB_OUTPUT
132+
echo "pr_action=updated" >> $GITHUB_OUTPUT
133+
else
134+
# Create new PR
135+
NEW_PR_URL=$(gh pr create \
136+
--repo containers/buildah \
137+
--base main \
138+
--head "$BRANCH_NAME" \
139+
--title "$PR_TITLE" \
140+
--body "$PR_BODY")
141+
echo "Created new PR: $NEW_PR_URL"
142+
echo "pr_url=$NEW_PR_URL" >> $GITHUB_OUTPUT
143+
echo "pr_action=created" >> $GITHUB_OUTPUT
144+
fi
145+
146+
- name: 'Comment on container-libs PR with the link to buildah PR'
147+
if: steps.check_go_changes.outputs.should_run == 'true'
148+
env:
149+
GH_TOKEN: ${{ secrets.VENDOR_TOKEN_PODMANBOT }}
150+
SELF_REPO_PR_NUMBER: ${{ github.event.pull_request.number }}
151+
TARGET_REPO_PR_URL: ${{ steps.create_pr.outputs.pr_url }}
152+
PR_ACTION: ${{ steps.create_pr.outputs.pr_action }}
153+
run: |
154+
if [ "${{ env.PR_ACTION }}" = "created" ]; then
155+
COMMENT_BODY="✅ A new PR has been created in buildah to vendor these changes: **${{ env.TARGET_REPO_PR_URL }}**"
156+
else
157+
COMMENT_BODY="✅ The existing PR in buildah has been updated with these changes: **${{ env.TARGET_REPO_PR_URL }}**"
158+
fi
159+
160+
gh pr comment ${{ env.SELF_REPO_PR_NUMBER }} \
161+
--repo ${{ github.repository }} \
162+
--body "$COMMENT_BODY"
163+
164+
- name: 'Skip workflow - No Go files changed'
165+
if: steps.check_go_changes.outputs.should_run == 'false'
166+
run: |
167+
echo "✅ Workflow completed successfully - No Go files were changed in this PR."
168+
echo "The downstream sync workflow was skipped as it only runs when .go files are modified."

0 commit comments

Comments
 (0)