kafka
1# Licensed to the Apache Software Foundation (ASF) under one or more
2# contributor license agreements. See the NOTICE file distributed with
3# this work for additional information regarding copyright ownership.
4# The ASF licenses this file to You under the Apache License, Version 2.0
5# (the "License"); you may not use this file except in compliance with
6# the License. You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16name: CI Requested17
18on:19workflow_run:20workflows: [CI]21types:22- requested23
24run-name: CI Requested for ${{ github.event.workflow_run.display_title}}25
26jobs:27check-pr-labels:28# Even though job conditionals are difficult to debug, this will reduce the number of unnecessary runs29if: |30github.event_name == 'workflow_run' &&
31github.event.workflow_run.event == 'pull_request' &&
32github.event.workflow_run.status == 'completed' &&
33github.event.workflow_run.conclusion == 'action_required'
34runs-on: ubuntu-latest35steps:36- name: Env37run: printenv38env:39GITHUB_CONTEXT: ${{ toJson(github) }}40- name: Checkout code41uses: actions/checkout@v442with:43persist-credentials:44false45- name: Check PR Labels46env:47GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}48REPO: ${{ github.repository }}49RUN_ID: ${{ github.event.workflow_run.id }}50HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}51HEAD_REPO: ${{ github.event.workflow_run.head_repository.owner.login }}52# Caution! This is a bit hacky. The GH documentation shows that the workflow_run event should include a list53# of referencing pull_requests. I think this might only be the case for pull requests originating from the54# base repository. To deal with fork PRs, we need to query the API for PRs for the owner's branch. This55# code assumes that the fork repo owner is the same as the organization for the "org:branch" syntax used56# in the query. Also, only the first matching PR from that org will be considered.57run: |58set +e
59PR_NUMBER=$(gh api \
60-H "Accept: application/vnd.github+json" \
61-H "X-GitHub-Api-Version: 2022-11-28" \
62/repos/$REPO/pulls?head=$HEAD_REPO:$HEAD_BRANCH \
63--jq '.[0].number')
64if [ -z "$PR_NUMBER" ]; then
65echo "Could not find the PR that triggered this workflow request";
66exit 1;
67fi
68gh pr view $PR_NUMBER --json labels -q '.labels[].name' | grep -q 'ci-approved'
69exitcode="$?"
70if [ $exitcode -ne 0 ]; then
71echo "No ci-approved label set on PR #$PR_NUMBER. Will not auto-approve.";
72exit 0;
73else
74echo "Found 'ci-approved' label on PR #$PR_NUMBER. Auto-approving workflow run $RUN_ID.";
75fi
76echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_ENV"
77echo "RUN_ID=$RUN_ID" >> "$GITHUB_ENV"
78- name: Approve Workflow Run79if: env.RUN_ID != ''80uses: ./.github/actions/gh-api-approve-run81with:82gh-token: ${{ secrets.GITHUB_TOKEN }}83repository: ${{ github.repository }}84run_id: ${{ env.RUN_ID }}85pr_number: ${{ env.PR_NUMBER }}86commit_sha: ${{ github.event.workflow_run.head_sha }}87