Skip to content

Commit 5ec20fd

Browse files
authored
feat: add excludeLabels option (#8)
1 parent fe82d77 commit 5ec20fd

File tree

7 files changed

+12414
-36
lines changed

7 files changed

+12414
-36
lines changed

.github/workflows/test.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
name: "build-test"
2-
on: [pull_request]
2+
on:
3+
pull_request:
4+
types:
5+
- opened
6+
- reopened
7+
- edited
8+
- synchronize
9+
- labeled
10+
- unlabeled
311

412
jobs:
513
test:
@@ -12,3 +20,6 @@ jobs:
1220
excludePaths: |
1321
dist/**
1422
README.md
23+
package-lock.json
24+
excludeLabels: |
25+
skip-size-check

__tests__/main.test.ts

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,113 @@
1-
import {Checker, Result} from "../src/checker";
1+
import {Checker, Result} from '../src/checker'
22

33
test('skips validation if excludeTitle matches', () => {
44
const excludeTitle = new RegExp('Skip')
55
const checker = new Checker({errorSize: 20, warningSize: 10, excludeTitle})
6-
const result = checker.check({title: 'Skip PR size check', files: [{additions: 100, filename: '1.txt'}]})
6+
const result = checker.check({
7+
title: 'Skip PR size check',
8+
files: [{additions: 100, filename: '1.txt'}],
9+
labels: []
10+
})
711
expect(result).toBe(Result.ok)
8-
});
12+
})
913

1014
test('does validation if excludeTitle not matches', () => {
1115
const excludeTitle = new RegExp('NO_VALIDATION')
1216
const checker = new Checker({errorSize: 20, warningSize: 10, excludeTitle})
13-
const result = checker.check({title: 'Skip PR size check', files: [{additions: 100, filename: '1.txt'}]})
17+
const result = checker.check({
18+
title: 'Skip PR size check',
19+
files: [{additions: 100, filename: '1.txt'}],
20+
labels: []
21+
})
22+
expect(result).toBe(Result.error)
23+
})
24+
25+
test('does validation if excludeLabels not matches', () => {
26+
const excludeTitle = new RegExp('NO_VALIDATION')
27+
const excludeLabels = ['skip']
28+
const checker = new Checker({errorSize: 20, warningSize: 10, excludeTitle, excludeLabels})
29+
const result = checker.check({
30+
title: 'Skip PR size check',
31+
files: [{additions: 100, filename: '1.txt'}],
32+
labels: ['no-skip']
33+
})
1434
expect(result).toBe(Result.error)
15-
});
35+
})
1636

1737
test('does validation if excludeTitle is undefined', () => {
1838
const checker = new Checker({errorSize: 20, warningSize: 10})
19-
const result = checker.check({title: 'Skip PR size check', files: [{additions: 100, filename: '1.txt'}]})
39+
const result = checker.check({
40+
title: 'Skip PR size check',
41+
files: [{additions: 100, filename: '1.txt'}],
42+
labels: []
43+
})
2044
expect(result).toBe(Result.error)
21-
});
45+
})
2246

2347
test('returns warning', () => {
2448
const checker = new Checker({errorSize: 20, warningSize: 10})
2549
const result = checker.check({
2650
title: 'PR',
27-
files: [{additions: 10, filename: '1.txt'}, {additions: 5, filename: '2.txt'}]
51+
files: [
52+
{additions: 10, filename: '1.txt'},
53+
{additions: 5, filename: '2.txt'}
54+
],
55+
labels: []
2856
})
2957
expect(result).toBe(Result.warning)
30-
});
58+
})
3159

3260
test('returns errors', () => {
3361
const checker = new Checker({errorSize: 20, warningSize: 10})
3462
const result = checker.check({
3563
title: 'PR',
36-
files: [{additions: 10, filename: '1.txt'}, {additions: 15, filename: '2.txt'}]
64+
files: [
65+
{additions: 10, filename: '1.txt'},
66+
{additions: 15, filename: '2.txt'}
67+
],
68+
labels: []
3769
})
3870
expect(result).toBe(Result.error)
39-
});
71+
})
4072

4173
test('returns OK', () => {
4274
const checker = new Checker({errorSize: 100, warningSize: 50})
4375
const result = checker.check({
4476
title: 'PR',
45-
files: [{additions: 10, filename: '1.txt'}, {additions: 15, filename: '2.txt'}]
77+
files: [
78+
{additions: 10, filename: '1.txt'},
79+
{additions: 15, filename: '2.txt'}
80+
],
81+
labels: []
4682
})
4783
expect(result).toBe(Result.ok)
48-
});
84+
})
4985

5086
test('skips files matching excludePath', () => {
5187
const checker = new Checker({
5288
errorSize: 40,
5389
warningSize: 30,
54-
excludePaths: ['**/test/**', 'README.md', '**/resources/**/*.json'],
90+
excludePaths: ['**/test/**', 'README.md', '**/resources/**/*.json']
91+
})
92+
const result = checker.check({
93+
title: 'PR',
94+
files: [
95+
{additions: 10, filename: '1.txt'},
96+
{additions: 15, filename: '2.txt'},
97+
{additions: 100, filename: 'some/test/file.txt'},
98+
{additions: 100, filename: 'README.md'},
99+
{additions: 100, filename: 'resources/1.json'}
100+
],
101+
labels: []
102+
})
103+
expect(result).toBe(Result.ok)
104+
})
105+
106+
test('skips files matching excludeLabels', () => {
107+
const checker = new Checker({
108+
errorSize: 40,
109+
warningSize: 30,
110+
excludeLabels: ['skip']
55111
})
56112
const result = checker.check({
57113
title: 'PR',
@@ -60,8 +116,9 @@ test('skips files matching excludePath', () => {
60116
{additions: 15, filename: '2.txt'},
61117
{additions: 100, filename: 'some/test/file.txt'},
62118
{additions: 100, filename: 'README.md'},
63-
{additions: 100, filename: 'resources/1.json'},
64-
]
119+
{additions: 100, filename: 'resources/1.json'}
120+
],
121+
labels: ['skip']
65122
})
66123
expect(result).toBe(Result.ok)
67-
});
124+
})

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ inputs:
3939
File paths that should be excluded from size calculation.
4040
default: ""
4141
required: false
42+
excludeLabels:
43+
description: >
44+
Labels that should be excluded from size calculation.
45+
default: ""
46+
required: false
4247
runs:
4348
using: "node16"
4449
main: "dist/index.js"

