Skip to content

Commit 3540cf6

Browse files
authored
Merge pull request #251 from daun/feat/custom-commenting-logic
Support custom commenting logic
2 parents c04cabd + 62eae39 commit 3540cf6

File tree

6 files changed

+192
-115
lines changed

6 files changed

+192
-115
lines changed

.github/workflows/check-dist.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959
6060
# If index.js was different than expected, upload the expected version as
6161
# a workflow artifact.
62-
- uses: actions/upload-artifact@v3
62+
- uses: actions/upload-artifact@v4
6363
if: ${{ failure() && steps.diff.conclusion == 'failure' }}
6464
with:
6565
name: dist

README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,37 @@ jobs:
4444
report-file: results.json
4545
```
4646
47-
### With all options
47+
### Usage with custom commenting logic
48+
49+
The action will do the work of creating comments and updating them whenever tests re-run. If you
50+
require custom logic for creating and updating comments, you can disable the default logic and use
51+
any other action in combination with the `summary` output of this action.
52+
53+
A few recommended actions are
54+
[Sticky Pull Request Comment](https://github.com/marketplace/actions/sticky-pull-request-comment)
55+
and [Create or Update Comment](https://github.com/marketplace/actions/create-or-update-comment).
56+
57+
```diff
58+
- uses: daun/playwright-report-summary@v3
59+
if: always()
60+
id: summary
61+
with:
62+
report-file: results.json
63+
+ create-comment: false
64+
65+
+ - uses: marocchino/sticky-pull-request-comment@v2
66+
+ with:
67+
+ message: ${{ steps.summary.outputs.summary }}
68+
```
69+
70+
## Options
4871

4972
```yaml
5073
- uses: daun/playwright-report-summary@v3
5174
if: always()
5275
with:
53-
# The GitHub access token to use for API requests. Defaults to the standard GITHUB_TOKEN.
76+
# The GitHub access token to use for API requests
77+
# Defaults to the standard GITHUB_TOKEN
5478
github-token: ''
5579
5680
# Path to the JSON report file generated by Playwright. Required.
@@ -64,10 +88,15 @@ jobs:
6488
# Example pipeline: https://playwright.dev/docs/test-sharding#publishing-report-on-the-web
6589
report-url: 'https://user.github.io/repo/yyyy-mm-dd-id/'
6690
91+
# Whether the action should create the actual comment. Set to false to implement
92+
# your own commenting logic.
93+
# Default: true
94+
create-comment: true
95+
6796
# Title/headline to use for the created pull request comment.
68-
# Default: Playwright test results
97+
# Default: 'Playwright test results'
6998
comment-title: 'Test results'
70-
99+
71100
# Additional information to include in the summary comment, markdown-formatted
72101
# Default: ''
73102
custom-info: 'For more information, [see our readme](http://link)'

__tests__/index.test.ts

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@
66
* variables following the pattern `INPUT_<INPUT_NAME>`.
77
*/
88

9-
import * as core from '@actions/core'
10-
import * as github from '@actions/github'
9+
import { Context } from '@actions/github/lib/context'
10+
import * as actionsCore from '@actions/core'
11+
import * as actionsGitHub from '@actions/github'
12+
1113
import * as index from '../src/index'
1214
import * as fs from '../src/fs'
1315
import * as report from '../src/report'
14-
import { Context } from '@actions/github/lib/context'
16+
import * as github from '../src/github'
1517

1618
// Mock the GitHub Actions core library
17-
jest.spyOn(core, 'info').mockImplementation(jest.fn())
18-
jest.spyOn(core, 'warning').mockImplementation(jest.fn())
19-
jest.spyOn(core, 'error').mockImplementation(jest.fn())
20-
const debugMock = jest.spyOn(core, 'debug').mockImplementation(jest.fn())
21-
const getInputMock = jest.spyOn(core, 'getInput').mockImplementation((name: string) => inputs[name] || '')
22-
const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation(jest.fn())
23-
const setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation(jest.fn())
19+
jest.spyOn(actionsCore, 'info').mockImplementation(jest.fn())
20+
jest.spyOn(actionsCore, 'warning').mockImplementation(jest.fn())
21+
jest.spyOn(actionsCore, 'error').mockImplementation(jest.fn())
22+
const debugMock = jest.spyOn(actionsCore, 'debug').mockImplementation(jest.fn())
23+
const getInputMock = jest.spyOn(actionsCore, 'getInput').mockImplementation((name: string) => inputs[name] || '')
24+
const setFailedMock = jest.spyOn(actionsCore, 'setFailed').mockImplementation(jest.fn())
25+
const setOutputMock = jest.spyOn(actionsCore, 'setOutput').mockImplementation(jest.fn())
2426

2527
// Mock the fs module
2628
const readFileMock = jest.spyOn(fs, 'readFile')
@@ -29,11 +31,14 @@ const readFileMock = jest.spyOn(fs, 'readFile')
2931
const parseReportMock = jest.spyOn(report, 'parseReport')
3032
const renderReportSummaryMock = jest.spyOn(report, 'renderReportSummary')
3133

