Adding the possibility to auto refresh the sticky comment position... (#66)

* Allow to refresh the uniq comment position to be the latest one in the PR feed.

* Allow to refresh the uniq comment position to be the latest one in the PR feed.

* Removed "auto-" from func name
 + now defaulting new option to "false"
This commit is contained in:
vincent-joignie-dd 2023-04-14 16:09:40 +02:00 committed by GitHub
parent a65df5f64f
commit fe6766b6b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 2641 additions and 2572 deletions

View file

@ -63,7 +63,7 @@ jobs:
## Configuration options ## Configuration options
| Input | Location | Description | Required | Default | | Input | Location | Description | Required | Default |
| ----------------- | -------- | ---------------------------------------------------------------------------------------------------- | -------- | ------------------ | | ------------------------ | -------- | ---------------------------------------------------------------------------------------------------- | -------- | ------------------ |
| message | with | The message you'd like displayed, supports Markdown and all valid Unicode characters. | maybe | | | 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-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-success | with | A message override, printed in case of success. | no | |
@ -72,6 +72,7 @@ jobs:
| status | with | Required if you want to use message status overrides. | no | {{ job.status }} | | 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 }} | | 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 | | | message-id | with | Message id to use when searching existing comments. If found, updates the existing (sticky comment). | no | |
| 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 | | 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 | | | 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 | | | issue | with | Optional issue number override. | no | |

View file

@ -11,6 +11,10 @@ inputs:
description: "An optional id to use for this message." description: "An optional id to use for this message."
default: "add-pr-comment" default: "add-pr-comment"
required: false required: false
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: "false"
required: false
repo-token: repo-token:
description: "A GitHub token for API access. Defaults to {{ github.token }}." description: "A GitHub token for API access. Defaults to {{ github.token }}."
default: "${{ github.token }}" default: "${{ github.token }}"

