Skip to content

Commit 0be193c

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

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
# Function to update module and verify
72+
update_module() {
73+
local module=$1
74+
echo "Updating module: $module"
75+
go mod edit -replace ${module}=github.com/flouthoc/container-libs/${module#go.podman.io/}@${COMMIT_SHA}
76+
GOWORK=off go mod tidy
77+
GOWORK=off go mod vendor
78+
GOWORK=off go mod verify
79+
}
80+
81+
# Update all required modules
82+
update_module "go.podman.io/common"
83+
update_module "go.podman.io/storage"
84+
update_module "go.podman.io/image/v5"
85+
86+
echo "Updated go.mod:"
87+
cat go.mod
88+
89+
- name: 'Commit and Push to buildah'
90+
if: steps.check_go_changes.outputs.should_run == 'true'
91+
run: |
92+
cd buildah
93+
git config user.name "github-actions[bot]"
94+
git config user.email "github-actions[bot]@users.noreply.github.com"
95+
96+
BRANCH_NAME="sync/container-libs-${{ github.event.pull_request.number }}"
97+
git switch $BRANCH_NAME
98+
99+
git add .
100+
git commit -m "feat: Vendor changes from podmanbot/container-libs#${{ github.event.pull_request.number }}"
101+
102+
# Force push to update the branch if the action re-runs on 'synchronize'
103+
git push origin $BRANCH_NAME --force
104+
105+
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
106+
107+
- name: 'Create or Update Pull Request in Buildah'
108+
if: steps.check_go_changes.outputs.should_run == 'true'
109+
id: create_pr
110+
env:
111+
GH_TOKEN: ${{ secrets.VENDOR_TOKEN_PODMANBOT }}
112+
SELF_REPO_PR_NUMBER: ${{ github.event.pull_request.number }}
113+
SELF_REPO_PR_URL: ${{ github.event.pull_request.html_url }}
114+
SELF_REPO_PR_TITLE: ${{ github.event.pull_request.title }}
115+
run: |
116+
cd buildah
117+
118+
BRANCH_NAME="sync/container-libs-${{ github.event.pull_request.number }}"
119+
PR_TITLE="Sync: ${{ env.SELF_REPO_PR_TITLE }}"
120+
PR_BODY="This PR automatically vendors changes from [repo-A#${{ env.SELF_REPO_PR_NUMBER }}](${{ env.SELF_REPO_PR_URL }})."
121+
122+
# Check if PR already exists for this branch
123+
echo "Searching for existing PR with branch: $BRANCH_NAME"
124+
125+
EXISTING_PR_URL=$(gh pr list --repo containers/buildah --head "$BRANCH_NAME" --json url --jq '.[0].url // empty' 2>/dev/null || echo "")
126+
127+
if [ -n "$EXISTING_PR_URL" ]; then
128+
echo "Found existing PR: $EXISTING_PR_URL"
129+
# Update existing PR title and body
130+
gh pr edit $EXISTING_PR_URL \
131+
--title "$PR_TITLE" \
132+
--body "$PR_BODY"
133+
echo "Updated existing PR: $EXISTING_PR_URL"
134+
echo "pr_url=$EXISTING_PR_URL" >> $GITHUB_OUTPUT
135+
echo "pr_action=updated" >> $GITHUB_OUTPUT
136+
else
137+
# Create new PR
138+
NEW_PR_URL=$(gh pr create \
139+
--repo containers/buildah \
140+
--base main \
141+
--head "$BRANCH_NAME" \
142+
--title "$PR_TITLE" \
143+
--body "$PR_BODY")
144+
echo "Created new PR: $NEW_PR_URL"
145+
echo "pr_url=$NEW_PR_URL" >> $GITHUB_OUTPUT
146+
echo "pr_action=created" >> $GITHUB_OUTPUT
147+
fi
148+
149+
- name: 'Comment on container-libs PR with the link to buildah PR'
150+
if: steps.check_go_changes.outputs.should_run == 'true'
151+
env:
152+
GH_TOKEN: ${{ secrets.VENDOR_TOKEN_PODMANBOT }}
153+
SELF_REPO_PR_NUMBER: ${{ github.event.pull_request.number }}
154+
TARGET_REPO_PR_URL: ${{ steps.create_pr.outputs.pr_url }}
155+
PR_ACTION: ${{ steps.create_pr.outputs.pr_action }}
156+
run: |
157+
if [ "${{ env.PR_ACTION }}" = "created" ]; then
158+
COMMENT_BODY="✅ A new PR has been created in buildah to vendor these changes: **${{ env.TARGET_REPO_PR_URL }}**"
159+
else
160+
COMMENT_BODY="✅ The existing PR in buildah has been updated with these changes: **${{ env.TARGET_REPO_PR_URL }}**"
161+
fi
162+
163+
gh pr comment ${{ env.SELF_REPO_PR_NUMBER }} \
164+
--repo ${{ github.repository }} \
165+
--body "$COMMENT_BODY"
166+
167+
- name: 'Skip workflow - No Go files changed'
168+
if: steps.check_go_changes.outputs.should_run == 'false'
169+
run: |
170+
echo "✅ Workflow completed successfully - No Go files were changed in this PR."
171+
echo "The downstream sync workflow was skipped as it only runs when .go files are modified."

0 commit comments

Comments
 (0)