Skip to content

Commit da1f8da

Browse files
authored
perf: optimize duplicate (#24)
* perf: optimize mark duplicate * add * add * add * add * add
1 parent 1cf278b commit da1f8da

File tree

10 files changed

+114
-26
lines changed

10 files changed

+114
-26
lines changed

README.en-US.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,14 @@ jobs:
297297

298298
#### `mark-duplicate`
299299

300-
Quickly mark duplicate issue.
300+
Quickly mark duplicate questions, only for issue new comments.
301301

302302
```yml
303303
name: Issue Mark Duplicate
304304
305305
on:
306306
issue_comment:
307-
types: [created, edited]
307+
types: [created]
308308
309309
jobs:
310310
mark-duplicate:
@@ -321,12 +321,16 @@ jobs:
321321
| -- | -- | -- | -- | -- |
322322
| actions | Action type | string | ✔ | v1.5 |
323323
| token | [Token explain](#token) | string | ✔ | v1.5 |
324-
| duplicate-command | Operation command, default is `/d` | string | ✖ | v1.5 |
324+
| duplicate-command | Simple commands can be set, such as: `/d` | string | ✖ | v1.6 |
325325
| duplicate-labels | Add additional labels to this issue | string | ✖ | v1.5 |
326326
| labels | Replace the labels of the issue | string | ✖ | v1.5 |
327327
| contents | Add [reaction](#reactions-types) for this comment | string | ✖ | v1.5 |
328+
| close-issue | Whether to close the issue at the same time | string | ✖ | v1.6 |
329+
330+
- `duplicate-command`: When setting concise commands, while still supporting the original `Duplicate of`
331+
- `close-issue`: Both `true` or `'true'` can take effect
328332

329-
⏫ [返回列表](#列-表)
333+
⏫ [Back to list](#List)
330334

331335
#### `open-issue`
332336

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,14 @@ jobs:
297297

298298
#### `mark-duplicate`
299299

300-
快捷标记重复问题。
300+
快捷标记重复问题,仅作用于 issue 新增评论
301301

302302
```yml
303303
name: Issue Mark Duplicate
304304
305305
on:
306306
issue_comment:
307-
types: [created, edited]
307+
types: [created]
308308
309309
jobs:
310310
mark-duplicate:
@@ -321,10 +321,14 @@ jobs:
321321
| -- | -- | -- | -- | -- |
322322
| actions | 操作类型 | string | ✔ | v1.5 |
323323
| token | [token 说明](#token) | string | ✔ | v1.5 |
324-
| duplicate-command | 操作命令,默认为 `/d` | string | ✖ | v1.5 |
324+
| duplicate-command | 可设置简洁命令,如:`/d` | string | ✖ | v1.6 |
325325
| duplicate-labels | 为该 issue 额外增加 labels | string | ✖ | v1.5 |
326326
| labels | 替换该 issue 的 labels | string | ✖ | v1.5 |
327327
| contents | 为该评论的增加 [reaction](#reactions-types) | string | ✖ | v1.5 |
328+
| close-issue | 是否同时关闭该 issue | string | ✖ | v1.6 |
329+
330+
- `duplicate-command`:当设置简洁命令时,同时仍支持原有 `Duplicate of`
331+
- `close-issue`:`true` 或 `'true'` 均可生效
328332

329333
⏫ [返回列表](#列-表)
330334

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ inputs:
5656
description: 'For mark-duplicate'
5757
duplicate-labels:
5858
description: 'For mark-duplicate add labels'
59+
close-issue:
60+
description: 'For mark-duplicate'
5961
outputs:
6062
issue-number:
6163
description: 'Create Issue Number'

dist/index.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6278,7 +6278,7 @@ const ALLREACTIONS = [
62786278
"eyes",
62796279
];
62806280

6281-
const { dealInput } = __webpack_require__(6254);
6281+
const { dealInput, testDuplicate } = __webpack_require__(6254);
62826282

62836283
const token = core.getInput('token');
62846284
const octokit = new Octokit({ auth: `token ${token}` });
@@ -6407,22 +6407,37 @@ async function doMarkDuplicate (owner, repo, labels) {
64076407
core.info(`This actions only support on 'issue_comment'!`);
64086408
return false;
64096409
}
6410-
const duplicateCommand = core.getInput("duplicate-command") || '/d';
6410+
if (context.payload.action != 'created') {
6411+
core.info(`This actions only support on 'issue_comment' created!`);
6412+
return false;
6413+
}
6414+
6415+
const duplicateCommand = core.getInput("duplicate-command");
64116416
const duplicateLabels = core.getInput("duplicate-labels");
6417+
const closeIssue = core.getInput("close-issue");
64126418

64136419
const commentId = context.payload.comment.id;
64146420
const commentBody = context.payload.comment.body;
64156421
const issueNumber = context.payload.issue.number;
64166422

6417-
if (commentBody.startsWith(duplicateCommand) && commentBody.split(' ')[0] == duplicateCommand) {
6418-
const nextBody = commentBody.replace(duplicateCommand, 'Duplicate of');
6419-
await doUpdateComment(owner, repo, commentId, nextBody, 'replace', true);
6423+
const ifCommandInput = !!duplicateCommand;
6424+
6425+
if ((ifCommandInput && commentBody.startsWith(duplicateCommand) && commentBody.split(' ')[0] == duplicateCommand) || testDuplicate(commentBody)) {
6426+
if (ifCommandInput) {
6427+
const nextBody = commentBody.replace(duplicateCommand, 'Duplicate of');
6428+
await doUpdateComment(owner, repo, commentId, nextBody, 'replace', true);
6429+
} else if (contents) {
6430+
await doCreateCommentContent(owner, repo, commentId, dealInput(contents));
6431+
}
64206432
if (duplicateLabels) {
64216433
await doAddLabels(owner, repo, issueNumber, duplicateLabels);
64226434
}
64236435
if (labels) {
64246436
await doSetLabels(owner, repo, issueNumber, labels);
64256437
}
6438+
if (closeIssue == 'true') {
6439+
await doCloseIssue(owner, repo, issueNumber);
6440+
}
64266441
} else {
64276442
core.info(`This comment body should start whith 'duplicate-command'`);
64286443
}
@@ -6905,13 +6920,27 @@ function dealInput (para) {
69056920
return arr;
69066921
};
69076922

6908-
function matchKeyword(content, keywords) {
6923+
function matchKeyword (content, keywords) {
69096924
return keywords.find(item => content.toLowerCase().includes(item));
69106925
};
69116926

6927+
function testDuplicate(body) {
6928+
if (!body || !body.startsWith('Duplicate of')) {
6929+
return false
6930+
}
6931+
6932+
let arr = body.split(' ');
6933+
if (arr[0] == 'Duplicate' && arr[1] == 'of') {
6934+
return true;
6935+
} else {
6936+
return false;
6937+
}
6938+
};
6939+
69126940
module.exports = {
69136941
dealInput,
69146942
matchKeyword,
6943+
testDuplicate,
69156944
};
69166945

69176946

docs/base.en-US.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,14 @@ jobs:
231231

232232
## `mark-duplicate`
233233

234-
Quickly mark duplicate issue.
234+
Quickly mark duplicate questions, only for issue new comments.
235235

236236
```yml
237237
name: Issue Mark Duplicate
238238
239239
on:
240240
issue_comment:
241-
types: [created, edited]
241+
types: [created]
242242
243243
jobs:
244244
mark-duplicate:
@@ -255,10 +255,20 @@ jobs:
255255
| -- | -- | -- | -- | -- |
256256
| actions | Action type | string | ✔ | v1.5 |
257257
| token | [Token explain](/en-US/guide/ref#-token) | string | ✔ | v1.5 |
258-
| duplicate-command | Operation command, default is `/d` | string | ✖ | v1.5 |
258+
| duplicate-command | Simple commands can be set, such as: `/d` | string | ✖ | v1.6 |
259259
| duplicate-labels | Add additional labels to this issue | string | ✖ | v1.5 |
260260
| labels | Replace the labels of the issue | string | ✖ | v1.5 |
261261
| contents | Add [reaction](/en-US/guide/ref#-reactions-type) for this comment | string | ✖ | v1.5 |
262+
| close-issue | Whether to close the issue at the same time | string | ✖ | v1.6 |
263+
264+
- `duplicate-command`: When setting concise commands, while still supporting the original `Duplicate of`
265+
- `close-issue`: Both `true` or `'true'` can take effect
266+
267+
<Alert>
268+
Note: Duplicate created with the concise command does not display the content of the red box in the figure below. But in fact this has no effect.
269+
</Alert>
270+
271+
![](../public/duplicate.png)
262272

263273
## `open-issue`
264274

docs/base.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,14 @@ jobs:
231231

232232
## `mark-duplicate`
233233

234-
快捷标记重复问题。
234+
快捷标记重复问题,仅作用于 issue 新增评论
235235

236236
```yml
237237
name: Issue Mark Duplicate
238238
239239
on:
240240
issue_comment:
241-
types: [created, edited]
241+
types: [created]
242242
243243
jobs:
244244
mark-duplicate:
@@ -255,10 +255,20 @@ jobs:
255255
| -- | -- | -- | -- | -- |
256256
| actions | 操作类型 | string | ✔ | v1.5 |
257257
| token | [token 说明](/guide/ref#-token-说明) | string | ✔ | v1.5 |
258-
| duplicate-command | 操作命令,默认为 `/d` | string | ✖ | v1.5 |
258+
| duplicate-command | 可设置简洁命令,如:`/d` | string | ✖ | v1.6 |
259259
| duplicate-labels | 为该 issue 额外增加 labels | string | ✖ | v1.5 |
260260
| labels | 替换该 issue 的 labels | string | ✖ | v1.5 |
261261
| contents | 为该评论的增加 [reaction](/guide/ref#-reactions-类型) | string | ✖ | v1.5 |
262+
| close-issue | 是否同时关闭该 issue | string | ✖ | v1.6 |
263+
264+
- `duplicate-command`:当设置简洁命令时,同时仍支持原有 `Duplicate of`
265+
- `close-issue`:`true` 或 `'true'` 均可生效
266+
267+
<Alert>
268+
注意:使用简洁命令创建的 Duplicate 不显示下图红框内容。但其实这个没有任何影响的。
269+
</Alert>
270+
271+
![](../public/duplicate.png)
262272

263273
## `open-issue`
264274

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "issue-helper",
3-
"version": "1.5.0",
3+
"version": "1.6.0",
44
"private": true,
55
"description": "Some operations on issue.",
66
"main": "src/main.js",

public/duplicate.png

53.6 KB
Loading

src/base.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const ALLREACTIONS = [
1616
"eyes",
1717
];
1818

19-
const { dealInput } = require('./util.js');
19+
const { dealInput, testDuplicate } = require('./util.js');
2020

2121
const token = core.getInput('token');
2222
const octokit = new Octokit({ auth: `token ${token}` });
@@ -145,22 +145,37 @@ async function doMarkDuplicate (owner, repo, labels) {
145145
core.info(`This actions only support on 'issue_comment'!`);
146146
return false;
147147
}
148-
const duplicateCommand = core.getInput("duplicate-command") || '/d';
148+
if (context.payload.action != 'created') {
149+
core.info(`This actions only support on 'issue_comment' created!`);
150+
return false;
151+
}
152+
153+
const duplicateCommand = core.getInput("duplicate-command");
149154
const duplicateLabels = core.getInput("duplicate-labels");
155+
const closeIssue = core.getInput("close-issue");
150156

151157
const commentId = context.payload.comment.id;
152158
const commentBody = context.payload.comment.body;
153159
const issueNumber = context.payload.issue.number;
154160

155-
if (commentBody.startsWith(duplicateCommand) && commentBody.split(' ')[0] == duplicateCommand) {
156-
const nextBody = commentBody.replace(duplicateCommand, 'Duplicate of');
157-
await doUpdateComment(owner, repo, commentId, nextBody, 'replace', true);
161+
const ifCommandInput = !!duplicateCommand;
162+
163+
if ((ifCommandInput && commentBody.startsWith(duplicateCommand) && commentBody.split(' ')[0] == duplicateCommand) || testDuplicate(commentBody)) {
164+
if (ifCommandInput) {
165+
const nextBody = commentBody.replace(duplicateCommand, 'Duplicate of');
166+
await doUpdateComment(owner, repo, commentId, nextBody, 'replace', true);
167+
} else if (contents) {
168+
await doCreateCommentContent(owner, repo, commentId, dealInput(contents));
169+
}
158170
if (duplicateLabels) {
159171
await doAddLabels(owner, repo, issueNumber, duplicateLabels);
160172
}
161173
if (labels) {
162174
await doSetLabels(owner, repo, issueNumber, labels);
163175
}
176+
if (closeIssue == 'true') {
177+
await doCloseIssue(owner, repo, issueNumber);
178+
}
164179
} else {
165180
core.info(`This comment body should start whith 'duplicate-command'`);
166181
}

src/util.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,25 @@ function dealInput (para) {
1111
return arr;
1212
};
1313

14-
function matchKeyword(content, keywords) {
14+
function matchKeyword (content, keywords) {
1515
return keywords.find(item => content.toLowerCase().includes(item));
1616
};
1717

18+
function testDuplicate(body) {
19+
if (!body || !body.startsWith('Duplicate of')) {
20+
return false
21+
}
22+
23+
let arr = body.split(' ');
24+
if (arr[0] == 'Duplicate' && arr[1] == 'of') {
25+
return true;
26+
} else {
27+
return false;
28+
}
29+
};
30+
1831
module.exports = {
1932
dealInput,
2033
matchKeyword,
34+
testDuplicate,
2135
};

0 commit comments

Comments
 (0)