|
|
import { context, getOctokit } from '@actions/github' |
|
|
import { info, getInput } from '@actions/core' |
|
|
const { default: stripAnsi } = require('strip-ansi') |
|
|
const fs = require('fs') |
|
|
|
|
|
|
|
|
|
|
|
type Octokit = ReturnType<typeof getOctokit> |
|
|
|
|
|
type Job = Awaited< |
|
|
ReturnType<Octokit['rest']['actions']['listJobsForWorkflowRun']> |
|
|
>['data']['jobs'][number] |
|
|
|
|
|
|
|
|
const BOT_COMMENT_MARKER = `<!-- __marker__ next.js integration stats __marker__ -->` |
|
|
|
|
|
const commentTitlePre = `## Failing next.js integration test suites` |
|
|
|
|
|
|
|
|
async function fetchJobLogsFromWorkflow( |
|
|
octokit: Octokit, |
|
|
job: Job |
|
|
): Promise<{ logs: string; job: Job }> { |
|
|
console.log( |
|
|
`fetchJobLogsFromWorkflow ${job.name}: Checking test results for the job` |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
const jobLogRedirectResponse = |
|
|
await octokit.rest.actions.downloadJobLogsForWorkflowRun({ |
|
|
accept: 'application/vnd.github.v3+json', |
|
|
...context.repo, |
|
|
job_id: job.id, |
|
|
}) |
|
|
|
|
|
console.log( |
|
|
`fetchJobLogsFromWorkflow ${job.name}: Trying to get logs from redirect url ${jobLogRedirectResponse.url}` |
|
|
) |
|
|
|
|
|
|
|
|
const jobLogsResponse = await fetch(jobLogRedirectResponse.url, { |
|
|
headers: { |
|
|
Accept: 'application/vnd.github.v3+json', |
|
|
}, |
|
|
}) |
|
|
|
|
|
console.log( |
|
|
`fetchJobLogsFromWorkflow ${job.name}: Logs response status ${jobLogsResponse.status}` |
|
|
) |
|
|
|
|
|
if (!jobLogsResponse.ok) { |
|
|
throw new Error( |
|
|
`Failed to get logsUrl, got status ${jobLogsResponse.status}` |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const logText: string = await jobLogsResponse.text() |
|
|
const dateTimeStripped = logText |
|
|
.split('\n') |
|
|
.map((line) => line.substring('2020-03-02T19:39:16.8832288Z '.length)) |
|
|
|
|
|
const logs = dateTimeStripped.join('\n') |
|
|
|
|
|
return { logs, job } |
|
|
} |
|
|
|
|
|
|
|
|
async function getInputs(): Promise<{ |
|
|
token: string |
|
|
octokit: Octokit |
|
|
prNumber: number | undefined |
|
|
sha: string |
|
|
noBaseComparison: boolean |
|
|
shouldExpandResultMessages: boolean |
|
|
}> { |
|
|
const token = getInput('token') |
|
|
const octokit = getOctokit(token) |
|
|
|
|
|
const shouldExpandResultMessages = |
|
|
getInput('expand_result_messages') === 'true' |
|
|
|
|
|
if (!shouldExpandResultMessages) { |
|
|
console.log('Test report comment will not include result messages.') |
|
|
} |
|
|
|
|
|
const prNumber = context?.payload?.pull_request?.number |
|
|
const sha = context?.sha |
|
|
|
|
|
|
|
|
const noBaseComparison = prNumber == null |
|
|
|
|
|
if (prNumber != null) { |
|
|
console.log('Trying to collect integration stats for PR', { |
|
|
prNumber, |
|
|
sha: sha, |
|
|
}) |
|
|
|
|
|
const comments = await octokit.paginate(octokit.rest.issues.listComments, { |
|
|
...context.repo, |
|
|
issue_number: prNumber, |
|
|
per_page: 200, |
|
|
}) |
|
|
|
|
|
console.log('Found total comments for PR', comments?.length || 0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const existingComments = comments?.filter( |
|
|
(comment) => |
|
|
comment?.user?.login === 'github-actions[bot]' && |
|
|
comment?.body?.includes(BOT_COMMENT_MARKER) |
|
|
) |
|
|
|
|
|
if (existingComments?.length) { |
|
|
console.log('Found existing comments, deleting them') |
|
|
for (const comment of existingComments) { |
|
|
await octokit.rest.issues.deleteComment({ |
|
|
...context.repo, |
|
|
comment_id: comment.id, |
|
|
}) |
|
|
} |
|
|
} |
|
|
} else { |
|
|
info('No PR number found in context, will not try to post comment.') |
|
|
} |
|
|
|
|
|
const inputs = { |
|
|
token, |
|
|
octokit, |
|
|
prNumber, |
|
|
sha, |
|
|
noBaseComparison, |
|
|
shouldExpandResultMessages, |
|
|
} |
|
|
|
|
|
console.log('getInputs: these inputs will be used to collect test results', { |
|
|
...inputs, |
|
|
token: !!token, |
|
|
}) |
|
|
|
|
|
return inputs |
|
|
} |
|
|
|
|
|
|
|
|
async function getJobResults( |
|
|
octokit: Octokit, |
|
|
token: string, |
|
|
sha: string |
|
|
): Promise<TestResultManifest> { |
|
|
console.log('Trying to collect next.js integration test logs') |
|
|
const jobs = await octokit.paginate( |
|
|
octokit.rest.actions.listJobsForWorkflowRun, |
|
|
{ |
|
|
...context.repo, |
|
|
run_id: context?.runId, |
|
|
per_page: 50, |
|
|
} |
|
|
) |
|
|
|
|
|
|
|
|
const integrationTestJobs = jobs?.filter((job) => |
|
|
/Next\.js integration test \([^)]*\) \([^)]*\)/.test(job.name) |
|
|
) |
|
|
|
|
|
console.log( |
|
|
`Logs found for ${integrationTestJobs.length} jobs`, |
|
|
integrationTestJobs.map((job) => job.name) |
|
|
) |
|
|
|
|
|
|
|
|
const fullJobLogsFromWorkflow = await Promise.all( |
|
|
integrationTestJobs.map((job) => fetchJobLogsFromWorkflow(octokit, job)) |
|
|
) |
|
|
|
|
|
console.log('Logs downloaded for all jobs') |
|
|
|
|
|
const [jobResults, flakyMonitorJobResults] = fullJobLogsFromWorkflow.reduce( |
|
|
(acc, { logs, job }) => { |
|
|
const subset = job.name.includes('FLAKY_SUBSET') |
|
|
const index = subset ? 1 : 0 |
|
|
|
|
|
const { id, run_id, run_url, html_url } = job |
|
|
console.log('Parsing logs for job', { id, run_id, run_url, html_url }) |
|
|
const splittedLogs = logs.split('--test output start--') |
|
|
|
|
|
splittedLogs.shift() |
|
|
for (const logLine of splittedLogs) { |
|
|
let testData: string | undefined |
|
|
try { |
|
|
testData = logLine.split('--test output end--')[0].trim()! |
|
|
|
|
|
const data = JSON.parse(testData) |
|
|
acc[index].push({ |
|
|
job: job.name, |
|
|
data, |
|
|
}) |
|
|
} catch (err) { |
|
|
console.log('Failed to parse test results', { |
|
|
id, |
|
|
run_id, |
|
|
run_url, |
|
|
html_url, |
|
|
testData, |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
return acc |
|
|
}, |
|
|
[[], []] as [Array<JobResult>, Array<JobResult>] |
|
|
) |
|
|
|
|
|
console.log(`Flakyness test subset results`, { flakyMonitorJobResults }) |
|
|
|
|
|
const testResultManifest: TestResultManifest = { |
|
|
ref: sha, |
|
|
flakyMonitorJobResults: flakyMonitorJobResults, |
|
|
result: jobResults, |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync( |
|
|
'./nextjs-test-results.json', |
|
|
JSON.stringify(testResultManifest, null, 2) |
|
|
) |
|
|
|
|
|
return testResultManifest |
|
|
} |
|
|
|
|
|
|
|
|
async function getTestResultDiffBase( |
|
|
_octokit: Octokit |
|
|
): Promise<TestResultManifest | null> { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return null |
|
|
} |
|
|
|
|
|
function withoutRetries(results: Array<JobResult>): Array<JobResult> { |
|
|
results = results.slice().reverse() |
|
|
const seenNames = new Set() |
|
|
results = results.filter((job) => { |
|
|
if ( |
|
|
job.data.testResults.some((testResult) => seenNames.has(testResult.name)) |
|
|
) { |
|
|
return false |
|
|
} |
|
|
job.data.testResults.forEach((testResult) => seenNames.add(testResult.name)) |
|
|
return true |
|
|
}) |
|
|
return results.reverse() |
|
|
} |
|
|
|
|
|
function getTestSummary( |
|
|
sha: string, |
|
|
baseResults: TestResultManifest | null, |
|
|
jobResults: TestResultManifest |
|
|
) { |
|
|
|
|
|
const { |
|
|
currentTestFailedSuiteCount, |
|
|
currentTestPassedSuiteCount, |
|
|
currentTestTotalSuiteCount, |
|
|
currentTestFailedCaseCount, |
|
|
currentTestPassedCaseCount, |
|
|
currentTestTotalCaseCount, |
|
|
currentTestFailedNames, |
|
|
} = withoutRetries(jobResults.result).reduce( |
|
|
(acc, value) => { |
|
|
const { data } = value |
|
|
acc.currentTestFailedSuiteCount += data.numFailedTestSuites |
|
|
acc.currentTestPassedSuiteCount += data.numPassedTestSuites |
|
|
acc.currentTestTotalSuiteCount += data.numTotalTestSuites |
|
|
acc.currentTestFailedCaseCount += data.numFailedTests |
|
|
acc.currentTestPassedCaseCount += data.numPassedTests |
|
|
acc.currentTestTotalCaseCount += data.numTotalTests |
|
|
for (const testResult of data.testResults ?? []) { |
|
|
if (testResult.status !== 'passed' && testResult.name.length > 2) { |
|
|
acc.currentTestFailedNames.push(testResult.name) |
|
|
} |
|
|
} |
|
|
return acc |
|
|
}, |
|
|
{ |
|
|
currentTestFailedSuiteCount: 0, |
|
|
currentTestPassedSuiteCount: 0, |
|
|
currentTestTotalSuiteCount: 0, |
|
|
currentTestFailedCaseCount: 0, |
|
|
currentTestPassedCaseCount: 0, |
|
|
currentTestTotalCaseCount: 0, |
|
|
currentTestFailedNames: [] as Array<string>, |
|
|
} |
|
|
) |
|
|
|
|
|
console.log( |
|
|
'Current test summary', |
|
|
JSON.stringify( |
|
|
{ |
|
|
currentTestFailedSuiteCount, |
|
|
currentTestPassedSuiteCount, |
|
|
currentTestTotalSuiteCount, |
|
|
currentTestFailedCaseCount, |
|
|
currentTestPassedCaseCount, |
|
|
currentTestTotalCaseCount, |
|
|
currentTestFailedNames, |
|
|
}, |
|
|
null, |
|
|
2 |
|
|
) |
|
|
) |
|
|
|
|
|
if (!baseResults) { |
|
|
console.log("There's no base to compare") |
|
|
|
|
|
return `### Test summary |
|
|
| | Current (${sha}) | Diff | |
|
|
|---|---|---| |
|
|
| Failed Suites | ${currentTestFailedSuiteCount} | N/A | |
|
|
| Failed Cases | ${currentTestFailedCaseCount} | N/A |` |
|
|
} |
|
|
|
|
|
const { |
|
|
baseTestFailedSuiteCount, |
|
|
baseTestPassedSuiteCount, |
|
|
baseTestTotalSuiteCount, |
|
|
baseTestFailedCaseCount, |
|
|
baseTestPassedCaseCount, |
|
|
baseTestTotalCaseCount, |
|
|
baseTestFailedNames, |
|
|
} = withoutRetries(baseResults.result).reduce( |
|
|
(acc, value) => { |
|
|
const { data } = value |
|
|
acc.baseTestFailedSuiteCount += data.numFailedTestSuites |
|
|
acc.baseTestPassedSuiteCount += data.numPassedTestSuites |
|
|
acc.baseTestTotalSuiteCount += data.numTotalTestSuites |
|
|
acc.baseTestFailedCaseCount += data.numFailedTests |
|
|
acc.baseTestPassedCaseCount += data.numPassedTests |
|
|
acc.baseTestTotalCaseCount += data.numTotalTests |
|
|
for (const testResult of data.testResults ?? []) { |
|
|
if (testResult.status !== 'passed' && testResult.name.length > 2) { |
|
|
acc.baseTestFailedNames.push(testResult.name) |
|
|
} |
|
|
} |
|
|
return acc |
|
|
}, |
|
|
{ |
|
|
baseTestFailedSuiteCount: 0, |
|
|
baseTestPassedSuiteCount: 0, |
|
|
baseTestTotalSuiteCount: 0, |
|
|
baseTestFailedCaseCount: 0, |
|
|
baseTestPassedCaseCount: 0, |
|
|
baseTestTotalCaseCount: 0, |
|
|
baseTestFailedNames: [] as Array<string>, |
|
|
} |
|
|
) |
|
|
|
|
|
console.log( |
|
|
'Base test summary', |
|
|
JSON.stringify( |
|
|
{ |
|
|
baseTestFailedSuiteCount, |
|
|
baseTestPassedSuiteCount, |
|
|
baseTestTotalSuiteCount, |
|
|
baseTestFailedCaseCount, |
|
|
baseTestPassedCaseCount, |
|
|
baseTestTotalCaseCount, |
|
|
baseTestFailedNames, |
|
|
}, |
|
|
null, |
|
|
2 |
|
|
) |
|
|
) |
|
|
|
|
|
let testSuiteDiff = ':zero:' |
|
|
const suiteCountDiff = baseTestFailedSuiteCount - currentTestFailedSuiteCount |
|
|
if (suiteCountDiff > 0) { |
|
|
testSuiteDiff = `:arrow_down_small: ${suiteCountDiff}` |
|
|
} else if (suiteCountDiff < 0) { |
|
|
testSuiteDiff = `:arrow_up_small: ${-suiteCountDiff}` |
|
|
} |
|
|
|
|
|
let testCaseDiff = ':zero:' |
|
|
const caseCountDiff = baseTestFailedCaseCount - currentTestFailedCaseCount |
|
|
if (caseCountDiff > 0) { |
|
|
testCaseDiff = `:arrow_down_small: ${caseCountDiff}` |
|
|
} else if (caseCountDiff < 0) { |
|
|
testCaseDiff = `:arrow_up_small: ${-caseCountDiff}` |
|
|
} |
|
|
|
|
|
|
|
|
let ret = `### Test summary |
|
|
| | ${`canary (${baseResults.ref}`} | Current (${sha}) | Diff (Failed) | |
|
|
|---|---|---|---| |
|
|
| Test suites | :red_circle: ${baseTestFailedSuiteCount} / :green_circle: ${baseTestPassedSuiteCount} (Total: ${baseTestTotalSuiteCount}) | :red_circle: ${currentTestFailedSuiteCount} / :green_circle: ${currentTestPassedSuiteCount} (Total: ${currentTestTotalSuiteCount}) | ${testSuiteDiff} | |
|
|
| Test cases | :red_circle: ${baseTestFailedCaseCount} / :green_circle: ${baseTestPassedCaseCount} (Total: ${baseTestTotalCaseCount}) | :red_circle: ${currentTestFailedCaseCount} / :green_circle: ${currentTestPassedCaseCount} (Total: ${currentTestTotalCaseCount}) | ${testCaseDiff} | |
|
|
|
|
|
` |
|
|
|
|
|
const fixedTests = baseTestFailedNames.filter( |
|
|
(name) => !currentTestFailedNames.includes(name) |
|
|
) |
|
|
const newFailedTests = currentTestFailedNames.filter( |
|
|
(name) => !baseTestFailedNames.includes(name) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (newFailedTests.length > 0) { |
|
|
ret += `\n:x: **Newly failed tests:**\n\n${newFailedTests |
|
|
.map((t) => (t.length > 5 ? `\t- ${t}` : t)) |
|
|
.join(' \n')}` |
|
|
} |
|
|
|
|
|
console.log('Newly failed tests', JSON.stringify(newFailedTests, null, 2)) |
|
|
console.log('Fixed tests', JSON.stringify(fixedTests, null, 2)) |
|
|
|
|
|
return ret |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const createFormattedComment = (comment: { |
|
|
header: Array<string> |
|
|
contents: Array<string> |
|
|
}) => { |
|
|
return ( |
|
|
[ |
|
|
`${commentTitlePre} ${BOT_COMMENT_MARKER}`, |
|
|
...(comment.header ?? []), |
|
|
].join(`\n`) + |
|
|
`\n\n` + |
|
|
comment.contents.join(`\n`) |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
const createCommentPostAsync = |
|
|
(octokit: Octokit, prNumber?: number) => async (body: string) => { |
|
|
if (!prNumber) { |
|
|
console.log( |
|
|
"This workflow run doesn't seem to be triggered via PR, there's no corresponding PR number. Skipping creating a comment." |
|
|
) |
|
|
return |
|
|
} |
|
|
|
|
|
const result = await octokit.rest.issues.createComment({ |
|
|
...context.repo, |
|
|
issue_number: prNumber, |
|
|
body, |
|
|
}) |
|
|
|
|
|
console.log('Created a new comment', result.data.html_url) |
|
|
} |
|
|
|
|
|
|
|
|
async function run() { |
|
|
const { |
|
|
token, |
|
|
octokit, |
|
|
prNumber, |
|
|
sha, |
|
|
noBaseComparison, |
|
|
shouldExpandResultMessages, |
|
|
} = await getInputs() |
|
|
|
|
|
|
|
|
const jobResults = await getJobResults(octokit, token, sha) |
|
|
|
|
|
|
|
|
const baseResults = noBaseComparison |
|
|
? null |
|
|
: await getTestResultDiffBase(octokit) |
|
|
|
|
|
const postCommentAsync = createCommentPostAsync(octokit, prNumber) |
|
|
|
|
|
const failedTestLists = [] |
|
|
const passedTestsLists = [] |
|
|
|
|
|
const perJobFailedLists = {} |
|
|
|
|
|
|
|
|
const comments = jobResults.result.reduce((acc, value, _idx) => { |
|
|
const { data: testData } = value |
|
|
|
|
|
const commentValues = [] |
|
|
|
|
|
|
|
|
const groupedFails = {} |
|
|
let resultMessage = '' |
|
|
for (const testResult of testData.testResults ?? []) { |
|
|
resultMessage += stripAnsi(testResult?.message) |
|
|
resultMessage += '\n\n' |
|
|
const failedAssertions = testResult?.assertionResults?.filter( |
|
|
(res) => res.status === 'failed' |
|
|
) |
|
|
|
|
|
for (const fail of failedAssertions ?? []) { |
|
|
const ancestorKey = fail?.ancestorTitles?.join(' > ')! |
|
|
|
|
|
if (!groupedFails[ancestorKey]) { |
|
|
groupedFails[ancestorKey] = [] |
|
|
} |
|
|
groupedFails[ancestorKey].push(fail) |
|
|
} |
|
|
} |
|
|
|
|
|
let hasFailedTest = false |
|
|
for (const test of testData.testResults ?? []) { |
|
|
if (test.status !== 'passed') { |
|
|
const failedTest = test.name |
|
|
if (!failedTestLists.includes(failedTest)) { |
|
|
commentValues.push(`\`${failedTest}\``) |
|
|
failedTestLists.push(failedTest) |
|
|
|
|
|
if (!perJobFailedLists[value.job]) { |
|
|
perJobFailedLists[value.job] = [] |
|
|
} |
|
|
perJobFailedLists[value.job].push(failedTest) |
|
|
} |
|
|
} else { |
|
|
passedTestsLists.push(test.name) |
|
|
} |
|
|
} |
|
|
if (hasFailedTest) commentValues.push(`\n`) |
|
|
|
|
|
|
|
|
|
|
|
if (shouldExpandResultMessages) { |
|
|
for (const group of Object.keys(groupedFails).sort()) { |
|
|
const fails = groupedFails[group] |
|
|
commentValues.push(`\n`) |
|
|
fails.forEach((fail) => { |
|
|
commentValues.push(`- ${group} > ${fail.title}`) |
|
|
}) |
|
|
} |
|
|
|
|
|
resultMessage = resultMessage.trim() |
|
|
const strippedResultMessage = |
|
|
resultMessage.length >= 50000 |
|
|
? resultMessage.substring(0, 50000) + |
|
|
`...\n(Test result messages are too long, cannot post full message in comment. See the action logs for the full message.)` |
|
|
: resultMessage |
|
|
if (resultMessage.length >= 50000) { |
|
|
console.log( |
|
|
'Test result messages are too long, comment will post stripped.' |
|
|
) |
|
|
} |
|
|
|
|
|
commentValues.push(`<details>`) |
|
|
commentValues.push(`<summary>Expand output</summary>`) |
|
|
commentValues.push(strippedResultMessage) |
|
|
commentValues.push(`</details>`) |
|
|
commentValues.push(`\n`) |
|
|
} |
|
|
|
|
|
|
|
|
const commentIdxToUpdate = acc.length - 1 |
|
|
if ( |
|
|
acc.length === 0 || |
|
|
commentValues.join(`\n`).length + |
|
|
acc[commentIdxToUpdate].contents.join(`\n`).length > |
|
|
60000 |
|
|
) { |
|
|
acc.push({ |
|
|
header: [`Commit: ${sha}`], |
|
|
contents: commentValues, |
|
|
}) |
|
|
} else { |
|
|
acc[commentIdxToUpdate].contents.push(...commentValues) |
|
|
} |
|
|
return acc |
|
|
}, []) |
|
|
|
|
|
const commentsWithSummary = [ |
|
|
|
|
|
{ |
|
|
header: [`Commit: ${sha}`], |
|
|
contents: [ |
|
|
getTestSummary(sha, noBaseComparison ? null : baseResults, jobResults), |
|
|
], |
|
|
}, |
|
|
...comments, |
|
|
] |
|
|
const isMultipleComments = comments.length > 1 |
|
|
|
|
|
try { |
|
|
|
|
|
fs.writeFileSync( |
|
|
'./failed-test-path-list.json', |
|
|
JSON.stringify( |
|
|
failedTestLists.filter((x) => x.length > 5), |
|
|
null, |
|
|
2 |
|
|
) |
|
|
) |
|
|
|
|
|
fs.writeFileSync( |
|
|
'./passed-test-path-list.json', |
|
|
JSON.stringify(passedTestsLists, null, 2) |
|
|
) |
|
|
|
|
|
if (!prNumber) { |
|
|
return |
|
|
} |
|
|
|
|
|
if (jobResults.result.length === 0) { |
|
|
console.log('No failed test results found :tada:') |
|
|
await postCommentAsync( |
|
|
`### Next.js test passes :green_circle: ${BOT_COMMENT_MARKER}` + |
|
|
`\nCommit: ${sha}\n` |
|
|
) |
|
|
return |
|
|
} |
|
|
|
|
|
for (const [idx, comment] of commentsWithSummary.entries()) { |
|
|
const value = { |
|
|
...comment, |
|
|
} |
|
|
if (isMultipleComments) { |
|
|
value.header.push( |
|
|
`**(Report ${idx + 1}/${commentsWithSummary.length})**` |
|
|
) |
|
|
} |
|
|
|
|
|
if (idx > 0) { |
|
|
value.contents = [ |
|
|
`<details>`, |
|
|
`<summary>Expand full test reports</summary>`, |
|
|
`\n`, |
|
|
...value.contents, |
|
|
`</details>`, |
|
|
] |
|
|
} |
|
|
const commentBodyText = createFormattedComment(value) |
|
|
await postCommentAsync(commentBodyText) |
|
|
} |
|
|
} catch (error) { |
|
|
console.error('Failed to post comment', error) |
|
|
|
|
|
|
|
|
throw error |
|
|
} |
|
|
} |
|
|
|
|
|
run() |
|
|
|