34+
// Mock the github module
35+
const createIssueCommentMock = jest.spyOn(github, 'createIssueComment')
36+
3237
// Mock the GitHub Actions context library
3338
// const contextMock = jest.spyOn(github, 'context')
3439

3540
// Mock the GitHub Actions Octokit instance
36-
type Octokit = ReturnType<typeof github.getOctokit>
41+
type Octokit = ReturnType<typeof actionsGitHub.getOctokit>
3742

3843
const octokitMock = {
3944
rest: {
@@ -48,7 +53,7 @@ const octokitMock = {
4853
}
4954
} as unknown as Octokit
5055

51-
const getOctokitMock = jest.spyOn(github, 'getOctokit').mockImplementation(() => octokitMock)
56+
const getOctokitMock = jest.spyOn(actionsGitHub, 'getOctokit').mockImplementation(() => octokitMock)
5257

5358
// Mock the action's entrypoint
5459
const runMock = jest.spyOn(index, 'run')
@@ -58,7 +63,7 @@ const runMock = jest.spyOn(index, 'run')
5863

5964
// Shallow clone original @actions/github context
6065
// @ts-expect-error missing issue and repo keys
61-
const originalContext: Context = { issue: {}, ...github.context }
66+
const originalContext: Context = { issue: {}, ...actionsGitHub.context }
6267

6368
const defaultContext = {
6469
eventName: 'pull_request',
@@ -91,7 +96,7 @@ const defaultContext = {
9196
let inputs: Record<string, string> = {}
9297

9398
function setContext(context: any): void {
94-
Object.defineProperty(github, 'context', { value: context, writable: true })
99+
Object.defineProperty(actionsGitHub, 'context', { value: context, writable: true })
95100
}
96101

97102
describe('action', () => {
@@ -127,6 +132,7 @@ describe('action', () => {
127132
expect(getInputMock).toHaveBeenCalledWith('report-tag')
128133
expect(getInputMock).toHaveBeenCalledWith('comment-title')
129134
expect(getInputMock).toHaveBeenCalledWith('icon-style')
135+
expect(getInputMock).toHaveBeenCalledWith('create-comment')
130136
expect(getInputMock).toHaveBeenCalledWith('job-summary')
131137
expect(getInputMock).toHaveBeenCalledWith('test-command')
132138
expect(getInputMock).toHaveBeenCalledWith('footer')
@@ -145,6 +151,8 @@ describe('action', () => {
145151
expect(debugMock).toHaveBeenNthCalledWith(2, 'Report url: (none)')
146152
expect(debugMock).toHaveBeenNthCalledWith(3, 'Report tag: (none)')
147153
expect(debugMock).toHaveBeenNthCalledWith(4, 'Comment title: Custom comment title')
154+
expect(debugMock).toHaveBeenNthCalledWith(5, 'Creating comment? yes')
155+
expect(debugMock).toHaveBeenNthCalledWith(6, 'Creating job summary? no')
148156
})
149157

150158
it('creates an Octokit instance', async () => {
@@ -194,17 +202,44 @@ describe('action', () => {
194202

195203
it('sets a summary and comment id output', async () => {
196204
inputs = {
197-
'report-file': '__tests__/__fixtures__/report-valid.json',
198-
'comment-title': 'Custom comment title'
205+
'report-file': '__tests__/__fixtures__/report-valid.json'
199206
}
200207

201208
await index.run()
202209

203210
expect(runMock).toHaveReturned()
204-
expect(setOutputMock).toHaveBeenNthCalledWith(1, 'summary', expect.anything())
211+
expect(setOutputMock).toHaveBeenNthCalledWith(1, 'summary', expect.stringContaining('# Playwright test results'))
205212
expect(setOutputMock).toHaveBeenNthCalledWith(2, 'comment-id', expect.anything())
206213
})
207214

215+
it('creates a comment', async () => {
216+
inputs = {
217+
'report-file': '__tests__/__fixtures__/report-valid.json'
218+
}
219+
220+
await index.run()
221+
222+
expect(runMock).toHaveReturned()
223+
expect(createIssueCommentMock).toHaveBeenCalledWith(
224+
expect.anything(),
225+
expect.objectContaining({
226+
body: expect.stringContaining('# Playwright test results')
227+
})
228+
)
229+
})
230+
231+
it('can disable creating a comment', async () => {
232+
inputs = {
233+
'report-file': '__tests__/__fixtures__/report-valid.json',
234+
'create-comment': 'false'
235+
}
236+
237+
await index.run()
238+
239+
expect(runMock).toHaveReturned()
240+
expect(createIssueCommentMock).not.toHaveBeenCalled()
241+
})
242+
208243
it('sets a failed status', async () => {
209244
inputs = {
210245
'report-file': 'file-does-not-exist.json'

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ inputs:
2020
report-url:
2121
description: 'URL to the generated html report, if uploaded'
2222
required: false
23+
create-comment:
24+
description: 'Create a pull request comment with the test result summary'
25+
required: false
26+
default: 'true'
2327
comment-title:
2428
description: 'Customize the title of the pull request comment'
2529
required: false

dist/index.js

Lines changed: 51 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)