diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 78e8ad04..1ba4f2da 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -15,4 +15,5 @@ jobs: Current branch is `${{ github.head_ref }}`. _(execution **${{ github.run_id }}** / attempt **${{ github.run_attempt }}**)_ comment_includes: Current branch + reactions: eyes, rocket GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/README.md b/README.md index 8a2d666b..64efaca7 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,22 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} ``` + +### Setting reactions + +You can also set some reactions on your comments through the `reactions` input. +It takes only valid reactions and adds it to the comment you've just created. (See https://docs.github.com/en/rest/reactions#reaction-types) + +```yml +- name: PR comment with reactions + uses: thollander/actions-comment-pull-request@v1 + with: + message: | + Hello world ! :wave: + reactions: eyes, rocket + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} +``` + ### Specifying which pull request to comment on You can explicitly input which pull request should be commented on by passing the `pr_number` input. @@ -78,6 +94,7 @@ _That is particularly interesting while committing multiple times in a PR and th | --- | --- | --- | --- | | `GITHUB_TOKEN` | Token that is used to create comments | ✅ | | | `message` | The comment body | ✅ | | +| `reactions` | List of reactions for the comment (comma separated). See https://docs.github.com/en/rest/reactions#reaction-types | | | | `pr_number` | The number of the pull request where to create the comment | | current pull request number (deduced from context) | | `comment_includes` | The text that should be used to find comment in case of replacement. | | | diff --git a/lib/index.js b/lib/index.js index 8dc1093b..f8852369 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8330,12 +8330,15 @@ var __importStar = (this && this.__importStar) || function (mod) { Object.defineProperty(exports, "__esModule", ({ value: true })); const github = __importStar(__nccwpck_require__(5438)); const core = __importStar(__nccwpck_require__(2186)); +// See https://docs.github.com/en/rest/reactions#reaction-types +const REACTIONS = ['+1', '-1', 'laugh', 'confused', 'heart', 'hooray', 'rocket', 'eyes']; async function run() { try { const message = core.getInput('message'); const github_token = core.getInput('GITHUB_TOKEN'); const pr_number = core.getInput('pr_number'); const comment_includes = core.getInput('comment_includes'); + const reactions = core.getInput('reactions'); const context = github.context; const pull_number = parseInt(pr_number) || context.payload.pull_request?.number; const octokit = github.getOctokit(github_token); @@ -8343,6 +8346,19 @@ async function run() { core.setFailed('No pull request in input neither in current context.'); return; } + async function addReactions(comment_id, reactions) { + const validReactions = reactions + .replace(/\s/g, '') + .split(',') + .filter((reaction) => REACTIONS.includes(reaction)); + await Promise.allSettled(validReactions.map(async (content) => { + await octokit.rest.reactions.createForIssueComment({ + ...context.repo, + comment_id, + content, + }); + })); + } if (comment_includes) { let comment; for await (const { data: comments } of octokit.paginate.iterator(octokit.rest.issues.listComments, { @@ -8359,17 +8375,19 @@ async function run() { comment_id: comment.id, body: message, }); + await addReactions(comment.id, reactions); return; } else { core.info('No comment has been found with asked pattern. Creating a new comment.'); } } - await octokit.rest.issues.createComment({ + const { data: comment } = await octokit.rest.issues.createComment({ ...context.repo, issue_number: pull_number, body: message, }); + await addReactions(comment.id, reactions); } catch (error) { if (error instanceof Error) { diff --git a/src/main.ts b/src/main.ts index b79e2a78..2881dd36 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,12 +2,17 @@ import * as github from '@actions/github'; import * as core from '@actions/core'; import { GetResponseDataTypeFromEndpointMethod } from '@octokit/types'; +// See https://docs.github.com/en/rest/reactions#reaction-types +const REACTIONS = ['+1', '-1', 'laugh', 'confused', 'heart', 'hooray', 'rocket', 'eyes'] as const; +type Reaction = typeof REACTIONS[number]; + async function run() { try { const message: string = core.getInput('message'); const github_token: string = core.getInput('GITHUB_TOKEN'); const pr_number: string = core.getInput('pr_number'); const comment_includes: string = core.getInput('comment_includes'); + const reactions: string = core.getInput('reactions'); const context = github.context; const pull_number = parseInt(pr_number) || context.payload.pull_request?.number; @@ -19,6 +24,23 @@ async function run() { return; } + async function addReactions(comment_id: number, reactions: string) { + const validReactions = reactions + .replace(/\s/g, '') + .split(',') + .filter((reaction) => REACTIONS.includes(reaction)); + + await Promise.allSettled( + validReactions.map(async (content) => { + await octokit.rest.reactions.createForIssueComment({ + ...context.repo, + comment_id, + content, + }); + }), + ); + } + if (comment_includes) { type ListCommentsResponseDataType = GetResponseDataTypeFromEndpointMethod< typeof octokit.rest.issues.listComments @@ -38,17 +60,20 @@ async function run() { comment_id: comment.id, body: message, }); + await addReactions(comment.id, reactions); return; } else { core.info('No comment has been found with asked pattern. Creating a new comment.'); } } - await octokit.rest.issues.createComment({ + const { data: comment } = await octokit.rest.issues.createComment({ ...context.repo, issue_number: pull_number, body: message, }); + + await addReactions(comment.id, reactions); } catch (error) { if (error instanceof Error) { core.setFailed(error.message);