|
| 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: 'Check for Go file changes' |
| 17 | + id: check_go_changes |
| 18 | + run: | |
| 19 | + # Get the list of changed files in the PR |
| 20 | + CHANGED_FILES=$(gh pr view ${{ github.event.pull_request.number }} --repo ${{ github.repository }} --json files --jq '.files[].path') |
| 21 | + |
| 22 | + # Filter for relevant Go files that should trigger downstream sync |
| 23 | + # Include: .go files (excluding test files and vendor directory) |
| 24 | + # Exclude: *_test.go files, vendor/ directory files |
| 25 | + RELEVANT_GO_FILES=$(echo "$CHANGED_FILES" | grep -E '\.go$' | grep -v '_test\.go$' | grep -v '^vendor/' || echo "") |
| 26 | + |
| 27 | + if [ -n "$RELEVANT_GO_FILES" ]; then |
| 28 | + echo "Relevant Go files changed (excluding tests and vendor):" |
| 29 | + echo "$RELEVANT_GO_FILES" | head -10 # Show only first 10 files to avoid huge logs |
| 30 | + if [ $(echo "$RELEVANT_GO_FILES" | wc -l) -gt 10 ]; then |
| 31 | + echo "... and $(( $(echo "$RELEVANT_GO_FILES" | wc -l) - 10 )) more files" |
| 32 | + fi |
| 33 | + echo "should_run=true" >> $GITHUB_OUTPUT |
| 34 | + echo "go_changes=true" >> $GITHUB_ENV |
| 35 | + else |
| 36 | + echo "No relevant Go files were changed in this PR." |
| 37 | + echo "Only test files, vendor files, or non-Go files were modified - skipping downstream sync." |
| 38 | + echo "should_run=false" >> $GITHUB_OUTPUT |
| 39 | + echo "go_changes=false" >> $GITHUB_ENV |
| 40 | + fi |
| 41 | + env: |
| 42 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 43 | + |
| 44 | + - name: 'Setup Go' |
| 45 | + if: steps.check_go_changes.outputs.should_run == 'true' |
| 46 | + uses: actions/setup-go@v4 |
| 47 | + with: |
| 48 | + go-version: '1.21' |
| 49 | + |
| 50 | + - name: 'Checkout forked buildah' |
| 51 | + if: steps.check_go_changes.outputs.should_run == 'true' |
| 52 | + uses: actions/checkout@v4 |
| 53 | + with: |
| 54 | + repository: 'flouthoc/buildah' # The target repository |
| 55 | + path: 'buildah' # Checkout into a sub-directory |
| 56 | + token: ${{ secrets.VENDOR_TOKEN_PODMANBOT }} |
| 57 | + |
| 58 | + - name: 'Vendor Code from this repo to buildah' |
| 59 | + if: steps.check_go_changes.outputs.should_run == 'true' |
| 60 | + run: | |
| 61 | + # Get the current commit SHA from the PR |
| 62 | + COMMIT_SHA="${{ github.event.pull_request.head.sha }}" |
| 63 | + echo "Using commit SHA: $COMMIT_SHA" |
| 64 | + |
| 65 | + cd buildah |
| 66 | + # Create a unique branch name based on the container-libs PR number |
| 67 | + BRANCH_NAME="sync/container-libs-${{ github.event.pull_request.number }}" |
| 68 | + git switch -c $BRANCH_NAME |
| 69 | + git remote add upstream https://github.com/containers/buildah.git |
| 70 | + git fetch upstream |
| 71 | + git rebase upstream/main |
| 72 | + |
| 73 | + |
| 74 | + echo "Current go.mod before update:" |
| 75 | + cat go.mod |
| 76 | + |
| 77 | + # Function to update module and verify |
| 78 | + update_module() { |
| 79 | + local module=$1 |
| 80 | + echo "Updating module: $module" |
| 81 | + go mod edit -replace ${module}=github.com/flouthoc/container-libs/${module#go.podman.io/}@${COMMIT_SHA} |
| 82 | + GOWORK=off go mod tidy |
| 83 | + } |
| 84 | + |
| 85 | + # Update all required modules |
| 86 | + update_module "go.podman.io/common" |
| 87 | + update_module "go.podman.io/storage" |
| 88 | + update_module "go.podman.io/image/v5" |
| 89 | + GOWORK=off go mod vendor |
| 90 | + GOWORK=off go mod verify |
| 91 | + |
| 92 | + echo "Updated go.mod:" |
| 93 | + cat go.mod |
| 94 | + |
| 95 | + - name: 'Commit and Push to buildah' |
| 96 | + if: steps.check_go_changes.outputs.should_run == 'true' |
| 97 | + run: | |
| 98 | + cd buildah |
| 99 | + git config user.name "github-actions[bot]" |
| 100 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 101 | + |
| 102 | + BRANCH_NAME="sync/container-libs-${{ github.event.pull_request.number }}" |
| 103 | + git switch $BRANCH_NAME |
| 104 | + |
| 105 | + git add . |
| 106 | + git commit -m "feat: Vendor changes from podmanbot/container-libs#${{ github.event.pull_request.number }}" |
| 107 | + |
| 108 | + # Force push to update the branch if the action re-runs on 'synchronize' |
| 109 | + git push origin $BRANCH_NAME --force |
| 110 | + |
| 111 | + echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV |
| 112 | +
|
| 113 | + - name: 'Create or Update Pull Request in Buildah' |
| 114 | + if: steps.check_go_changes.outputs.should_run == 'true' |
| 115 | + id: create_pr |
| 116 | + env: |
| 117 | + GH_TOKEN: ${{ secrets.VENDOR_TOKEN_PODMANBOT }} |
| 118 | + SELF_REPO_PR_NUMBER: ${{ github.event.pull_request.number }} |
| 119 | + SELF_REPO_PR_URL: ${{ github.event.pull_request.html_url }} |
| 120 | + SELF_REPO_PR_TITLE: ${{ github.event.pull_request.title }} |
| 121 | + run: | |
| 122 | + cd buildah |
| 123 | + |
| 124 | + BRANCH_NAME="sync/container-libs-${{ github.event.pull_request.number }}" |
| 125 | + PR_TITLE="Sync: ${{ env.SELF_REPO_PR_TITLE }}" |
| 126 | + PR_BODY="This PR automatically vendors changes from [repo-A#${{ env.SELF_REPO_PR_NUMBER }}](${{ env.SELF_REPO_PR_URL }})." |
| 127 | + |
| 128 | + # Check if PR already exists for this branch |
| 129 | + echo "Searching for existing PR with branch: $BRANCH_NAME" |
| 130 | + |
| 131 | + EXISTING_PR_URL=$(gh pr list --repo flouthoc/buildah --head "$BRANCH_NAME" --json url --jq '.[0].url // empty' 2>/dev/null || echo "") |
| 132 | + |
| 133 | + if [ -n "$EXISTING_PR_URL" ]; then |
| 134 | + echo "Found existing PR: $EXISTING_PR_URL" |
| 135 | + # Update existing PR title and body |
| 136 | + gh pr edit $EXISTING_PR_URL \ |
| 137 | + --title "$PR_TITLE" \ |
| 138 | + --body "$PR_BODY" |
| 139 | + echo "Updated existing PR: $EXISTING_PR_URL" |
| 140 | + echo "pr_url=$EXISTING_PR_URL" >> $GITHUB_OUTPUT |
| 141 | + echo "pr_action=updated" >> $GITHUB_OUTPUT |
| 142 | + else |
| 143 | + # Create new PR |
| 144 | + NEW_PR_URL=$(gh pr create \ |
| 145 | + --repo flouthoc/buildah \ |
| 146 | + --base main \ |
| 147 | + --head "$BRANCH_NAME" \ |
| 148 | + --title "$PR_TITLE" \ |
| 149 | + --body "$PR_BODY") |
| 150 | + echo "Created new PR: $NEW_PR_URL" |
| 151 | + echo "pr_url=$NEW_PR_URL" >> $GITHUB_OUTPUT |
| 152 | + echo "pr_action=created" >> $GITHUB_OUTPUT |
| 153 | + fi |
| 154 | +
|
| 155 | + - name: 'Comment on container-libs PR with the link to buildah PR' |
| 156 | + if: steps.check_go_changes.outputs.should_run == 'true' |
| 157 | + env: |
| 158 | + GH_TOKEN: ${{ secrets.VENDOR_TOKEN_PODMANBOT }} |
| 159 | + SELF_REPO_PR_NUMBER: ${{ github.event.pull_request.number }} |
| 160 | + TARGET_REPO_PR_URL: ${{ steps.create_pr.outputs.pr_url }} |
| 161 | + PR_ACTION: ${{ steps.create_pr.outputs.pr_action }} |
| 162 | + run: | |
| 163 | + if [ "${{ env.PR_ACTION }}" = "created" ]; then |
| 164 | + COMMENT_BODY="✅ A new PR has been created in buildah to vendor these changes: **${{ env.TARGET_REPO_PR_URL }}**" |
| 165 | + else |
| 166 | + COMMENT_BODY="✅ The existing PR in buildah has been updated with these changes: **${{ env.TARGET_REPO_PR_URL }}**" |
| 167 | + fi |
| 168 | + |
| 169 | + gh pr comment ${{ env.SELF_REPO_PR_NUMBER }} \ |
| 170 | + --repo ${{ github.repository }} \ |
| 171 | + --body "$COMMENT_BODY" |
| 172 | +
|
| 173 | + - name: 'Skip workflow - No relevant Go files changed' |
| 174 | + if: steps.check_go_changes.outputs.should_run == 'false' |
| 175 | + run: | |
| 176 | + echo "✅ Workflow completed successfully - No relevant Go files were changed in this PR." |
| 177 | + echo "The downstream sync workflow was skipped as it only runs when non-test .go files (excluding vendor/) are modified." |
0 commit comments