From a46ce4e1ada50afe6b4def141eb0ee2df0598a9c Mon Sep 17 00:00:00 2001 From: Hannam Rhee Date: Mon, 21 Aug 2023 17:39:27 +0900 Subject: [PATCH] Revert "Fix to correctly leave Gradle build scan results as a comment (#5126)" This reverts commit 1ce4c69e3b9886a2fbdd55f6c52d4cbf4734b3eb. --- .github/actions/comment-build-scan.ts | 18 ++++----- .github/actions/get-pr-number-by-sha.ts | 49 +++++++++++++++++++++++++ .github/actions/package-lock.json | 34 +++++++++++++++++ .github/actions/package.json | 4 +- .github/workflows/actions_build.yml | 4 +- .github/workflows/gradle-build-scan.yml | 40 ++++++++------------ 6 files changed, 113 insertions(+), 36 deletions(-) create mode 100644 .github/actions/get-pr-number-by-sha.ts diff --git a/.github/actions/comment-build-scan.ts b/.github/actions/comment-build-scan.ts index 0085f44bfee..149535f5ce4 100644 --- a/.github/actions/comment-build-scan.ts +++ b/.github/actions/comment-build-scan.ts @@ -24,7 +24,7 @@ async function main(): Promise { const prNumber = parseInt(process.env.PR_NUMBER); const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/"); - console.log(`💻 Getting jobs for ${process.env.RUN_ID} ...`); + console.log(`💻 Getting jobs for ${process.env.RUN_ID} ...`) const {data: {jobs}} = await octokit.rest.actions.listJobsForWorkflowRun({ owner: owner, repo: repo, @@ -42,33 +42,33 @@ async function main(): Promise { const [jobName, scanUrl]= scan.split(" "); const job = jobs.find(job => job.name === jobName); if (job.conclusion === 'success') { - commentBody += `| [${job.name}](${job.url}) | ✅ | ${scanUrl} |\n`; + commentBody += `| [${job.name}](${job.url}) | ✅| ${scanUrl} |\n`; } else { commentBody += `| [${job.name}](${job.url}) | ❌ (${job.conclusion}) | ${scanUrl} |\n`; } } - console.log(`💻 Getting comments for #${prNumber} ...`); - const scanComment = await findScanComment(octokit, owner, repo, prNumber); + console.log(`💻 Getting comments for #${prNumber} ...`) + const scanComment = await findScanComment(octokit, owner, repo, prNumber) if (scanComment) { // Update the previous comment - console.log(`📝 Updating the previous comment: ${scanComment.html_url} ...`); + console.log(`📝 Updating the previous comment: ${scanComment.html_url} ...`) await octokit.rest.issues.updateComment({ owner, repo, comment_id: scanComment.id, body: commentBody - }); + }) } else { // If no previous comment, create a new one - console.log(`📝 Creating a new comment for #${prNumber} ...`); + console.log(`📝 Creating a new comment for #${prNumber} ...`) const { data: newComment } = await octokit.rest.issues.createComment({ owner, repo, issue_number: prNumber, body: commentBody - }); - console.log(`💬 A new comment has been created: ${newComment.html_url}`); + }) + console.log(`💬 A new comment has been created: ${newComment.html_url}`) } } diff --git a/.github/actions/get-pr-number-by-sha.ts b/.github/actions/get-pr-number-by-sha.ts new file mode 100644 index 00000000000..648d3b57481 --- /dev/null +++ b/.github/actions/get-pr-number-by-sha.ts @@ -0,0 +1,49 @@ +/* + * Copyright 2023 LINE Corporation + * + * LINE Corporation 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: + * + * https://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. + */ + +import { Octokit } from 'octokit'; +import * as core from '@actions/core'; + +main(); + +async function main(): Promise { + const octokit = new Octokit({auth: process.env.GITHUB_TOKEN}); + const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/"); + + const sha = process.env.COMMIT_SHA; + console.log(`💻 Getting pull request number for ${sha} ...`) + const {data: {check_suites}} = await octokit.rest.checks.listSuitesForRef({ + owner: owner, + repo: repo, + ref: sha, + }); + + let prNumber = 0; + for (const item of check_suites) { + if (item.pull_requests.length > 0) { + prNumber = item.pull_requests[0].number; + break; + } + } + if (prNumber === 0) { + // The build is not triggered by a pull request. + console.log("❔ No pull request found."); + return; + } else { + console.log(`✅ Pull request number: ${prNumber}`); + core.setOutput("pr_number", prNumber); + } +} diff --git a/.github/actions/package-lock.json b/.github/actions/package-lock.json index b4422cae17c..8035130f7dd 100644 --- a/.github/actions/package-lock.json +++ b/.github/actions/package-lock.json @@ -5,10 +5,28 @@ "packages": { "": { "dependencies": { + "@actions/core": "^1.10.0", "octokit": "^2.1.0", "ts-node": "^10.9.1" } }, + "node_modules/@actions/core": { + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.10.0.tgz", + "integrity": "sha512-2aZDDa3zrrZbP5ZYg159sNoLRb61nQ7awl5pSvIq5Qpj81vwDzdMRKzkWJGJuwVvWpvZKx7vspJALyvaaIQyug==", + "dependencies": { + "@actions/http-client": "^2.0.1", + "uuid": "^8.3.2" + } + }, + "node_modules/@actions/http-client": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-2.1.1.tgz", + "integrity": "sha512-qhrkRMB40bbbLo7gF+0vu+X+UawOvQQqNAA/5Unx774RS8poaOhThDOG6BGmxvAnxhQnDp2BG/ZUm65xZILTpw==", + "dependencies": { + "tunnel": "^0.0.6" + } + }, "node_modules/@cspotcode/source-map-support": { "version": "0.8.1", "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", @@ -733,6 +751,14 @@ } } }, + "node_modules/tunnel": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.6.tgz", + "integrity": "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg==", + "engines": { + "node": ">=0.6.11 <=0.7.0 || >=0.7.3" + } + }, "node_modules/typescript": { "version": "5.1.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.3.tgz", @@ -760,6 +786,14 @@ "resolved": "https://registry.npmjs.org/universal-user-agent/-/universal-user-agent-6.0.0.tgz", "integrity": "sha512-isyNax3wXoKaulPDZWHQqbmIx1k2tb9fb3GGDBRxCscfYV2Ch7WxPArBsFEG8s/safwXTT7H4QGhaIkTp9447w==" }, + "node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/v8-compile-cache-lib": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", diff --git a/.github/actions/package.json b/.github/actions/package.json index 2711b4456af..f9f728e39da 100644 --- a/.github/actions/package.json +++ b/.github/actions/package.json @@ -1,9 +1,11 @@ { "scripts": { "post-release": "ts-node post-release.ts", - "comment-build-scan": "ts-node comment-build-scan.ts" + "comment-build-scan": "ts-node comment-build-scan.ts", + "get-pr-number-by-sha": "ts-node get-pr-number-by-sha.ts" }, "dependencies": { + "@actions/core": "^1.10.0", "octokit": "^2.1.0", "ts-node": "^10.9.1" } diff --git a/.github/workflows/actions_build.yml b/.github/workflows/actions_build.yml index b809805ec0d..595dbe3b6fb 100644 --- a/.github/workflows/actions_build.yml +++ b/.github/workflows/actions_build.yml @@ -21,7 +21,6 @@ env: # Used by GitHub CLI GH_TOKEN: ${{ github.token }} RUN_ID: ${{ github.run_id }} - PR_NUMBER: ${{ github.event.pull_request.number }} jobs: build: @@ -122,13 +121,14 @@ jobs: -Porg.gradle.java.installations.paths=${{ steps.setup-build-jdk.outputs.path }},${{ steps.setup-jdk.outputs.path }} shell: bash env: + PR_NUMBER: ${{ github.event.pull_request.number }} COMMIT_SHA: ${{ github.event.pull_request.head.sha }} - name: Upload Gradle build scan if: always() uses: actions/upload-artifact@v3 with: - name: ${{ env.PR_NUMBER && format('{0}-', env.PR_NUMBER) || '' }}build-scan-${{ env.JOB_NAME }} + name: build-scan-${{ env.JOB_NAME }} path: ~/.gradle/build-scan-data - if: ${{ matrix.snapshot && github.ref_name == 'main' }} diff --git a/.github/workflows/gradle-build-scan.yml b/.github/workflows/gradle-build-scan.yml index ffff259a323..ff83773ed00 100644 --- a/.github/workflows/gradle-build-scan.yml +++ b/.github/workflows/gradle-build-scan.yml @@ -13,7 +13,6 @@ env: COMMIT_SHA: ${{ github.event.workflow_run.head_sha }} # Used by Octokit GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - DOWNLOAD_DIR: build-scans/ jobs: upload-gradle-build-scan: @@ -38,21 +37,21 @@ jobs: run_id: ${{ env.RUN_ID }} name: build-scan.* name_is_regexp: true - path: ${{ env.DOWNLOAD_DIR }} + path: build-scans check_artifacts: true search_artifacts: true - - id: get-pr-number - name: Get PR number + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: 18 + + - id: get-pr-number-by-sha + name: Get PR number by SHA + working-directory: .github/actions run: | - PR_NUMBER=$(ls $DOWNLOAD_DIR | head -1 | sed -n 's/\([0-9]*\)-build-scan.*/\1/p') - if [ -z "$PR_NUMBER" ]; then - echo "❔️No pull request number found." - else - echo "✅ Pull request number: ${PR_NUMBER}" - echo "PR_NUMBER=${PR_NUMBER}" >> "$GITHUB_OUTPUT" - fi - shell: bash + npm ci + npm run get-pr-number-by-sha - name: Setup Gradle uses: gradle/gradle-build-action@v2 @@ -60,6 +59,7 @@ jobs: - id: upload-build-scans name: Upload build scans run: | + DOWNLOAD_DIR="build-scans/" BUILD_SCAN_DIR="${HOME}/.gradle/build-scan-data" BUILD_SCANS="" @@ -71,7 +71,7 @@ jobs: echo "🚚 Copying build scan: ${DOWNLOAD_DIR}${SCAN}/ to $BUILD_SCAN_DIR ..." cp -r ${DOWNLOAD_DIR}${SCAN}/* $BUILD_SCAN_DIR - JOB_NAME=$(echo ${SCAN} | sed 's/.*build-scan-\(.*\)/\1/') + JOB_NAME=$(echo ${SCAN} | sed 's/build-scan-\(.*\)/\1/') echo "📤 Uploading build scan for job ${JOB_NAME} ..." ./gradlew --no-daemon --stacktrace clean buildScanPublishPrevious echo "✅ Published build scan: ${JOB_NAME}" @@ -85,19 +85,11 @@ jobs: echo "BUILD_SCANS=${BUILD_SCANS}" >> "$GITHUB_OUTPUT" shell: bash - - name: Setup Node.js - uses: actions/setup-node@v3 - with: - node-version: 18 - - id: create-or-update-comment name: Create or update comment - if: steps.get-pr-number.outputs.PR_NUMBER + if: steps.get-pr-number-by-sha.outputs.pr_number working-directory: .github/actions - run: | - npm ci - npm run comment-build-scan - shell: bash + run: npm run comment-build-scan env: BUILD_SCANS: ${{ steps.upload-build-scans.outputs.BUILD_SCANS }} - PR_NUMBER: ${{ steps.get-pr-number.outputs.PR_NUMBER }} + PR_NUMBER: ${{ steps.get-pr-number-by-sha.outputs.pr_number }}