Skip to content

Commit

Permalink
Implemented multi reactions?
Browse files Browse the repository at this point in the history
  • Loading branch information
shel committed Apr 16, 2020
1 parent 9f32099 commit 38027e8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
48 changes: 34 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,40 @@ const REACTION_TYPES = [
];

async function addReaction(octokit, repo, comment_id, reactionType) {
if (REACTION_TYPES.includes(reactionType)) {
await octokit.reactions.createForIssueComment({
owner: repo[0],
repo: repo[1],
comment_id: comment_id,
content: reactionType
});
core.info(`Set '${reactionType}' reaction on comment.`);
} else {
core.setFailed("Invalid 'reaction-type'.");
return;
}
let ReactionsSet = [...new Set(reactionType.split(', ')
.filter(item => {
if(!REACTION_TYPES.includes(item)){
core.info(`Skipping invalid 'reaction-type' '${item}'.`);
return false;
}
return true;
}))];

if(!ReactionsSet){
core.setFailed(`Can't find any valid 'reaction-type' in provided value: ${reactionType}.`);
return false;
}

let results = await Promise.allSettled(ReactionsSet.map(async (item) => {
await octokit.reactions.createForIssueComment({
owner: repo[0],
repo: repo[1],
comment_id: comment_id,
content: item
});
core.info(`Setting '${item}' reaction on comment.`);
}));

for(let i = 0, l = results.length; i<l; i++){
if(results[i].status === 'fulfilled'){
core.info(`Added reaction '${ReactionsSet[i]}' to comment id '${comment_id}'.`);
}
else if(results[i].status === 'rejected'){
core.info(`Adding reaction '${ReactionsSet[i]}' to comment id '${comment_id}' failed with ${results[i].reason}.`);
}
}
ReactionsSet = undefined;
results = undefined;
}

async function run() {
Expand Down Expand Up @@ -90,7 +112,6 @@ async function run() {
// Set a comment reaction
if (inputs.reactionType) {
await addReaction(octokit, repo, inputs.commentId, inputs.reactionType);
core.info(`Added reaction '${inputs.reactionType}' to comment id '${inputs.commentId}'.`);
}
} else if (inputs.issueNumber) {
// Create a comment
Expand All @@ -110,7 +131,6 @@ async function run() {
// Set a comment reaction
if (inputs.reactionType) {
await addReaction(octokit, repo, comment.id, inputs.reactionType);
core.info(`Added reaction '${inputs.reactionType}' to comment id '${comment.id}'.`);
}
} else {
core.setFailed("Missing either 'issue-number' or 'comment-id'.");
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"lint": "eslint index.js",
"package": "ncc build index.js -o dist",
"package": "ncc build index.js -m -o dist",
"test": "eslint index.js && jest --passWithNoTests"
},
"repository": {
Expand Down

0 comments on commit 38027e8

Please sign in to comment.