Skip to content

Commit

Permalink
Add function to manage a GitHub check
Browse files Browse the repository at this point in the history
[changelog:added]
  • Loading branch information
cdupuis committed Jun 22, 2020
1 parent ef4d15f commit 9ed9428
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
* limitations under the License.
*/

export { api, formatMarkers, convergeLabel } from "./operation";
export { api, formatMarkers, convergeLabel, UpdateCheck, CreateCheck, Check, openCheck } from "./operation";
95 changes: 95 additions & 0 deletions lib/github/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
*/

import { Octokit } from "@octokit/rest"; // eslint-disable-line @typescript-eslint/no-unused-vars
import { Endpoints } from "@octokit/types";
import { Contextual } from "../handler";
import { AuthenticatedRepositoryId } from "../repository/id";
import { GitHubAppCredential, GitHubCredential } from "../secret/provider";
import chunk = require("lodash.chunk");

const DefaultGitHubApiUrl = "https://api.github.com/";

Expand Down Expand Up @@ -97,3 +99,96 @@ export async function convergeLabel(
});
}
}

export interface CreateCheck {
sha: string;
name: string;
title: string;
body: string;
}

export interface UpdateCheck {
conclusion: "success" | "failure" | "neutral" | "cancelled" | "skipped" | "timed_out" | "action_required";
body?: string;
annotations?: Array<{
path: string;
startLine: number;
endLine: number;
startColumn?: number;
endColumn?: number;
annotationLevel: "notice" | "warning" | "failure";
message: string;
title?: string;
}>;
}

export interface Check {
update: (parameters: UpdateCheck) => Promise<void>;
}

export async function openCheck(
ctx: Contextual<any, any>,
id: AuthenticatedRepositoryId<GitHubCredential | GitHubAppCredential>,
parameters: CreateCheck,
): Promise<Check> {
const start = new Date().toISOString();
const check = await api(id).checks.create({
owner: id.owner,
repo: id.repo,
head_sha: parameters.sha,
name: parameters.name,
started_at: start,
external_id: ctx.correlationId,
details_url: ctx.audit.url,
status: "in_progress",
output: {
title: parameters.title,
summary: `${parameters.body}
${formatMarkers(ctx)}`,
},
});
return {
update: async params => {
await api(id).checks.update({
owner: id.owner,
repo: id.repo,
check_run_id: check.data.id,
conclusion: params.conclusion,
completed_at: new Date().toISOString(),
status: "completed",
});
await updateAnnotation(ctx, id, check, params);
},
};
}

async function updateAnnotation(
ctx: Contextual<any, any>,
id: AuthenticatedRepositoryId<GitHubCredential | GitHubAppCredential>,
check: Endpoints["POST /repos/:owner/:repo/check-runs"]["response"],
parameters: UpdateCheck,
): Promise<void> {
const gh = api(id);
const chunks = chunk(parameters.annotations || [], 50);
for (const chunk of chunks) {
await gh.checks.update({
owner: id.owner,
repo: id.repo,
check_run_id: check.data.id,
output: {
title: check.data.output.title,
summary: parameters.body ? `${parameters.body}\n${formatMarkers(ctx)}` : check.data.output.summary,
annotations: chunk.map(c => ({
annotation_level: c.annotationLevel,
title: c.title,
end_column: c.endColumn,
end_line: c.endLine,
message: c.message,
path: c.path,
start_column: c.startColumn,
start_line: c.startLine,
})),
},
});
}
}
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@types/git-url-parse": "^9.0.0",
"@types/js-yaml": "^3.12.4",
"@types/lodash.clonedeep": "^4.5.6",
"@types/lodash.chunk": "^4.2.6",
"@types/lodash.forown": "^4.4.6",
"@types/lodash.map": "^4.6.13",
"@types/lodash.merge": "^4.6.6",
Expand All @@ -50,6 +51,7 @@
"js-yaml": "^3.13.1",
"jszip": "^3.4.0",
"lodash.clonedeep": "^4.5.0",
"lodash.chunk": "^4.2.0",
"lodash.forown": "^4.4.0",
"lodash.map": "^4.6.0",
"lodash.merge": "^4.6.2",
Expand Down

0 comments on commit 9ed9428

Please sign in to comment.