diff --git a/.github/comment-body-addition.md b/.github/comment-body-addition.md index 30f398d9..706143fa 100644 --- a/.github/comment-body-addition.md +++ b/.github/comment-body-addition.md @@ -1 +1 @@ -**Edit:** Some additional info +This is still the second line. \ No newline at end of file diff --git a/.github/comment-body.md b/.github/comment-body.md index 465112f4..6f774b79 100644 --- a/.github/comment-body.md +++ b/.github/comment-body.md @@ -1,5 +1,2 @@ This is a multi-line test comment read from a file. -- With GitHub **Markdown** :sparkles: -- Created by [create-or-update-comment][1] - -[1]: https://github.com/peter-evans/create-or-update-comment +This is the second line. \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 75b1e830..42bca922 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -112,6 +112,7 @@ jobs: with: comment-id: ${{ steps.couc2.outputs.comment-id }} body-file: .github/comment-body-addition.md + append-separator: 'space' reactions: eyes, rocket package: diff --git a/action.yml b/action.yml index e708102a..bbf0e1ab 100644 --- a/action.yml +++ b/action.yml @@ -18,6 +18,9 @@ inputs: edit-mode: description: 'The mode when updating a comment, "replace" or "append".' default: 'append' + append-separator: + description: 'The separator to use when appending to an existing comment. (`newline`, `space`, `none`)' + default: 'newline' reactions: description: 'A comma or newline separated list of reactions to add to the comment.' outputs: diff --git a/dist/index.js b/dist/index.js index 55cbaf9b..b15c6725 100644 --- a/dist/index.js +++ b/dist/index.js @@ -98,6 +98,16 @@ function addReactions(octokit, owner, repo, commentId, reactions) { } }); } +function appendSeparator(body, separator) { + switch (separator) { + case 'newline': + return body + '\n'; + case 'space': + return body + ' '; + default: // none + return body; + } +} function createOrUpdateComment(inputs) { return __awaiter(this, void 0, void 0, function* () { const [owner, repo] = inputs.repository.split('/'); @@ -118,7 +128,7 @@ function createOrUpdateComment(inputs) { repo: repo, comment_id: inputs.commentId }); - commentBody = comment.body + '\n'; + commentBody = appendSeparator(comment.body ? comment.body : '', inputs.appendSeparator); } commentBody = commentBody + body; core.debug(`Comment body: ${commentBody}`); @@ -220,6 +230,7 @@ function run() { body: core.getInput('body'), bodyFile: core.getInput('body-file'), editMode: core.getInput('edit-mode'), + appendSeparator: core.getInput('append-separator'), reactions: utils.getInputAsArray('reactions') }; core.debug(`Inputs: ${(0, util_1.inspect)(inputs)}`); @@ -227,6 +238,10 @@ function run() { core.setFailed(`Invalid edit-mode '${inputs.editMode}'.`); return; } + if (!['newline', 'space', 'none'].includes(inputs.appendSeparator)) { + core.setFailed(`Invalid append-separator '${inputs.appendSeparator}'.`); + return; + } if (inputs.bodyFile && inputs.body) { core.setFailed("Only one of 'body' or 'body-file' can be set."); return; diff --git a/src/create-or-update-comment.ts b/src/create-or-update-comment.ts index dd11e48f..c9af3237 100644 --- a/src/create-or-update-comment.ts +++ b/src/create-or-update-comment.ts @@ -10,6 +10,7 @@ export interface Inputs { body: string bodyFile: string editMode: string + appendSeparator: string reactions: string[] } @@ -83,6 +84,17 @@ async function addReactions( } } +function appendSeparator(body: string, separator: string): string { + switch (separator) { + case 'newline': + return body + '\n' + case 'space': + return body + ' ' + default: // none + return body + } +} + export async function createOrUpdateComment(inputs: Inputs): Promise { const [owner, repo] = inputs.repository.split('/') const body = getBody(inputs) @@ -105,7 +117,10 @@ export async function createOrUpdateComment(inputs: Inputs): Promise { repo: repo, comment_id: inputs.commentId }) - commentBody = comment.body + '\n' + commentBody = appendSeparator( + comment.body ? comment.body : '', + inputs.appendSeparator + ) } commentBody = commentBody + body diff --git a/src/main.ts b/src/main.ts index 2de7d545..e8bfa0ac 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,6 +14,7 @@ async function run(): Promise { body: core.getInput('body'), bodyFile: core.getInput('body-file'), editMode: core.getInput('edit-mode'), + appendSeparator: core.getInput('append-separator'), reactions: utils.getInputAsArray('reactions') } core.debug(`Inputs: ${inspect(inputs)}`) @@ -23,6 +24,11 @@ async function run(): Promise { return } + if (!['newline', 'space', 'none'].includes(inputs.appendSeparator)) { + core.setFailed(`Invalid append-separator '${inputs.appendSeparator}'.`) + return + } + if (inputs.bodyFile && inputs.body) { core.setFailed("Only one of 'body' or 'body-file' can be set.") return