dist/index.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4466,16 +4466,22 @@ function run() {
44664466
const warningMessage = core.getInput('warningMessage');
44674467
const excludeTitle = core.getInput('excludeTitle');
44684468
const excludePaths = utils_1.getInputAsArray('excludePaths');
4469+
const excludeLabels = utils_1.getInputAsArray('excludeLabels');
44694470
const checker = new checker_1.Checker({
44704471
errorSize,
44714472
warningSize,
44724473
excludeTitle: excludeTitle.length === 0 ? undefined : new RegExp(excludeTitle),
4473-
excludePaths
4474+
excludePaths,
4475+
excludeLabels
44744476
});
44754477
const prParams = Object.assign(Object.assign({}, github_1.context.repo), { pull_number: pr.number });
44764478
const response = yield gitHub.pulls.listFiles(prParams);
44774479
const pullRequest = yield gitHub.pulls.get(prParams);
4478-
const result = checker.check({ title: pullRequest.data.title, files: response.data });
4480+
const result = checker.check({
4481+
title: pullRequest.data.title,
4482+
files: response.data,
4483+
labels: pullRequest.data.labels.map(it => it.name)
4484+
});
44794485
switch (result) {
44804486
case checker_1.Result.ok:
44814487
break;
@@ -4534,7 +4540,7 @@ exports.getUserAgent = getUserAgent;
45344540
/***/ 215:
45354541
/***/ (function(module) {
45364542

4537-
module.exports = {"_from":"@octokit/rest@^16.43.1","_id":"@octokit/[email protected]","_inBundle":false,"_integrity":"sha512-gfFKwRT/wFxq5qlNjnW2dh+qh74XgTQ2B179UX5K1HYCluioWj8Ndbgqw2PVqa1NnVJkGHp2ovMpVn/DImlmkw==","_location":"/@octokit/rest","_phantomChildren":{"@octokit/types":"2.12.1","deprecation":"2.3.1","once":"1.4.0"},"_requested":{"type":"range","registry":true,"raw":"@octokit/rest@^16.43.1","name":"@octokit/rest","escapedName":"@octokit%2frest","scope":"@octokit","rawSpec":"^16.43.1","saveSpec":null,"fetchSpec":"^16.43.1"},"_requiredBy":["/@actions/github"],"_resolved":"https://registry.npmjs.org/@octokit/rest/-/rest-16.43.1.tgz","_shasum":"3b11e7d1b1ac2bbeeb23b08a17df0b20947eda6b","_spec":"@octokit/rest@^16.43.1","_where":"/Users/ookami/projects/devops/gh-pr-size-watcher/node_modules/@actions/github","author":{"name":"Gregor Martynus","url":"https://github.com/gr2m"},"bugs":{"url":"https://github.com/octokit/rest.js/issues"},"bundleDependencies":false,"bundlesize":[{"path":"./dist/octokit-rest.min.js.gz","maxSize":"33 kB"}],"contributors":[{"name":"Mike de Boer","email":"[email protected]"},{"name":"Fabian Jakobs","email":"[email protected]"},{"name":"Joe Gallo","email":"[email protected]"},{"name":"Gregor Martynus","url":"https://github.com/gr2m"}],"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"},"deprecated":false,"description":"GitHub REST API client for Node.js","devDependencies":{"@gimenete/type-writer":"^0.1.3","@octokit/auth":"^1.1.1","@octokit/fixtures-server":"^5.0.6","@octokit/graphql":"^4.2.0","@types/node":"^13.1.0","bundlesize":"^0.18.0","chai":"^4.1.2","compression-webpack-plugin":"^3.1.0","cypress":"^3.0.0","glob":"^7.1.2","http-proxy-agent":"^4.0.0","lodash.camelcase":"^4.3.0","lodash.merge":"^4.6.1","lodash.upperfirst":"^4.3.1","lolex":"^5.1.2","mkdirp":"^1.0.0","mocha":"^7.0.1","mustache":"^4.0.0","nock":"^11.3.3","npm-run-all":"^4.1.2","nyc":"^15.0.0","prettier":"^1.14.2","proxy":"^1.0.0","semantic-release":"^17.0.0","sinon":"^8.0.0","sinon-chai":"^3.0.0","sort-keys":"^4.0.0","string-to-arraybuffer":"^1.0.0","string-to-jsdoc-comment":"^1.0.0","typescript":"^3.3.1","webpack":"^4.0.0","webpack-bundle-analyzer":"^3.0.0","webpack-cli":"^3.0.0"},"files":["index.js","index.d.ts","lib","plugins"],"homepage":"https://github.com/octokit/rest.js#readme","keywords":["octokit","github","rest","api-client"],"license":"MIT","name":"@octokit/rest","nyc":{"ignore":["test"]},"publishConfig":{"access":"public"},"release":{"publish":["@semantic-release/npm",{"path":"@semantic-release/github","assets":["dist/*","!dist/*.map.gz"]}]},"repository":{"type":"git","url":"git+https://github.com/octokit/rest.js.git"},"scripts":{"build":"npm-run-all build:*","build:browser":"npm-run-all build:browser:*","build:browser:development":"webpack --mode development --entry . --output-library=Octokit --output=./dist/octokit-rest.js --profile --json > dist/bundle-stats.json","build:browser:production":"webpack --mode production --entry . --plugin=compression-webpack-plugin --output-library=Octokit --output-path=./dist --output-filename=octokit-rest.min.js --devtool source-map","build:ts":"npm run -s update-endpoints:typescript","coverage":"nyc report --reporter=html && open coverage/index.html","generate-bundle-report":"webpack-bundle-analyzer dist/bundle-stats.json --mode=static --no-open --report dist/bundle-report.html","lint":"prettier --check '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","lint:fix":"prettier --write '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","postvalidate:ts":"tsc --noEmit --target es6 test/typescript-validate.ts","prebuild:browser":"mkdirp dist/","pretest":"npm run -s lint","prevalidate:ts":"npm run -s build:ts","start-fixtures-server":"octokit-fixtures-server","test":"nyc mocha test/mocha-node-setup.js \"test/*/**/*-test.js\"","test:browser":"cypress run --browser chrome","update-endpoints":"npm-run-all update-endpoints:*","update-endpoints:fetch-json":"node scripts/update-endpoints/fetch-json","update-endpoints:typescript":"node scripts/update-endpoints/typescript","validate:ts":"tsc --target es6 --noImplicitAny index.d.ts"},"types":"index.d.ts","version":"16.43.1"};
4543+
module.exports = {"name":"@octokit/rest","version":"16.43.1","publishConfig":{"access":"public"},"description":"GitHub REST API client for Node.js","keywords":["octokit","github","rest","api-client"],"author":"Gregor Martynus (https://github.com/gr2m)","contributors":[{"name":"Mike de Boer","email":"[email protected]"},{"name":"Fabian Jakobs","email":"[email protected]"},{"name":"Joe Gallo","email":"[email protected]"},{"name":"Gregor Martynus","url":"https://github.com/gr2m"}],"repository":"https://github.com/octokit/rest.js","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"},"devDependencies":{"@gimenete/type-writer":"^0.1.3","@octokit/auth":"^1.1.1","@octokit/fixtures-server":"^5.0.6","@octokit/graphql":"^4.2.0","@types/node":"^13.1.0","bundlesize":"^0.18.0","chai":"^4.1.2","compression-webpack-plugin":"^3.1.0","cypress":"^3.0.0","glob":"^7.1.2","http-proxy-agent":"^4.0.0","lodash.camelcase":"^4.3.0","lodash.merge":"^4.6.1","lodash.upperfirst":"^4.3.1","lolex":"^5.1.2","mkdirp":"^1.0.0","mocha":"^7.0.1","mustache":"^4.0.0","nock":"^11.3.3","npm-run-all":"^4.1.2","nyc":"^15.0.0","prettier":"^1.14.2","proxy":"^1.0.0","semantic-release":"^17.0.0","sinon":"^8.0.0","sinon-chai":"^3.0.0","sort-keys":"^4.0.0","string-to-arraybuffer":"^1.0.0","string-to-jsdoc-comment":"^1.0.0","typescript":"^3.3.1","webpack":"^4.0.0","webpack-bundle-analyzer":"^3.0.0","webpack-cli":"^3.0.0"},"types":"index.d.ts","scripts":{"coverage":"nyc report --reporter=html && open coverage/index.html","lint":"prettier --check '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","lint:fix":"prettier --write '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","pretest":"npm run -s lint","test":"nyc mocha test/mocha-node-setup.js \"test/*/**/*-test.js\"","test:browser":"cypress run --browser chrome","build":"npm-run-all build:*","build:ts":"npm run -s update-endpoints:typescript","prebuild:browser":"mkdirp dist/","build:browser":"npm-run-all build:browser:*","build:browser:development":"webpack --mode development --entry . --output-library=Octokit --output=./dist/octokit-rest.js --profile --json > dist/bundle-stats.json","build:browser:production":"webpack --mode production --entry . --plugin=compression-webpack-plugin --output-library=Octokit --output-path=./dist --output-filename=octokit-rest.min.js --devtool source-map","generate-bundle-report":"webpack-bundle-analyzer dist/bundle-stats.json --mode=static --no-open --report dist/bundle-report.html","update-endpoints":"npm-run-all update-endpoints:*","update-endpoints:fetch-json":"node scripts/update-endpoints/fetch-json","update-endpoints:typescript":"node scripts/update-endpoints/typescript","prevalidate:ts":"npm run -s build:ts","validate:ts":"tsc --target es6 --noImplicitAny index.d.ts","postvalidate:ts":"tsc --noEmit --target es6 test/typescript-validate.ts","start-fixtures-server":"octokit-fixtures-server"},"license":"MIT","files":["index.js","index.d.ts","lib","plugins"],"nyc":{"ignore":["test"]},"release":{"publish":["@semantic-release/npm",{"path":"@semantic-release/github","assets":["dist/*","!dist/*.map.gz"]}]},"bundlesize":[{"path":"./dist/octokit-rest.min.js.gz","maxSize":"33 kB"}]};
45384544

45394545
/***/ }),
45404546

