Skip to content

Commit

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

A pull request number fails to get retrieved from a commit SHA of a PR. Interestingly, it was not a problem while testing the feature in the personal repo. Because that happens only when the commit is made from a forked repository.

For example, the PR information of 529961c is retrieved by https://api.github.com/repos/minwoox/armeria/commits/529961c4a43fb884cf780421218de4f56f1aafcc/pulls but https://api.github.com/repos/line/armeria/commits/529961c4a43fb884cf780421218de4f56f1aafcc/pulls returns an empty array. Note that the owners of the repository are different.

As a result, the scan results are not updated as a comment of a PR. https://github.com/line/armeria/blob/0cd7094b763e6c501b08e2cd44bb1835eaa46eea/.github/workflows/gradle-build-scan.yml#L90-L90

Modifications:

- Prepend a pull request number to the artifact names of scan results.
- Remove `get-pr-number-by-sha.ts` and its dependencies

Result:

Correctly update build scan results.
  • Loading branch information
ikhoon authored Aug 19, 2023
1 parent 0cd7094 commit 1ce4c69
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 113 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: 0 additions & 49 deletions .github/actions/get-pr-number-by-sha.ts

This file was deleted.

34 changes: 0 additions & 34 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: 1 addition & 3 deletions .github/actions/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{
"scripts": {
"post-release": "ts-node post-release.ts",
"comment-build-scan": "ts-node comment-build-scan.ts",
"get-pr-number-by-sha": "ts-node get-pr-number-by-sha.ts"
"comment-build-scan": "ts-node comment-build-scan.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,6 +21,7 @@ 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 @@ -119,14 +120,13 @@ 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: build-scan-${{ env.JOB_NAME }}
name: ${{ env.PR_NUMBER && format('{0}-', env.PR_NUMBER) || '' }}build-scan-${{ env.JOB_NAME }}
path: ~/.gradle/build-scan-data

- if: ${{ matrix.snapshot && github.ref_name == 'main' }}
Expand Down
40 changes: 24 additions & 16 deletions .github/workflows/gradle-build-scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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 @@ -37,29 +38,28 @@ jobs:
run_id: ${{ env.RUN_ID }}
name: build-scan.*
name_is_regexp: true
path: build-scans
path: ${{ env.DOWNLOAD_DIR }}
check_artifacts: true
search_artifacts: true

- 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
- id: get-pr-number
name: Get PR number
run: |
npm ci
npm run get-pr-number-by-sha
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

- 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,11 +85,19 @@ 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-by-sha.outputs.pr_number
if: steps.get-pr-number.outputs.PR_NUMBER
working-directory: .github/actions
run: npm run comment-build-scan
run: |
npm ci
npm run comment-build-scan
shell: bash
env:
BUILD_SCANS: ${{ steps.upload-build-scans.outputs.BUILD_SCANS }}
PR_NUMBER: ${{ steps.get-pr-number-by-sha.outputs.pr_number }}
PR_NUMBER: ${{ steps.get-pr-number.outputs.PR_NUMBER }}

0 comments on commit 1ce4c69

Please sign in to comment.