Skip to content

Commit 2a95105

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

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
echo "Current go.mod before update:"
43+
cat go.mod
44+
45+
# Update the go.mod file to use the specific commit
46+
go mod edit -replace go.podman.io/common=github.com/containers/container-libs/common@${COMMIT_SHA}
47+
48+
echo "After go mod edit"
49+
cat go.mod
50+
51+
# Download and verify the module
52+
go mod tidy
53+
go mod vendor
54+
go mod verify
55+
56+
echo "Updated go.mod:"
57+
cat go.mod
58+
59+
- name: 'Commit and Push to buildah'
60+
run: |
61+
cd buildah
62+
git config user.name "github-actions[bot]"
63+
git config user.email "github-actions[bot]@users.noreply.github.com"
64+
65+
BRANCH_NAME="sync/container-libs-${{ github.event.pull_request.number }}"
66+
git switch -c $BRANCH_NAME
67+
68+
git add .
69+
git commit -m "feat: Vendor changes from podmanbot/container-libs#${{ github.event.pull_request.number }}"
70+
71+
# Force push to update the branch if the action re-runs on 'synchronize'
72+
git push origin $BRANCH_NAME --force
73+
74+
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
75+
76+
- name: 'Create or Update Pull Request in Buildah'
77+
id: create_pr
78+
env:
79+
GH_TOKEN: ${{ secrets.VENDOR_TOKEN_PODMANBOT }}
80+
SELF_REPO_PR_NUMBER: ${{ github.event.pull_request.number }}
81+
SELF_REPO_PR_URL: ${{ github.event.pull_request.html_url }}
82+
SELF_REPO_PR_TITLE: ${{ github.event.pull_request.title }}
83+
run: |
84+
cd buildah
85+
86+
PR_TITLE="Sync: ${{ env.SELF_REPO_PR_TITLE }}"
87+
PR_BODY="This PR automatically vendors changes from [repo-A#${{ env.SELF_REPO_PR_NUMBER }}](${{ env.SELF_REPO_PR_URL }})."
88+
89+
# Check if PR already exists for this branch
90+
EXISTING_PR_URL=$(gh pr list --head ${{ env.BRANCH_NAME }} --json url --jq '.[0].url' 2>/dev/null || echo "")
91+
92+
if [ -n "$EXISTING_PR_URL" ]; then
93+
echo "Found existing PR: $EXISTING_PR_URL"
94+
# Update existing PR title and body
95+
gh pr edit $EXISTING_PR_URL \
96+
--title "$PR_TITLE" \
97+
--body "$PR_BODY"
98+
echo "Updated existing PR: $EXISTING_PR_URL"
99+
echo "pr_url=$EXISTING_PR_URL" >> $GITHUB_OUTPUT
100+
echo "pr_action=updated" >> $GITHUB_OUTPUT
101+
else
102+
# Create new PR
103+
NEW_PR_URL=$(gh pr create \
104+
--repo containers/buildah \
105+
--base main \
106+
--head ${{ env.BRANCH_NAME }} \
107+
--title "$PR_TITLE" \
108+
--body "$PR_BODY")
109+
echo "Created new PR: $NEW_PR_URL"
110+
echo "pr_url=$NEW_PR_URL" >> $GITHUB_OUTPUT
111+
echo "pr_action=created" >> $GITHUB_OUTPUT
112+
fi
113+
114+
- name: 'Comment on container-libs PR with the link to buildah PR'
115+
env:
116+
# GH_TOKEN: ${{ secrets.VENDOR_TOKEN_PODMANBOT }}
117+
SELF_REPO_PR_NUMBER: ${{ github.event.pull_request.number }}
118+
TARGET_REPO_PR_URL: ${{ steps.create_pr.outputs.pr_url }}
119+
PR_ACTION: ${{ steps.create_pr.outputs.pr_action }}
120+
run: |
121+
if [ "${{ env.PR_ACTION }}" = "created" ]; then
122+
COMMENT_BODY="✅ A new PR has been created in buildah to vendor these changes: **${{ env.TARGET_REPO_PR_URL }}**"
123+
else
124+
COMMENT_BODY="✅ The existing PR in buildah has been updated with these changes: **${{ env.TARGET_REPO_PR_URL }}**"
125+
fi
126+
127+
gh pr comment ${{ env.SELF_REPO_PR_NUMBER }} \
128+
--repo ${{ github.repository }} \
129+
--body "$COMMENT_BODY"

0 commit comments

Comments
 (0)