2528
dist/index.js vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,6 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.createComment = exports.updateComment = exports.getExistingCommentId = void 0; exports.createComment = exports.deleteComment = exports.updateComment = exports.getExistingCommentId = void 0;
async function getExistingCommentId(octokit, owner, repo, issueNumber, messageId) { async function getExistingCommentId(octokit, owner, repo, issueNumber, messageId) {
const parameters = { const parameters = {
owner, owner,
@ -31,6 +31,16 @@ async function updateComment(octokit, owner, repo, existingCommentId, body) {
return updatedComment.data; return updatedComment.data;
} }
exports.updateComment = updateComment; exports.updateComment = updateComment;
async function deleteComment(octokit, owner, repo, existingCommentId, body) {
const deletedComment = await octokit.rest.issues.deleteComment({
comment_id: existingCommentId,
owner,
repo,
body,
});
return deletedComment.data;
}
exports.deleteComment = deleteComment;
async function createComment(octokit, owner, repo, issueNumber, body) { async function createComment(octokit, owner, repo, issueNumber, body) {
const createdComment = await octokit.rest.issues.createComment({ const createdComment = await octokit.rest.issues.createComment({
issue_number: issueNumber, issue_number: issueNumber,

View file

@ -41,6 +41,7 @@ async function getInputs() {
const issue = core.getInput('issue', { required: false }); const issue = core.getInput('issue', { required: false });
const proxyUrl = core.getInput('proxy-url', { required: false }).replace(/\/$/, ''); const proxyUrl = core.getInput('proxy-url', { required: false }).replace(/\/$/, '');
const allowRepeats = core.getInput('allow-repeats', { required: true }) === 'true'; const allowRepeats = core.getInput('allow-repeats', { required: true }) === 'true';
const refreshMessagePosition = core.getInput('refresh-message-position', { required: false }) === 'true';
if (messageInput && messagePath) { if (messageInput && messagePath) {
throw new Error('must specify only one, message or message-path'); throw new Error('must specify only one, message or message-path');
} }
@ -73,6 +74,7 @@ async function getInputs() {
} }
const [owner, repo] = repoFullName.split('/'); const [owner, repo] = repoFullName.split('/');
return { return {
refreshMessagePosition,
allowRepeats, allowRepeats,
message, message,
messageId: `<!-- ${messageId} -->`, messageId: `<!-- ${messageId} -->`,

View file

@ -31,7 +31,7 @@ const issues_1 = require("./issues");
const proxy_1 = require("./proxy"); const proxy_1 = require("./proxy");
const run = async () => { const run = async () => {
try { try {
const { allowRepeats, message, messageId, repoToken, proxyUrl, issue, pullRequestNumber, commitSha, repo, owner, } = await (0, config_1.getInputs)(); const { allowRepeats, message, messageId, refreshMessagePosition, repoToken, proxyUrl, issue, pullRequestNumber, commitSha, repo, owner, } = await (0, config_1.getInputs)();
const octokit = github.getOctokit(repoToken); const octokit = github.getOctokit(repoToken);
let issueNumber; let issueNumber;
if (issue) { if (issue) {
@ -72,7 +72,13 @@ const run = async () => {
core.setOutput(existingCommentId ? 'comment-updated' : 'comment-created', 'true'); core.setOutput(existingCommentId ? 'comment-updated' : 'comment-created', 'true');
} }
else if (existingCommentId) { else if (existingCommentId) {
if (refreshMessagePosition) {
await (0, comments_1.deleteComment)(octokit, owner, repo, existingCommentId, body);
comment = await (0, comments_1.createComment)(octokit, owner, repo, issueNumber, body);
}
else {
comment = await (0, comments_1.updateComment)(octokit, owner, repo, existingCommentId, body); comment = await (0, comments_1.updateComment)(octokit, owner, repo, existingCommentId, body);
}
core.setOutput('comment-updated', 'true'); core.setOutput('comment-updated', 'true');
} }
else { else {

View file

@ -53,6 +53,23 @@ export async function updateComment(
return updatedComment.data 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( export async function createComment(
octokit: InstanceType<typeof GitHub>, octokit: InstanceType<typeof GitHub>,
owner: string, owner: string,

View file

@ -3,6 +3,7 @@ import * as github from '@actions/github'
import fs from 'node:fs/promises' import fs from 'node:fs/promises'
interface Inputs { interface Inputs {
refreshMessagePosition: boolean
allowRepeats: boolean allowRepeats: boolean
message?: string message?: string
messageId: string messageId: string
@ -30,6 +31,8 @@ export async function getInputs(): Promise<Inputs> {
const issue = core.getInput('issue', { required: false }) const issue = core.getInput('issue', { required: false })
const proxyUrl = core.getInput('proxy-url', { required: false }).replace(/\/$/, '') const proxyUrl = core.getInput('proxy-url', { required: false }).replace(/\/$/, '')
const allowRepeats = core.getInput('allow-repeats', { required: true }) === 'true' const allowRepeats = core.getInput('allow-repeats', { required: true }) === 'true'
const refreshMessagePosition =
core.getInput('refresh-message-position', { required: false }) === 'true'
if (messageInput && messagePath) { if (messageInput && messagePath) {
throw new Error('must specify only one, message or message-path') throw new Error('must specify only one, message or message-path')
@ -74,6 +77,7 @@ export async function getInputs(): Promise<Inputs> {
const [owner, repo] = repoFullName.split('/') const [owner, repo] = repoFullName.split('/')
return { return {
refreshMessagePosition,
allowRepeats, allowRepeats,
message, message,
messageId: `<!-- ${messageId} -->`, messageId: `<!-- ${messageId} -->`,

View file

@ -5,6 +5,7 @@ import {
CreateIssueCommentResponseData, CreateIssueCommentResponseData,
getExistingCommentId, getExistingCommentId,
updateComment, updateComment,
deleteComment,
} from './comments' } from './comments'
import { getInputs } from './config' import { getInputs } from './config'
import { getIssueNumberFromCommitPullsList } from './issues' import { getIssueNumberFromCommitPullsList } from './issues'
@ -16,6 +17,7 @@ const run = async (): Promise<void> => {
allowRepeats, allowRepeats,
message, message,
messageId, messageId,
refreshMessagePosition,
repoToken, repoToken,
proxyUrl, proxyUrl,
issue, issue,
@ -74,7 +76,12 @@ const run = async (): Promise<void> => {
}) })
core.setOutput(existingCommentId ? 'comment-updated' : 'comment-created', 'true') core.setOutput(existingCommentId ? 'comment-updated' : 'comment-created', 'true')
} else if (existingCommentId) { } else if (existingCommentId) {
if (refreshMessagePosition) {
await deleteComment(octokit, owner, repo, existingCommentId, body)
comment = await createComment(octokit, owner, repo, issueNumber, body)
} else {
comment = await updateComment(octokit, owner, repo, existingCommentId, body) comment = await updateComment(octokit, owner, repo, existingCommentId, body)
}
core.setOutput('comment-updated', 'true') core.setOutput('comment-updated', 'true')
} else { } else {
comment = await createComment(octokit, owner, repo, issueNumber, body) comment = await createComment(octokit, owner, repo, issueNumber, body)