From e622ff79211941d5a76cad86b32d83822b22a963 Mon Sep 17 00:00:00 2001 From: Ross Reicks Date: Thu, 11 Jul 2024 14:38:48 -0500 Subject: [PATCH] chore: add create pr comment flag --- action.yml | 8 +++++++- dist/index.js | 13 +++++++++++++ src/main.ts | 21 ++++++++++++--------- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/action.yml b/action.yml index 2ab5953..fcd47aa 100644 --- a/action.yml +++ b/action.yml @@ -1,6 +1,6 @@ name: Test Reporter description: | - Shows test results in GitHub UI: .NET (xUnit, NUnit, MSTest), Dart, Flutter, Java (JUnit), JavaScript (JEST, Mocha) + Shows test results in GitHub UI: .NET (xUnit, NUnit, MSTest), Dart, Flutter, Java (JUnit), JavaScript (JEST, Mocha), Apex (Salesforce via json) author: Michal Dorner inputs: artifact: @@ -90,6 +90,12 @@ inputs: description: GitHub Access Token required: false default: ${{ github.token }} + create-pr-comment: + description: | + Allows you to create a comment on the pull request with the test results. + If enabled, the comment will contain a summary of the test results + default: 'false' + required: false outputs: conclusion: description: | diff --git a/dist/index.js b/dist/index.js index 1958723..3da0990 100644 --- a/dist/index.js +++ b/dist/index.js @@ -279,6 +279,7 @@ class TestReporter { useActionsSummary = core.getInput('use-actions-summary', { required: false }) === 'true'; badgeTitle = core.getInput('badge-title', { required: false }); token = core.getInput('token', { required: true }); + createPRComment = core.getInput('create-pr-comment', { required: false }) === 'true'; octokit; context = (0, github_utils_1.getCheckRunContext)(); constructor() { @@ -418,6 +419,18 @@ class TestReporter { core.info(`Check run HTML: ${resp.data.html_url}`); core.setOutput('url', resp.data.url); core.setOutput('url_html', resp.data.html_url); + if (this.createPRComment) { + const { pull_request } = github.context.payload; + if (pull_request) { + core.info('Attaching Test Summary as a comment to the PR'); + const comment = `## Test Summary\n\n${summary}`; + await this.octokit.rest.issues.createComment({ + ...github.context.repo, + issue_number: pull_request.number, + body: comment + }); + } + } } return results; } diff --git a/src/main.ts b/src/main.ts index 8fd2bab..242e6ae 100644 --- a/src/main.ts +++ b/src/main.ts @@ -49,6 +49,7 @@ class TestReporter { readonly useActionsSummary = core.getInput('use-actions-summary', {required: false}) === 'true' readonly badgeTitle = core.getInput('badge-title', {required: false}) readonly token = core.getInput('token', {required: true}) + readonly createPRComment = core.getInput('create-pr-comment', {required: false}) === 'true' readonly octokit: InstanceType readonly context = getCheckRunContext() @@ -220,18 +221,20 @@ class TestReporter { core.setOutput('url', resp.data.url) core.setOutput('url_html', resp.data.html_url) - const {pull_request} = github.context.payload + if (this.createPRComment) { + const {pull_request} = github.context.payload - if (pull_request) { - core.info('Attaching Test Summary as a comment to the PR') + if (pull_request) { + core.info('Attaching Test Summary as a comment to the PR') - const comment = `## Test Summary\n\n${summary}` + const comment = `## Test Summary\n\n${summary}` - await this.octokit.rest.issues.createComment({ - ...github.context.repo, - issue_number: pull_request.number, - body: comment - }) + await this.octokit.rest.issues.createComment({ + ...github.context.repo, + issue_number: pull_request.number, + body: comment + }) + } } }