Skip to content

Commit

Permalink
Revert "Fix to correctly leave Gradle build scan results as a comment (
Browse files Browse the repository at this point in the history
…line#5126)"

This reverts commit 1ce4c69.
  • Loading branch information
jrhee17 committed Aug 21, 2023
1 parent a4a8149 commit a46ce4e
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 36 deletions.
18 changes: 9 additions & 9 deletions .github/actions/comment-build-scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function main(): Promise<void> {
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,
Expand All @@ -42,33 +42,33 @@ async function main(): Promise<void> {
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}`)
}
}

Expand Down
49 changes: 49 additions & 0 deletions .github/actions/get-pr-number-by-sha.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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);
}
}
34 changes: 34 additions & 0 deletions .github/actions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion .github/actions/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/actions_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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' }}
Expand Down
40 changes: 16 additions & 24 deletions .github/workflows/gradle-build-scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -38,28 +37,29 @@ 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

- id: upload-build-scans
name: Upload build scans
run: |
DOWNLOAD_DIR="build-scans/"
BUILD_SCAN_DIR="${HOME}/.gradle/build-scan-data"
BUILD_SCANS=""
Expand All @@ -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}"
Expand All @@ -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 }}

0 comments on commit a46ce4e

Please sign in to comment.