@@ -10404,15 +10410,17 @@ var Result;
1040410410
})(Result = exports.Result || (exports.Result = {}));
1040510411
class Checker {
1040610412
constructor(params) {
10407-
var _a;
10413+
var _a, _b;
1040810414
this.errorSize = params.errorSize;
1040910415
this.warningSize = params.warningSize;
1041010416
this.excludeTitle = params.excludeTitle;
1041110417
this.excludePaths = (_a = params.excludePaths, (_a !== null && _a !== void 0 ? _a : []));
10418+
this.excludeLabels = (_b = params.excludeLabels, (_b !== null && _b !== void 0 ? _b : []));
1041210419
}
1041310420
check(pr) {
1041410421
core.debug(`PR title: ${pr.title}`);
10415-
if (this.shouldSkip(pr.title))
10422+
core.debug(`PR labels: ${pr.labels.join(', ')}`);
10423+
if (this.shouldSkip(pr.title, pr.labels))
1041610424
return Result.ok;
1041710425
const files = pr.files.filter(f => !this.excludePaths.some(p => minimatch_1.default(f.filename, p)));
1041810426
if (core.isDebug()) {
@@ -10426,9 +10434,9 @@ class Checker {
1042610434
return Result.warning;
1042710435
return Result.ok;
1042810436
}
10429-
shouldSkip(title) {
10437+
shouldSkip(title, labels) {
1043010438
var _a, _b;
10431-
return _b = (_a = this.excludeTitle) === null || _a === void 0 ? void 0 : _a.test(title), (_b !== null && _b !== void 0 ? _b : false);
10439+
return (_b = (_a = this.excludeTitle) === null || _a === void 0 ? void 0 : _a.test(title), (_b !== null && _b !== void 0 ? _b : labels.filter(it => this.excludeLabels.includes(it)).length > 0));
1043210440
}
1043310441
static getAdditions(data) {
1043410442
return data.map(v => v.additions).reduce((acc, v) => acc + v, 0);

0 commit comments

Comments
 (0)