Skip to content

Commit

Permalink
Express support to Ukraine (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
slavcodev authored Mar 7, 2022
1 parent 811c40c commit 90fb3eb
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 32 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ Security - in case of vulnerabilities.

_TBD_

## [1.6.0] 2022-03-07

### Added

- Added footer to comment and add a boolean option `comment_footer`, defaults to `true`.
- Expressed the support of Ukraine in this difficult time.

## [1.5.0] 2022-03-01

### Changed
Expand Down
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
[![Status][ico-github-actions]][link-github]
[![Latest Version][ico-version]][link-github]
[![License][ico-license]][link-license]
[![License][ico-stand-with-ukraine]][link-stand-with-ukraine]

[ico-github-actions]: https://github.com/slavcodev/coverage-monitor-action/workflows/build/badge.svg
[ico-version]: https://img.shields.io/github/tag/slavcodev/coverage-monitor-action.svg?label=latest
[ico-license]: https://img.shields.io/badge/License-MIT-blue.svg
[ico-stand-with-ukraine]: https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/badges/StandWithUkraine.svg

[link-github]: https://github.com/slavcodev/coverage-monitor-action
[link-license]: LICENSE
[link-contributing]: .github/CONTRIBUTING.md
[link-stand-with-ukraine]: https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md

A GitHub Action that monitor coverage.

Expand All @@ -30,14 +33,15 @@ Create a workflow `.yml` file in your repositories `.github/workflows` directory
| `coverage_format` | Format of coverage, supported: `auto`, `clover` and `json-summary`. Defaults to `auto`. |
| `working_dir` | The working directory of the action. Defaults to workflow workspace. |
| `clover_file` | **Deprecated.** Path to Clover XML file. Prefer `coverage_path` instead of `clover_file`. |
| `threshold_alert` | Mark the build as unstable when coverage is less than this threshold. Default to `50`. |
| `threshold_warning` | Warning when coverage is less than this threshold. Default to `90`. |
| `threshold_metric` | A metric to check threshold on, supported: `statements`, `lines`, `methods` or `branches`. Default to `lines`. |
| `threshold_alert` | Mark the build as unstable when coverage is less than this threshold. Defaults to `50`. |
| `threshold_warning` | Warning when coverage is less than this threshold. Defaults to `90`. |
| `threshold_metric` | A metric to check threshold on, supported: `statements`, `lines`, `methods` or `branches`. Defaults to `lines`. |
| `check` | Whether check the coverage thresholds. Default to `true`. Ignored when event does not support checks, is not `pull_request`. |
| `status_context` | A string label to differentiate this status from the status of other systems. Default to `Coverage Report`. |
| `status_context` | A string label to differentiate this status from the status of other systems. Defaults to `Coverage Report`. |
| `comment` | Whether comment the coverage report. Default to `true`. Ignored when event does not support comments, is not `pull_request`. |
| `comment_context` | A string label to differentiate the comment posted by this action. Default to `Coverage Report`. |
| `comment_mode` | A mode for comments, supported: `replace`, `update` or `insert`. Default to `replace`. |
| `comment_context` | A string label to differentiate the comment posted by this action. Defaults to `Coverage Report`. |
| `comment_mode` | A mode for comments, supported: `replace`, `update` or `insert`. Defaults to `replace`. |
| `comment_footer` | Whether comment may contain footer. Defaults to `true`.

### Example workflow

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ inputs:
description: "A mode for comments, supported: `replace`, `update` or `insert`."
required: false
default: 'replace'
comment_footer:
description: "Whether comment may contain footer."
required: false
default: 'true'
runs:
using: 'node16'
main: 'dist/index.js'
21 changes: 13 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class App {
requests.push(client.createStatus(config.check.context, report));
}
if (config.comment) {
requests.push(client.commentReport(config.comment.mode, config.comment.context, report));
requests.push(client.commentReport(config.comment, config.comment.context, report));
}
if (requests.length > 0) {
yield Promise.all(requests);
Expand All @@ -150,17 +150,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
const MetricType_1 = __importDefault(__nccwpck_require__(8865));
class Comment {
constructor(metrics, result, context) {
constructor(metrics, result, context, footer) {
this.metrics = metrics;
this.result = result;
this.context = context;
this.footer = footer;
}
static generateBadgeUrl({ rate, level }) {
return `https://img.shields.io/static/v1?label=coverage&message=${Math.round(rate / 100)}%&color=${level}`;
}
static generateEmoji({ rate }) {
return rate === 10000 ? ' 🎉' : '';
}
static generateFooter() {
return `\n### [![StandWithUkraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/badges/StandWithUkraine.svg)](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md)`;
}
static generateTableRow(title, { rate, total, covered }) {
return total ? `| ${title}: | ${rate / 100}% ( ${covered} / ${total} ) |\n` : '';
}
Expand All @@ -178,7 +182,7 @@ ${[
Comment.generateTableRow('Methods', this.metrics[MetricType_1.default.Methods]),
Comment.generateTableRow('Lines', this.metrics[MetricType_1.default.Lines]),
Comment.generateTableRow('Branches', this.metrics[MetricType_1.default.Branches]),
].join('')}`;
].join('')}${this.footer ? Comment.generateFooter() : ''}`;
}
}
exports["default"] = Comment;
Expand Down Expand Up @@ -471,8 +475,8 @@ class Report {
this.threshold = threshold;
this.result = this.metrics[this.threshold.metric].report(threshold);
}
toComment(context) {
return new Comment_1.default(this.metrics, this.result, context);
toComment(context, footer) {
return new Comment_1.default(this.metrics, this.result, context, footer);
}
toStatus(context, targetUrl) {
return new Status_1.default(this.result, context, targetUrl);
Expand Down Expand Up @@ -681,6 +685,7 @@ class GitHubAdapter {
const check = __classPrivateFieldGet(GitHubAdapter, _a, "m", _GitHubAdapter_toBool).call(GitHubAdapter, __classPrivateFieldGet(this, _GitHubAdapter_core, "f").getInput('check'), true);
const statusContext = __classPrivateFieldGet(this, _GitHubAdapter_core, "f").getInput('status_context') || 'Coverage Report';
const comment = __classPrivateFieldGet(GitHubAdapter, _a, "m", _GitHubAdapter_toBool).call(GitHubAdapter, __classPrivateFieldGet(this, _GitHubAdapter_core, "f").getInput('comment'), true);
const commentFooter = __classPrivateFieldGet(GitHubAdapter, _a, "m", _GitHubAdapter_toBool).call(GitHubAdapter, __classPrivateFieldGet(this, _GitHubAdapter_core, "f").getInput('comment_footer'), true);
const commentContext = __classPrivateFieldGet(this, _GitHubAdapter_core, "f").getInput('comment_context') || 'Coverage Report';
const commentMode = (__classPrivateFieldGet(this, _GitHubAdapter_core, "f").getInput('comment_mode') || CommentMode_1.default.Replace);
if (cloverFile && coveragePath !== cloverFile) {
Expand All @@ -704,7 +709,7 @@ class GitHubAdapter {
coverageFormat,
workingDir,
threshold: { alert: thresholdAlert, warning: thresholdWarning, metric: thresholdMetric },
comment: comment ? { context: commentContext, mode: commentMode } : undefined,
comment: comment ? { context: commentContext, mode: commentMode, footer: commentFooter } : undefined,
check: check ? { context: statusContext } : undefined,
});
}
Expand Down Expand Up @@ -799,12 +804,12 @@ class GitHubClientAdapter {
yield __classPrivateFieldGet(this, _GitHubClientAdapter_client, "f").repos.createCommitStatus(Object.assign(Object.assign(Object.assign({}, __classPrivateFieldGet(this, _GitHubClientAdapter_context, "f").repo), { sha: __classPrivateFieldGet(this, _GitHubClientAdapter_pr, "f").sha }), report.toStatus(context, __classPrivateFieldGet(this, _GitHubClientAdapter_pr, "f").url)));
});
}
commentReport(mode, context, report) {
commentReport({ mode, footer }, context, report) {
return __awaiter(this, void 0, void 0, function* () {
if (!__classPrivateFieldGet(this, _GitHubClientAdapter_pr, "f")) {
return;
}
const comment = report.toComment(context);
const comment = report.toComment(context, footer);
switch (mode) {
case CommentMode_1.default.Insert:
return __classPrivateFieldGet(this, _GitHubClientAdapter_instances, "m", _GitHubClientAdapter_insertComment).call(this, {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "coverage-monitor-action",
"version": "1.5.0",
"version": "1.6.0",
"private": true,
"description": "A GitHub Action that monitor coverage.",
"main": "lib/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/types/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class App {
}

if (config.comment) {
requests.push(client.commentReport(config.comment.mode, config.comment.context, report));
requests.push(client.commentReport(config.comment, config.comment.context, report));
}

if (requests.length > 0) {
Expand Down
14 changes: 12 additions & 2 deletions src/types/Comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import MetricType from './MetricType';
import ReportResult from './ReportResult';

export default class Comment {
constructor(readonly metrics: MetricCollection, readonly result: ReportResult, readonly context: string) {}
constructor(
readonly metrics: MetricCollection,
readonly result: ReportResult,
readonly context: string,
readonly footer: boolean,
) {}

static generateBadgeUrl({rate, level}: ReportResult): string {
return `https://img.shields.io/static/v1?label=coverage&message=${Math.round(rate / 100)}%&color=${level}`;
Expand All @@ -13,6 +18,11 @@ export default class Comment {
static generateEmoji({rate}: ReportResult): string {
return rate === 10000 ? ' 🎉' : '';
}

static generateFooter(): string {
return `\n### [![StandWithUkraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/badges/StandWithUkraine.svg)](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md)`;
}

static generateTableRow(title: string, {rate, total, covered}: Metric): string {
return total ? `| ${title}: | ${rate / 100}% ( ${covered} / ${total} ) |\n` : '';
}
Expand All @@ -32,6 +42,6 @@ ${[
Comment.generateTableRow('Methods', this.metrics[MetricType.Methods]),
Comment.generateTableRow('Lines', this.metrics[MetricType.Lines]),
Comment.generateTableRow('Branches', this.metrics[MetricType.Branches]),
].join('')}`;
].join('')}${this.footer ? Comment.generateFooter() : ''}`;
}
}
7 changes: 7 additions & 0 deletions src/types/CommentConfiguration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import CommentMode from './CommentMode';

export default interface CommentConfiguration {
mode: CommentMode;
context: string;
footer: boolean;
}
7 changes: 2 additions & 5 deletions src/types/Configuration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Bips from './Bips';
import CommentMode from './CommentMode';
import CommentConfiguration from './CommentConfiguration';
import MetricType from './MetricType';

export default class Configuration {
Expand All @@ -12,10 +12,7 @@ export default class Configuration {
alert: Bips;
warning: Bips;
};
readonly comment?: {
mode: CommentMode;
context: string;
};
readonly comment?: CommentConfiguration;
readonly check?: {
context: string;
};
Expand Down
4 changes: 2 additions & 2 deletions src/types/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export default class Report {
this.result = this.metrics[this.threshold.metric].report(threshold);
}

toComment(context: string): Comment {
return new Comment(this.metrics, this.result, context);
toComment(context: string, footer: boolean): Comment {
return new Comment(this.metrics, this.result, context, footer);
}

toStatus(context: string, targetUrl: string): Status {
Expand Down
3 changes: 2 additions & 1 deletion src/types/github/GitHubAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default class GitHubAdapter {
const statusContext = this.#core.getInput('status_context') || 'Coverage Report';

const comment = GitHubAdapter.#toBool(this.#core.getInput('comment'), true);
const commentFooter = GitHubAdapter.#toBool(this.#core.getInput('comment_footer'), true);
const commentContext = this.#core.getInput('comment_context') || 'Coverage Report';
const commentMode = (this.#core.getInput('comment_mode') || CommentMode.Replace) as CommentMode;

Expand Down Expand Up @@ -86,7 +87,7 @@ export default class GitHubAdapter {
coverageFormat,
workingDir,
threshold: {alert: thresholdAlert, warning: thresholdWarning, metric: thresholdMetric},
comment: comment ? {context: commentContext, mode: commentMode} : undefined,
comment: comment ? {context: commentContext, mode: commentMode, footer: commentFooter} : undefined,
check: check ? {context: statusContext} : undefined,
});
}
Expand Down
5 changes: 3 additions & 2 deletions src/types/github/GitHubClientAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import CommentConfiguration from '../CommentConfiguration';
import CommentMode from '../CommentMode';
import GitHubClient from './GitHubClient';
import GitHubComment from './GiHubComment';
Expand Down Expand Up @@ -28,12 +29,12 @@ export default class GitHubClientAdapter {
});
}

async commentReport(mode: CommentMode, context: string, report: Report): Promise<void> {
async commentReport({mode, footer}: CommentConfiguration, context: string, report: Report): Promise<void> {
if (!this.#pr) {
return;
}

const comment = report.toComment(context);
const comment = report.toComment(context, footer);

switch (mode) {
case CommentMode.Insert:
Expand Down
30 changes: 28 additions & 2 deletions tests/types/Comment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe(`${Comment.name}`, () => {
| Branches: | 40% ( 4 / 10 ) |
`;

expect(report.toComment('foobar coverage').generateTable()).toBe(expectedString);
expect(report.toComment('foobar coverage', false).generateTable()).toBe(expectedString);
});

it('hides metric rows in table when metric is not available (total is zero)', async () => {
Expand All @@ -84,6 +84,32 @@ describe(`${Comment.name}`, () => {
| Branches: | 40% ( 4 / 10 ) |
`;

expect(report.toComment('Coverage Report').generateTable()).toBe(expectedString);
expect(report.toComment('Coverage Report', false).generateTable()).toBe(expectedString);
});

it('appends footer if enabled', async () => {
expect.hasAssertions();

const report: Report = new Report(
{
statements: new Metric({total: 10, covered: 1}),
lines: new Metric({total: 10, covered: 2}),
methods: new Metric({total: 0, covered: 0}),
branches: new Metric({total: 10, covered: 4}),
},
new Threshold(MetricType.Branches, 30, 5000),
);

const expectedString = `<!-- coverage-monitor-action: Coverage Report -->
## Coverage Report
| Totals | ![Coverage](https://img.shields.io/static/v1?label=coverage&message=40%&color=yellow) |
| :-- | :-- |
| Statements: | 10% ( 1 / 10 ) |
| Lines: | 20% ( 2 / 10 ) |
| Branches: | 40% ( 4 / 10 ) |
${Comment.generateFooter()}`;

expect(report.toComment('Coverage Report', true).generateTable()).toBe(expectedString);
});
});
17 changes: 17 additions & 0 deletions tests/types/GitHubAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe(`${GitHubAdapter.name}`, () => {
comment: {
context: 'Coverage Report',
mode: 'replace',
footer: true,
},
check: {
context: 'Coverage Report',
Expand Down Expand Up @@ -214,6 +215,7 @@ describe(`${GitHubAdapter.name}`, () => {
comment: {
context: 'Foobar',
mode: 'insert',
footer: true,
},
},
},
Expand All @@ -232,6 +234,21 @@ describe(`${GitHubAdapter.name}`, () => {
comment: {
context: 'Coverage Report',
mode: 'replace',
footer: true,
},
},
},
{
scenario: 'comment footer disabled ',
input: {github_token: '***', coverage_path: 'clover.xml', comment_footer: false},
expected: {
...defaultOutput,
githubToken: '***',
coveragePath: 'clover.xml',
comment: {
context: 'Coverage Report',
mode: 'replace',
footer: false,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion tests/types/Report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe(`${Report.name}`, () => {
it('provides report comment', async () => {
expect.hasAssertions();
const report: Report = new Report(metrics, new Threshold(MetricType.Branches, 3000, 5000));
const comment = report.toComment('Comment context');
const comment = report.toComment('Comment context', false);
expect(comment.context).toBe('Comment context');
expect(comment.result).toStrictEqual({
total: 10,
Expand Down

0 comments on commit 90fb3eb

Please sign in to comment.