|  | 
|  | 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 $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 | +          BRANCH_NAME="sync/container-libs-${{ github.event.pull_request.number }}" | 
|  | 88 | +          PR_TITLE="Sync: ${{ env.SELF_REPO_PR_TITLE }}" | 
|  | 89 | +          PR_BODY="This PR automatically vendors changes from [repo-A#${{ env.SELF_REPO_PR_NUMBER }}](${{ env.SELF_REPO_PR_URL }})." | 
|  | 90 | +           | 
|  | 91 | +          # Check if PR already exists for this branch | 
|  | 92 | +          echo $BRANCH_NAME | 
|  | 93 | +          gh pr list --head $BRANCH_NAME --json url | 
|  | 94 | +          EXISTING_PR_URL=$(gh pr list --head $BRANCH_NAME --json url --jq '.[0].url' 2>/dev/null || echo "") | 
|  | 95 | +
 | 
|  | 96 | +          echo "Printing exisiting PR URL" | 
|  | 97 | +          echo $EXISTING_PR_URL | 
|  | 98 | +           | 
|  | 99 | +          if [ -n "$EXISTING_PR_URL" ]; then | 
|  | 100 | +            echo "Found existing PR: $EXISTING_PR_URL" | 
|  | 101 | +            # Update existing PR title and body | 
|  | 102 | +            gh pr edit $EXISTING_PR_URL \ | 
|  | 103 | +              --title "$PR_TITLE" \ | 
|  | 104 | +              --body "$PR_BODY" | 
|  | 105 | +            echo "Updated existing PR: $EXISTING_PR_URL" | 
|  | 106 | +            echo "pr_url=$EXISTING_PR_URL" >> $GITHUB_OUTPUT | 
|  | 107 | +            echo "pr_action=updated" >> $GITHUB_OUTPUT | 
|  | 108 | +          else | 
|  | 109 | +            # Create new PR | 
|  | 110 | +            NEW_PR_URL=$(gh pr create \ | 
|  | 111 | +              --repo flouthoc/buildah \ | 
|  | 112 | +              --base main \ | 
|  | 113 | +              --head ${{ env.BRANCH_NAME }} \ | 
|  | 114 | +              --title "$PR_TITLE" \ | 
|  | 115 | +              --body "$PR_BODY") | 
|  | 116 | +            echo "Created new PR: $NEW_PR_URL" | 
|  | 117 | +            echo "pr_url=$NEW_PR_URL" >> $GITHUB_OUTPUT | 
|  | 118 | +            echo "pr_action=created" >> $GITHUB_OUTPUT | 
|  | 119 | +          fi | 
|  | 120 | +
 | 
|  | 121 | +      - name: 'Comment on container-libs PR with the link to buildah PR' | 
|  | 122 | +        env: | 
|  | 123 | +          GH_TOKEN: ${{ secrets.VENDOR_TOKEN_PODMANBOT }} | 
|  | 124 | +          SELF_REPO_PR_NUMBER: ${{ github.event.pull_request.number }} | 
|  | 125 | +          TARGET_REPO_PR_URL: ${{ steps.create_pr.outputs.pr_url }} | 
|  | 126 | +          PR_ACTION: ${{ steps.create_pr.outputs.pr_action }} | 
|  | 127 | +        run: | | 
|  | 128 | +          if [ "${{ env.PR_ACTION }}" = "created" ]; then | 
|  | 129 | +            COMMENT_BODY="✅ A new PR has been created in buildah to vendor these changes: **${{ env.TARGET_REPO_PR_URL }}**" | 
|  | 130 | +          else | 
|  | 131 | +            COMMENT_BODY="✅ The existing PR in buildah has been updated with these changes: **${{ env.TARGET_REPO_PR_URL }}**" | 
|  | 132 | +          fi | 
|  | 133 | +           | 
|  | 134 | +          gh pr comment ${{ env.SELF_REPO_PR_NUMBER }} \ | 
|  | 135 | +            --repo ${{ github.repository }} \ | 
|  | 136 | +            --body "$COMMENT_BODY" | 
0 commit comments