Skip to content

Commit

Permalink
feat: reactions (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
thollander authored May 24, 2022
1 parent 3233c31 commit cbe607a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ jobs:
Current branch is `${{ github.head_ref }}`.
_(execution **${{ github.run_id }}** / attempt **${{ github.run_attempt }}**)_
comment_includes: Current branch
reactions: eyes, rocket
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
### Setting reactions
You can also set some reactions on your comments through the `reactions` input.
It takes only valid reactions and adds it to the comment you've just created. (See https://docs.github.com/en/rest/reactions#reaction-types)

```yml
- name: PR comment with reactions
uses: thollander/actions-comment-pull-request@v1
with:
message: |
Hello world ! :wave:
reactions: eyes, rocket
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

### Specifying which pull request to comment on

You can explicitly input which pull request should be commented on by passing the `pr_number` input.
Expand Down Expand Up @@ -78,6 +94,7 @@ _That is particularly interesting while committing multiple times in a PR and th
| --- | --- | --- | --- |
| `GITHUB_TOKEN` | Token that is used to create comments | ✅ | |
| `message` | The comment body | ✅ | |
| `reactions` | List of reactions for the comment (comma separated). See https://docs.github.com/en/rest/reactions#reaction-types | | |
| `pr_number` | The number of the pull request where to create the comment | | current pull request number (deduced from context) |
| `comment_includes` | The text that should be used to find comment in case of replacement. | | |

Expand Down
20 changes: 19 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8330,19 +8330,35 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
const github = __importStar(__nccwpck_require__(5438));
const core = __importStar(__nccwpck_require__(2186));
// See https://docs.github.com/en/rest/reactions#reaction-types
const REACTIONS = ['+1', '-1', 'laugh', 'confused', 'heart', 'hooray', 'rocket', 'eyes'];
async function run() {
try {
const message = core.getInput('message');
const github_token = core.getInput('GITHUB_TOKEN');
const pr_number = core.getInput('pr_number');
const comment_includes = core.getInput('comment_includes');
const reactions = core.getInput('reactions');
const context = github.context;
const pull_number = parseInt(pr_number) || context.payload.pull_request?.number;
const octokit = github.getOctokit(github_token);
if (!pull_number) {
core.setFailed('No pull request in input neither in current context.');
return;
}
async function addReactions(comment_id, reactions) {
const validReactions = reactions
.replace(/\s/g, '')
.split(',')
.filter((reaction) => REACTIONS.includes(reaction));
await Promise.allSettled(validReactions.map(async (content) => {
await octokit.rest.reactions.createForIssueComment({
...context.repo,
comment_id,
content,
});
}));
}
if (comment_includes) {
let comment;
for await (const { data: comments } of octokit.paginate.iterator(octokit.rest.issues.listComments, {
Expand All @@ -8359,17 +8375,19 @@ async function run() {
comment_id: comment.id,
body: message,
});
await addReactions(comment.id, reactions);
return;
}
else {
core.info('No comment has been found with asked pattern. Creating a new comment.');
}
}
await octokit.rest.issues.createComment({
const { data: comment } = await octokit.rest.issues.createComment({
...context.repo,
issue_number: pull_number,
body: message,
});
await addReactions(comment.id, reactions);
}
catch (error) {
if (error instanceof Error) {
Expand Down
27 changes: 26 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import * as github from '@actions/github';
import * as core from '@actions/core';
import { GetResponseDataTypeFromEndpointMethod } from '@octokit/types';

// See https://docs.github.com/en/rest/reactions#reaction-types
const REACTIONS = ['+1', '-1', 'laugh', 'confused', 'heart', 'hooray', 'rocket', 'eyes'] as const;
type Reaction = typeof REACTIONS[number];

async function run() {
try {
const message: string = core.getInput('message');
const github_token: string = core.getInput('GITHUB_TOKEN');
const pr_number: string = core.getInput('pr_number');
const comment_includes: string = core.getInput('comment_includes');
const reactions: string = core.getInput('reactions');

const context = github.context;
const pull_number = parseInt(pr_number) || context.payload.pull_request?.number;
Expand All @@ -19,6 +24,23 @@ async function run() {
return;
}

async function addReactions(comment_id: number, reactions: string) {
const validReactions = <Reaction[]>reactions
.replace(/\s/g, '')
.split(',')
.filter((reaction) => REACTIONS.includes(<Reaction>reaction));

await Promise.allSettled(
validReactions.map(async (content) => {
await octokit.rest.reactions.createForIssueComment({
...context.repo,
comment_id,
content,
});
}),
);
}

if (comment_includes) {
type ListCommentsResponseDataType = GetResponseDataTypeFromEndpointMethod<
typeof octokit.rest.issues.listComments
Expand All @@ -38,17 +60,20 @@ async function run() {
comment_id: comment.id,
body: message,
});
await addReactions(comment.id, reactions);
return;
} else {
core.info('No comment has been found with asked pattern. Creating a new comment.');
}
}

await octokit.rest.issues.createComment({
const { data: comment } = await octokit.rest.issues.createComment({
...context.repo,
issue_number: pull_number,
body: message,
});

await addReactions(comment.id, reactions);
} catch (error) {
if (error instanceof Error) {
core.setFailed(error.message);
Expand Down

0 comments on commit cbe607a

Please sign in to comment.