chore(ci): simplify github reporter (#5928)

This commit is contained in:
Michael Kriese 2020-04-09 13:51:06 +02:00 committed by GitHub
parent 4ce5860a11
commit a7255fc523
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 90 additions and 223 deletions

View file

@ -139,8 +139,6 @@ jobs:
- name: Unit tests
run: yarn jest --maxWorkers=2 --ci --coverage ${{ env.coverage }}
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
- name: Upload coverage
uses: actions/upload-artifact@v1
@ -215,7 +213,9 @@ jobs:
run: yarn install --frozen-lockfile --link-duplicates
- name: Lint
run: yarn lint
run: |
yarn eslint -f ./tmp/tools/eslint-gh-reporter.js
yarn prettier
- name: Test schema
run: yarn test-schema

View file

@ -179,7 +179,6 @@
},
"devDependencies": {
"@actions/core": "1.2.3",
"@actions/github": "2.1.1",
"@babel/cli": "7.8.4",
"@babel/core": "7.9.0",
"@babel/node": "7.8.7",
@ -190,13 +189,13 @@
"@babel/preset-typescript": "7.9.0",
"@jest/reporters": "25.2.6",
"@jest/test-result": "25.2.6",
"@octokit/rest": "16.43.1",
"@semantic-release/exec": "5.0.0",
"@types/bunyan": "1.8.6",
"@types/cacache": "12.0.1",
"@types/chai": "4.2.11",
"@types/clean-git-ref": "2.0.0",
"@types/convert-hrtime": "2.0.0",
"@types/eslint": "6.8.0",
"@types/fs-extra": "8.1.0",
"@types/github-url-from-git": "1.5.0",
"@types/global-agent": "2.1.0",

View file

@ -0,0 +1,46 @@
import { CLIEngine, Linter } from 'eslint';
import { error } from '@actions/core';
import { issueCommand } from '@actions/core/lib/command';
import { relative } from 'path';
import stripAnsi from 'strip-ansi';
const ROOT = process.cwd();
type Level = 'debug' | 'warning' | 'error';
function getCmd(severity: Linter.Severity): Level {
switch (severity) {
case 2:
return 'error';
case 1:
return 'warning';
default:
return 'debug';
}
}
function getPath(path: string): string {
return relative(ROOT, path).replace(/\\/g, '/');
}
const formatter: CLIEngine.Formatter = results => {
try {
for (const { filePath, messages } of results) {
const file = getPath(filePath);
for (const { severity, line, column, ruleId, message } of messages) {
const cmd = getCmd(severity);
const pos = { line: line.toString(), col: column.toString() };
issueCommand(
cmd,
{ file, ...pos },
stripAnsi(`[${ruleId}] ${message}`)
);
}
}
} catch (e) {
error(`Unexpected error: ${e}`);
}
return '';
};
export = formatter;

View file

@ -1,26 +1,24 @@
import { AggregatedResult, BaseReporter, Context } from '@jest/reporters';
import { AssertionResult, TestResult } from '@jest/test-result';
import { error, info } from '@actions/core';
import { GitHub } from '@actions/github';
import { Octokit } from '@octokit/rest';
import { error } from '@actions/core';
import { issueCommand } from '@actions/core/lib/command';
import { relative } from 'path';
import stripAnsi from 'strip-ansi';
import { getEnv } from './utils';
const name = 'jest-results';
const ROOT = process.cwd();
type Level = 'notice' | 'warning' | 'failure';
type Level = 'debug' | 'warning' | 'error';
function getLevel(test: AssertionResult): Level {
function getCmd(test: AssertionResult): Level {
switch (test.status) {
case 'failed':
return 'failure';
return 'error';
case 'pending':
case 'todo':
return 'warning';
default:
return 'notice';
return 'debug';
}
}
@ -29,155 +27,49 @@ function getPath(suite: TestResult): string {
}
const ignoreStates = new Set(['passed', 'pending']);
const MAX_ANNOTATIONS = 50;
const lineRe = /\.spec\.ts:(?<line>\d+):(?<col>\d+)\)/;
function getPos(
msg: string
): Pick<
Octokit.ChecksCreateParamsOutputAnnotations,
'end_column' | 'end_line' | 'start_column' | 'start_line'
> {
function getPos(msg: string): Record<string, string> {
const pos = lineRe.exec(msg);
if (!pos || !pos.groups) {
return { start_line: 0, end_line: 0 };
return {};
}
const line = parseInt(pos.groups.line, 10);
const col = parseInt(pos.groups.col, 10);
const line = pos.groups.line;
const col = pos.groups.col;
return {
start_line: line,
end_line: line,
start_column: col,
end_column: col,
line,
col,
};
}
class GitHubReporter extends BaseReporter {
private readonly _api: GitHub | null = null;
constructor() {
super();
// eslint-disable-next-line class-methods-use-this
onRunComplete(_contexts: Set<Context>, testResult: AggregatedResult): void {
try {
const token = getEnv('GITHUB_TOKEN');
if (!token) {
if (getEnv('GITHUB_ACTIONS') !== 'true') {
return;
}
this._api = new GitHub(token);
} catch (e) {
error(`Unexpected error: ${e}`);
}
}
async onRunComplete(
_contexts: Set<Context>,
testResult: AggregatedResult
): Promise<void> {
try {
if (getEnv('GITHUB_ACTIONS') !== 'true' || !this._api) {
return;
}
const annotations: Octokit.ChecksCreateParamsOutputAnnotations[] = [];
const success = testResult.numFailedTests === 0;
for (const suite of testResult.testResults.filter(s => !s.skipped)) {
const path = getPath(suite);
const file = getPath(suite);
for (const test of suite.testResults.filter(
t => !ignoreStates.has(t.status)
)) {
if (annotations.length === MAX_ANNOTATIONS) {
await this._createOrUpdate(success, annotations);
annotations.length = 0;
break;
}
const message =
stripAnsi(test.failureMessages?.join('\n ')) ||
`test status: ${test.status}`;
const pos = getPos(message);
const cmd = getCmd(test);
annotations.push({
title: test.fullName.substr(0, 255),
message,
path,
annotation_level: getLevel(test),
...pos,
});
issueCommand(cmd, { file, ...pos }, message);
}
}
if (annotations.length) {
await this._createOrUpdate(success, annotations);
}
} catch (e) {
error(`Unexpected error: ${e}`);
}
}
private async _createOrUpdate(
success: boolean,
annotations: Octokit.ChecksCreateParamsOutputAnnotations[]
): Promise<void> {
if (!this._api) {
return;
}
const run_id = parseInt(getEnv('GITHUB_RUN_ID'), 10);
const [owner, repo] = getEnv('GITHUB_REPOSITORY').split('/');
const checkArgs = {
owner,
repo,
};
const { data: wf } = await this._api.actions.getWorkflowRun({
owner,
repo,
run_id,
});
const ref = wf.head_sha;
info(`repo: ${owner} / ${repo}`);
info(`sha: ${ref}`);
const output: Octokit.ChecksCreateParamsOutput = {
summary: 'Jest test results',
title: 'Jest',
};
if (annotations.length) {
output.annotations = annotations;
}
const { data } = await this._api.checks.listForRef({
...checkArgs,
check_name: name,
ref,
filter: 'latest',
});
const check = data.check_runs.find(c => c.name === name);
if (check) {
info(`Update check run: ${check.name} (${check.id}) ${check.html_url}`);
await this._api.checks.update({
...checkArgs,
check_run_id: check.id,
completed_at: new Date().toISOString(),
conclusion:
success && check.conclusion === 'success' ? 'success' : 'failure',
output,
});
return;
}
info(`Create check run`);
await this._api.checks.create({
...checkArgs,
name,
head_sha: ref,
completed_at: new Date().toISOString(),
conclusion: success ? 'success' : 'failure',
output: { ...output },
});
}
}
export = GitHubReporter;

View file

@ -3,11 +3,10 @@
"type": "module",
"dependencies": {
"@actions/core": "1.2.3",
"@actions/github": "2.1.1",
"@jest/reporters": "25.2.6",
"@jest/test-result": "25.2.6",
"@octokit/rest": "16.43.1",
"commander": "4.1.1",
"eslint": "6.8.0",
"fs-extra": "9.0.0",
"got": "9.6.0",
"lodash": "4.17.15",

111
yarn.lock
View file

@ -7,22 +7,6 @@
resolved "https://registry.yarnpkg.com/@actions/core/-/core-1.2.3.tgz#e844b4fa0820e206075445079130868f95bfca95"
integrity sha512-Wp4xnyokakM45Uuj4WLUxdsa8fJjKVl1fDTsPbTEcTcuu0Nb26IPQbOtjmnfaCPGcaoPOOqId8H9NapZ8gii4w==
"@actions/github@2.1.1":
version "2.1.1"
resolved "https://registry.yarnpkg.com/@actions/github/-/github-2.1.1.tgz#bcabedff598196d953f58ba750d5e75549a75142"
integrity sha512-kAgTGUx7yf5KQCndVeHSwCNZuDBvPyxm5xKTswW2lofugeuC1AZX73nUUVDNaysnM9aKFMHv9YCdVJbg7syEyA==
dependencies:
"@actions/http-client" "^1.0.3"
"@octokit/graphql" "^4.3.1"
"@octokit/rest" "^16.43.1"
"@actions/http-client@^1.0.3":
version "1.0.6"
resolved "https://registry.yarnpkg.com/@actions/http-client/-/http-client-1.0.6.tgz#6f9267ca50e1d74d8581f4a894a943cd4c97b49a"
integrity sha512-LGmio4w98UyGX33b/W6V6Nx/sQHRXZ859YlMkn36wPsXPB82u8xTVlA/Dq2DXrm6lEq9RVmisRJa1c+HETAIJA==
dependencies:
tunnel "0.0.6"
"@babel/cli@7.8.4":
version "7.8.4"
resolved "https://registry.yarnpkg.com/@babel/cli/-/cli-7.8.4.tgz#505fb053721a98777b2b175323ea4f090b7d3c1c"
@ -1107,13 +1091,6 @@
"@octokit/types" "^2.0.0"
universal-user-agent "^4.0.0"
"@octokit/plugin-paginate-rest@^1.1.1":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-1.1.2.tgz#004170acf8c2be535aba26727867d692f7b488fc"
integrity sha512-jbsSoi5Q1pj63sC16XIUboklNw+8tL9VOnJsWycWYR78TKss5PVpIPb1TUUcMQ+bBh7cY579cVAWmf5qG+dw+Q==
dependencies:
"@octokit/types" "^2.0.1"
"@octokit/plugin-paginate-rest@^2.0.0":
version "2.0.2"
resolved "https://registry.yarnpkg.com/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-2.0.2.tgz#fee7a81a4cc7d03784aaf9225499dd6e27f6d01e"
@ -1126,14 +1103,6 @@
resolved "https://registry.yarnpkg.com/@octokit/plugin-request-log/-/plugin-request-log-1.0.0.tgz#eef87a431300f6148c39a7f75f8cfeb218b2547e"
integrity sha512-ywoxP68aOT3zHCLgWZgwUJatiENeHE7xJzYjfz8WI0goynp96wETBF+d95b8g/uL4QmS6owPVlaxiz3wyMAzcw==
"@octokit/plugin-rest-endpoint-methods@2.4.0":
version "2.4.0"
resolved "https://registry.yarnpkg.com/@octokit/plugin-rest-endpoint-methods/-/plugin-rest-endpoint-methods-2.4.0.tgz#3288ecf5481f68c494dd0602fc15407a59faf61e"
integrity sha512-EZi/AWhtkdfAYi01obpX0DF7U6b1VRr30QNQ5xSFPITMdLSfhcBqjamE3F+sKcxPbD7eZuMHu3Qkk2V+JGxBDQ==
dependencies:
"@octokit/types" "^2.0.1"
deprecation "^2.3.1"
"@octokit/plugin-rest-endpoint-methods@^3.0.0":
version "3.2.0"
resolved "https://registry.yarnpkg.com/@octokit/plugin-rest-endpoint-methods/-/plugin-rest-endpoint-methods-3.2.0.tgz#ecc4bc594a57ebfb418b8c4a8c0f200455759004"
@ -1142,15 +1111,6 @@
"@octokit/types" "^2.0.1"
deprecation "^2.3.1"
"@octokit/request-error@^1.0.2":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@octokit/request-error/-/request-error-1.2.1.tgz#ede0714c773f32347576c25649dc013ae6b31801"
integrity sha512-+6yDyk1EES6WK+l3viRDElw96MvwfJxCt45GvmjDUKWjYIb3PJZQkq3i46TwGwoPD4h8NmTrENmtyA1FwbmhRA==
dependencies:
"@octokit/types" "^2.0.0"
deprecation "^2.0.0"
once "^1.4.0"
"@octokit/request-error@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@octokit/request-error/-/request-error-2.0.0.tgz#94ca7293373654400fbb2995f377f9473e00834b"
@ -1160,7 +1120,7 @@
deprecation "^2.0.0"
once "^1.4.0"
"@octokit/request@^5.2.0", "@octokit/request@^5.3.0", "@octokit/request@^5.3.1":
"@octokit/request@^5.3.0", "@octokit/request@^5.3.1":
version "5.3.4"
resolved "https://registry.yarnpkg.com/@octokit/request/-/request-5.3.4.tgz#fbc950bf785d59da3b0399fc6d042c8cf52e2905"
integrity sha512-qyj8G8BxQyXjt9Xu6NvfvOr1E0l35lsXtwm3SopsYg/JWXjlsnwqLc8rsD2OLguEL/JjLfBvrXr4az7z8Lch2A==
@ -1174,28 +1134,6 @@
once "^1.4.0"
universal-user-agent "^5.0.0"
"@octokit/rest@16.43.1", "@octokit/rest@^16.43.1":
version "16.43.1"
resolved "https://registry.yarnpkg.com/@octokit/rest/-/rest-16.43.1.tgz#3b11e7d1b1ac2bbeeb23b08a17df0b20947eda6b"
integrity sha512-gfFKwRT/wFxq5qlNjnW2dh+qh74XgTQ2B179UX5K1HYCluioWj8Ndbgqw2PVqa1NnVJkGHp2ovMpVn/DImlmkw==
dependencies:
"@octokit/auth-token" "^2.4.0"
"@octokit/plugin-paginate-rest" "^1.1.1"
"@octokit/plugin-request-log" "^1.0.0"
"@octokit/plugin-rest-endpoint-methods" "2.4.0"
"@octokit/request" "^5.2.0"
"@octokit/request-error" "^1.0.2"
atob-lite "^2.0.0"
before-after-hook "^2.0.0"
btoa-lite "^1.0.0"
deprecation "^2.0.0"
lodash.get "^4.4.2"
lodash.set "^4.3.2"
lodash.uniq "^4.5.0"
octokit-pagination-methods "^1.1.0"
once "^1.4.0"
universal-user-agent "^4.0.0"
"@octokit/rest@^17.0.0":
version "17.0.0"
resolved "https://registry.yarnpkg.com/@octokit/rest/-/rest-17.0.0.tgz#1f44d96005f5946665fd42a85cd3e428172f01dc"
@ -1444,6 +1382,19 @@
resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
integrity sha512-OCutwjDZ4aFS6PB1UZ988C4YgwlBHJd6wCeQqaLdmadZ/7e+w79+hbMUFC1QXDNCmdyoRfAFdm0RypzwR+Qpag==
"@types/eslint@6.8.0":
version "6.8.0"
resolved "https://registry.yarnpkg.com/@types/eslint/-/eslint-6.8.0.tgz#5f2289b9f01316da7cf31c9e63109a10602a23cb"
integrity sha512-hqzmggoxkOubpgTdcOltkfc5N8IftRJqU70d1jbOISjjZVPvjcr+CLi2CI70hx1SUIRkLgpglTy9w28nGe2Hsw==
dependencies:
"@types/estree" "*"
"@types/json-schema" "*"
"@types/estree@*":
version "0.0.44"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.44.tgz#980cc5a29a3ef3bea6ff1f7d021047d7ea575e21"
integrity sha512-iaIVzr+w2ZJ5HkidlZ3EJM8VTZb2MJLCjw3V+505yVts0gRC4UMvjw0d1HPtGqI/HQC/KdsYtayfzl+AXY2R8g==
"@types/events@*":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7"
@ -1522,6 +1473,11 @@
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-3.12.3.tgz#abf383c5b639d0aa8b8c4a420d6a85f703357d6c"
integrity sha512-otRe77JNNWzoVGLKw8TCspKswRoQToys4tuL6XYVBFxjgeM0RUrx7m3jkaTdxILxeGry3zM8mGYkGXMeQ02guA==
"@types/json-schema@*":
version "7.0.4"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.4.tgz#38fd73ddfd9b55abb1e1b2ed578cb55bd7b7d339"
integrity sha512-8+KAKzEvSUdeo+kmqnKrqgeE+LcA0tjYWFY7RPProVYwnqDjukzO+3b6dLD56rYX5TdWejnEOLJYOIeh4CXKuA==
"@types/json-schema@^7.0.3":
version "7.0.3"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636"
@ -2064,11 +2020,6 @@ at-least-node@^1.0.0:
resolved "https://registry.yarnpkg.com/at-least-node/-/at-least-node-1.0.0.tgz#602cd4b46e844ad4effc92a8011a3c46e0238dc2"
integrity sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==
atob-lite@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/atob-lite/-/atob-lite-2.0.0.tgz#0fef5ad46f1bd7a8502c65727f0367d5ee43d696"
integrity sha1-D+9a1G8b16hQLGVyfwNn1e5D1pY=
atob@^2.1.1:
version "2.1.2"
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
@ -2204,7 +2155,7 @@ bcrypt-pbkdf@^1.0.0:
dependencies:
tweetnacl "^0.14.3"
before-after-hook@^2.0.0, before-after-hook@^2.1.0:
before-after-hook@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/before-after-hook/-/before-after-hook-2.1.0.tgz#b6c03487f44e24200dd30ca5e6a1979c5d2fb635"
integrity sha512-IWIbu7pMqyw3EAJHzzHbWa85b6oud/yfKYg5rqB5hNE8CeMi3nX+2C2sj0HswfblST86hpVEOAb9x34NZd6P7A==
@ -2313,11 +2264,6 @@ bser@^2.0.0:
dependencies:
node-int64 "^0.4.0"
btoa-lite@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/btoa-lite/-/btoa-lite-1.0.0.tgz#337766da15801210fdd956c22e9c6891ab9d0337"
integrity sha1-M3dm2hWAEhD92VbCLpxokaudAzc=
buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
@ -6159,11 +6105,6 @@ lodash.isstring@^4.0.1:
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=
lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
integrity sha1-2HV7HagH3eJIFrDWqEvqGnYjCyM=
lodash.sortby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
@ -6194,7 +6135,7 @@ lodash.union@~4.6.0:
resolved "https://registry.yarnpkg.com/lodash.union/-/lodash.union-4.6.0.tgz#48bb5088409f16f1821666641c44dd1aaae3cd88"
integrity sha1-SLtQiECfFvGCFmZkHETdGqrjzYg=
lodash.uniq@^4.5.0, lodash.uniq@~4.5.0:
lodash.uniq@~4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
integrity sha1-0CJTc662Uq3BvILklFM5qEJ1R3M=
@ -7283,11 +7224,6 @@ object.values@^1.1.0:
function-bind "^1.1.1"
has "^1.0.3"
octokit-pagination-methods@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/octokit-pagination-methods/-/octokit-pagination-methods-1.1.0.tgz#cf472edc9d551055f9ef73f6e42b4dbb4c80bea4"
integrity sha512-fZ4qZdQ2nxJvtcasX7Ghl+WlWS/d9IgnBIwFZXVNNZUmzpno91SX5bc5vuxiuKoCtK78XxGGNuSCrDC7xYB3OQ==
once@^1.3.0, once@^1.3.1, once@^1.4.0, once@~1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
@ -9615,11 +9551,6 @@ tunnel@0.0.4:
resolved "https://registry.yarnpkg.com/tunnel/-/tunnel-0.0.4.tgz#2d3785a158c174c9a16dc2c046ec5fc5f1742213"
integrity sha1-LTeFoVjBdMmhbcLARuxfxfF0IhM=
tunnel@0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/tunnel/-/tunnel-0.0.6.tgz#72f1314b34a5b192db012324df2cc587ca47f92c"
integrity sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg==
tweetnacl@^0.14.3, tweetnacl@~0.14.0:
version "0.14.5"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64"