Skip to content

Commit ae4cb33

Browse files
authored
Merge pull request #1 from mb2dev/feature/add-reactions
add reactions
2 parents 5b8564b + d9a33ac commit ae4cb33

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ jobs:
1212
uses: ./
1313
with:
1414
message: "Hello, It's my first comment with Github action !"
15-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
15+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
16+
reactions: "+1 | -1 | laugh | hooray | confused | heart | rocket | eyes"

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ inputs:
77
GITHUB_TOKEN:
88
description: 'Github token of the repository'
99
required: true
10+
reactions:
11+
description: 'A list of reaction separated by | to add to the comment'
12+
required: false
1013
runs:
1114
using: 'docker'
1215
image: 'Dockerfile'
1316
args:
1417
- ${{ inputs.message }}
1518
- ${{ inputs.GITHUB_TOKEN }}
19+
- ${{ inputs.reactions }}

lib/main.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
1212
const core = require("@actions/core");
1313
const github = require("@actions/github");
1414
const rest_1 = require("@octokit/rest");
15+
const REACTION_LIST = [
16+
"+1",
17+
"-1",
18+
"laugh",
19+
"hooray",
20+
"confused",
21+
"heart",
22+
"rocket",
23+
"eyes"
24+
];
1525
function run() {
26+
var _a;
1627
return __awaiter(this, void 0, void 0, function* () {
1728
try {
1829
const message = core.getInput('message');
30+
const reactions = core.getInput('reactions');
1931
const github_token = core.getInput('GITHUB_TOKEN');
2032
const context = github.context;
2133
if (context.payload.pull_request == null) {
@@ -28,10 +40,40 @@ function run() {
2840
});
2941
const new_comment = yield octokit.issues.createComment(Object.assign(Object.assign({}, context.repo), { issue_number: pull_request_number, body: message }));
3042
;
43+
if (reactions) {
44+
const repo = (_a = process.env.GITHUB_REPOSITORY) === null || _a === void 0 ? void 0 : _a.split('/');
45+
yield addReactions(octokit, repo, new_comment.data.id, reactions);
46+
}
3147
}
3248
catch (error) {
3349
core.setFailed(error.message);
3450
}
3551
});
3652
}
53+
function addReactions(octokit, repo, commentId, reactions) {
54+
return __awaiter(this, void 0, void 0, function* () {
55+
let ReactionsSet = [
56+
...new Set(reactions
57+
.replace(/\s/g, "")
58+
.split("|")
59+
.filter((reaction) => {
60+
if (!REACTION_LIST.includes(reaction)) {
61+
return false;
62+
}
63+
return true;
64+
})),
65+
];
66+
if (!ReactionsSet) {
67+
return false;
68+
}
69+
let results = yield Promise.all(ReactionsSet.map((reaction) => __awaiter(this, void 0, void 0, function* () {
70+
yield octokit.reactions.createForIssueComment({
71+
owner: repo[0],
72+
repo: repo[1],
73+
comment_id: commentId,
74+
content: reaction,
75+
});
76+
})));
77+
});
78+
}
3779
run();

src/main.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@ const core = require("@actions/core");
22
const github = require("@actions/github");
33
import { Octokit } from '@octokit/rest';
44

5+
const REACTION_LIST = [
6+
"+1",
7+
"-1",
8+
"laugh",
9+
"hooray",
10+
"confused",
11+
"heart",
12+
"rocket",
13+
"eyes"
14+
]
515

616
async function run() {
717
try {
818
const message = core.getInput('message');
19+
const reactions = core.getInput('reactions');
920
const github_token = core.getInput('GITHUB_TOKEN');
1021

1122
const context = github.context;
@@ -27,11 +38,55 @@ async function run() {
2738
});
2839
;
2940

41+
if(reactions){
42+
const repo = process.env.GITHUB_REPOSITORY?.split('/');
43+
await addReactions(octokit, repo, new_comment.data.id, reactions)
44+
}
3045

3146
} catch (error) {
3247
core.setFailed(error.message);
3348
}
3449
}
3550

51+
/**
52+
* Add reactions on a comment
53+
*
54+
* @param octokit
55+
* @param repo
56+
* @param commentId
57+
* @param reactions
58+
*/
59+
async function addReactions(octokit, repo, commentId, reactions){
60+
61+
let ReactionsSet = [
62+
...new Set(
63+
reactions
64+
.replace(/\s/g, "")
65+
.split("|")
66+
.filter((reaction) => {
67+
if (!REACTION_LIST.includes(reaction)) {
68+
return false;
69+
}
70+
return true;
71+
})
72+
),
73+
];
74+
75+
if (!ReactionsSet) {
76+
return false;
77+
}
78+
79+
let results = await Promise.all(
80+
ReactionsSet.map(async (reaction) => {
81+
await octokit.reactions.createForIssueComment({
82+
owner: repo[0],
83+
repo: repo[1],
84+
comment_id: commentId,
85+
content: reaction,
86+
});
87+
})
88+
);
89+
}
90+
3691

3792
run();

0 commit comments

Comments
 (0)