Skip to content

Commit 0058d00

Browse files
authored
Add remove_label_when_approval_missing input. (#6)
Removing label when you lost or never had an approval status might not be what you desire to do. In some workflows when the label is manually set, removing it is automatically is harmful and it's better to leave the decision about removal to the commiters. Adding the parameter solves the problem.
1 parent 4c5190f commit 0058d00

File tree

11 files changed

+2725
-640
lines changed

11 files changed

+2725
-640
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
key: ${{ env.cache-name }}-${{ github.job }}-${{ hashFiles('package.json','package-lock.json') }}
2121
- name: "Install dependencies for npm"
2222
run: |
23+
npm install npm@latest
2324
npm ci
2425
- name: Cache pre-commit env
2526
uses: actions/cache@v2

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ repos:
3232
- id: build
3333
name: Build package for distribution
3434
language: system
35-
entry: bash -c "npm run release"
35+
entry: ./scripts/transpile_if_needed.sh
3636
files: .*\.ts$
3737
require_serial: true
3838
pass_filenames: false

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Setting label is optional that only output can be used in the workflow.
3030

3131
The required input `require_committers_approval` says is approval can be done by people with read access to the repo
3232
or by anyone. It may be useful in repositories which requires committers approvals like [Apache Software Foundation](https://github.com/apache/)
33-
projects.
33+
projects. By default, when the PR does not have "approved" status or looses its aproval status the label is removed.
34+
This behaviour can be disabled when `remove_label_when_approval_missing` is set to false.
3435

3536
It can be used in workflows triggered by "pull_request_review" or "workflow_run".
3637
When used on "pull_request_review" any workflows triggered from pull request created from fork will fail
@@ -43,19 +44,20 @@ Pull request number can be obtained by using [potiuk/get-workflow-origin)](https
4344

4445
## Inputs
4546

46-
| Input | Required | Example | Comment |
47-
|-------------------------------|----------|-------------------------------------------------------------------|-------------------------------------------------------------------------------|
48-
| `token` | yes | `${{ secrets.GITHUB_TOKEN }}` | The github token passed from `${{ secrets.GITHUB_TOKEN }}` |
49-
| `label` | no | `Approved by committers` | Label to be added/removed to the Pull Request if approved/not approved |
50-
| `require_committers_approval` | no | `true` | Is approval from user with write permission required |
51-
| `comment` | no | `PR approved by at least one committer and no changes requested.` | Add optional comment to the PR when approved (requires label input to be set) |
52-
| `pullRequestNumber` | no | `${{ steps.source-run-info.outputs.pullRequestNumber }}` | Pull request number if triggered by "worfklow_run" |
47+
| Input | Required | Example | Comment |
48+
|--------------------------------------|----------|-------------------------------------------------------------------|-------------------------------------------------------------------------------|
49+
| `token` | yes | `${{ secrets.GITHUB_TOKEN }}` | The github token passed from `${{ secrets.GITHUB_TOKEN }}` |
50+
| `label` | no | `Approved by committers` | Label to be added/removed to the Pull Request if approved/not approved |
51+
| `require_committers_approval` | no | `true` | Is approval from user with write permission required |
52+
| `remove_label_when_approval_missing` | no | `true` | Should label be removed when approval is missing |
53+
| `comment` | no | `PR approved by at least one committer and no changes requested.` | Add optional comment to the PR when approved (requires label input to be set) |
54+
| `pullRequestNumber` | no | `${{ steps.source-run-info.outputs.pullRequestNumber }}` | Pull request number if triggered by "worfklow_run" |
5355

5456
## Outputs
5557

5658
| Output | |
5759
|----------------|------------------------------|
58-
| `isApproved` | is Pull Reqeuest approved |
60+
| `isApproved` | is Pull Request approved |
5961
| `labelSet` | was label set |
6062
| `labelRemoved` | was label removed |
6163

@@ -115,6 +117,7 @@ jobs:
115117
token: ${{ secrets.GITHUB_TOKEN }}
116118
label: 'ready to merge (committers)'
117119
require_committers_approval: 'true'
120+
remove_label_when_approval_missing: 'false'
118121
comment: 'PR approved by at least one committer and no changes requested.'
119122
- name: Label when approved by anyone
120123
uses: TobKed/[email protected]

action.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ inputs:
1111
require_committers_approval:
1212
description: 'Is approval from person with write access to repo required'
1313
required: false
14+
remove_label_when_approval_missing:
15+
description: 'Should label be removed when approval is missing'
16+
required: false
1417
comment:
1518
description: 'Comment to be added to Pull Request if approved'
1619
required: false
@@ -19,7 +22,7 @@ inputs:
1922
required: false
2023
outputs:
2124
isApproved:
22-
description: 'Is Pull Reqeuest approved'
25+
description: 'Is Pull Request approved'
2326
labelSet:
2427
description: 'Was label set'
2528
labelRemoved:

dist/index.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,10 +1483,7 @@ function getPullRequest(octokit, owner, repo, pullRequestNumber) {
14831483
});
14841484
}
14851485
function getPullRequestLabels(pullRequest) {
1486-
const labelNames = pullRequest
1487-
? pullRequest.labels.map(label => label.name)
1488-
: [];
1489-
return labelNames;
1486+
return pullRequest ? pullRequest.labels.map(label => label.name) : [];
14901487
}
14911488
function getReviews(octokit, owner, repo, number, getComitters) {
14921489
return __awaiter(this, void 0, void 0, function* () {
@@ -1591,17 +1588,19 @@ function addComment(octokit, owner, repo, pullRequestNumber, comment) {
15911588
});
15921589
});
15931590
}
1594-
function printDebug(item, description = '') {
1595-
return __awaiter(this, void 0, void 0, function* () {
1596-
const itemJson = JSON.stringify(item);
1597-
core.debug(`\n ######### ${description} ######### \n: ${itemJson}\n\n`);
1598-
});
1599-
}
1591+
// async function printDebug(
1592+
// item: object | string | boolean | number,
1593+
// description: string = ''
1594+
// ): Promise<void> {
1595+
// const itemJson = JSON.stringify(item)
1596+
// core.debug(`\n ######### ${description} ######### \n: ${itemJson}\n\n`)
1597+
// }
16001598
function run() {
16011599
return __awaiter(this, void 0, void 0, function* () {
16021600
const token = core.getInput('token', { required: true });
16031601
const userLabel = core.getInput('label') || 'not set';
16041602
const requireCommittersApproval = core.getInput('require_committers_approval') === 'true';
1603+
const removeLabelWhenApprovalMissing = core.getInput('remove_label_when_approval_missing') === 'true';
16051604
const comment = core.getInput('comment') || '';
16061605
const pullRequestNumberInput = core.getInput('pullRequestNumber') || 'not set';
16071606
const octokit = new github.GitHub(token);
@@ -1649,7 +1648,10 @@ function run() {
16491648
let isLabelShouldBeRemoved = false;
16501649
if (userLabel !== 'not set') {
16511650
isLabelShouldBeSet = isApproved && !labelNames.includes(userLabel);
1652-
isLabelShouldBeRemoved = !isApproved && labelNames.includes(userLabel);
1651+
isLabelShouldBeRemoved =
1652+
!isApproved &&
1653+
labelNames.includes(userLabel) &&
1654+
removeLabelWhenApprovalMissing;
16531655
if (isLabelShouldBeSet) {
16541656
yield setLabel(octokit, owner, repo, pullRequest.number, userLabel);
16551657
if (comment !== '') {
@@ -2237,7 +2239,7 @@ module.exports = require("https");
22372239
/***/ 215:
22382240
/***/ (function(module) {
22392241

2240-
module.exports = {"name":"@octokit/rest","version":"16.43.0","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"}],"_resolved":"https://registry.npmjs.org/@octokit/rest/-/rest-16.43.0.tgz","_integrity":"sha512-u+OwrTxHuppVcssGmwCmb4jgPNzsRseJ2rS5PrZk2ASC+WkaF5Q7wu8zVtJ4OA24jK6aRymlwA2uwL36NU9nAA==","_from":"@octokit/[email protected]"};
2242+
module.exports = {"_args":[["@octokit/[email protected]","/home/jarek/code/label-when-approved-action"]],"_from":"@octokit/[email protected]","_id":"@octokit/[email protected]","_inBundle":false,"_integrity":"sha512-u+OwrTxHuppVcssGmwCmb4jgPNzsRseJ2rS5PrZk2ASC+WkaF5Q7wu8zVtJ4OA24jK6aRymlwA2uwL36NU9nAA==","_location":"/@octokit/rest","_phantomChildren":{},"_requested":{"type":"version","registry":true,"raw":"@octokit/[email protected]","name":"@octokit/rest","escapedName":"@octokit%2frest","scope":"@octokit","rawSpec":"16.43.0","saveSpec":null,"fetchSpec":"16.43.0"},"_requiredBy":["/@actions/github"],"_resolved":"https://registry.npmjs.org/@octokit/rest/-/rest-16.43.0.tgz","_spec":"16.43.0","_where":"/home/jarek/code/label-when-approved-action","author":{"name":"Gregor Martynus","url":"https://github.com/gr2m"},"bugs":{"url":"https://github.com/octokit/rest.js/issues"},"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"},"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.0"};
22412243

22422244
/***/ }),
22432245

@@ -3628,6 +3630,12 @@ function convertBody(buffer, headers) {
36283630
// html4
36293631
if (!res && str) {
36303632
res = /<meta[\s]+?http-equiv=(['"])content-type\1[\s]+?content=(['"])(.+?)\2/i.exec(str);
3633+
if (!res) {
3634+
res = /<meta[\s]+?content=(['"])(.+?)\1[\s]+?http-equiv=(['"])content-type\3/i.exec(str);
3635+
if (res) {
3636+
res.pop(); // drop last quote
3637+
}
3638+
}
36313639

36323640
if (res) {
36333641
res = /charset=(.*)/i.exec(res.pop());
@@ -4635,7 +4643,7 @@ function fetch(url, opts) {
46354643
// HTTP fetch step 5.5
46364644
switch (request.redirect) {
46374645
case 'error':
4638-
reject(new FetchError(`redirect mode is set to error: ${request.url}`, 'no-redirect'));
4646+
reject(new FetchError(`uri requested responds with a redirect, redirect mode is set to error: ${request.url}`, 'no-redirect'));
46394647
finalize();
46404648
return;
46414649
case 'manual':
@@ -4674,7 +4682,8 @@ function fetch(url, opts) {
46744682
method: request.method,
46754683
body: request.body,
46764684
signal: request.signal,
4677-
timeout: request.timeout
4685+
timeout: request.timeout,
4686+
size: request.size
46784687
};
46794688

46804689
// HTTP-redirect fetch step 9

0 commit comments

Comments
 (0)