Skip to content

Commit 05b1d9e

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

File tree

1 file changed

+137
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)