|
| 1 | +name: Approve Workflow Runs |
| 2 | + |
| 3 | +permissions: |
| 4 | + actions: write |
| 5 | + contents: read |
| 6 | + |
| 7 | +on: |
| 8 | + pull_request_target: |
| 9 | + types: |
| 10 | + - labeled |
| 11 | + - synchronize |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event.number }} |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +jobs: |
| 18 | + ok-to-test: |
| 19 | + if: contains(github.event.pull_request.labels.*.name, 'ok-to-test') || github.event_name == 'pull_request_target' |
| 20 | + runs-on: ubuntu-latest |
| 21 | + continue-on-error: true |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Check if author is a Kubeflow GitHub member |
| 25 | + id: membership-check |
| 26 | + uses: actions/github-script@v7 |
| 27 | + with: |
| 28 | + script: | |
| 29 | + const username = context.payload.pull_request.user.login; |
| 30 | + const org = context.repo.owner; |
| 31 | + try { |
| 32 | + const res = await github.rest.orgs.checkMembershipForUser({ |
| 33 | + org, |
| 34 | + username |
| 35 | + }); |
| 36 | + core.setOutput("is_member", true); |
| 37 | + } catch (error) { |
| 38 | + if (error.status === 404) { |
| 39 | + core.setOutput("is_member", false); |
| 40 | + } else { |
| 41 | + throw error; |
| 42 | + } |
| 43 | + } |
| 44 | +
|
| 45 | + - name: Approve Pending Workflow Runs |
| 46 | + if: steps.membership-check.outputs.is_member == 'true' || contains(github.event.pull_request.labels.*.name, 'ok-to-test') |
| 47 | + uses: actions/github-script@v7 |
| 48 | + with: |
| 49 | + retries: 3 |
| 50 | + script: | |
| 51 | + const request = { |
| 52 | + owner: context.repo.owner, |
| 53 | + repo: context.repo.repo, |
| 54 | + event: "pull_request", |
| 55 | + status: "action_required", |
| 56 | + head_sha: context.payload.pull_request.head.sha, |
| 57 | + } |
| 58 | +
|
| 59 | + core.info(`Getting workflow runs that need approval for commit ${request.head_sha}`) |
| 60 | + const runs = await github.paginate(github.rest.actions.listWorkflowRunsForRepo, request) |
| 61 | +
|
| 62 | + core.info(`Found ${runs.length} workflow runs that need approval`) |
| 63 | + for (const run of runs) { |
| 64 | + core.info(`Approving workflow run ${run.id}`) |
| 65 | + const approveReq = { |
| 66 | + owner: context.repo.owner, |
| 67 | + repo: context.repo.repo, |
| 68 | + run_id: run.id, |
| 69 | + } |
| 70 | + await github.rest.actions.approveWorkflowRun(approveReq) |
| 71 | + } |
0 commit comments