From 81d8443be0c22b09e586f16dd17b49a26134c4f0 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sat, 23 May 2020 10:12:27 -0700 Subject: [PATCH 01/15] Use comment --- src/github-api.ts | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/github-api.ts b/src/github-api.ts index 25282ba..2973726 100644 --- a/src/github-api.ts +++ b/src/github-api.ts @@ -1,24 +1,20 @@ import * as github from "@actions/github"; import { config } from "./config"; -import * as octorest from "@octokit/rest"; +import * as octokit from "@octokit/rest"; type Conclusion = "success" | "failure" | "neutral" | "cancelled" | "timed_out" | "action_required" | undefined; -async function createCheck(summary: string, conclusion: Conclusion, octokit: github.GitHub) { - const checkRequest: octorest.Octokit.RequestOptions & octorest.Octokit.ChecksCreateParams = { +async function createCheck(summary: string, conclusion: Conclusion, githubKit: github.GitHub) { + github.context.issue; + const checkRequest: octokit.Octokit.RequestOptions & octokit.Octokit.IssuesCreateCommentParams = { ...github.context.repo, - head_sha: github.context.sha, - name: "Storyshots", - conclusion, - output: { - title: "Jest Test Results", - summary, - text: summary, - }, + issue_number: github.context.issue.number, + body: summary, }; try { - await octokit.checks.create(checkRequest); + await githubKit.issues.createComment(checkRequest); + // await githubKit.checks.create(checkRequest); } catch (error) { throw new Error( `Request to create annotations failed - request: ${JSON.stringify(checkRequest)} - error: ${error.message} ` @@ -37,10 +33,10 @@ interface TestInfo { export async function publishTestResults(testInformation: TestInfo) { const { time, passed, failed, total, conclusion } = testInformation; - const octokit = new github.GitHub(config.accessToken); + const githubKit = new github.GitHub(config.accessToken); const summary = "#### These are all the test results I was able to find from your jest-junit reporter\n" + `**${total}** tests were completed in **${time}s** with **${passed}** passed ✔ and **${failed}** failed ✖ tests.`; - await createCheck(summary, conclusion, octokit); + await createCheck(summary, conclusion, githubKit); } From 8b41229f1f0c9ecf12ffc766c3b5260540045b1c Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sat, 23 May 2020 10:15:48 -0700 Subject: [PATCH 02/15] Simple logging --- src/github-api.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/github-api.ts b/src/github-api.ts index 2973726..3fc0fe8 100644 --- a/src/github-api.ts +++ b/src/github-api.ts @@ -12,6 +12,9 @@ async function createCheck(summary: string, conclusion: Conclusion, githubKit: g body: summary, }; + // eslint-disable-next-line no-console + console.log(JSON.stringify(checkRequest)); + try { await githubKit.issues.createComment(checkRequest); // await githubKit.checks.create(checkRequest); From 9e91ae9165eddc8de0086dca4681e31b0bb7ad00 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sat, 23 May 2020 10:29:44 -0700 Subject: [PATCH 03/15] Get PR number in more reliable manner --- src/github-api.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/github-api.ts b/src/github-api.ts index 3fc0fe8..e35a00d 100644 --- a/src/github-api.ts +++ b/src/github-api.ts @@ -5,10 +5,14 @@ import * as octokit from "@octokit/rest"; type Conclusion = "success" | "failure" | "neutral" | "cancelled" | "timed_out" | "action_required" | undefined; async function createCheck(summary: string, conclusion: Conclusion, githubKit: github.GitHub) { - github.context.issue; + const path = process.env["GITHUB_REF"] ?? ""; // should be of the fomr refs/pull/:prNumber/merge + const prNumber = /refs\/pull\/(\d+)\/merge/g.exec(path); + if (prNumber === null) return; + + const [, issue_number] = prNumber; const checkRequest: octokit.Octokit.RequestOptions & octokit.Octokit.IssuesCreateCommentParams = { ...github.context.repo, - issue_number: github.context.issue.number, + issue_number: Number.parseInt(issue_number), body: summary, }; From 9fe5e26a456462509ae994fee976f7c8d52d43c9 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sat, 23 May 2020 10:30:26 -0700 Subject: [PATCH 04/15] Use console.error --- src/github-api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/github-api.ts b/src/github-api.ts index e35a00d..49a140d 100644 --- a/src/github-api.ts +++ b/src/github-api.ts @@ -17,7 +17,7 @@ async function createCheck(summary: string, conclusion: Conclusion, githubKit: g }; // eslint-disable-next-line no-console - console.log(JSON.stringify(checkRequest)); + console.error(JSON.stringify(checkRequest)); try { await githubKit.issues.createComment(checkRequest); From 0ac67b6712d65107fa8cd0ff9845cbcd38702b53 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sat, 23 May 2020 10:31:30 -0700 Subject: [PATCH 05/15] Throw error when cant find pr number --- src/github-api.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/github-api.ts b/src/github-api.ts index 49a140d..68c08fb 100644 --- a/src/github-api.ts +++ b/src/github-api.ts @@ -7,7 +7,9 @@ type Conclusion = "success" | "failure" | "neutral" | "cancelled" | "timed_out" async function createCheck(summary: string, conclusion: Conclusion, githubKit: github.GitHub) { const path = process.env["GITHUB_REF"] ?? ""; // should be of the fomr refs/pull/:prNumber/merge const prNumber = /refs\/pull\/(\d+)\/merge/g.exec(path); - if (prNumber === null) return; + if (prNumber === null) { + throw new Error("Could not find PR number"); + } const [, issue_number] = prNumber; const checkRequest: octokit.Octokit.RequestOptions & octokit.Octokit.IssuesCreateCommentParams = { From b3f72e0eb7c2ba93a384fea0268265a27e6c12fb Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sat, 23 May 2020 10:33:52 -0700 Subject: [PATCH 06/15] Always throw --- src/github-api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/github-api.ts b/src/github-api.ts index 68c08fb..97c2122 100644 --- a/src/github-api.ts +++ b/src/github-api.ts @@ -19,7 +19,7 @@ async function createCheck(summary: string, conclusion: Conclusion, githubKit: g }; // eslint-disable-next-line no-console - console.error(JSON.stringify(checkRequest)); + throw new Error(JSON.stringify(checkRequest)); try { await githubKit.issues.createComment(checkRequest); From 1e29ae519f796ef2cb4867da009cd0dbb1838781 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sat, 23 May 2020 10:35:48 -0700 Subject: [PATCH 07/15] pack --- dist/index.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/dist/index.js b/dist/index.js index 987ddcb..461b7d2 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29690,14 +29690,21 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.publishTestResults = void 0; const github = __importStar(__webpack_require__(469)); const config_1 = __webpack_require__(478); -function createCheckWithAnnotations(summary, conclusion, octokit) { +function createCheck(summary, conclusion, githubKit) { + var _a; return __awaiter(this, void 0, void 0, function* () { - const checkRequest = Object.assign(Object.assign({}, github.context.repo), { head_sha: github.context.sha, name: "Storyshots", conclusion, output: { - title: "Jest Test Results", - summary, - } }); + const path = (_a = process.env["GITHUB_REF"]) !== null && _a !== void 0 ? _a : ""; // should be of the fomr refs/pull/:prNumber/merge + const prNumber = /refs\/pull\/(\d+)\/merge/g.exec(path); + if (prNumber === null) { + throw new Error("Could not find PR number"); + } + const [, issue_number] = prNumber; + const checkRequest = Object.assign(Object.assign({}, github.context.repo), { issue_number: Number.parseInt(issue_number), body: summary }); + // eslint-disable-next-line no-console + throw new Error(JSON.stringify(checkRequest)); try { - yield octokit.checks.create(checkRequest); + yield githubKit.issues.createComment(checkRequest); + // await githubKit.checks.create(checkRequest); } catch (error) { throw new Error(`Request to create annotations failed - request: ${JSON.stringify(checkRequest)} - error: ${error.message} `); @@ -29707,10 +29714,10 @@ function createCheckWithAnnotations(summary, conclusion, octokit) { function publishTestResults(testInformation) { return __awaiter(this, void 0, void 0, function* () { const { time, passed, failed, total, conclusion } = testInformation; - const octokit = new github.GitHub(config_1.config.accessToken); + const githubKit = new github.GitHub(config_1.config.accessToken); const summary = "#### These are all the test results I was able to find from your jest-junit reporter\n" + `**${total}** tests were completed in **${time}s** with **${passed}** passed ✔ and **${failed}** failed ✖ tests.`; - yield createCheckWithAnnotations(summary, conclusion, octokit); + yield createCheck(summary, conclusion, githubKit); }); } exports.publishTestResults = publishTestResults; From bd7c4bb06457648c55ae82ef8fa560fddbb514a9 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sat, 23 May 2020 10:37:01 -0700 Subject: [PATCH 08/15] remove intentional error --- src/github-api.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/github-api.ts b/src/github-api.ts index 97c2122..f77af5e 100644 --- a/src/github-api.ts +++ b/src/github-api.ts @@ -18,9 +18,6 @@ async function createCheck(summary: string, conclusion: Conclusion, githubKit: g body: summary, }; - // eslint-disable-next-line no-console - throw new Error(JSON.stringify(checkRequest)); - try { await githubKit.issues.createComment(checkRequest); // await githubKit.checks.create(checkRequest); From 4458b65bd2bad616d63fe148da8c963e3de8df5d Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sat, 23 May 2020 10:41:07 -0700 Subject: [PATCH 09/15] Add buildpack command --- dist/index.js | 2 -- package.json | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index 461b7d2..f012197 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29700,8 +29700,6 @@ function createCheck(summary, conclusion, githubKit) { } const [, issue_number] = prNumber; const checkRequest = Object.assign(Object.assign({}, github.context.repo), { issue_number: Number.parseInt(issue_number), body: summary }); - // eslint-disable-next-line no-console - throw new Error(JSON.stringify(checkRequest)); try { yield githubKit.issues.createComment(checkRequest); // await githubKit.checks.create(checkRequest); diff --git a/package.json b/package.json index b9207f5..0b66875 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "main": "lib/main.js", "scripts": { "build": "tsc --extendedDiagnostics", + "buildpack": "npm run build && npm run pack", "format": "prettier --write **/*.ts", "format-check": "prettier --check **/*.ts", "lint": "eslint src/**/*.ts", From 8ffc10e0ba51fee45aeacf889853b3e89f05e557 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sat, 23 May 2020 10:42:36 -0700 Subject: [PATCH 10/15] Try getting issue number from github directly --- src/github-api.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/github-api.ts b/src/github-api.ts index f77af5e..5175476 100644 --- a/src/github-api.ts +++ b/src/github-api.ts @@ -5,16 +5,16 @@ import * as octokit from "@octokit/rest"; type Conclusion = "success" | "failure" | "neutral" | "cancelled" | "timed_out" | "action_required" | undefined; async function createCheck(summary: string, conclusion: Conclusion, githubKit: github.GitHub) { - const path = process.env["GITHUB_REF"] ?? ""; // should be of the fomr refs/pull/:prNumber/merge - const prNumber = /refs\/pull\/(\d+)\/merge/g.exec(path); - if (prNumber === null) { - throw new Error("Could not find PR number"); - } + // const path = process.env["GITHUB_REF"] ?? ""; // should be of the fomr refs/pull/:prNumber/merge + // const prNumber = /refs\/pull\/(\d+)\/merge/g.exec(path); + // if (prNumber === null) { + // throw new Error("Could not find PR number"); + // } - const [, issue_number] = prNumber; + // const [, issue_number] = prNumber; const checkRequest: octokit.Octokit.RequestOptions & octokit.Octokit.IssuesCreateCommentParams = { ...github.context.repo, - issue_number: Number.parseInt(issue_number), + issue_number: github.context.issue.number, body: summary, }; From ab59dd1c4248c7ae95bc0b24aa3ae61d81515e6f Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sat, 23 May 2020 10:43:33 -0700 Subject: [PATCH 11/15] Actually pack results of last commit --- dist/index.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/dist/index.js b/dist/index.js index f012197..5cbd7c2 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29691,15 +29691,14 @@ exports.publishTestResults = void 0; const github = __importStar(__webpack_require__(469)); const config_1 = __webpack_require__(478); function createCheck(summary, conclusion, githubKit) { - var _a; return __awaiter(this, void 0, void 0, function* () { - const path = (_a = process.env["GITHUB_REF"]) !== null && _a !== void 0 ? _a : ""; // should be of the fomr refs/pull/:prNumber/merge - const prNumber = /refs\/pull\/(\d+)\/merge/g.exec(path); - if (prNumber === null) { - throw new Error("Could not find PR number"); - } - const [, issue_number] = prNumber; - const checkRequest = Object.assign(Object.assign({}, github.context.repo), { issue_number: Number.parseInt(issue_number), body: summary }); + // const path = process.env["GITHUB_REF"] ?? ""; // should be of the fomr refs/pull/:prNumber/merge + // const prNumber = /refs\/pull\/(\d+)\/merge/g.exec(path); + // if (prNumber === null) { + // throw new Error("Could not find PR number"); + // } + // const [, issue_number] = prNumber; + const checkRequest = Object.assign(Object.assign({}, github.context.repo), { issue_number: github.context.issue.number, body: summary }); try { yield githubKit.issues.createComment(checkRequest); // await githubKit.checks.create(checkRequest); From e96305506e6c5010bf50d8e426c81d22353fc02a Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sat, 23 May 2020 10:48:43 -0700 Subject: [PATCH 12/15] Only post comment when failed --- dist/index.js | 17 +++++++---------- src/github-api.ts | 26 +++++++++++++++----------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/dist/index.js b/dist/index.js index 5cbd7c2..c9faf4c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29692,19 +29692,16 @@ const github = __importStar(__webpack_require__(469)); const config_1 = __webpack_require__(478); function createCheck(summary, conclusion, githubKit) { return __awaiter(this, void 0, void 0, function* () { - // const path = process.env["GITHUB_REF"] ?? ""; // should be of the fomr refs/pull/:prNumber/merge - // const prNumber = /refs\/pull\/(\d+)\/merge/g.exec(path); - // if (prNumber === null) { - // throw new Error("Could not find PR number"); - // } - // const [, issue_number] = prNumber; - const checkRequest = Object.assign(Object.assign({}, github.context.repo), { issue_number: github.context.issue.number, body: summary }); + const commentRequest = Object.assign(Object.assign({}, github.context.repo), { issue_number: github.context.issue.number, body: summary }); + const checkRequest = Object.assign(Object.assign({}, github.context.repo), { head_sha: github.context.sha, name: "Storyshots", conclusion }); try { - yield githubKit.issues.createComment(checkRequest); - // await githubKit.checks.create(checkRequest); + if (conclusion === "failure") { + yield githubKit.issues.createComment(commentRequest); + } + yield githubKit.checks.create(checkRequest); } catch (error) { - throw new Error(`Request to create annotations failed - request: ${JSON.stringify(checkRequest)} - error: ${error.message} `); + throw new Error(`Request to create annotations failed - request: ${JSON.stringify(commentRequest)} - error: ${error.message} `); } }); } diff --git a/src/github-api.ts b/src/github-api.ts index 5175476..ec56da0 100644 --- a/src/github-api.ts +++ b/src/github-api.ts @@ -5,25 +5,29 @@ import * as octokit from "@octokit/rest"; type Conclusion = "success" | "failure" | "neutral" | "cancelled" | "timed_out" | "action_required" | undefined; async function createCheck(summary: string, conclusion: Conclusion, githubKit: github.GitHub) { - // const path = process.env["GITHUB_REF"] ?? ""; // should be of the fomr refs/pull/:prNumber/merge - // const prNumber = /refs\/pull\/(\d+)\/merge/g.exec(path); - // if (prNumber === null) { - // throw new Error("Could not find PR number"); - // } - - // const [, issue_number] = prNumber; - const checkRequest: octokit.Octokit.RequestOptions & octokit.Octokit.IssuesCreateCommentParams = { + const commentRequest: octokit.Octokit.RequestOptions & octokit.Octokit.IssuesCreateCommentParams = { ...github.context.repo, issue_number: github.context.issue.number, body: summary, }; + const checkRequest: octokit.Octokit.RequestOptions & octokit.Octokit.ChecksCreateParams = { + ...github.context.repo, + head_sha: github.context.sha, + name: "Storyshots", + conclusion, + }; + try { - await githubKit.issues.createComment(checkRequest); - // await githubKit.checks.create(checkRequest); + if (conclusion === "failure") { + await githubKit.issues.createComment(commentRequest); + } + await githubKit.checks.create(checkRequest); } catch (error) { throw new Error( - `Request to create annotations failed - request: ${JSON.stringify(checkRequest)} - error: ${error.message} ` + `Request to create annotations failed - request: ${JSON.stringify(commentRequest)} - error: ${ + error.message + } ` ); } } From c9ea6671fee752f78926c5bd1a38237a7c71f184 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sat, 23 May 2020 10:51:18 -0700 Subject: [PATCH 13/15] Still use output --- dist/index.js | 5 ++++- src/github-api.ts | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/dist/index.js b/dist/index.js index c9faf4c..3e7cf20 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29693,7 +29693,10 @@ const config_1 = __webpack_require__(478); function createCheck(summary, conclusion, githubKit) { return __awaiter(this, void 0, void 0, function* () { const commentRequest = Object.assign(Object.assign({}, github.context.repo), { issue_number: github.context.issue.number, body: summary }); - const checkRequest = Object.assign(Object.assign({}, github.context.repo), { head_sha: github.context.sha, name: "Storyshots", conclusion }); + const checkRequest = Object.assign(Object.assign({}, github.context.repo), { head_sha: github.context.sha, name: "Storyshots", conclusion, output: { + title: "Jest Test Results", + summary, + } }); try { if (conclusion === "failure") { yield githubKit.issues.createComment(commentRequest); diff --git a/src/github-api.ts b/src/github-api.ts index ec56da0..5f1f9b5 100644 --- a/src/github-api.ts +++ b/src/github-api.ts @@ -16,6 +16,10 @@ async function createCheck(summary: string, conclusion: Conclusion, githubKit: g head_sha: github.context.sha, name: "Storyshots", conclusion, + output: { + title: "Jest Test Results", + summary, + }, }; try { From 2a1ebcbaa8e351fb688cdfce2b5e8f072edd8818 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sat, 23 May 2020 11:52:40 -0700 Subject: [PATCH 14/15] Testing --- dist/index.js | 1 + src/github-api.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/dist/index.js b/dist/index.js index 3e7cf20..10e03ae 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29701,6 +29701,7 @@ function createCheck(summary, conclusion, githubKit) { if (conclusion === "failure") { yield githubKit.issues.createComment(commentRequest); } + console.log("Sending check request", JSON.stringify(checkRequest)); yield githubKit.checks.create(checkRequest); } catch (error) { diff --git a/src/github-api.ts b/src/github-api.ts index 5f1f9b5..2f92c60 100644 --- a/src/github-api.ts +++ b/src/github-api.ts @@ -26,6 +26,7 @@ async function createCheck(summary: string, conclusion: Conclusion, githubKit: g if (conclusion === "failure") { await githubKit.issues.createComment(commentRequest); } + console.log("Sending check request", JSON.stringify(checkRequest)); await githubKit.checks.create(checkRequest); } catch (error) { throw new Error( From ad97769178dbf26b778dc15b75a811010d198f84 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Sat, 23 May 2020 11:56:48 -0700 Subject: [PATCH 15/15] Bump version --- dist/index.js | 1 - package.json | 2 +- src/github-api.ts | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/dist/index.js b/dist/index.js index 10e03ae..3e7cf20 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29701,7 +29701,6 @@ function createCheck(summary, conclusion, githubKit) { if (conclusion === "failure") { yield githubKit.issues.createComment(commentRequest); } - console.log("Sending check request", JSON.stringify(checkRequest)); yield githubKit.checks.create(checkRequest); } catch (error) { diff --git a/package.json b/package.json index 0b66875..cb7b7d3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jest-storyshots-reporter", - "version": "1.0.0", + "version": "1.0.1", "private": true, "description": "Github actions for reporting jest storyshots results", "main": "lib/main.js", diff --git a/src/github-api.ts b/src/github-api.ts index 2f92c60..5f1f9b5 100644 --- a/src/github-api.ts +++ b/src/github-api.ts @@ -26,7 +26,6 @@ async function createCheck(summary: string, conclusion: Conclusion, githubKit: g if (conclusion === "failure") { await githubKit.issues.createComment(commentRequest); } - console.log("Sending check request", JSON.stringify(checkRequest)); await githubKit.checks.create(checkRequest); } catch (error) { throw new Error(