├── .prettierignore ├── .prettierrc.js ├── scripts ├── pub.sh ├── check-commit.js ├── release.js └── tag.js ├── CONTRIBUTING.md ├── .github └── workflows │ └── test.yml ├── .gitignore ├── package.json ├── action.yml ├── LICENSE ├── CHANGELOG.md ├── README.md └── src └── main.js /.prettierignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | lib/ 3 | docs-dist/ 4 | node_modules/ 5 | -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | const fabric = require('@umijs/fabric'); 2 | 3 | module.exports = { 4 | ...fabric.prettier, 5 | arrowParens: 'avoid', 6 | }; 7 | -------------------------------------------------------------------------------- /scripts/pub.sh: -------------------------------------------------------------------------------- 1 | echo "[TEST] check format" 2 | npm run format-check 3 | 4 | echo "[TEST] test package" 5 | npm run package 6 | 7 | echo "[TEST] test commit" 8 | npm run check-commit 9 | 10 | echo "[Action] do tag" 11 | npm run tag 12 | 13 | echo "[Action] do release" 14 | npm run release 15 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | ## Development 4 | 5 | ## Publish 6 | 7 | - Add new release changelog 8 | - Commit to main 9 | - run below 10 | 11 | ```sh 12 | npm run pub 13 | # Waiting ... 14 | # - When open a new url 15 | # - Check tag and changelog content 16 | # - Click Publish 17 | ``` 18 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | setup: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - name: checkout 10 | uses: actions/checkout@main 11 | 12 | - name: install 13 | run: yarn install 14 | 15 | - name: format-check 16 | run: yarn format-check 17 | 18 | - name: package 19 | run: yarn package 20 | -------------------------------------------------------------------------------- /scripts/check-commit.js: -------------------------------------------------------------------------------- 1 | const chalk = require('chalk'); 2 | const simpleGit = require('simple-git/promise'); 3 | 4 | const cwd = process.cwd(); 5 | const git = simpleGit(cwd); 6 | 7 | async function checkCommit({ files }) { 8 | if (files.length) { 9 | console.log(chalk.yellow('🙄 You forgot something to commit.')); 10 | files.forEach(({ path: filePath }) => { 11 | console.log(' -', chalk.red(filePath)); 12 | }); 13 | console.log(''); 14 | process.exit(1); 15 | } 16 | } 17 | 18 | async function run() { 19 | const status = await git.status(); 20 | await checkCommit(status); 21 | } 22 | 23 | run(); 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # @source: https://github.com/xrkffgg/gitignore/blob/master/.gitignore 2 | 3 | # production 4 | # /dist 5 | /docs-dist 6 | /lib 7 | 8 | # Log file 9 | *.log 10 | npm-debug.log* 11 | yarn-debug.log* 12 | yarn-error.log* 13 | 14 | # Private 15 | 16 | # misc 17 | .DS_Store 18 | 19 | # dependencies 20 | node_modules 21 | yarn.lock 22 | package-lock.json 23 | 24 | # local env files 25 | .env.local 26 | .env.*.local 27 | 28 | # Compiled file 29 | *.class 30 | *.css.map 31 | *.sass.map 32 | *.scss.map 33 | 34 | # Editor directories and files 35 | .idea 36 | .vscode 37 | *.suo 38 | *.ntvs* 39 | *.njsproj 40 | *.sln 41 | *.sw? 42 | ~$*.* 43 | 44 | # umi 45 | .umi 46 | .umi-production 47 | .umi-test 48 | .env.local 49 | 50 | # cache 51 | .sass-cache/ 52 | 53 | # test 54 | coverage 55 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "description": "Maintain just one comment in Issue and PR.", 4 | "repository": { 5 | "type": "git", 6 | "url": "https://github.com/actions-cool/maintain-one-comment", 7 | "branch": "main" 8 | }, 9 | "license": "MIT", 10 | "author": "xrkffgg", 11 | "main": "src/main.js", 12 | "scripts": { 13 | "check-commit": "node ./scripts/check-commit.js", 14 | "package": "ncc build", 15 | "format": "prettier --write src/*.js", 16 | "format-check": "prettier --check src/*.js", 17 | "tag": "node ./scripts/tag.js", 18 | "release": "node ./scripts/release", 19 | "pub": "sh -e ./scripts/pub.sh" 20 | }, 21 | "dependencies": { 22 | "@actions/core": "^1.10.0", 23 | "@actions/github": "^5.1.1", 24 | "@octokit/rest": "^19.0.13", 25 | "actions-util": "^1.1.4" 26 | }, 27 | "devDependencies": { 28 | "@umijs/fabric": "^2.5.6", 29 | "@vercel/ncc": "^0.36.1", 30 | "chalk": "^4.1.2", 31 | "new-github-release-url": "^1.0.0", 32 | "open": "^7.3.0", 33 | "prettier": "^2.2.1", 34 | "simple-git": "^2.46.0" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | # https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions 2 | name: 'Maintain One Comment' 3 | description: 'Maintain just one comment in Issue and PR.' 4 | author: 'xrkffgg' 5 | 6 | branding: 7 | # https://actions-cool.github.io/github-action-branding/ 8 | icon: 'message-circle' 9 | color: 'green' 10 | 11 | inputs: 12 | token: 13 | description: Secret GitHub API token to use for making API requests. 14 | default: ${{ github.token }} 15 | required: true 16 | body: 17 | description: Comment content. 18 | emojis: 19 | description: Add emojis to comment. 20 | number: 21 | description: Manually control the issue or PR number 22 | update-mode: 23 | description: Update comment mode. Default replace. Option append. 24 | comment-auth: 25 | description: Comment auth filter. 26 | body-include: 27 | description: Comment body filter. 28 | delete: 29 | description: Will delete all comments 30 | 31 | outputs: 32 | comment-id: 33 | description: 'Create or update comment ID' 34 | 35 | runs: 36 | using: 'node20' 37 | main: 'dist/index.js' 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021-present xrkffgg 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /scripts/release.js: -------------------------------------------------------------------------------- 1 | const chalk = require('chalk'); 2 | const open = require('open'); 3 | const newGithubReleaseUrl = require('new-github-release-url'); 4 | const { readFileSync } = require('fs'); 5 | const path = require('path'); 6 | 7 | let tag = ''; 8 | 9 | const CHANGELOG_NAME = 'CHANGELOG.md'; 10 | const user = 'actions-cool'; 11 | const repo = 'maintain-one-comment'; 12 | 13 | function getChangelog(content) { 14 | const lines = content.split('\n'); 15 | const changeLog = []; 16 | const pin = /^## /; 17 | let begin = false; 18 | for (let i = 0; i < lines.length; i += 1) { 19 | const line = lines[i]; 20 | if (begin && pin.test(line)) { 21 | break; 22 | } 23 | if (begin && line) { 24 | changeLog.push(line); 25 | } 26 | if (!begin) { 27 | begin = pin.test(line); 28 | if (begin) { 29 | tag = line.substring(3, line.length).trim(); 30 | } 31 | } 32 | } 33 | return changeLog.join('\n'); 34 | } 35 | 36 | const changelogPath = path.join(__dirname, '..', CHANGELOG_NAME); 37 | const changelog = readFileSync(changelogPath, 'utf-8'); 38 | 39 | const body = getChangelog(changelog); 40 | 41 | async function run() { 42 | const url = newGithubReleaseUrl({ 43 | user, 44 | repo, 45 | tag, 46 | body: body, 47 | }); 48 | 49 | await open(url); 50 | 51 | console.log(chalk.yellow('🚀 Please check tag and changelog. Then click publish!')); 52 | }; 53 | 54 | run(); 55 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 10 | 11 | # Changelog 12 | 13 | ## v3.2.0 14 | 15 | `2024.05.22` 16 | 17 | - 🚀 feat: get pr number for workflow_run & up Node 20. [#9](https://github.com/actions-cool/maintain-one-comment/pull/9) [@sxzz](https://github.com/sxzz) 18 | 19 | ## v3.1.1 20 | 21 | `2023.09.01` 22 | 23 | - 🤖 chore: bump to Node 16. [#7](https://github.com/actions-cool/maintain-one-comment/pull/7) [@gastaldi](https://github.com/gastaldi) 24 | 25 | ## v3.1.0 26 | 27 | `2023.06.30` 28 | 29 | - 🛠 refactor: `delete` will act on newly created comment. [#5](https://github.com/actions-cool/maintain-one-comment/pull/5) [@thyandrecardoso](https://github.com/thyandrecardoso) 30 | - 🤖 chore: update deps. [#6](https://github.com/actions-cool/maintain-one-comment/pull/6) [@plainheart](https://github.com/plainheart) 31 | 32 | 33 | ## v3.0.0 34 | 35 | `2022.08.01` 36 | 37 | - refactor: add default `body-include`. 38 | - feat: add `delete`. 39 | 40 | ## v2.0.2 41 | 42 | `2021.10.19` 43 | 44 | - ⚡️ chore: add pub. 45 | 46 | ## v2.0.1 47 | 48 | `2021.10.19` 49 | 50 | - 💄 Add thanks. 51 | 52 | ## v2.0.0 53 | 54 | `2021.04.13` 55 | 56 | - refactor 57 | - remove `body` required. 58 | - when no `body` input will delete the filter comment. 59 | 60 | ## v1.2.1 61 | 62 | `2021.03.19` 63 | 64 | - fix: list comments miss. 65 | 66 | ## v1.2.0 67 | 68 | - feat: add outputs `comment-id`. 69 | 70 | ## v1.1.0 71 | 72 | - feat: add `number`. 73 | 74 | ## v1.0.0 75 | 76 | `2021.02.18` 77 | 78 | - 🎉 Init. 79 | -------------------------------------------------------------------------------- /scripts/tag.js: -------------------------------------------------------------------------------- 1 | const chalk = require('chalk'); 2 | const simpleGit = require('simple-git/promise'); 3 | const { execSync } = require('child_process'); 4 | const { readFileSync } = require('fs'); 5 | const path = require('path'); 6 | 7 | const CHANGELOG_NAME = 'CHANGELOG.md'; 8 | const CHANGELOG_PATH = path.join(__dirname, '..', CHANGELOG_NAME); 9 | const CHANGELOG = readFileSync(CHANGELOG_PATH, 'utf-8'); 10 | 11 | const cwd = process.cwd(); 12 | const git = simpleGit(cwd); 13 | 14 | async function run() { 15 | execSync(`git pull`); 16 | 17 | const data = await git.tags(); 18 | const tags = data.all; 19 | let tag = tags.reverse()[0]; 20 | console.log(chalk.green(`[Git Query] tag: ${tag}`)); 21 | 22 | const tagChangelog = getChangelogTag(CHANGELOG); 23 | if (tagChangelog && tag != tagChangelog) { 24 | console.log(chalk.yellow(`[Git Action] Push new ${tagChangelog} tag!`)); 25 | execSync(`git tag ${tagChangelog}`); 26 | execSync(`git push origin ${tagChangelog}:${tagChangelog}`); 27 | execSync(`git pull`); 28 | tag = tagChangelog; 29 | } else { 30 | console.log(chalk.yellow('🙄 Please add new release changelog first.')); 31 | console.log(''); 32 | process.exit(1); 33 | } 34 | 35 | const tagSimple = tag.startsWith('v') ? tag.substring(0, 2) : tag.substring(0, 1); 36 | console.log(chalk.green(`[Git Query] tagSimple: ${tagSimple}`)); 37 | 38 | if (tags.includes(tagSimple)) { 39 | console.log(chalk.yellow(`[Git Action] Delete ${tagSimple} tag`)); 40 | execSync(`git push origin :refs/tags/${tagSimple}`); 41 | } 42 | 43 | console.log(chalk.yellow(`[Git Action] Add new simple ${tagSimple} tag`)); 44 | execSync(`git push origin ${tag}:${tagSimple}`); 45 | console.log(chalk.green('🎉 Done!')); 46 | } 47 | 48 | function getChangelogTag(content) { 49 | const lines = content.split('\n'); 50 | const pin = /^## /; 51 | let begin = false; 52 | let tag = ''; 53 | 54 | for (let i = 0; i < lines.length; i += 1) { 55 | const line = lines[i]; 56 | if (begin && pin.test(line)) { 57 | break; 58 | } 59 | if (!begin) { 60 | begin = pin.test(line); 61 | if (begin) { 62 | tag = line.substring(3, line.length); 63 | } 64 | } 65 | } 66 | 67 | return tag.trim(); 68 | } 69 | 70 | run(); 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 📌 Maintain One Comment 2 | 3 | [![](https://github.com/actions-cool/maintain-one-comment/actions/workflows/test.yml/badge.svg)](https://github.com/actions-cool/maintain-one-comment/actions/workflows/test.yml) 4 | [![](https://img.shields.io/badge/marketplace-maintain--one--comment-blueviolet?style=flat-square)](https://github.com/marketplace/actions/maintain-one-comment) 5 | [![](https://img.shields.io/github/v/release/actions-cool/maintain-one-comment?style=flat-square&color=orange)](https://github.com/actions-cool/maintain-one-comment/releases) 6 | 7 | Maintain just one comment in Issue and PR. 8 | 9 | - This Action is only applicable to triggers related to issue and pull_request 10 | - When the **filtered comments** do not exist, will add a comment 11 | - When the **filtered comments** is only one, this comment will be updated 12 | - When the number of **filtered comments** exceeds 1, no operation will be performed 13 | 14 | ## Preview 15 | - Issue: https://github.com/actions-cool/maintain-one-comment/issues/1 16 | - PR: https://github.com/actions-cool/maintain-one-comment/pull/2 17 | 18 | ## How to use? 19 | ```yml 20 | name: Maintain One Comment 21 | 22 | on: 23 | issues: 24 | types: [opened, edited] 25 | issue_comment: 26 | types: [created, edited] 27 | pull_request: 28 | types: [assigned, opened, synchronize, edited] 29 | 30 | jobs: 31 | comment: 32 | runs-on: ubuntu-latest 33 | steps: 34 | - name: maintain-comment 35 | uses: actions-cool/maintain-one-comment@v3 36 | with: 37 | token: ${{ secrets.GITHUB_TOKEN }} 38 | body: | 39 | Hi 😀 40 | emojis: '+1, laugh' 41 | body-include: '' 42 | ``` 43 | 44 | ### Inputs 45 | 46 | | Name | Desc | Type | Required | 47 | | -- | -- | -- | -- | 48 | | token | GitHub token | string | ✖ | 49 | | number | Manually control the issue or PR number | string | ✖ | 50 | | body | Create comment body | string | ✖ | 51 | | emojis | Add [emoji](#emoji-list) | string | ✖ | 52 | | update-mode | Comment update mode. Options: `replace` `append`. Default: `replace` | string | ✖ | 53 | | comment-auth | Filter comment auth | string | ✖ | 54 | | body-include | Filter comment body | string | ✖ | 55 | | delete | Will delete all filter comments. Default `false` | boolean | ✖ | 56 | 57 | - `number`: When no input, it will be the issue or PR number that triggered. When input, it is the highest priority 58 | - `body`: When has 1 comment, and no body input will delete this filter comment 59 | 60 | ### Outputs 61 | 62 | - `comment-id`: Return the ID of create or updated comment. 63 | - About `comment-id` use, can refer: https://github.com/actions-cool/issues-helper 64 | 65 | ## Note 66 | 67 | - When PR come from fork, it requires `pull_request_target` to comment (Reasons for github built-in permissions). When use `pull_request_target`, must [read](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request_target) 68 | 69 | ## Emoji List 70 | 71 | | input | emoji | 72 | | -- | -- | 73 | | `+1` | 👍 | 74 | | `-1` | 👎 | 75 | | `laugh` | 😄 | 76 | | `confused` | 😕 | 77 | | `heart` | ❤️ | 78 | | `hooray` | 🎉 | 79 | | `rocket` | 🚀 | 80 | | `eyes` | 👀 | 81 | 82 | ## Changelog 83 | 84 | [CHANGELOG](./CHANGELOG.md) 85 | 86 | ## LICENSE 87 | 88 | [MIT](./LICENSE) 89 | -------------------------------------------------------------------------------- /src/main.js: -------------------------------------------------------------------------------- 1 | const core = require('@actions/core'); 2 | const github = require('@actions/github'); 3 | const { Octokit } = require('@octokit/rest'); 4 | 5 | // ************************************************ 6 | const token = core.getInput('token'); 7 | const octokit = new Octokit({ auth: `token ${token}` }); 8 | const context = github.context; 9 | const defaultBody = ''; 10 | 11 | const { dealStringToArr, THANKS } = require('actions-util'); 12 | 13 | // ************************************************ 14 | async function run() { 15 | try { 16 | const owner = context.repo.owner; 17 | const repo = context.repo.repo; 18 | 19 | // 维护评论 20 | const body = core.getInput('body'); 21 | const emojis = core.getInput('emojis'); 22 | let updateMode = core.getInput('update-mode'); 23 | if (updateMode !== 'append') { 24 | updateMode = 'replace'; 25 | } 26 | 27 | const commentAuth = core.getInput('comment-auth'); 28 | const bodyInclude = core.getInput('body-include') || defaultBody; 29 | const doDelete = core.getInput('delete') === 'true'; 30 | 31 | // 手动 number 32 | const inputNumber = core.getInput('number'); 33 | 34 | let number, pr; 35 | if (inputNumber) { 36 | number = inputNumber; 37 | } else if (context.eventName.includes('issue')) { 38 | number = context.payload.issue.number; 39 | } else if (context.eventName.includes('pull_request')) { 40 | number = context.payload.pull_request.number; 41 | } else if ( 42 | context.eventName === 'workflow_run' && 43 | (pr = context.payload.workflow_run.pull_requests[0]) 44 | ) { 45 | number = pr.number; 46 | } else { 47 | core.info( 48 | `Now eventName: ${context.eventName}. And input number is empty. This Action only support issue and pull_request related!`, 49 | ); 50 | return false; 51 | } 52 | 53 | async function listComments(page = 1) { 54 | let { data: comments } = await octokit.issues.listComments({ 55 | owner, 56 | repo, 57 | issue_number: number, 58 | per_page: 100, 59 | page, 60 | }); 61 | if (comments.length >= 100) { 62 | comments = comments.concat(await listComments(page + 1)); 63 | } 64 | return comments; 65 | } 66 | 67 | const commentList = await listComments(); 68 | core.info(`Actions: [find-comments][${number}] success!`); 69 | let comments = []; 70 | commentList.forEach(item => { 71 | const a = commentAuth ? item.user.login === commentAuth : true; 72 | const b = bodyInclude ? item.body.includes(bodyInclude) : true; 73 | if (a && b) { 74 | comments.push({ 75 | id: item.id, 76 | auth: item.user.login, 77 | body: item.body, 78 | }); 79 | } 80 | }); 81 | core.info(`filter-comments: ${JSON.stringify(comments)}`); 82 | core.info(`filter-comments-length: ${comments.length}`); 83 | 84 | if (doDelete) { 85 | for (const { id } of comments) { 86 | await octokit.issues.deleteComment({ 87 | owner, 88 | repo, 89 | comment_id: id, 90 | }); 91 | } 92 | core.info(`Actions: [delete-comments] success!`); 93 | } else { 94 | if (comments.length === 0 && body.length > 0) { 95 | const commentBody = `${body}\n${bodyInclude}`; 96 | const { data } = await octokit.issues.createComment({ 97 | owner, 98 | repo, 99 | issue_number: number, 100 | body: commentBody, 101 | }); 102 | core.info(`Actions: [create-comment][${commentBody}] success!`); 103 | core.setOutput('comment-id', data.id); 104 | 105 | if (emojis) { 106 | dealStringToArr(emojis).forEach(async item => { 107 | if (testEmoji(item)) { 108 | await octokit.reactions.createForIssueComment({ 109 | owner, 110 | repo, 111 | comment_id: data.id, 112 | content: item, 113 | }); 114 | core.info(`Actions: [create-emoji][${item}] success!`); 115 | } 116 | }); 117 | } 118 | } else if (comments.length === 1) { 119 | let commentId = comments[0].id; 120 | if (!body) { 121 | await octokit.issues.deleteComment({ 122 | owner, 123 | repo, 124 | comment_id: commentId, 125 | }); 126 | core.info(`Actions: [delete-comment][${commentId}] success!`); 127 | return false; 128 | } 129 | const comment = await octokit.issues.getComment({ 130 | owner, 131 | repo, 132 | comment_id: commentId, 133 | }); 134 | const comment_body = comment.data.body; 135 | 136 | let params = { 137 | owner, 138 | repo, 139 | comment_id: commentId, 140 | }; 141 | 142 | let commentBody; 143 | if (updateMode === 'append') { 144 | commentBody = `${comment_body}\n${body}`; 145 | } else { 146 | commentBody = body; 147 | } 148 | params.body = `${commentBody}\n${bodyInclude}`; 149 | await octokit.issues.updateComment(params); 150 | core.setOutput('comment-id', commentId); 151 | core.info(`Actions: [update-comment][${params.body}] success!`); 152 | } 153 | } 154 | 155 | core.info(THANKS); 156 | } catch (error) { 157 | core.setFailed(error.message); 158 | } 159 | } 160 | 161 | const ALLEMOJIS = ['+1', '-1', 'laugh', 'confused', 'heart', 'hooray', 'rocket', 'eyes']; 162 | 163 | function testEmoji(con) { 164 | if (ALLEMOJIS.includes(con)) { 165 | return true; 166 | } else { 167 | core.info(`This emoji: ${con} not supported!`); 168 | return false; 169 | } 170 | } 171 | 172 | // ************************************************ 173 | run(); 174 | --------------------------------------------------------------------------------