From 5a0b58c5e377cbc10d2bee774b608ff37c3bf627 Mon Sep 17 00:00:00 2001 From: xrkffgg Date: Tue, 22 Dec 2020 18:14:54 +0800 Subject: [PATCH] feat: add more --- .gitignore | 2 +- README.md | 205 ++++++++++++++++++++++++++++++++++++++++++------ action.yml | 2 +- src/advanced.js | 33 ++++++++ src/base.js | 50 +++++++++--- src/main.js | 45 +++++++++-- src/ultimate.js | 6 ++ 7 files changed, 302 insertions(+), 41 deletions(-) create mode 100644 src/advanced.js create mode 100644 src/ultimate.js diff --git a/.gitignore b/.gitignore index 1c5e02c..6258244 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # @source: https://github.com/xrkffgg/gitignore/blob/master/.gitignore # production -/dist +# /dist /docs-dist # Log file diff --git a/README.md b/README.md index 3ba0446..8ba4e8e 100644 --- a/README.md +++ b/README.md @@ -17,17 +17,23 @@ English | [简体中文](./README.zh-CN.md) - ⭐ 基 础 - [`add-assignees`](#add-assignees) - [`add-labels`](#add-labels) + - [`close-issue`](#close-issue) - [`create-comment`](#create-comment) - [`create-issue`](#create-issue) - [`delete-comment`](#delete-comment) - [`lock-issue`](#lock-issue) + - [`open-issue`](#open-issue) - [`remove-assignees`](#remove-assignees) - [`set-labels`](#set-labels) - [`unlock-issue`](#unlock-issue) - [`update-comment`](#update-comment) - [`update-issue`](#update-issue) - ⭐ 进 阶 + - [`find-comment`](#find-comments) - ⭐ 高 级 + - 222 +- 🌰 例 子 + - [`find-comments + create-comment + update-comment`](#find-comments--create-comment--update-comment) ## 🚀 使 用 @@ -37,14 +43,14 @@ English | [简体中文](./README.zh-CN.md) #### `add-assignees` -当一个 issue 新增时,将这个 issue 指定某人或多人。 +当一个 issue 新增或修改时,将这个 issue 指定某人或多人。 ```yml name: Add Assigness on: issues: - types: [opened] + types: [opened, edited] jobs: add-assigness: @@ -67,8 +73,8 @@ jobs: | assignees | 指定人。当不填或者为空字符、空数组时,不指定 | string \| string\[] | ✖ | v1 | - 其中的 `name` 可根据自行根据实际情况修改 -- [on 参考](#触发机制) -- `${{ github.event.issue.number }}` 表示当前 issue。[更多参考](https://docs.github.com/en/free-pro-team@latest/developers/webhooks-and-events)。 +- [on 参考](#github-docs) +- `${{ github.event.issue.number }}` 表示当前 issue,[更多参考](https://docs.github.com/en/free-pro-team@latest/developers/webhooks-and-events)。 ⏫ [返回列表](#列-表) @@ -106,6 +112,29 @@ jobs: ⏫ [返回列表](#列-表) +#### `close-issue` + +关闭指定 issue。当输入 `body` 时,会同时进行评论。 + +```yml +- name: Close issue + uses: actions-cool/issue-helper@v1 + with: + actions: 'close-issue' + token: ${{ secrets.GITHUB_TOKEN }} + issue-number: xxx + body: 'This is auto closed.' +``` + +| 参数 | 描述 | 类型 | 必填 | 版本 | +| -- | -- | -- | -- | -- | +| actions | actions 类型,当为数组时,会进行多个操作 | string \| string\[] | ✔ | v1 | +| token | [token 说明](#token) | string | ✖ | v1 | +| issue-number | 指定的 issue | number | ✔ | v1 | +| body | 关闭 issue 时,可进行评论 | string | ✖ | v1 | + +⏫ [返回列表](#列-表) + #### `create-comment` 当新增一个指定 label 时,对该 issue 进行评论。 @@ -145,8 +174,8 @@ jobs: - `body` 默认为:`Currently at ${owner}/${repo}. And this is default comment.` - 其中 `${owner}/${repo}` 表示当前仓库 -- 返回 `comment-id`,可用于之后操作。用法参考 -- `${{ github.event.issue.user.login }}` 表示该 issue 的创建者。 +- 返回 `comment-id`,可用于之后操作。[用法参考](#输出使用) +- `${{ github.event.issue.user.login }}` 表示该 issue 的创建者 ⏫ [返回列表](#列-表) @@ -188,14 +217,14 @@ jobs: | contents | 为新增 issue 增加 [reaction](#reactions-types) | string \| string\[] | ✖ | v1 | - `title` 默认为:`Default Title` -- `body` 默认同上 -- 返回 `issue_number`,用法同上 +- `body` 默认值同上 +- 返回 `issue-number`,[用法参考](#输出使用) ⏫ [返回列表](#列-表) #### `delete-comment` -根据 [`comment_id`](#comment_id-获取) 删除指定评论。单个应用场景不多,可参考高级用法。 +根据 [`comment_id`](#comment_id-获取) 删除指定评论。 ```yml - name: Delete comment @@ -246,6 +275,28 @@ jobs: ⏫ [返回列表](#列-表) +#### `open-issue` + +打开指定 issue。 + +```yml +- name: Open issue + uses: actions-cool/issue-helper@v1 + with: + actions: 'open-issue' + token: ${{ secrets.GITHUB_TOKEN }} + issue-number: xxx +``` + +| 参数 | 描述 | 类型 | 必填 | 版本 | +| -- | -- | -- | -- | -- | +| actions | actions 类型,当为数组时,会进行多个操作 | string \| string\[] | ✔ | v1 | +| token | [token 说明](#token) | string | ✖ | v1 | +| issue-number | 指定的 issue | number | ✔ | v1 | +| body | 打开 issue 时,可进行评论 | string | ✖ | v1 | + +⏫ [返回列表](#列-表) + #### `remove-assignees` 移除 issue 指定人员。 @@ -315,18 +366,28 @@ jobs: #### `update-comment` -根据 [`comment_id`](#comment_id-获取) 更新指定评论。单个应用场景不多,可参考高级用法。 +根据 [`comment_id`](#comment_id-获取) 更新指定评论。 + +下面的例子展示的是,为每个新增的 comment 增加 👀 。 ```yml -- name: Update comment - uses: actions-cool/issue-helper@v1 - with: - actions: 'update-comment' - token: ${{ secrets.GITHUB_TOKEN }} - comment-id: xxx - body: 'xxxx' - update-mode: 'replace' - contents: '+1' +name: Add eyes to each comment + +on: + issue_comment: + types: [created] + +jobs: + update-comment: + runs-on: ubuntu-latest + steps: + - name: Update comment + uses: actions-cool/issue-helper@v1 + with: + actions: 'update-comment' + token: ${{ secrets.GITHUB_TOKEN }} + comment-id: ${{ github.event.comment.id }} + contents: 'eyes' ``` | 参数 | 描述 | 类型 | 必填 | 版本 | @@ -338,7 +399,7 @@ jobs: | update-mode | 更新模式。`replace` 替换,`append` 附加 | string | ✖ | v1 | | contents | 为 comment 增加 [reaction](#reactions-types) | string \| string\[] | ✖ | v1 | -- `body` 默认同上 +- `body` 不输入时,会保持原有 - `update-mode` 为 `append` 时,会进行附加操作。非 `append` 都会进行替换。仅对 `body` 生效。 ⏫ [返回列表](#列-表) @@ -382,8 +443,93 @@ jobs: ### ⭐ 进 阶 +#### `find-comments` + +查找当前仓库 1 号 issue 中,创建者是 k ,内容包含 `this` 的评论列表。 + +```yml +- name: Find comments + uses: actions-cool/issue-helper@v1 + with: + actions: 'find-comments' + token: ${{ secrets.GITHUB_TOKEN }} + issue-number: 1 + comment-auth: k + body-includes: 'this' +``` + +| 参数 | 描述 | 类型 | 必填 | 版本 | +| -- | -- | -- | -- | -- | +| actions | actions 类型,当为数组时,会进行多个操作 | string \| string\[] | ✔ | v1 | +| token | [token 说明](#token) | string | ✖ | v1 | +| issue-number | 指定的 issue | number | ✔ | v1 | +| comment-auth | 评论创建者,不填时会查询所有 | string | ✖ | v1 | +| body-includes | 评论内容包含过滤,不填时无校验 | string | ✖ | v1 | + +- 返回 `comments`, 格式如下: + +```js +[ + {id: 1, body: 'xxx'}, + {id: 2, body: 'xxxx'} +] +``` + +⏫ [返回列表](#列-表) + ### ⭐ 高 级 +## 🌰 例 子 + +以下列举一些例子,请灵活参考。 + +### `find-comments + create-comment + update-comment` + +假设场景:当 issue 修改时,查找是否有 k 创建的包含 `error` 的评论,如果只有一个,则更新该 comment,如果没有,则新增一个 comment。 + +```yml +name: Test + +on: + isssue: + types: [edited] + +jobs: + do-test: + runs-on: ubuntu-latest + steps: + - name: find comments + uses: actions-cool/issue-helper@v1 + id: fcid + with: + actions: 'find-comments' + token: ${{ secrets.GITHUB_TOKEN }} + issue-number: ${{ github.event.issue.number }} + comment-auth: k + body-includes: 'error' + + - name: create comment + if: ${{ steps.fcid.outputs.comments.length == 0 }} + uses: actions-cool/issue-helper@v1 + with: + actions: 'create-comment' + token: ${{ secrets.GITHUB_TOKEN }} + issue-number: ${{ github.event.issue.number }} + body: 'Some error!' + + - name: update comment + if: ${{ steps.fcid.outputs.comments.length == 1 }} + uses: actions-cool/issue-helper@v1 + with: + actions: 'update-comment' + token: ${{ secrets.GITHUB_TOKEN }} + comment-id: ${{ steps.fcid.outputs.comments[0].id }} + body: 'Some error again!' + update-mode: 'append' +``` + +⏫ [返回列表](#列-表) + ## 🎁 参 考 ### token @@ -401,12 +547,21 @@ jobs: ⏫ [返回列表](#列-表) -### 条件判断 +### 输出使用 +```yml +- name: Create issue + uses: actions-cool/issue-helper@v1 + id: createissue + with: + actions: 'create-issue' + token: ${{ secrets.GITHUB_TOKEN }} +- name: Check outputs + run: echo "Outputs issue_number is ${{ steps.createissue.outputs.issue-number }}" +``` +### GitHub Docs -### 触发机制 - -- [Workflow syntax for GitHub Actions](https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#on) -- [Events that trigger workflows](https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows) +- [GitHub Actions 语法](https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#on) +- [工作流触发机制](https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows) ⏫ [返回列表](#列-表) diff --git a/action.yml b/action.yml index 9b04904..901f7a5 100644 --- a/action.yml +++ b/action.yml @@ -9,7 +9,7 @@ inputs: description: 'github_token' default: ${{ github.token }} outputs: - issue_number: + issue-number: description: 'Create Issue Number' comment-id: description: 'Create comment ID' diff --git a/src/advanced.js b/src/advanced.js new file mode 100644 index 0000000..c0cd3c3 --- /dev/null +++ b/src/advanced.js @@ -0,0 +1,33 @@ +require('dotenv').config(); +const core = require("@actions/core"); +const { Octokit } = require('@octokit/rest'); + +const token = core.getInput('token') || process.env.GH_TOKEN; +const octokit = new Octokit({ auth: `token ${token}` }); + +const commentAuth = core.getInput("comment-auth") || 'xrkffgg'; +const bodyIncludes = core.getInput('body-includes') || 'is'; + +async function doFindComments (owner, repo, issueNumber) { + const res = await octokit.issues.listComments({ + owner, + repo, + issue_number: issueNumber + }); + core.info(`Actions: [find-comments][${issueNumber}] success!`); + let comments = []; + res.data.forEach(item => { + if ((commentAuth ? item.user.login === commentAuth : true) && (bodyIncludes ? item.body.includes(bodyIncludes) : true)) { + comments.push({ + id: item.id, + body: item.body + }) + } + }) + core.setOutput("comments", comments); +}; + + +module.exports = { + doFindComments, +}; diff --git a/src/base.js b/src/base.js index 6496889..8202092 100644 --- a/src/base.js +++ b/src/base.js @@ -38,6 +38,20 @@ async function doAddLabels (owner, repo, issueNumber, labels) { core.info(`Actions: [add-labels][${labels}] success!`); }; +async function doCloseIssue (owner, repo, issueNumber) { + if (core.getInput("body")) { + await doCreateComment(owner, repo, issueNumber, core.getInput("body")) + } + + await octokit.issues.update({ + owner, + repo, + issue_number: issueNumber, + state: 'closed' + }); + core.info(`Actions: [close-issue][${issueNumber}] success!`); +}; + async function doCreateComment (owner, repo, issueNumber, body) { const { data } = await octokit.issues.createComment({ owner, @@ -92,7 +106,7 @@ async function doCreateIssue (owner, repo, title, body, labels, assignees) { } const { data } = await octokit.issues.create(params); core.info(`Actions: [create-issue][${title}] success!`); - core.setOutput("issue_number", data.number); + core.setOutput("issue-number", data.number); if (contents) { await doCreateIssueContent(owner, repo, data.number, contents); @@ -141,6 +155,20 @@ async function doLockIssue (owner, repo, issueNumber) { core.info(`Actions: [lock-issue][${issueNumber}] success!`); }; +async function doOpenIssue (owner, repo, issueNumber) { + if (core.getInput("body")) { + await doCreateComment(owner, repo, issueNumber, core.getInput("body")) + } + + await octokit.issues.update({ + owner, + repo, + issue_number: issueNumber, + state: 'open' + }); + core.info(`Actions: [open-issue][${issueNumber}] success!`); +}; + async function doRemoveAssignees (owner, repo, issueNumber, assignees) { await octokit.issues.removeAssignees({ owner, @@ -190,14 +218,16 @@ async function doUpdateComment ( comment_id: commentId }; - if (updateMode === 'append') { - params.body = `${comment_body}\n${body}`; - } else { - params.body = body; - } + if (core.getInput("body")) { + if (updateMode === 'append') { + params.body = `${comment_body}\n${body}`; + } else { + params.body = body; + } - await octokit.issues.updateComment(params); - core.info(`Actions: [update-comment][${commentId}] success!`); + await octokit.issues.updateComment(params); + core.info(`Actions: [update-comment][${commentId}] success!`); + } if (contents) { await doCreateCommentContent(owner, repo, commentId, contents); @@ -280,15 +310,17 @@ function testContent(con) { module.exports = { doAddAssignees, doAddLabels, + doCloseIssue, doCreateComment, doCreateCommentContent, doCreateIssue, doCreateIssueContent, doDeleteComment, doLockIssue, + doOpenIssue, doRemoveAssignees, doSetLabels, doUnlockIssue, + doUpdateComment, doUpdateIssue, - doUpdateComment }; diff --git a/src/main.js b/src/main.js index 392753a..087297d 100644 --- a/src/main.js +++ b/src/main.js @@ -4,38 +4,53 @@ const github = require("@actions/github"); const { doAddAssignees, doAddLabels, + doCloseIssue, doCreateComment, doCreateCommentContent, doCreateIssue, doCreateIssueContent, doDeleteComment, doLockIssue, + doOpenIssue, doRemoveAssignees, doSetLabels, doUnlockIssue, + doUpdateComment, doUpdateIssue, - doUpdateComment } = require('./base.js'); +const { + doFindComments, +} = require('./advanced.js'); + const ALLACTIONS = [ + // base 'add-assignees', 'add-labels', + 'close-issue', 'create-comment', 'create-issue', 'delete-comment', 'lock-issue', + 'open-issue', 'remove-assignees', 'set-labels', 'unlock-issue', 'update-comment', 'update-issue', + + // advanced + 'find-comments', ]; async function main() { try { - const owner = github.context.repo.owner; - const repo = github.context.repo.repo; - const issueNumber = core.getInput('issue-number'); + // const owner = github.context.repo.owner; + // const repo = github.context.repo.repo; + const owner = 'actions-cool'; + const repo = 'issue-helper'; + + const issueNumber = core.getInput('issue-number') || 1; const commentId = core.getInput('comment-id'); const defaultBody = `Currently at ${owner}/${repo}. And this is default comment.` @@ -54,7 +69,8 @@ async function main() { updateMode = 'replace'; } - const actions = core.getInput("actions", { required: true }); + // const actions = core.getInput("actions", { required: true }); + const actions = 'find-comments'; if (typeof(actions) === 'object') { actions.forEach(item => { @@ -74,12 +90,16 @@ async function main() { async function choseActions(action) { switch (action) { + // base case 'add-assignees': await doAddAssignees(owner, repo, issueNumber, assignees); break; case 'add-labels': await doAddLabels(owner, repo, issueNumber, labels); break; + case 'close-issue': + await doCloseIssue(owner, repo, issueNumber); + break; case 'create-comment': await doCreateComment(owner, repo, issueNumber, body); break; @@ -92,6 +112,9 @@ async function main() { case 'lock-issue': await doLockIssue(owner, repo, issueNumber); break; + case 'open-issue': + await doOpenIssue(owner, repo, issueNumber); + break; case 'remove-assignees': await doRemoveAssignees(owner, repo, issueNumber, assignees); break; @@ -123,6 +146,18 @@ async function main() { labels ); break; + // advanced + case 'find-comments': + await doFindComments( + owner, + repo, + issueNumber + ); + break; + + // ultimate + + // default default: break; } diff --git a/src/ultimate.js b/src/ultimate.js new file mode 100644 index 0000000..5220bc3 --- /dev/null +++ b/src/ultimate.js @@ -0,0 +1,6 @@ +require('dotenv').config(); +const core = require("@actions/core"); +const { Octokit } = require('@octokit/rest'); + +const token = core.getInput('token') || process.env.GH_TOKEN; +const octokit = new Octokit({ auth: `token ${token}` });