|
| 1 | +name: Merge Contribution to Internal |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_review: |
| 5 | + types: [submitted] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + pull-requests: read |
| 10 | + checks: read |
| 11 | + actions: read |
| 12 | + |
| 13 | +jobs: |
| 14 | + check-merge-state: |
| 15 | + if: github.event.review.state == 'approved' |
| 16 | + runs-on: ubuntu-latest |
| 17 | + env: |
| 18 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 19 | + SOURCE_REPO: ${{ github.repository }} |
| 20 | + outputs: |
| 21 | + checks_passed: ${{ steps.verify_checks.outputs.checks_passed }} |
| 22 | + steps: |
| 23 | + - name: Wait for Status Checks and Verify |
| 24 | + id: verify_checks |
| 25 | + env: |
| 26 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 27 | + run: | |
| 28 | + MAX_ATTEMPTS=120 # 120 attempts * 30 seconds = 60 minutes max |
| 29 | + ATTEMPT=0 |
| 30 | + |
| 31 | + while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do |
| 32 | + echo "Checking status checks (attempt $((ATTEMPT + 1))/$MAX_ATTEMPTS)..." |
| 33 | + |
| 34 | + # Get PR status checks |
| 35 | + STATUS_JSON=$(gh pr view $PR_NUMBER --json statusCheckRollup --repo $SOURCE_REPO) |
| 36 | + |
| 37 | + # Filter for CheckRun entries only and not this current job |
| 38 | + CHECK_RUNS=$(echo "$STATUS_JSON" | jq '[.statusCheckRollup[] | select(.__typename == "CheckRun") | select(.name != "check-merge-state" and .name != "dispatch-to-internal")]') |
| 39 | + |
| 40 | + # Check if all checks are completed |
| 41 | + INCOMPLETE_COUNT=$(echo "$CHECK_RUNS" | jq '[.[] | select(.status != "COMPLETED")] | length') |
| 42 | + |
| 43 | + if [ "$INCOMPLETE_COUNT" -eq 0 ]; then |
| 44 | + echo "All checks are completed!" |
| 45 | + |
| 46 | + # Check conclusions - must be SUCCESS, NEUTRAL, or SKIPPED |
| 47 | + FAILED_CHECKS=$(echo "$CHECK_RUNS" | jq '[.[] | select(.conclusion != "SUCCESS" and .conclusion != "NEUTRAL" and .conclusion != "SKIPPED")]') |
| 48 | + FAILED_COUNT=$(echo "$FAILED_CHECKS" | jq 'length') |
| 49 | + |
| 50 | + if [ "$FAILED_COUNT" -eq 0 ]; then |
| 51 | + echo "All checks passed with acceptable conclusions!" |
| 52 | + echo "checks_passed=true" >> $GITHUB_OUTPUT |
| 53 | + break |
| 54 | + else |
| 55 | + echo "Some checks failed:" |
| 56 | + echo "$FAILED_CHECKS" | jq -r '.[] | " - \(.name): \(.conclusion)"' |
| 57 | + echo "checks_passed=false" >> $GITHUB_OUTPUT |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | + else |
| 61 | + echo "$INCOMPLETE_COUNT checks still running. Waiting 30 seconds..." |
| 62 | + ATTEMPT=$((ATTEMPT + 1)) |
| 63 | + if [ $ATTEMPT -lt $MAX_ATTEMPTS ]; then |
| 64 | + sleep 30 |
| 65 | + fi |
| 66 | + fi |
| 67 | + done |
| 68 | + |
| 69 | + if [ $ATTEMPT -eq $MAX_ATTEMPTS ]; then |
| 70 | + echo "Timed out waiting for checks to complete" |
| 71 | + echo "checks_passed=false" >> $GITHUB_OUTPUT |
| 72 | + exit 1 |
| 73 | + fi |
| 74 | +
|
| 75 | + # OSS App |
| 76 | + - name: Generate GitHub App Token |
| 77 | + id: pr_app_token |
| 78 | + uses: actions/create-github-app-token@v2 |
| 79 | + with: |
| 80 | + app-id: ${{ vars.WELLARCHITECTED_OSS_APP_ID }} |
| 81 | + private-key: ${{ secrets.WELLARCHITECTED_OSS_APP_PRIVATE_KEY }} |
| 82 | + owner: github |
| 83 | + repositories: | |
| 84 | + github-well-architected |
| 85 | + permission-pull-requests: write |
| 86 | + permission-contents: write |
| 87 | + |
| 88 | + - name: Enable Auto-Merge |
| 89 | + if: steps.verify_checks.outputs.checks_passed == 'true' |
| 90 | + env: |
| 91 | + GH_TOKEN: ${{ steps.pr_app_token.outputs.token }} |
| 92 | + run: | |
| 93 | + echo "Enabling auto-merge for PR #$PR_NUMBER..." |
| 94 | + gh pr merge $PR_NUMBER --auto --squash --repo $SOURCE_REPO |
| 95 | + echo "Auto-merge enabled successfully!" |
| 96 | +
|
| 97 | + dispatch-to-internal: |
| 98 | + needs: check-merge-state |
| 99 | + if: needs.check-merge-state.outputs.checks_passed == 'true' && github.event.review.state == 'approved' |
| 100 | + runs-on: ubuntu-latest |
| 101 | + env: |
| 102 | + SOURCE_REPO: github/github-well-architected |
| 103 | + TARGET_REPO: github/github-well-architected-internal |
| 104 | + PR_HEAD_BRANCH: ${{ github.event.pull_request.head.ref }} |
| 105 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 106 | + ENVIRONMENT: staging |
| 107 | + steps: |
| 108 | + # OSS App |
| 109 | + - name: Generate GitHub App Token |
| 110 | + id: dispatch_app_token |
| 111 | + uses: actions/create-github-app-token@v2 |
| 112 | + with: |
| 113 | + app-id: ${{ vars.WELLARCHITECTED_OSS_APP_ID }} |
| 114 | + private-key: ${{ secrets.WELLARCHITECTED_OSS_APP_PRIVATE_KEY }} |
| 115 | + owner: github |
| 116 | + repositories: | |
| 117 | + github-well-architected |
| 118 | + github-well-architected-internal |
| 119 | + permission-deployments: write |
| 120 | + permission-contents: write |
| 121 | + |
| 122 | + - name: Create deployment |
| 123 | + id: create_deployment |
| 124 | + env: |
| 125 | + GH_TOKEN: ${{ steps.dispatch_app_token.outputs.token }} |
| 126 | + run: | |
| 127 | + ################################################################ |
| 128 | + # Check for existing deployments |
| 129 | + # If found, delete the latest one to avoid outdated deployments |
| 130 | +
|
| 131 | + EXISTING_DEPLOYMENT_LATEST=$(gh api \ |
| 132 | + -H "Accept: application/vnd.github+json" \ |
| 133 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 134 | + "/repos/$SOURCE_REPO/deployments?ref=$PR_HEAD_BRANCH&environment=$ENVIRONMENT" \ |
| 135 | + --jq '[.[] | select(.task == "deploy")] | sort_by(.created_at) | reverse | .[0].id // empty') |
| 136 | +
|
| 137 | + if [ -n "$EXISTING_DEPLOYMENT_LATEST" ] && [ "$EXISTING_DEPLOYMENT_LATEST" != "null" ]; then |
| 138 | + echo "Found existing deployment with ID: $EXISTING_DEPLOYMENT_LATEST" |
| 139 | + echo "existing_deployment_id=$EXISTING_DEPLOYMENT_LATEST" >> $GITHUB_OUTPUT |
| 140 | +
|
| 141 | + # Delete existing deployment |
| 142 | + gh api \ |
| 143 | + --method DELETE \ |
| 144 | + -H "Accept: application/vnd.github+json" \ |
| 145 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 146 | + /repos/$SOURCE_REPO/deployments/$EXISTING_DEPLOYMENT_LATEST |
| 147 | + fi |
| 148 | +
|
| 149 | + ################################################################ |
| 150 | + # Create new deployment |
| 151 | +
|
| 152 | + DEPLOYMENT_RESPONSE=$(gh api \ |
| 153 | + --method POST \ |
| 154 | + -H "Accept: application/vnd.github+json" \ |
| 155 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 156 | + /repos/$SOURCE_REPO/deployments \ |
| 157 | + --input - <<-EOF |
| 158 | + { |
| 159 | + "ref": "$PR_HEAD_BRANCH", |
| 160 | + "environment": "$ENVIRONMENT", |
| 161 | + "description": "Deploy requested from PR $PR_NUMBER (branch: $PR_HEAD_BRANCH)", |
| 162 | + "auto_merge": false, |
| 163 | + "required_contexts": [] |
| 164 | + } |
| 165 | + EOF |
| 166 | + ) |
| 167 | + DEPLOYMENT_ID=$(echo "$DEPLOYMENT_RESPONSE" | jq -r '.id') |
| 168 | + echo "deployment_id=$DEPLOYMENT_ID" >> $GITHUB_OUTPUT |
| 169 | + echo "Created new deployment with ID: $DEPLOYMENT_ID" |
| 170 | + |
| 171 | + - name: Update Deployment Status |
| 172 | + env: |
| 173 | + GH_TOKEN: ${{ steps.dispatch_app_token.outputs.token }} |
| 174 | + NEW_DEPLOYMENT_ID: ${{ steps.create_deployment.outputs.deployment_id }} |
| 175 | + EXISTING_DEPLOYMENT_LATEST: ${{ steps.create_deployment.outputs.existing_deployment_id }} |
| 176 | + run: | |
| 177 | + gh api \ |
| 178 | + --method POST \ |
| 179 | + -H "Accept: application/vnd.github+json" \ |
| 180 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 181 | + /repos/$SOURCE_REPO/deployments/$NEW_DEPLOYMENT_ID/statuses \ |
| 182 | + -f "state=in_progress" \ |
| 183 | + -f "description=Deployment dispatched to internal repository for processing." |
| 184 | +
|
| 185 | + - name: Repository Dispatch to Internal Repo |
| 186 | + env: |
| 187 | + GH_TOKEN: ${{ steps.dispatch_app_token.outputs.token }} |
| 188 | + run: | |
| 189 | + gh api \ |
| 190 | + --method POST \ |
| 191 | + -H "Accept: application/vnd.github+json" \ |
| 192 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 193 | + /repos/$TARGET_REPO/dispatches \ |
| 194 | + -f 'event_type=opensource-sync-to-publish' \ |
| 195 | + -f "client_payload[pr_url]=${{ github.event.pull_request.html_url }}" \ |
| 196 | + -f "client_payload[pr_number]=$PR_NUMBER" \ |
| 197 | + -f "client_payload[head_ref]=$PR_HEAD_BRANCH" \ |
0 commit comments