Add update-only configuration option (#92)

This commit is contained in:
Alex Hatzenbuhler 2023-05-02 17:11:22 -05:00 committed by GitHub
parent 387ece43e3
commit 1dff58b1a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 3311 additions and 10787 deletions

1
.gitignore vendored
View file

@ -7,6 +7,7 @@ node_modules/
# Editors # Editors
.vscode .vscode
.idea/**
# Logs # Logs
logs logs

View file

@ -82,6 +82,7 @@ jobs:
| 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 | |
| update-only | with | Only update the comment if it already exists. | no | |
| GITHUB_TOKEN | env | Valid GitHub token, can alternatively be defined in the env. | no | | | GITHUB_TOKEN | env | Valid GitHub token, can alternatively be defined in the env. | no | |
## Advanced Uses ## Advanced Uses
@ -197,4 +198,4 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<!-- ALL-CONTRIBUTORS-LIST:END --> <!-- ALL-CONTRIBUTORS-LIST:END -->
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

View file

@ -24,6 +24,7 @@ type Inputs = {
'message-failure'?: string 'message-failure'?: string
'message-cancelled'?: string 'message-cancelled'?: string
'message-skipped'?: string 'message-skipped'?: string
'update-only'?: string
status?: 'success' | 'failure' | 'cancelled' | 'skipped' status?: 'success' | 'failure' | 'cancelled' | 'skipped'
} }
@ -175,6 +176,15 @@ describe('add-pr-comment action', () => {
expect(core.setOutput).toHaveBeenCalledWith('comment-created', 'true') expect(core.setOutput).toHaveBeenCalledWith('comment-created', 'true')
}) })
it('does not create a comment when updateOnly is true and no existing comment is found', async () => {
inputs.message = simpleMessage
inputs['allow-repeats'] = 'true'
inputs['update-only'] = 'true'
await expect(run()).resolves.not.toThrow()
expect(core.setOutput).toHaveBeenCalledWith('comment-created', 'false')
})
it('creates a comment in another repo', async () => { it('creates a comment in another repo', async () => {
inputs.message = simpleMessage inputs.message = simpleMessage
inputs['repo-owner'] = 'my-owner' inputs['repo-owner'] = 'my-owner'

View file

@ -53,6 +53,9 @@ inputs:
issue: issue:
description: "Override the message when a run is cancelled." description: "Override the message when a run is cancelled."
required: false required: false
update-only:
description: "Only update the comment if it already exists."
required: false
outputs: outputs:
comment-created: comment-created:
description: "Whether a comment was created." description: "Whether a comment was created."

2724
dist/index.js vendored

File diff suppressed because it is too large Load diff

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -44,6 +44,7 @@ async function getInputs() {
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'; const refreshMessagePosition = core.getInput('refresh-message-position', { required: false }) === 'true';
const updateOnly = core.getInput('update-only', { 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');
} }
@ -87,6 +88,7 @@ async function getInputs() {
commitSha: github.context.sha, commitSha: github.context.sha,
owner: repoOwner || payload.repo.owner, owner: repoOwner || payload.repo.owner,
repo: repoName || payload.repo.repo, repo: repoName || payload.repo.repo,
updateOnly: updateOnly,
}; };
} }
exports.getInputs = getInputs; exports.getInputs = getInputs;

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, refreshMessagePosition, repoToken, proxyUrl, issue, pullRequestNumber, commitSha, repo, owner, } = await (0, config_1.getInputs)(); const { allowRepeats, message, messageId, refreshMessagePosition, repoToken, proxyUrl, issue, pullRequestNumber, commitSha, repo, owner, updateOnly, } = await (0, config_1.getInputs)();
const octokit = github.getOctokit(repoToken); const octokit = github.getOctokit(repoToken);
let issueNumber; let issueNumber;
if (issue) { if (issue) {
@ -57,6 +57,12 @@ const run = async () => {
core.debug(`existing comment found with id: ${existingCommentId}`); core.debug(`existing comment found with id: ${existingCommentId}`);
} }
} }
// if no existing comment and updateOnly is true, exit
if (!existingCommentId && updateOnly) {
core.info('no existing comment found and update-only is true, exiting');
core.setOutput('comment-created', 'false');
return;
}
let comment; let comment;
const body = `${messageId}\n\n${message}`; const body = `${messageId}\n\n${message}`;
if (proxyUrl) { if (proxyUrl) {
@ -94,6 +100,10 @@ const run = async () => {
} }
} }
catch (err) { catch (err) {
if (process.env.NODE_ENV === 'test') {
// eslint-disable-next-line no-console
console.log(err);
}
if (err instanceof Error) { if (err instanceof Error) {
core.setFailed(err.message); core.setFailed(err.message);
} }

11330
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -19,6 +19,7 @@ interface Inputs {
pullRequestNumber?: number pullRequestNumber?: number
repo: string repo: string
owner: string owner: string
updateOnly: boolean
} }
export async function getInputs(): Promise<Inputs> { export async function getInputs(): Promise<Inputs> {
@ -35,6 +36,7 @@ export async function getInputs(): Promise<Inputs> {
const allowRepeats = core.getInput('allow-repeats', { required: true }) === 'true' const allowRepeats = core.getInput('allow-repeats', { required: true }) === 'true'
const refreshMessagePosition = const refreshMessagePosition =
core.getInput('refresh-message-position', { required: false }) === 'true' core.getInput('refresh-message-position', { required: false }) === 'true'
const updateOnly = core.getInput('update-only', { 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')
@ -88,5 +90,6 @@ export async function getInputs(): Promise<Inputs> {
commitSha: github.context.sha, commitSha: github.context.sha,
owner: repoOwner || payload.repo.owner, owner: repoOwner || payload.repo.owner,
repo: repoName || payload.repo.repo, repo: repoName || payload.repo.repo,
updateOnly: updateOnly,
} }
} }

View file

@ -25,6 +25,7 @@ const run = async (): Promise<void> => {
commitSha, commitSha,
repo, repo,
owner, owner,
updateOnly,
} = await getInputs() } = await getInputs()
const octokit = github.getOctokit(repoToken) const octokit = github.getOctokit(repoToken)
@ -60,6 +61,13 @@ const run = async (): Promise<void> => {
} }
} }
// if no existing comment and updateOnly is true, exit
if (!existingCommentId && updateOnly) {
core.info('no existing comment found and update-only is true, exiting')
core.setOutput('comment-created', 'false')
return
}
let comment: CreateIssueCommentResponseData | null | undefined let comment: CreateIssueCommentResponseData | null | undefined
const body = `${messageId}\n\n${message}` const body = `${messageId}\n\n${message}`