Skip to content

Commit d63d3b7

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

File tree

1 file changed

+130
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)