Skip to content

Commit c0d0f80

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

File tree

1 file changed

+167
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)