Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid deleting a comment that was just created before and prevent creating empty comments #5

Merged
merged 1 commit into from
Jun 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 57 additions & 66 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,70 +75,7 @@ async function run() {
});
core.info(`filter-comments: ${JSON.stringify(comments)}`);
core.info(`filter-comments-length: ${comments.length}`);
let newCommentId;
if (comments.length === 0) {
const commentBody = `${body}\n${bodyInclude}`;
const { data } = await octokit.issues.createComment({
owner,
repo,
issue_number: number,
body: commentBody,
});
core.info(`Actions: [create-comment][${commentBody}] success!`);
core.setOutput('comment-id', data.id);
newCommentId = data.id;

if (emojis) {
dealStringToArr(emojis).forEach(async item => {
if (testEmoji(item)) {
await octokit.reactions.createForIssueComment({
owner,
repo,
comment_id: data.id,
content: item,
});
core.info(`Actions: [create-emoji][${item}] success!`);
}
});
}
} else if (comments.length === 1) {
let commentId = comments[0].id;
if (!body) {
await octokit.issues.deleteComment({
owner,
repo,
comment_id: commentId,
});
core.info(`Actions: [delete-comment][${commentId}] success!`);
return false;
}
const comment = await octokit.issues.getComment({
owner,
repo,
comment_id: commentId,
});
const comment_body = comment.data.body;

let params = {
owner,
repo,
comment_id: commentId,
};

let commentBody;
if (updateMode === 'append') {
commentBody = `${comment_body}\n${body}`;
} else {
commentBody = body;
}
params.body = `${commentBody}\n${bodyInclude}`;
await octokit.issues.updateComment(params);
core.setOutput('comment-id', commentId);
core.info(`Actions: [update-comment][${params.body}] success!`);
} else {
let length = comments.length;
core.info(`The comments length is ${length}.`);
}
if (doDelete) {
for (const { id } of comments) {
await octokit.issues.deleteComment({
Expand All @@ -147,12 +84,66 @@ async function run() {
comment_id: id,
});
}
if (newCommentId) {
await octokit.issues.deleteComment({
core.info(`Actions: [delete-comments] success!`);
} else {
if (comments.length === 0 && body.length > 0) {
const commentBody = `${body}\n${bodyInclude}`;
const { data } = await octokit.issues.createComment({
owner,
repo,
comment_id: newCommentId,
issue_number: number,
body: commentBody,
});
core.info(`Actions: [create-comment][${commentBody}] success!`);
core.setOutput('comment-id', data.id);

if (emojis) {
dealStringToArr(emojis).forEach(async item => {
if (testEmoji(item)) {
await octokit.reactions.createForIssueComment({
owner,
repo,
comment_id: data.id,
content: item,
});
core.info(`Actions: [create-emoji][${item}] success!`);
}
});
}
} else if (comments.length === 1) {
let commentId = comments[0].id;
if (!body) {
await octokit.issues.deleteComment({
owner,
repo,
comment_id: commentId,
});
core.info(`Actions: [delete-comment][${commentId}] success!`);
return false;
}
const comment = await octokit.issues.getComment({
owner,
repo,
comment_id: commentId,
});
const comment_body = comment.data.body;

let params = {
owner,
repo,
comment_id: commentId,
};

let commentBody;
if (updateMode === 'append') {
commentBody = `${comment_body}\n${body}`;
} else {
commentBody = body;
}
params.body = `${commentBody}\n${bodyInclude}`;
await octokit.issues.updateComment(params);
core.setOutput('comment-id', commentId);
core.info(`Actions: [update-comment][${params.body}] success!`);
}
}

Expand Down