From 0a075a663e9c442a457d88db40198aa668756dd1 Mon Sep 17 00:00:00 2001 From: Y Ethan Guo Date: Fri, 23 Feb 2024 14:27:42 -0800 Subject: [PATCH] [HUDI-7438] Fix Azure CI report check with new issue comments --- .github/workflows/azure_ci_check.yml | 102 ++++++++++++++++---- .github/workflows/create_azure_ci_check.yml | 39 ++++++++ 2 files changed, 121 insertions(+), 20 deletions(-) create mode 100644 .github/workflows/create_azure_ci_check.yml diff --git a/.github/workflows/azure_ci_check.yml b/.github/workflows/azure_ci_check.yml index 347d9c2959fbe..ea2714926b9dd 100644 --- a/.github/workflows/azure_ci_check.yml +++ b/.github/workflows/azure_ci_check.yml @@ -18,16 +18,21 @@ name: Azure CI on: + pull_request: + types: [ opened, edited, reopened, synchronize ] issue_comment: types: [ created, edited, deleted ] permissions: + checks: write pull-requests: read issues: read jobs: check-azure-ci-report: - if: "!contains(github.event.pull_request.body, 'HOTFIX: SKIP AZURE CI')" + if: true #| + #github.event.issue.pull_request != null && + #!contains(github.event.pull_request.body, 'HOTFIX: SKIP AZURE CI') runs-on: ubuntu-latest steps: - name: Get last commit hash @@ -36,13 +41,35 @@ jobs: with: github-token: ${{secrets.GITHUB_TOKEN}} script: | - const pr = context.payload.pull_request; - const lastCommitHash = pr.head.sha; - console.log(`Last commit hash: ${lastCommitHash}`); - // Set the output variable to be used in subsequent step - core.setOutput("last_commit_hash", lastCommitHash); + const repoOwner = context.repo.owner; + const repoName = context.repo.repo; + const issueNumber = context.payload.issue.number; + + // Fetch the latest commit associated with the PR + const { data: commits } = await github.rest.repos.listCommits({ + owner: repoOwner, + repo: repoName, + per_page: 1, + sha: issueNumber + }); + + if (commits.length > 0) { + const latestCommit = commits[0]; + const commitHash = latestCommit.sha; + const commitAuthor = latestCommit.commit.author.name; + + console.log(`Latest commit hash: ${commitHash}`); + console.log(`Commit author: ${commitAuthor}`); + // Set the output variable to be used in subsequent step + core.setOutput("last_commit_hash", commitHash); + } else { + console.log(`No commit found for the PR.`); + core.setFailed("No commit found for the PR."); + return false; + } - name: Check Azure CI report in PR comment + id: check_report_in_pr_comment uses: actions/github-script@v7 with: github-token: ${{secrets.GITHUB_TOKEN}} @@ -61,32 +88,67 @@ jobs: const botComments = comments.data.filter(comment => comment.user.login === botUsername); const lastComment = botComments.pop(); + let message = ''; + let result = false; + if (lastComment) { const reportPrefix = '${lastCommitHash} Azure: ' const successReportString = '${reportPrefix}[SUCCESS]' const failureReportString = '${reportPrefix}[FAILURE]' if (lastComment.body.includes(reportPrefix)) { if (lastComment.body.includes(successReportString)) { - console.log(`Azure CI succeeded on the latest commit of the PR.`); - return true; + message = 'Azure CI succeeded on the latest commit of the PR.'; + result = true; } else if (lastComment.body.includes(failureReportString)) { - console.log(`Azure CI failed on the latest commit of the PR.`); - core.setFailed("Azure CI failed on the latest commit of the PR."); - return false; + message = 'Azure CI failed on the latest commit of the PR'; + result = false; } else { - console.log(`Azure CI is in progress on the latest commit of the PR.`); - core.setFailed("Azure CI is in progress on the latest commit of the PR."); - return false; + message = 'Azure CI is in progress on the latest commit of the PR.'; + result = false; } } else { - console.log(`No Azure CI report on the latest commit of the PR.`); - core.setFailed("No Azure CI report on the latest commit of the PR."); - return false; + message = 'No Azure CI report on the latest commit of the PR.'; + result = false; } } else { - console.log(`Azure CI report does not seem to be ready yet.`); - core.setFailed("Azure CI report does not seem to be ready yet."); - return false; + message = 'Azure CI report does not seem to be ready yet.'; + result = false; } + core.setOutput("check_result_message", message); + core.setOutput("is_check_successful", result); env: GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + + - name: Attach Azure CI report check to the PR + uses: actions/github-script@v5 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + let conclusionString = 'failure'; + if (is_check_successful) { + conclusionString = 'success'; + } + await github.checks.create({ + owner: context.repo.owner, + repo: context.repo.repo, + name: 'Azure CI Report Check', + head_sha: steps.last_commit.outputs.last_commit_hash, + status: 'completed', + conclusion: '${conclusionString}', + completed_at: new Date(), + output: { + title: 'Azure CI Report Check', + summary: '${{ steps.check_report_in_pr_comment.outputs.check_result_message }}', + } + }); + + - name: Remove Azure CI Blocker check + run: | + curl -X GET \ + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ + -H "Accept: application/vnd.github.v3+json" \ + https://api.github.com/repos/${{ github.repository }}/commits/${{ steps.last_commit.outputs.last_commit_hash }}/check-runs | jq '.check_runs[] | select(.name | contains("create-azure-ci-report-check")) | .id' | xargs -I {} curl -X PATCH \ + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ + -H "Accept: application/vnd.github.v3+json" \ + -d '{"status": "neutral"}' \ + https://api.github.com/repos/${{ github.repository }}/check-runs/{} diff --git a/.github/workflows/create_azure_ci_check.yml b/.github/workflows/create_azure_ci_check.yml new file mode 100644 index 0000000000000..12902295cd7bf --- /dev/null +++ b/.github/workflows/create_azure_ci_check.yml @@ -0,0 +1,39 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +name: Azure CI Blocker + +on: + pull_request: + types: [ opened, edited, reopened, synchronize ] + +permissions: + pull-requests: read + issues: read + +jobs: + create-azure-ci-report-check: + if: "!contains(github.event.pull_request.body, 'HOTFIX: SKIP AZURE CI')" + runs-on: ubuntu-latest + steps: + - name: Create a placeholder check for Azure CI report + uses: actions/github-script@v7 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + core.setFailed("Please wait for the Azure CI report on the latest commit of the PR."); + return false;