Add action

This commit is contained in:
Peter Evans 2019-11-24 16:44:26 +09:00
parent 911d287d94
commit eaa43711c7
7 changed files with 17237 additions and 0 deletions

17
.eslintrc.json Normal file
View File

@ -0,0 +1,17 @@
{
"env": {
"commonjs": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
}
}

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

24
action.yml Normal file
View File

@ -0,0 +1,24 @@
name: 'Create or Update Comment'
description: 'Create or update an issue or pull request comment'
inputs:
token:
description: 'A GitHub access token.'
required: true
repository:
description: 'The full name of the repository to create or update a comment.'
issue-number:
description: 'The number of the issue in which to create a comment.'
comment-id:
description: 'The id of the comment to update.'
body:
description: 'The comment body.'
edit-mode:
description: 'The mode when updating a comment, "replace" or "append".'
reaction-type:
description: 'The reaction to add to the comment.'
runs:
using: 'node12'
main: 'dist/index.js'
branding:
icon: 'message-square'
color: 'gray-dark'

11315
dist/index.js vendored Normal file

File diff suppressed because one or more lines are too long

121
index.js Normal file
View File

@ -0,0 +1,121 @@
const { inspect } = require("util");
const core = require("@actions/core");
const github = require("@actions/github");
const REACTION_TYPES = [
"+1",
"-1",
"laugh",
"confused",
"heart",
"hooray",
"rocket",
"eyes"
];
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;
}
}
async function run() {
try {
const inputs = {
token: core.getInput("token"),
repository: core.getInput("repository"),
issueNumber: core.getInput("issue-number"),
commentId: core.getInput("comment-id"),
body: core.getInput("body"),
editMode: core.getInput("edit-mode"),
reactionType: core.getInput("reaction-type")
};
core.debug(`Inputs: ${inspect(inputs)}`);
const repository = inputs.repository
? inputs.repository
: process.env.GITHUB_REPOSITORY;
const repo = repository.split("/");
core.debug(`repository: ${repository}`);
const editMode = inputs.editMode ? inputs.editMode : "append";
core.debug(`editMode: ${editMode}`);
if (!["append", "replace"].includes(editMode)) {
core.setFailed(`Invalid edit-mode '${editMode}'.`);
return;
}
const octokit = new github.GitHub(inputs.token);
if (inputs.issueNumber) {
// Create a comment
if (!inputs.body) {
core.setFailed("Missing comment 'body'.");
return;
}
const { data: comment } = await octokit.issues.createComment({
owner: repo[0],
repo: repo[1],
issue_number: inputs.issueNumber,
body: inputs.body
});
core.info(`Created comment on issue '${inputs.issueNumber}'.`);
// Set a comment reaction
if (inputs.reactionType) {
await addReaction(octokit, repo, comment.id, inputs.reactionType);
}
} else if (inputs.commentId) {
// Edit a comment
if (!inputs.body && !inputs.reactionType) {
core.setFailed("Missing either comment 'body' or 'reaction-type'.");
return;
}
if (inputs.body) {
var commentBody = "";
if (editMode == "append") {
// Get the comment body
const { data: comment } = await octokit.issues.getComment({
owner: repo[0],
repo: repo[1],
comment_id: inputs.commentId
});
commentBody = comment.body + "\n";
}
commentBody = commentBody + inputs.body;
core.debug(`Comment body: ${commentBody}`);
await octokit.issues.updateComment({
owner: repo[0],
repo: repo[1],
comment_id: inputs.commentId,
body: commentBody
});
core.info(`Updated comment id '${inputs.commentId}'.`);
}
// Set a comment reaction
if (inputs.reactionType) {
await addReaction(octokit, repo, inputs.commentId, inputs.reactionType);
}
} else {
core.setFailed("Missing either 'issue-number' or 'comment-id'.");
return;
}
} catch (error) {
core.debug(inspect(error));
core.setFailed(error.message);
}
}
run();

5728
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

31
package.json Normal file
View File

@ -0,0 +1,31 @@
{
"name": "create-or-update-comment",
"version": "1.0.0",
"description": "Create or update an issue or pull request comment",
"main": "index.js",
"scripts": {
"lint": "eslint index.js",
"package": "ncc build index.js -o dist",
"test": "eslint index.js && jest --passWithNoTests"
},
"repository": {
"type": "git",
"url": "git+https://github.com/peter-evans/create-or-update-comment.git"
},
"keywords": [],
"author": "Peter Evans",
"license": "MIT",
"bugs": {
"url": "https://github.com/peter-evans/create-or-update-comment/issues"
},
"homepage": "https://github.com/peter-evans/create-or-update-comment#readme",
"dependencies": {
"@actions/core": "^1.1.1",
"@actions/github": "^1.1.0"
},
"devDependencies": {
"@zeit/ncc": "^0.20.5",
"eslint": "^6.3.0",
"jest": "^24.9.0"
}
}