/** * 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'; import MarkdownReporter from './markdownReporter'; import type { MetadataWithCommitInfo } from 'playwright/src/isomorphic/types'; 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 = ''; class GHAMarkdownReporter extends MarkdownReporter { override async publishReport(report: string) { 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) { if (comment.user?.login === 'github-actions[bot]' && comment.body?.includes(magicComment)) { 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; return { prNumber: parseInt(prHref?.split('/').pop() ?? '', 10), prHref }; } } function formatComment(lines: string[]) { let body = lines.join('\n'); if (body.length > 65535) body = body.substring(0, 65000) + `... ${body.length - 65000} more characters`; return body; } export default GHAMarkdownReporter;