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