2025-03-28 15:32:59 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed 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
|
|
|
|
*
|
|
|
|
* http://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 { context, getOctokit } from '@actions/github';
|
|
|
|
import * as core from '@actions/core';
|
|
|
|
|
2025-04-02 17:26:33 -07:00
|
|
|
import MarkdownReporter from './markdownReporter';
|
|
|
|
|
2025-03-28 15:32:59 -07:00
|
|
|
import type { MetadataWithCommitInfo } from 'playwright/src/isomorphic/types';
|
2025-04-01 16:48:57 -07:00
|
|
|
import type { FullConfig } from '@playwright/test';
|
2025-03-28 15:32:59 -07:00
|
|
|
|
|
|
|
function getGithubToken() {
|
|
|
|
const token = process.env.GITHUB_TOKEN || core.getInput('github-token');
|
|
|
|
if (!token) {
|
|
|
|
core.setFailed('Missing "github-token" input');
|
|
|
|
throw new Error('Missing "github-token" input');
|
|
|
|
}
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
const octokit = getOctokit(getGithubToken());
|
|
|
|
|
|
|
|
const magicComment = '<!-- Generated by Playwright markdown reporter -->';
|
|
|
|
|
|
|
|
class GHAMarkdownReporter extends MarkdownReporter {
|
2025-04-01 16:48:57 -07:00
|
|
|
declare config: FullConfig;
|
2025-03-28 15:32:59 -07:00
|
|
|
|
2025-04-01 16:48:57 -07:00
|
|
|
override async publishReport(report: string) {
|
2025-03-28 15:32:59 -07:00
|
|
|
core.info('Publishing report to PR.');
|
|
|
|
const { prNumber, prHref } = this.pullRequestFromMetadata();
|
|
|
|
if (!prNumber) {
|
|
|
|
core.info(`No PR number found, skipping GHA comment. PR href: ${prHref}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
core.info(`Posting comment to PR ${prHref}`);
|
|
|
|
|
|
|
|
await this.collapsePreviousComments(prNumber);
|
|
|
|
await this.addNewReportComment(prNumber, report);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async collapsePreviousComments(prNumber: number) {
|
|
|
|
const { data: comments } = await octokit.rest.issues.listComments({
|
|
|
|
...context.repo,
|
|
|
|
issue_number: prNumber,
|
|
|
|
});
|
|
|
|
for (const comment of comments) {
|
2025-04-01 16:48:57 -07:00
|
|
|
if (comment.user?.login === 'github-actions[bot]' && comment.body?.includes(magicComment)) {
|
2025-03-28 15:32:59 -07:00
|
|
|
core.info(`Minimizing comment: ${comment.html_url}`);
|
|
|
|
await octokit.graphql(`
|
|
|
|
mutation {
|
|
|
|
minimizeComment(input: {subjectId: "${comment.node_id}", classifier: OUTDATED}) {
|
|
|
|
clientMutationId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async addNewReportComment(prNumber: number, report: string) {
|
|
|
|
const reportUrl = process.env.HTML_REPORT_URL;
|
|
|
|
const mergeWorkflowUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
|
|
|
|
|
|
|
|
const { data: response } = await octokit.rest.issues.createComment({
|
|
|
|
...context.repo,
|
|
|
|
issue_number: prNumber,
|
|
|
|
body: formatComment([
|
|
|
|
magicComment,
|
|
|
|
`### [Test results](${reportUrl}) for "${context.payload.workflow_run?.name}"`,
|
|
|
|
report,
|
|
|
|
'',
|
|
|
|
`Merge [workflow run](${mergeWorkflowUrl}).`
|
|
|
|
]),
|
|
|
|
});
|
|
|
|
core.info(`Posted comment: ${response.html_url}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
private pullRequestFromMetadata() {
|
|
|
|
const metadata = this.config.metadata as MetadataWithCommitInfo;
|
|
|
|
const prHref = metadata.ci?.prHref;
|
2025-04-01 16:48:57 -07:00
|
|
|
return { prNumber: parseInt(prHref?.split('/').pop() ?? '', 10), prHref };
|
2025-03-28 15:32:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-01 16:48:57 -07:00
|
|
|
function formatComment(lines: string[]) {
|
2025-03-28 15:32:59 -07:00
|
|
|
let body = lines.join('\n');
|
|
|
|
if (body.length > 65535)
|
|
|
|
body = body.substring(0, 65000) + `... ${body.length - 65000} more characters`;
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default GHAMarkdownReporter;
|