Skip to content

Commit c762782

Browse files
committed
✨ Add Github Action for typescript coverage
πŸ”§ Update ts-config to exclude action folder πŸ”§ Baseline action.yml file βž• Add github actions related packages Add ocktokit dep and baseline action πŸ’š Add baseline action Add types for the UpdateCheckRunOptions Add markdown commit package Add markdown generation code 🚧 Tweak more stuff
1 parent f9c9327 commit c762782

File tree

12 files changed

+16146
-3
lines changed

12 files changed

+16146
-3
lines changed

β€Ž.github/workflows/ci.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
test:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: preetjdp/typescript-coverage-report@Add-Github-Action
12+
with:
13+
github-token: ${{ secrets.GITHUB_TOKEN }}

β€Žaction.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: "Typescript Coverage Report"
2+
author: "Preet Parekh <[email protected]>"
3+
description: "Github Actin for generating TypeScript coverage reports"
4+
branding:
5+
icon: "search"
6+
color: "orange"
7+
inputs:
8+
name:
9+
description: |
10+
The name of the check ("Typescript Coverage Report" by default).
11+
In case of multiple Typescript Coverage Report Actions, use `name` to prevent GitHub from overwriting results.
12+
For example, "Check Public API" and "Check Internal API".
13+
# annotations:
14+
# description: "Use annotation (enabled by default)"
15+
# fail-on-breaking:
16+
# description: "Fail on breaking changes (enabled by default)"
17+
# schema:
18+
# description: |
19+
# Ref and Path to GraphQL Schema (e.g. "master:schema.graphql")
20+
# * Ref is needed where 'endpoint' is not defined
21+
# required: true
22+
# endpoint:
23+
# description: |
24+
# Url to your GraphQL API
25+
# When using an endpoint, 'schema' should point to a file (without a reference - branch name for example)
26+
github-token:
27+
description: "Github Token"
28+
# experimental_merge:
29+
# description: |
30+
# Merge Pull Request's branch with the target branch to get the schema.
31+
# Helps to get the correct state of schema when Pull Request is behind the target branch
32+
# (disabled by default)
33+
# outputs:
34+
# changes:
35+
# description: "Total number of changes"
36+
runs:
37+
using: "node12"
38+
main: "action/index.js"

β€Žaction/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žaction/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as core from "@actions/core";
2+
3+
import { run } from "./run";
4+
5+
run().catch((e) => {
6+
core.setFailed(e.message || e);
7+
});

β€Žaction/run.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import * as core from "@actions/core";
2+
import * as github from "@actions/github";
3+
4+
import { generate } from "../src/lib/reporters/markdown";
5+
import getCoverage from "../src/lib/getCoverage";
6+
import { getCurrentCommitSha, updateCheckRun } from "./utils";
7+
8+
const CHECK_NAME = "Typescript Coverage Report";
9+
10+
export const run = async (): Promise<void> => {
11+
core.info(`Typescript Coverage Reporter started`);
12+
13+
const ref = process.env.GITHUB_SHA;
14+
const commitSha = getCurrentCommitSha();
15+
16+
core.info(`Ref: ${ref}`);
17+
core.info(`Commit SHA: ${commitSha}`);
18+
19+
/**
20+
* env:
21+
* GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
22+
*/
23+
const token = core.getInput("github-token") || process.env.GITHUB_TOKEN;
24+
const checkName = core.getInput("name") || CHECK_NAME;
25+
26+
if (!token) {
27+
return core.setFailed("Github Token is missing");
28+
}
29+
30+
const octokit = github.getOctokit(token);
31+
32+
// repo
33+
const { owner, repo } = github.context.repo;
34+
35+
core.info(`Creating a check named "${checkName}"`);
36+
37+
const check = await octokit.checks.create({
38+
owner,
39+
repo,
40+
name: checkName,
41+
// eslint-disable-next-line @typescript-eslint/camelcase
42+
head_sha: commitSha,
43+
status: "in_progress"
44+
});
45+
46+
const checkId = check.data.id;
47+
48+
core.info(`Check ID: ${checkId}`);
49+
50+
const coverageResult = await getCoverage();
51+
const markdownReport = generate(coverageResult, 90);
52+
53+
updateCheckRun(octokit, checkId, {
54+
conclusion: "success",
55+
name: "This is just a test",
56+
output: {
57+
title: "Typescript Coverage Report",
58+
summary: `**Typescript Coverage Percentage**: ${coverageResult.percentage.toFixed(
59+
2
60+
)}%`,
61+
text: markdownReport
62+
}
63+
});
64+
};

β€Žaction/utils.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import * as github from "@actions/github";
2+
import * as core from "@actions/core";
3+
import { RestEndpointMethodTypes } from "@octokit/rest";
4+
import { execSync } from "child_process";
5+
6+
function getCurrentCommitSha() {
7+
const sha = execSync(`git rev-parse HEAD`).toString().trim();
8+
9+
try {
10+
const msg = execSync(`git show ${sha} -s --format=%s`).toString().trim();
11+
const PR_MSG = /Merge (\w+) into \w+/i;
12+
13+
if (PR_MSG.test(msg)) {
14+
const result = PR_MSG.exec(msg);
15+
16+
if (result) {
17+
return result[1];
18+
}
19+
}
20+
} catch (e) {
21+
//
22+
}
23+
return sha;
24+
}
25+
26+
type OctokitInstance = ReturnType<typeof github.getOctokit>;
27+
28+
type UpdateCheckRunOptions = Required<
29+
Pick<
30+
RestEndpointMethodTypes["checks"]["update"]["parameters"],
31+
"conclusion" | "output"
32+
>
33+
> &
34+
Partial<
35+
Pick<RestEndpointMethodTypes["checks"]["update"]["parameters"], "name">
36+
>;
37+
38+
async function updateCheckRun(
39+
octokit: OctokitInstance,
40+
checkId: number,
41+
{ conclusion, output }: UpdateCheckRunOptions
42+
) {
43+
core.info(`Updating check: ${checkId}`);
44+
await octokit.checks.update({
45+
// eslint-disable-next-line @typescript-eslint/camelcase
46+
check_run_id: checkId,
47+
// eslint-disable-next-line @typescript-eslint/camelcase
48+
completed_at: new Date().toISOString(),
49+
status: "completed",
50+
...github.context.repo,
51+
conclusion,
52+
output
53+
});
54+
55+
// Fail
56+
if (conclusion === "failure") {
57+
return core.setFailed(output.title!);
58+
}
59+
}
60+
61+
export { getCurrentCommitSha, updateCheckRun };

0 commit comments

Comments
Β (0)