Skip to content

Commit

Permalink
Add option to add comment to PR (#1)
Browse files Browse the repository at this point in the history
* Add optional comment to the PR

* fixup! Add optional comment to the PR
  • Loading branch information
TobKed authored Oct 27, 2020
1 parent 90ae5d6 commit d664559
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 26 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,20 @@ projects.

## Inputs

| Input | Required | Example | Comment |
|-------------------------------|----------|-------------------------------|-------------------------------------------------------------------------|
| `token` | yes | `${{ secrets.GITHUB_TOKEN }}` | The github token passed from `${{ secrets.GITHUB_TOKEN }}` |
| `label` | no | `Approved by committers` | Label to be added/removed to the Pull Request if approved/not approved |
| `require_committers_approval` | no | `true` | Is approval from user with write permission required |
| Input | Required | Example | Comment |
|-------------------------------|----------|-----------------------------------------------------------------|-------------------------------------------------------------------------|
| `token` | yes | `${{ secrets.GITHUB_TOKEN }}` | The github token passed from `${{ secrets.GITHUB_TOKEN }}` |
| `label` | no | `spproved by committers` | Label to be added/removed to the Pull Request if approved/not approved |
| `require_committers_approval` | no | `true` | Is approval from user with write permission required |
| `comment` | no | `This became approved, rerun tests manually or rebase and push` | Add optional comment to the PR when approved |

## Outputs

| Output | |
|----------------|------------------------------|
| `isApproved` | is Pull Reqeuest is approved |
| `labelSet` | is label was set |
| `labelRemoved` | is label was removed |
| `isApproved` | is Pull Reqeuest approved |
| `labelSet` | was label set |
| `labelRemoved` | was label removed |

# Examples

Expand Down
17 changes: 13 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@ description: 'Set label when PR is approved.'
author: 'TobKed'
inputs:
token:
description: The GITHUB_TOKEN secret of the repository
description: 'The GITHUB_TOKEN secret of the repository'
required: true
label:
description: A label to be checked/added/removed
description: 'A label to be checked/added/removed'
required: false
require_committers_approval:
description: Is approval from person with write access to repo required
description: 'Is approval from person with write access to repo required'
required: false
default: 'false'
comment:
description: 'Comment to be added to Pull Request if approved'
required: false
outputs:
isApproved:
description: 'Is Pull Reqeuest approved'
labelSet:
description: 'Was label set'
labelRemoved:
description: 'Was label removed'
runs:
using: 'node12'
main: 'dist/index.js'
Expand Down
29 changes: 22 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,18 @@ function removeLabel(octokit, owner, repo, pullRequestNumber, label) {
});
});
}
function addComment(octokit, owner, repo, pullRequestNumber, comment) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Adding comment: ${comment}`);
yield octokit.issues.createComment({
owner,
repo,
// eslint-disable-next-line @typescript-eslint/camelcase
issue_number: pullRequestNumber,
body: comment
});
});
}
function getWorkflowId(octokit, runId, owner, repo) {
return __awaiter(this, void 0, void 0, function* () {
const reply = yield octokit.actions.getWorkflowRun({
Expand Down Expand Up @@ -1650,10 +1662,9 @@ function run() {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
const token = core.getInput('token', { required: true });
const userLabel = core.getInput('label', { required: false }) || 'not set';
const requireCommittersApproval = core.getInput('require_committers_approval', {
required: false
}) === 'true';
const userLabel = core.getInput('label') || 'not set';
const requireCommittersApproval = core.getInput('require_committers_approval') === 'true';
const comment = core.getInput('comment') || '';
const octokit = new github.GitHub(token);
const context = github.context;
const repository = getRequiredEnv('GITHUB_REPOSITORY');
Expand All @@ -1665,7 +1676,8 @@ function run() {
const sha = (_b = context.payload.pull_request) === null || _b === void 0 ? void 0 : _b.head.sha;
core.info(`\n############### Set Label When Approved start ##################\n` +
`label: "${userLabel}"\n` +
`requireCommittersApproval: ${requireCommittersApproval}`);
`requireCommittersApproval: ${requireCommittersApproval}\n` +
`comment: ${comment}`);
if (eventName !== 'pull_request_review') {
throw Error(`This action is only useful in "pull_request_review" triggered runs and you used it in "${eventName}"`);
}
Expand All @@ -1683,10 +1695,13 @@ function run() {
isLabelShouldBeSet = isApproved && !labelNames.includes(userLabel);
isLabelShouldBeRemoved = !isApproved && labelNames.includes(userLabel);
if (isLabelShouldBeSet) {
setLabel(octokit, owner, repo, pullRequest.number, userLabel);
yield setLabel(octokit, owner, repo, pullRequest.number, userLabel);
if (comment !== '') {
yield addComment(octokit, owner, repo, pullRequest.number, comment);
}
}
else if (isLabelShouldBeRemoved) {
removeLabel(octokit, owner, repo, pullRequest.number, userLabel);
yield removeLabel(octokit, owner, repo, pullRequest.number, userLabel);
}
}
//// Future option to rerun workflows if PR approved
Expand Down
34 changes: 27 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ async function removeLabel(
})
}

async function addComment(
octokit: github.GitHub,
owner: string,
repo: string,
pullRequestNumber: number,
comment: string
): Promise<void> {
core.info(`Adding comment: ${comment}`)
await octokit.issues.createComment({
owner,
repo,
// eslint-disable-next-line @typescript-eslint/camelcase
issue_number: pullRequestNumber,
body: comment
})
}

async function getWorkflowId(
octokit: github.GitHub,
runId: number,
Expand Down Expand Up @@ -248,11 +265,10 @@ async function printDebug(

async function run(): Promise<void> {
const token = core.getInput('token', {required: true})
const userLabel = core.getInput('label', {required: false}) || 'not set'
const userLabel = core.getInput('label') || 'not set'
const requireCommittersApproval =
core.getInput('require_committers_approval', {
required: false
}) === 'true'
core.getInput('require_committers_approval') === 'true'
const comment = core.getInput('comment') || ''
const octokit = new github.GitHub(token)
const context = github.context
const repository = getRequiredEnv('GITHUB_REPOSITORY')
Expand All @@ -266,7 +282,8 @@ async function run(): Promise<void> {
core.info(
`\n############### Set Label When Approved start ##################\n` +
`label: "${userLabel}"\n` +
`requireCommittersApproval: ${requireCommittersApproval}`
`requireCommittersApproval: ${requireCommittersApproval}\n` +
`comment: ${comment}`
)

if (eventName !== 'pull_request_review') {
Expand Down Expand Up @@ -305,9 +322,12 @@ async function run(): Promise<void> {
isLabelShouldBeRemoved = !isApproved && labelNames.includes(userLabel)

if (isLabelShouldBeSet) {
setLabel(octokit, owner, repo, pullRequest.number, userLabel)
await setLabel(octokit, owner, repo, pullRequest.number, userLabel)
if (comment !== '') {
await addComment(octokit, owner, repo, pullRequest.number, comment)
}
} else if (isLabelShouldBeRemoved) {
removeLabel(octokit, owner, repo, pullRequest.number, userLabel)
await removeLabel(octokit, owner, repo, pullRequest.number, userLabel)
}
}

Expand Down

0 comments on commit d664559

Please sign in to comment.