|
15 | 15 | */ |
16 | 16 |
|
17 | 17 | import { MarkdownReporter } from 'playwright/lib/internalsForTest'; |
18 | | -import * as github from '@actions/github'; |
| 18 | +import { context, getOctokit } from '@actions/github'; |
19 | 19 | import * as core from '@actions/core'; |
20 | 20 |
|
21 | 21 | import type { MetadataWithCommitInfo } from 'playwright/src/isomorphic/types'; |
22 | 22 |
|
| 23 | +function getGithubToken() { |
| 24 | + const token = process.env.GITHUB_TOKEN || core.getInput('github-token'); |
| 25 | + if (!token) { |
| 26 | + core.setFailed('Missing "github-token" input'); |
| 27 | + throw new Error('Missing "github-token" input'); |
| 28 | + } |
| 29 | + return token; |
| 30 | +} |
| 31 | + |
| 32 | +const octokit = getOctokit(getGithubToken()); |
| 33 | + |
23 | 34 | const magicComment = '<!-- Generated by Playwright markdown reporter -->'; |
| 35 | + |
24 | 36 | class GHAMarkdownReporter extends MarkdownReporter { |
25 | 37 | // declare config: FullConfig; |
26 | 38 |
|
27 | 39 | async publishReport(report: string) { |
28 | 40 | core.info('Publishing report to PR.'); |
29 | | - const metadata = this.config.metadata as MetadataWithCommitInfo; |
30 | | - const prHref = metadata.ci?.prHref; |
31 | | - const prNumber = parseInt(prHref?.split('/').pop(), 10); |
| 41 | + const { prNumber, prHref } = this.pullRequestFromMetadata(); |
32 | 42 | if (!prNumber) { |
33 | | - core.info(`No PR number found, skipping GHA comment. prHref: ${prHref}`); |
| 43 | + core.info(`No PR number found, skipping GHA comment. PR href: ${prHref}`); |
34 | 44 | return; |
35 | 45 | } |
36 | 46 | core.info(`Posting comment to PR ${prHref}`); |
37 | 47 |
|
38 | | - const token = process.env.GITHUB_TOKEN || core.getInput('github-token'); |
39 | | - if (!token) { |
40 | | - core.setFailed('Missing "github-token" input'); |
41 | | - return; |
42 | | - } |
43 | | - const octokit = github.getOctokit(token); |
44 | | - const context = github.context; |
45 | | - |
46 | | - const reportUrl = process.env.HTML_REPORT_URL; |
47 | | - const mergeWorkflowUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; |
| 48 | + await this.collapsePreviousComments(prNumber); |
| 49 | + await this.addNewReportComment(prNumber, report); |
| 50 | + } |
48 | 51 |
|
49 | | - { |
50 | | - // Mark previous comments as outdated by minimizing them. |
51 | | - const { data: comments } = await octokit.rest.issues.listComments({ |
52 | | - ...context.repo, |
53 | | - issue_number: prNumber, |
54 | | - }); |
55 | | - for (const comment of comments) { |
56 | | - if (comment.user.login === 'github-actions[bot]' && comment.body.includes(magicComment)) { |
57 | | - core.info(`Minimizing comment: ${comment.html_url}`); |
58 | | - await octokit.graphql(` |
59 | | - mutation { |
60 | | - minimizeComment(input: {subjectId: "${comment.node_id}", classifier: OUTDATED}) { |
61 | | - clientMutationId |
62 | | - } |
| 52 | + private async collapsePreviousComments(prNumber: number) { |
| 53 | + const { data: comments } = await octokit.rest.issues.listComments({ |
| 54 | + ...context.repo, |
| 55 | + issue_number: prNumber, |
| 56 | + }); |
| 57 | + for (const comment of comments) { |
| 58 | + if (comment.user.login === 'github-actions[bot]' && comment.body.includes(magicComment)) { |
| 59 | + core.info(`Minimizing comment: ${comment.html_url}`); |
| 60 | + await octokit.graphql(` |
| 61 | + mutation { |
| 62 | + minimizeComment(input: {subjectId: "${comment.node_id}", classifier: OUTDATED}) { |
| 63 | + clientMutationId |
63 | 64 | } |
64 | | - `); |
65 | | - } |
| 65 | + } |
| 66 | + `); |
66 | 67 | } |
67 | 68 | } |
| 69 | + } |
| 70 | + |
| 71 | + private async addNewReportComment(prNumber: number, report: string) { |
| 72 | + const reportUrl = process.env.HTML_REPORT_URL; |
| 73 | + const mergeWorkflowUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; |
68 | 74 |
|
69 | 75 | const { data: response } = await octokit.rest.issues.createComment({ |
70 | 76 | ...context.repo, |
71 | 77 | issue_number: prNumber, |
72 | 78 | body: formatComment([ |
73 | 79 | magicComment, |
74 | | - `### [Test results](${reportUrl}) for "${github.context.payload.workflow_run?.name}"`, |
| 80 | + `### [Test results](${reportUrl}) for "${context.payload.workflow_run?.name}"`, |
75 | 81 | report, |
76 | 82 | '', |
77 | 83 | `Merge [workflow run](${mergeWorkflowUrl}).` |
78 | 84 | ]), |
79 | 85 | }); |
80 | 86 | core.info(`Posted comment: ${response.html_url}`); |
81 | 87 | } |
| 88 | + |
| 89 | + private pullRequestFromMetadata() { |
| 90 | + const metadata = this.config.metadata as MetadataWithCommitInfo; |
| 91 | + const prHref = metadata.ci?.prHref; |
| 92 | + return { prNumber: parseInt(prHref?.split('/').pop(), 10), prHref }; |
| 93 | + } |
82 | 94 | } |
83 | 95 |
|
84 | 96 | function formatComment(lines) { |
|
0 commit comments