diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index d6dd879c..e82eec0c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -15,6 +15,7 @@ jobs: - name: Comment PR with message uses: ./ + id: nrt_message with: message: | Current branch is `${{ github.head_ref }}`. @@ -55,5 +56,11 @@ jobs: comment_tag: nrt_create_if_not_exists create_if_not_exists: false + - name: Check outputs + run: | + echo "id : ${{ steps.nrt_message.outputs.id }}" + echo "body : ${{ steps.nrt_message.outputs.body }}" + echo "html_url : ${{ steps.nrt_message.outputs.html_url }}" + - name: (AFTER) Setup test cases run: rm /tmp/foobar.txt \ No newline at end of file diff --git a/README.md b/README.md index 959abc86..f116ad1e 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,35 @@ This will delete the comment at the end of the job. | `mode` | Mode that will be used to update comment (upsert/recreate/delete) | | upsert | | `create_if_not_exists` | Whether a comment should be created even if `comment_tag` is not found | | true | + +## Outputs + +### Action outputs + +You can get some outputs from this actions : + +| Name | Description | +| --- | --- | +| `id` | Comment id that was created or updated | +| `body` | Comment body | +| `html_url` | URL of the comment created or updated | + +## Example + +```yaml +- name: Comment PR + uses: thollander/actions-comment-pull-request@v2 + id: hello + with: + message: | + Hello world ! :wave: +- name: Check outputs + run: | + echo "id : ${{ steps.hello.outputs.id }}" + echo "body : ${{ steps.hello.outputs.body }}" + echo "html_url : ${{ steps.hello.outputs.html_url }}" +``` + ## Permissions Depending on the permissions granted to your token, you may lack some rights. diff --git a/lib/index.js b/lib/index.js index 1969ff31..6ad06355 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9582,6 +9582,40 @@ async function run() { }); })); } + async function createComment({ owner, repo, issue_number, body, }) { + const { data: comment } = await octokit.rest.issues.createComment({ + owner, + repo, + issue_number, + body, + }); + core.setOutput('id', comment.id); + core.setOutput('body', comment.body); + core.setOutput('html_url', comment.html_url); + await addReactions(comment.id, reactions); + return comment; + } + async function updateComment({ owner, repo, comment_id, body, }) { + const { data: comment } = await octokit.rest.issues.updateComment({ + owner, + repo, + comment_id, + body, + }); + core.setOutput('id', comment.id); + core.setOutput('body', comment.body); + core.setOutput('html_url', comment.html_url); + await addReactions(comment.id, reactions); + return comment; + } + async function deleteComment({ owner, repo, comment_id }) { + const { data: comment } = await octokit.rest.issues.deleteComment({ + owner, + repo, + comment_id, + }); + return comment; + } const comment_tag_pattern = comment_tag ? `` : null; @@ -9598,25 +9632,23 @@ async function run() { } if (comment) { if (mode === 'upsert') { - await octokit.rest.issues.updateComment({ + await updateComment({ ...context.repo, comment_id: comment.id, body, }); - await addReactions(comment.id, reactions); return; } else if (mode === 'recreate') { - await octokit.rest.issues.deleteComment({ + await deleteComment({ ...context.repo, comment_id: comment.id, }); - const { data: newComment } = await octokit.rest.issues.createComment({ + await createComment({ ...context.repo, issue_number, body, }); - await addReactions(newComment.id, reactions); return; } else if (mode === 'delete') { @@ -9635,12 +9667,11 @@ async function run() { return; } } - const { data: comment } = await octokit.rest.issues.createComment({ + await createComment({ ...context.repo, issue_number, body, }); - await addReactions(comment.id, reactions); } catch (error) { if (error instanceof Error) { diff --git a/src/main.ts b/src/main.ts index 4e05a8c9..62e1ee81 100644 --- a/src/main.ts +++ b/src/main.ts @@ -55,6 +55,70 @@ async function run() { ); } + async function createComment({ + owner, + repo, + issue_number, + body, + }: { + owner: string; + repo: string; + issue_number: number; + body: string; + }) { + const { data: comment } = await octokit.rest.issues.createComment({ + owner, + repo, + issue_number, + body, + }); + + core.setOutput('id', comment.id); + core.setOutput('body', comment.body); + core.setOutput('html_url', comment.html_url); + + await addReactions(comment.id, reactions); + + return comment; + } + + async function updateComment({ + owner, + repo, + comment_id, + body, + }: { + owner: string; + repo: string; + comment_id: number; + body: string; + }) { + const { data: comment } = await octokit.rest.issues.updateComment({ + owner, + repo, + comment_id, + body, + }); + + core.setOutput('id', comment.id); + core.setOutput('body', comment.body); + core.setOutput('html_url', comment.html_url); + + await addReactions(comment.id, reactions); + + return comment; + } + + async function deleteComment({ owner, repo, comment_id }: { owner: string; repo: string; comment_id: number }) { + const { data: comment } = await octokit.rest.issues.deleteComment({ + owner, + repo, + comment_id, + }); + + return comment; + } + const comment_tag_pattern = comment_tag ? `` : null; @@ -75,26 +139,23 @@ async function run() { if (comment) { if (mode === 'upsert') { - await octokit.rest.issues.updateComment({ + await updateComment({ ...context.repo, comment_id: comment.id, body, }); - await addReactions(comment.id, reactions); return; } else if (mode === 'recreate') { - await octokit.rest.issues.deleteComment({ + await deleteComment({ ...context.repo, comment_id: comment.id, }); - const { data: newComment } = await octokit.rest.issues.createComment({ + await createComment({ ...context.repo, issue_number, body, }); - - await addReactions(newComment.id, reactions); return; } else if (mode === 'delete') { core.debug('Registering this comment to be deleted.'); @@ -112,13 +173,11 @@ async function run() { } } - const { data: comment } = await octokit.rest.issues.createComment({ + await createComment({ ...context.repo, issue_number, body, }); - - await addReactions(comment.id, reactions); } catch (error) { if (error instanceof Error) { core.setFailed(error.message);