Skip to content

Commit

Permalink
fix comment debug (#23)
Browse files Browse the repository at this point in the history
* show comment info if recommending comment has already been posted

* update to show existing comment url

* show debug comment on console.log than in message by summary

* fix to set message after post a message

* remove unused properties

* fix logic
  • Loading branch information
tnyo43 committed Mar 20, 2024
1 parent eced567 commit 3d10fb3
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 89 deletions.
85 changes: 40 additions & 45 deletions dist/index.js

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

22 changes: 12 additions & 10 deletions src/comments/getCommentContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ import {
type User,
} from './types';
import { getLoginNames } from './getLoginNames';
import { isAlreadyCommented } from './isAlreadyCommented';

type Args = {
threshold: number;
};
import { getExistingCommentUrl } from './getExistingCommentUrl';

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

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

if (isAlreadyCommented(comments)) {
const existingCommentUrl = getExistingCommentUrl(comments);
if (existingCommentUrl) {
console.log(
'a recommending comment has already been posted: ',
existingCommentUrl,
);
return null;
}

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

const numberOfComments = comments.length + reviewComments.length;
if (numberOfComments < args.threshold) {
console.log('number of the obtained comments is ', numberOfComments);

if (numberOfComments < threshold) {
console.log("It's not necessary to send a recommending comment yet.");
return null;
}

Expand All @@ -54,7 +58,5 @@ export async function getCommentContent(

return {
logins,
numberOfComments,
threshold: args.threshold,
};
}
9 changes: 9 additions & 0 deletions src/comments/getExistingCommentUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { type Comment } from './types';
import { ACTION_IDENTIFY_TEXT } from './constants';

export function getExistingCommentUrl(comments: Comment[]): string | undefined {
const comment = comments.find((comment) =>
comment.body?.startsWith(ACTION_IDENTIFY_TEXT),
);
return comment?.html_url;
}
8 changes: 0 additions & 8 deletions src/comments/isAlreadyCommented.ts

This file was deleted.

34 changes: 17 additions & 17 deletions src/comments/postComment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setFailed } from '@actions/core';
import { ACTION_IDENTIFY_TEXT } from './constants';
import {
type CommentContent,
Expand All @@ -12,22 +13,11 @@ Hey ${content.logins.map((login) => '@' + login).join(', ')}!
It seems the discussion is dragging on. Perhaps instead of text communication, you could try having a conversation via face-to-face or video call, or even try mob programming?
`;
}
function debugText(content: CommentContent) {
return `
<details>
<summary>number of comments</summary>
the number of the comments is ${content.numberOfComments}
threshold: ${content.threshold}
</details>
`;
}

function getText(content: CommentContent) {
return `${ACTION_IDENTIFY_TEXT}
${MainText(content)}
${debugText(content)}
`;
}

Expand All @@ -38,10 +28,20 @@ export async function postComment(
) {
const { owner, repo, prNumber } = octokitContext;

await octokit.rest.issues.createComment({
owner,
repo,
issue_number: prNumber,
body: getText(content),
});
try {
const result = await octokit.rest.issues.createComment({
owner,
repo,
issue_number: prNumber,
body: getText(content),
});

console.log(
'a recommending comment has been posted: ',
result.data.html_url,
);
} catch (error) {
console.error(error);
setFailed('failed to post comment');
}
}
4 changes: 1 addition & 3 deletions src/comments/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ export type User = {
export type Comment = {
user: User | null;
body?: string;
html_url: string;
};

export type CommentContent = {
logins: string[];

numberOfComments: number;
threshold: number;
};
10 changes: 4 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ export async function run(): Promise<void> {
const { token, prNumber, threshold } = 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,
});
);

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

0 comments on commit 3d10fb3

Please sign in to comment.