Skip to content

Commit 6f01976

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

File tree

1 file changed

+132
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)