Skip to content

Commit

Permalink
Allow to refresh the uniq comment position to be the latest one in th…
Browse files Browse the repository at this point in the history
…e PR feed.
  • Loading branch information
vincent-joignie-dd committed Nov 14, 2022
1 parent 8645f3f commit a46b7c0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,21 @@ jobs:
## Configuration options
| Input | Location | Description | Required | Default |
| ----------------- | -------- | ---------------------------------------------------------------------------------------------------- | -------- | ------------------ |
| message | with | The message you'd like displayed, supports Markdown and all valid Unicode characters. | maybe | |
| message-path | with | Path to a message you'd like displayed. Will be read and displayed just like a normal message. | maybe | |
| message-success | with | A message override, printed in case of success. | no | |
| message-failure | with | A message override, printed in case of failure. | no | |
| message-cancelled | with | A message override, printed in case of cancelled. | no | |
| status | with | Required if you want to use message status overrides. | no | {{ job.status }} |
| repo-token | with | Valid GitHub token, either the temporary token GitHub provides or a personal access token. | no | {{ github.token }} |
| message-id | with | Message id to use when searching existing comments. If found, updates the existing (sticky comment). | no | |
| allow-repeats | with | Boolean flag to allow identical messages to be posted each time this action is run. | no | false |
| proxy-url | with | String for your proxy service URL if you'd like this to work with fork-based PRs. | no | |
| issue | with | Optional issue number override. | no | |
| GITHUB_TOKEN | env | Valid GitHub token, can alternatively be defined in the env. | no | |
| Input | Location | Description | Required | Default |
| ----------------------------- | -------- | ---------------------------------------------------------------------------------------------------- | -------- | ------------------ |
| message | with | The message you'd like displayed, supports Markdown and all valid Unicode characters. | maybe | |
| message-path | with | Path to a message you'd like displayed. Will be read and displayed just like a normal message. | maybe | |
| message-success | with | A message override, printed in case of success. | no | |
| message-failure | with | A message override, printed in case of failure. | no | |
| message-cancelled | with | A message override, printed in case of cancelled. | no | |
| status | with | Required if you want to use message status overrides. | no | {{ job.status }} |
| repo-token | with | Valid GitHub token, either the temporary token GitHub provides or a personal access token. | no | {{ github.token }} |
| message-id | with | Message id to use when searching existing comments. If found, updates the existing (sticky comment). | no | |
| auto-refresh-message-position | with | Should the sticky message be the last one in the PR's feed. | no | false |
| allow-repeats | with | Boolean flag to allow identical messages to be posted each time this action is run. | no | false |
| proxy-url | with | String for your proxy service URL if you'd like this to work with fork-based PRs. | no | |
| issue | with | Optional issue number override. | no | |
| GITHUB_TOKEN | env | Valid GitHub token, can alternatively be defined in the env. | no | |
## Advanced Uses
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ inputs:
description: "An optional id to use for this message."
default: "add-pr-comment"
required: false
auto-refresh-message-position:
description: "If a message with the same id, this option allow to refresh the position of the message to be the last one posted."
default: "true"
required: false
repo-token:
description: "A GitHub token for API access. Defaults to {{ github.token }}."
default: "${{ github.token }}"
Expand Down
17 changes: 17 additions & 0 deletions src/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ export async function updateComment(
return updatedComment.data
}

export async function deleteComment(
octokit: InstanceType<typeof GitHub>,
owner: string,
repo: string,
existingCommentId: number,
body: string,
): Promise<CreateIssueCommentResponseData> {
const deletedComment = await octokit.rest.issues.deleteComment({
comment_id: existingCommentId,
owner,
repo,
body,
})

return deletedComment.data
}

export async function createComment(
octokit: InstanceType<typeof GitHub>,
owner: string,
Expand Down
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as github from '@actions/github'
import fs from 'node:fs/promises'

interface Inputs {
autoRefreshMessagePosition: boolean
allowRepeats: boolean
message?: string
messageId: string
Expand Down Expand Up @@ -30,6 +31,8 @@ export async function getInputs(): Promise<Inputs> {
const issue = core.getInput('issue', { required: false })
const proxyUrl = core.getInput('proxy-url', { required: false }).replace(/\/$/, '')
const allowRepeats = core.getInput('allow-repeats', { required: true }) === 'true'
const autoRefreshMessagePosition =
core.getInput('auto-refresh-message-position', { required: false }) === 'true'

if (messageInput && messagePath) {
throw new Error('must specify only one, message or message-path')
Expand Down Expand Up @@ -74,6 +77,7 @@ export async function getInputs(): Promise<Inputs> {
const [owner, repo] = repoFullName.split('/')

return {
autoRefreshMessagePosition,
allowRepeats,
message,
messageId: `<!-- ${messageId} -->`,
Expand Down
9 changes: 8 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CreateIssueCommentResponseData,
getExistingCommentId,
updateComment,
deleteComment,
} from './comments'
import { getInputs } from './config'
import { getIssueNumberFromCommitPullsList } from './issues'
Expand All @@ -16,6 +17,7 @@ const run = async (): Promise<void> => {
allowRepeats,
message,
messageId,
autoRefreshMessagePosition,
repoToken,
proxyUrl,
issue,
Expand Down Expand Up @@ -74,7 +76,12 @@ const run = async (): Promise<void> => {
})
core.setOutput(existingCommentId ? 'comment-updated' : 'comment-created', 'true')
} else if (existingCommentId) {
comment = await updateComment(octokit, owner, repo, existingCommentId, body)
if (autoRefreshMessagePosition) {
await deleteComment(octokit, owner, repo, existingCommentId, body)
comment = await createComment(octokit, owner, repo, issueNumber, body)
} else {
comment = await updateComment(octokit, owner, repo, existingCommentId, body)
}
core.setOutput('comment-updated', 'true')
} else {
comment = await createComment(octokit, owner, repo, issueNumber, body)
Expand Down

0 comments on commit a46b7c0

Please sign in to comment.