mirror of
https://gitea.com/Lydanne/issues-helper.git
synced 2025-08-19 18:25:58 +08:00
perf: optimize duplicate (#24)
* perf: optimize mark duplicate * add * add * add * add * add
This commit is contained in:
25
src/base.js
25
src/base.js
@@ -16,7 +16,7 @@ const ALLREACTIONS = [
|
||||
"eyes",
|
||||
];
|
||||
|
||||
const { dealInput } = require('./util.js');
|
||||
const { dealInput, testDuplicate } = require('./util.js');
|
||||
|
||||
const token = core.getInput('token');
|
||||
const octokit = new Octokit({ auth: `token ${token}` });
|
||||
@@ -145,22 +145,37 @@ async function doMarkDuplicate (owner, repo, labels) {
|
||||
core.info(`This actions only support on 'issue_comment'!`);
|
||||
return false;
|
||||
}
|
||||
const duplicateCommand = core.getInput("duplicate-command") || '/d';
|
||||
if (context.payload.action != 'created') {
|
||||
core.info(`This actions only support on 'issue_comment' created!`);
|
||||
return false;
|
||||
}
|
||||
|
||||
const duplicateCommand = core.getInput("duplicate-command");
|
||||
const duplicateLabels = core.getInput("duplicate-labels");
|
||||
const closeIssue = core.getInput("close-issue");
|
||||
|
||||
const commentId = context.payload.comment.id;
|
||||
const commentBody = context.payload.comment.body;
|
||||
const issueNumber = context.payload.issue.number;
|
||||
|
||||
if (commentBody.startsWith(duplicateCommand) && commentBody.split(' ')[0] == duplicateCommand) {
|
||||
const nextBody = commentBody.replace(duplicateCommand, 'Duplicate of');
|
||||
await doUpdateComment(owner, repo, commentId, nextBody, 'replace', true);
|
||||
const ifCommandInput = !!duplicateCommand;
|
||||
|
||||
if ((ifCommandInput && commentBody.startsWith(duplicateCommand) && commentBody.split(' ')[0] == duplicateCommand) || testDuplicate(commentBody)) {
|
||||
if (ifCommandInput) {
|
||||
const nextBody = commentBody.replace(duplicateCommand, 'Duplicate of');
|
||||
await doUpdateComment(owner, repo, commentId, nextBody, 'replace', true);
|
||||
} else if (contents) {
|
||||
await doCreateCommentContent(owner, repo, commentId, dealInput(contents));
|
||||
}
|
||||
if (duplicateLabels) {
|
||||
await doAddLabels(owner, repo, issueNumber, duplicateLabels);
|
||||
}
|
||||
if (labels) {
|
||||
await doSetLabels(owner, repo, issueNumber, labels);
|
||||
}
|
||||
if (closeIssue == 'true') {
|
||||
await doCloseIssue(owner, repo, issueNumber);
|
||||
}
|
||||
} else {
|
||||
core.info(`This comment body should start whith 'duplicate-command'`);
|
||||
}
|
||||
|
16
src/util.js
16
src/util.js
@@ -11,11 +11,25 @@ function dealInput (para) {
|
||||
return arr;
|
||||
};
|
||||
|
||||
function matchKeyword(content, keywords) {
|
||||
function matchKeyword (content, keywords) {
|
||||
return keywords.find(item => content.toLowerCase().includes(item));
|
||||
};
|
||||
|
||||
function testDuplicate(body) {
|
||||
if (!body || !body.startsWith('Duplicate of')) {
|
||||
return false
|
||||
}
|
||||
|
||||
let arr = body.split(' ');
|
||||
if (arr[0] == 'Duplicate' && arr[1] == 'of') {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
dealInput,
|
||||
matchKeyword,
|
||||
testDuplicate,
|
||||
};
|
||||
|
Reference in New Issue
Block a user