Skip to content

Commit

Permalink
show comment info if recommending comment has already been posted
Browse files Browse the repository at this point in the history
  • Loading branch information
tnyo43 committed Mar 20, 2024
1 parent eced567 commit a8a3c1d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
25 changes: 12 additions & 13 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions src/comments/getCommentContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import {
import { getLoginNames } from './getLoginNames';
import { isAlreadyCommented } from './isAlreadyCommented';

type Args = {
threshold: number;
};

export async function getCommentContent(
octokit: Octokit,
octokitContext: OctokitContext,
args: Args,
threshold: number,
option: {
debug: boolean;
},
): Promise<CommentContent | null> {
const { owner, repo, prNumber } = octokitContext;

Expand All @@ -26,7 +25,7 @@ export async function getCommentContent(
})
).data;

if (isAlreadyCommented(comments)) {
if (isAlreadyCommented(comments, option)) {
return null;
}

Expand All @@ -39,7 +38,7 @@ export async function getCommentContent(
).data;

const numberOfComments = comments.length + reviewComments.length;
if (numberOfComments < args.threshold) {
if (numberOfComments < threshold) {
return null;
}

Expand All @@ -55,6 +54,6 @@ export async function getCommentContent(
return {
logins,
numberOfComments,
threshold: args.threshold,
threshold,
};
}
11 changes: 9 additions & 2 deletions src/comments/isAlreadyCommented.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { type Comment } from './types';
import { ACTION_IDENTIFY_TEXT } from './constants';

export function isAlreadyCommented(comments: Comment[]): boolean {
return comments.some((comment) =>
export function isAlreadyCommented(
comments: Comment[],
option: { debug: boolean },
): boolean {
const comment = comments.find((comment) =>
comment.body?.startsWith(ACTION_IDENTIFY_TEXT),
);
if (option.debug) {
console.log('a recommending comment has already been posted', comment);
}
return !!comment;
}
13 changes: 6 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ import { postComment } from './comments/postComment';
*/
export async function run(): Promise<void> {
try {
const { token, prNumber, threshold } = getOption();
const { token, prNumber, threshold, debug } = getOption();

const octokit = getOctokit(token);
const owner = context.repo.owner;
const repo = context.repo.repo;
const octokitContext: OctokitContext = {
owner: context.repo.owner,
repo: context.repo.repo,
prNumber,
};

core.debug(`owner: ${owner}, repo: ${repo}, PR #${prNumber}`);

const commentContent = await getCommentContent(octokit, octokitContext, {
const commentContent = await getCommentContent(
octokit,
octokitContext,
threshold,
});
{ debug },
);

if (commentContent) {
await postComment(octokit, octokitContext, commentContent);
Expand Down

0 comments on commit a8a3c1d

Please sign in to comment.