Skip to content

Commit

Permalink
append-separator
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-evans committed Mar 31, 2023
1 parent 1ad97c8 commit 4510ba1
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/comment-body-addition.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
**Edit:** Some additional info
This is still the second line.
5 changes: 1 addition & 4 deletions .github/comment-body.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 16 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('/');
Expand All @@ -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}`);
Expand Down Expand Up @@ -220,13 +230,18 @@ 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)}`);
if (!['append', 'replace'].includes(inputs.editMode)) {
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;
Expand Down
17 changes: 16 additions & 1 deletion src/create-or-update-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface Inputs {
body: string
bodyFile: string
editMode: string
appendSeparator: string
reactions: string[]
}

Expand Down Expand Up @@ -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<void> {
const [owner, repo] = inputs.repository.split('/')
const body = getBody(inputs)
Expand All @@ -105,7 +117,10 @@ export async function createOrUpdateComment(inputs: Inputs): Promise<void> {
repo: repo,
comment_id: inputs.commentId
})
commentBody = comment.body + '\n'
commentBody = appendSeparator(
comment.body ? comment.body : '',
inputs.appendSeparator
)
}

commentBody = commentBody + body
Expand Down
6 changes: 6 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async function run(): Promise<void> {
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)}`)
Expand All @@ -23,6 +24,11 @@ async function run(): Promise<void> {
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
Expand Down

0 comments on commit 4510ba1

Please sign in to comment.