From c81901f651e36cb09ede89b9d58ef34e76c11249 Mon Sep 17 00:00:00 2001 From: xrkffgg Date: Wed, 30 Dec 2020 13:48:38 +0800 Subject: [PATCH] feat: add mark-duplicate (#23) * feat: add mark-duplicate * add * add * add * add * add * add * add * change * update --- README.en-US.md | 36 +++++++++++++++++++++++++++++++- README.md | 36 +++++++++++++++++++++++++++++++- action.yml | 4 ++++ docs/log.md => changelog.md | 6 ++++++ dist/index.js | 41 +++++++++++++++++++++++++++++++++++-- docs/base.en-US.md | 31 ++++++++++++++++++++++++++++ docs/base.md | 31 ++++++++++++++++++++++++++++ docs/changelog.en-US.md | 2 +- docs/changelog.md | 2 +- package.json | 2 +- src/base.js | 34 ++++++++++++++++++++++++++++-- src/main.js | 7 +++++++ 12 files changed, 223 insertions(+), 9 deletions(-) rename docs/log.md => changelog.md (81%) diff --git a/README.en-US.md b/README.en-US.md index 872e206..1096c23 100644 --- a/README.en-US.md +++ b/README.en-US.md @@ -12,7 +12,7 @@ A GitHub Action that easily helps you automatically manage issues -[Online documentation](https://actions-cool.github.io/issues-helper) | [Changelog](https://github.com/actions-cool/issues-helper/blob/main/docs/log.md) +[Online documentation](https://actions-cool.github.io/issues-helper) | [Changelog](https://github.com/actions-cool/issues-helper/blob/main/changelog.md) ## 😎 Why use GitHub Action? @@ -34,6 +34,7 @@ When the following list does not have the features you want, you can submit it i - [`create-issue`](#create-issue) - [`delete-comment`](#delete-comment) - [`lock-issue`](#lock-issue) + - [`mark-duplicate`](#mark-duplicate) - [`open-issue`](#open-issue) - [`remove-assignees`](#remove-assignees) - [`remove-labels`](#remove-labels) @@ -294,6 +295,39 @@ jobs: ⏫ [Back to list](#List) +#### `mark-duplicate` + +Quickly mark duplicate issue. + +```yml +name: Issue Mark Duplicate + +on: + issue_comment: + types: [created, edited] + +jobs: + mark-duplicate: + runs-on: ubuntu-latest + steps: + - name: mark-duplicate + uses: actions-cool/issues-helper@v1.5 + with: + actions: 'mark-duplicate' + token: ${{ secrets.GITHUB_TOKEN }} +``` + +| Param | Desc | Type | Required | Version | +| -- | -- | -- | -- | -- | +| actions | Action type | string | ✔ | v1.5 | +| token | [Token explain](#token) | string | ✔ | v1.5 | +| duplicate-command | Operation command, default is `/d` | string | ✖ | v1.5 | +| duplicate-labels | Add additional labels to this issue | string | ✖ | v1.5 | +| labels | Replace the labels of the issue | string | ✖ | v1.5 | +| contents | Add [reaction](#reactions-types) for this comment | string | ✖ | v1.5 | + +⏫ [返回列表](#列-表) + #### `open-issue` Open the specified issue. diff --git a/README.md b/README.md index 8f10b71..4d67a96 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ 一个轻松帮你自动管理 issues 的 GitHub Action -[在线文档](https://actions-cool.github.io/issues-helper/) | [更新日志](https://github.com/actions-cool/issues-helper/blob/main/docs/log.md) +[在线文档](https://actions-cool.github.io/issues-helper/) | [更新日志](https://github.com/actions-cool/issues-helper/blob/main/changelog.md) ## 😎 为什么用 GitHub Action? @@ -34,6 +34,7 @@ - [`create-issue`](#create-issue) - [`delete-comment`](#delete-comment) - [`lock-issue`](#lock-issue) + - [`mark-duplicate`](#mark-duplicate) - [`open-issue`](#open-issue) - [`remove-assignees`](#remove-assignees) - [`remove-labels`](#remove-labels) @@ -294,6 +295,39 @@ jobs: ⏫ [返回列表](#列-表) +#### `mark-duplicate` + +快捷标记重复问题。 + +```yml +name: Issue Mark Duplicate + +on: + issue_comment: + types: [created, edited] + +jobs: + mark-duplicate: + runs-on: ubuntu-latest + steps: + - name: mark-duplicate + uses: actions-cool/issues-helper@v1.5 + with: + actions: 'mark-duplicate' + token: ${{ secrets.GITHUB_TOKEN }} +``` + +| 参数 | 描述 | 类型 | 必填 | 版本 | +| -- | -- | -- | -- | -- | +| actions | 操作类型 | string | ✔ | v1.5 | +| token | [token 说明](#token) | string | ✔ | v1.5 | +| duplicate-command | 操作命令,默认为 `/d` | string | ✖ | v1.5 | +| duplicate-labels | 为该 issue 额外增加 labels | string | ✖ | v1.5 | +| labels | 替换该 issue 的 labels | string | ✖ | v1.5 | +| contents | 为该评论的增加 [reaction](#reactions-types) | string | ✖ | v1.5 | + +⏫ [返回列表](#列-表) + #### `open-issue` 打开指定 issue。 diff --git a/action.yml b/action.yml index 43804d3..caa3064 100644 --- a/action.yml +++ b/action.yml @@ -52,6 +52,10 @@ inputs: description: 'Query use' inactive-label: description: 'Issue label set use' + duplicate-command: + description: 'For mark-duplicate' + duplicate-labels: + description: 'For mark-duplicate add labels' outputs: issue-number: description: 'Create Issue Number' diff --git a/docs/log.md b/changelog.md similarity index 81% rename from docs/log.md rename to changelog.md index 10a2ffb..eadf3f8 100644 --- a/docs/log.md +++ b/changelog.md @@ -1,3 +1,9 @@ +## v1.5 + +`2020.12.30` + +- feat: add `mark-duplicate`. [#23](https://github.com/actions-cool/issues-helper/pull/23) + ## v1.4 `2020.12.29` diff --git a/dist/index.js b/dist/index.js index 5609605..3634a65 100644 --- a/dist/index.js +++ b/dist/index.js @@ -6286,6 +6286,8 @@ const octokit = new Octokit({ auth: `token ${token}` }); const contents = core.getInput("contents"); const issueContents = core.getInput("issue-contents"); +const context = github.context; + async function doAddAssignees (owner, repo, issueNumber, assignees) { await octokit.issues.addAssignees({ owner, @@ -6400,6 +6402,32 @@ async function doLockIssue (owner, repo, issueNumber) { core.info(`Actions: [lock-issue][${issueNumber}] success!`); }; +async function doMarkDuplicate (owner, repo, labels) { + if (context.eventName != 'issue_comment') { + core.info(`This actions only support on 'issue_comment'!`); + return false; + } + const duplicateCommand = core.getInput("duplicate-command") || '/d'; + const duplicateLabels = core.getInput("duplicate-labels"); + + 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); + if (duplicateLabels) { + await doAddLabels(owner, repo, issueNumber, duplicateLabels); + } + if (labels) { + await doSetLabels(owner, repo, issueNumber, labels); + } + } else { + core.info(`This comment body should start whith 'duplicate-command'`); + } +}; + async function doOpenIssue (owner, repo, issueNumber) { await octokit.issues.update({ owner, @@ -6466,7 +6494,8 @@ async function doUpdateComment ( repo, commentId, body, - updateMode + updateMode, + ifUpdateBody, ) { const comment = await octokit.issues.getComment({ owner, @@ -6481,7 +6510,7 @@ async function doUpdateComment ( comment_id: commentId }; - if (core.getInput("body")) { + if (core.getInput("body") || ifUpdateBody) { if (updateMode === 'append') { params.body = `${comment_body}\n${body}`; } else { @@ -6617,6 +6646,7 @@ module.exports = { doCreateIssue, doCreateIssueContent, doDeleteComment, + doMarkDuplicate, doLockIssue, doOpenIssue, doRemoveAssignees, @@ -6646,6 +6676,7 @@ const { doCreateIssue, doCreateIssueContent, doDeleteComment, + doMarkDuplicate, doLockIssue, doOpenIssue, doRemoveAssignees, @@ -6674,6 +6705,7 @@ const ALLACTIONS = [ 'create-issue', 'delete-comment', 'lock-issue', + 'mark-duplicate', 'open-issue', 'remove-assignees', 'remove-labels', @@ -6715,7 +6747,9 @@ async function main() { updateMode = 'replace'; } + // actions const actions = core.getInput("actions", { required: true }); + const actionsArr = actions.split(','); actionsArr.forEach(item => { testActions(item.trim()); @@ -6753,6 +6787,9 @@ async function main() { case 'lock-issue': await doLockIssue(owner, repo, issueNumber); break; + case 'mark-duplicate': + await doMarkDuplicate(owner, repo, labels); + break; case 'open-issue': await doOpenIssue(owner, repo, issueNumber); break; diff --git a/docs/base.en-US.md b/docs/base.en-US.md index 917c6cc..4a8b096 100644 --- a/docs/base.en-US.md +++ b/docs/base.en-US.md @@ -229,6 +229,37 @@ jobs: | token | [Token explain](/en-US/guide/ref#-token) | string | ✔ | v1 | | issue-number | The number of issue | number | ✔ | v1 | +## `mark-duplicate` + +Quickly mark duplicate issue. + +```yml +name: Issue Mark Duplicate + +on: + issue_comment: + types: [created, edited] + +jobs: + mark-duplicate: + runs-on: ubuntu-latest + steps: + - name: mark-duplicate + uses: actions-cool/issues-helper@v1.5 + with: + actions: 'mark-duplicate' + token: ${{ secrets.GITHUB_TOKEN }} +``` + +| Param | Desc | Type | Required | Version | +| -- | -- | -- | -- | -- | +| actions | Action type | string | ✔ | v1.5 | +| token | [Token explain](/en-US/guide/ref#-token) | string | ✔ | v1.5 | +| duplicate-command | Operation command, default is `/d` | string | ✖ | v1.5 | +| duplicate-labels | Add additional labels to this issue | string | ✖ | v1.5 | +| labels | Replace the labels of the issue | string | ✖ | v1.5 | +| contents | Add [reaction](/en-US/guide/ref#-reactions-type) for this comment | string | ✖ | v1.5 | + ## `open-issue` Open the specified issue. diff --git a/docs/base.md b/docs/base.md index b79f3fc..af49fe4 100644 --- a/docs/base.md +++ b/docs/base.md @@ -229,6 +229,37 @@ jobs: | token | [token 说明](/guide/ref#-token-说明) | string | ✔ | v1 | | issue-number | 指定的 issue | number | ✔ | v1 | +## `mark-duplicate` + +快捷标记重复问题。 + +```yml +name: Issue Mark Duplicate + +on: + issue_comment: + types: [created, edited] + +jobs: + mark-duplicate: + runs-on: ubuntu-latest + steps: + - name: mark-duplicate + uses: actions-cool/issues-helper@v1.5 + with: + actions: 'mark-duplicate' + token: ${{ secrets.GITHUB_TOKEN }} +``` + +| 参数 | 描述 | 类型 | 必填 | 版本 | +| -- | -- | -- | -- | -- | +| actions | 操作类型 | string | ✔ | v1.5 | +| token | [token 说明](/guide/ref#-token-说明) | string | ✔ | v1.5 | +| duplicate-command | 操作命令,默认为 `/d` | string | ✖ | v1.5 | +| duplicate-labels | 为该 issue 额外增加 labels | string | ✖ | v1.5 | +| labels | 替换该 issue 的 labels | string | ✖ | v1.5 | +| contents | 为该评论的增加 [reaction](/guide/ref#-reactions-类型) | string | ✖ | v1.5 | + ## `open-issue` 打开指定 issue。 diff --git a/docs/changelog.en-US.md b/docs/changelog.en-US.md index bb046b2..a8c24a1 100644 --- a/docs/changelog.en-US.md +++ b/docs/changelog.en-US.md @@ -21,4 +21,4 @@ toc: menu uses: actions-cool/issues-helper@main ``` - + diff --git a/docs/changelog.md b/docs/changelog.md index 2af26a4..9df4545 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -21,4 +21,4 @@ toc: menu uses: actions-cool/issues-helper@main ``` - + diff --git a/package.json b/package.json index 146325d..ceb0d4f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "issue-helper", - "version": "1.4.0", + "version": "1.5.0", "private": true, "description": "Some operations on issue.", "main": "src/main.js", diff --git a/src/base.js b/src/base.js index 1df0276..094bada 100644 --- a/src/base.js +++ b/src/base.js @@ -24,6 +24,8 @@ const octokit = new Octokit({ auth: `token ${token}` }); const contents = core.getInput("contents"); const issueContents = core.getInput("issue-contents"); +const context = github.context; + async function doAddAssignees (owner, repo, issueNumber, assignees) { await octokit.issues.addAssignees({ owner, @@ -138,6 +140,32 @@ async function doLockIssue (owner, repo, issueNumber) { core.info(`Actions: [lock-issue][${issueNumber}] success!`); }; +async function doMarkDuplicate (owner, repo, labels) { + if (context.eventName != 'issue_comment') { + core.info(`This actions only support on 'issue_comment'!`); + return false; + } + const duplicateCommand = core.getInput("duplicate-command") || '/d'; + const duplicateLabels = core.getInput("duplicate-labels"); + + 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); + if (duplicateLabels) { + await doAddLabels(owner, repo, issueNumber, duplicateLabels); + } + if (labels) { + await doSetLabels(owner, repo, issueNumber, labels); + } + } else { + core.info(`This comment body should start whith 'duplicate-command'`); + } +}; + async function doOpenIssue (owner, repo, issueNumber) { await octokit.issues.update({ owner, @@ -204,7 +232,8 @@ async function doUpdateComment ( repo, commentId, body, - updateMode + updateMode, + ifUpdateBody, ) { const comment = await octokit.issues.getComment({ owner, @@ -219,7 +248,7 @@ async function doUpdateComment ( comment_id: commentId }; - if (core.getInput("body")) { + if (core.getInput("body") || ifUpdateBody) { if (updateMode === 'append') { params.body = `${comment_body}\n${body}`; } else { @@ -355,6 +384,7 @@ module.exports = { doCreateIssue, doCreateIssueContent, doDeleteComment, + doMarkDuplicate, doLockIssue, doOpenIssue, doRemoveAssignees, diff --git a/src/main.js b/src/main.js index 9d8ed18..969c64b 100644 --- a/src/main.js +++ b/src/main.js @@ -10,6 +10,7 @@ const { doCreateIssue, doCreateIssueContent, doDeleteComment, + doMarkDuplicate, doLockIssue, doOpenIssue, doRemoveAssignees, @@ -38,6 +39,7 @@ const ALLACTIONS = [ 'create-issue', 'delete-comment', 'lock-issue', + 'mark-duplicate', 'open-issue', 'remove-assignees', 'remove-labels', @@ -79,7 +81,9 @@ async function main() { updateMode = 'replace'; } + // actions const actions = core.getInput("actions", { required: true }); + const actionsArr = actions.split(','); actionsArr.forEach(item => { testActions(item.trim()); @@ -117,6 +121,9 @@ async function main() { case 'lock-issue': await doLockIssue(owner, repo, issueNumber); break; + case 'mark-duplicate': + await doMarkDuplicate(owner, repo, labels); + break; case 'open-issue': await doOpenIssue(owner, repo, issueNumber); break;