-
Notifications
You must be signed in to change notification settings - Fork 0
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
Refinement comment functions #15
Changes from 12 commits
fc2ec01
2cd6e94
b3c59ce
c54eb34
e615d2f
af83d51
88aacb2
c8a6a62
208e075
6847a28
9adef27
9eeb00a
56dae34
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const ACTION_IDENTIFY_TEXT = | ||
'<!-- a sentence for identifying bot recommend-mobpro-action -->'; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
type CommentContent, | ||
Comment on lines
+1
to
+2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. resend test |
||
type Octokit, | ||
type OctokitContext, | ||
type User, | ||
} from './types'; | ||
import { getLoginNames } from './getLoginNames'; | ||
import { isAlreadyCommented } from './isAlreadyCommented'; | ||
|
||
type Args = { | ||
threshold: number; | ||
}; | ||
|
||
export async function getCommentContent( | ||
octokit: Octokit, | ||
octokitContext: OctokitContext, | ||
args: Args, | ||
): Promise<CommentContent | null> { | ||
const { owner, repo, prNumber } = octokitContext; | ||
|
||
const comments = ( | ||
await octokit.rest.issues.listComments({ | ||
owner, | ||
repo, | ||
issue_number: prNumber, | ||
}) | ||
).data; | ||
|
||
if (isAlreadyCommented(comments)) { | ||
return null; | ||
} | ||
|
||
const reviewComments = ( | ||
await octokit.rest.pulls.listReviewComments({ | ||
owner, | ||
repo, | ||
pull_number: prNumber, | ||
}) | ||
).data; | ||
|
||
const numberOfComments = comments.length + reviewComments.length; | ||
if (numberOfComments < args.threshold) { | ||
return null; | ||
} | ||
|
||
const users1: User[] = comments | ||
.map((comment) => comment.user) | ||
.filter((user): user is Exclude<typeof user, null> => user !== null); | ||
const users2: User[] = reviewComments | ||
.map((comment) => comment.user) | ||
.filter((user): user is Exclude<typeof user, null> => user !== null); | ||
|
||
const logins = getLoginNames(users1.concat(users2)); | ||
|
||
return { | ||
logins, | ||
numberOfComments, | ||
threshold: args.threshold, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { getLoginNames } from './getLoginNames'; | ||
|
||
test('if the input is an empty array, the output is also empty', () => { | ||
const result = getLoginNames([]); | ||
expect(result).toStrictEqual([]); | ||
}); | ||
|
||
describe('if the input is an array without duplicates, the output is a array with the same elements in alphabetical order', () => { | ||
test('in case: ["alice", "bob", "chris"]', () => { | ||
const result = getLoginNames([ | ||
{ login: 'alice', type: 'User' }, | ||
{ login: 'bob', type: 'User' }, | ||
{ login: 'chris', type: 'User' }, | ||
]); | ||
expect(result).toStrictEqual(['alice', 'bob', 'chris']); | ||
}); | ||
|
||
test('in case: ["freddy", "eric", "daniel"]', () => { | ||
const result = getLoginNames([ | ||
{ login: 'freddy', type: 'User' }, | ||
{ login: 'eric', type: 'User' }, | ||
{ login: 'daniel', type: 'User' }, | ||
]); | ||
expect(result).toStrictEqual(['daniel', 'eric', 'freddy']); | ||
}); | ||
}); | ||
|
||
describe('if the input is an array containing duplicates, the output is an array where each name appears only once', () => { | ||
test('in case: ["alice", "bob", "chris", "alice", "chris", "alice"]', () => { | ||
const result = getLoginNames([ | ||
{ login: 'alice', type: 'User' }, | ||
{ login: 'bob', type: 'User' }, | ||
{ login: 'chris', type: 'User' }, | ||
{ login: 'alice', type: 'User' }, | ||
{ login: 'chris', type: 'User' }, | ||
{ login: 'alice', type: 'User' }, | ||
]); | ||
expect(result).toStrictEqual(['alice', 'bob', 'chris']); | ||
}); | ||
}); | ||
|
||
test('if the input has elements whose "type" property is "Bot", the output is an array without those elements', () => { | ||
const result = getLoginNames([ | ||
{ login: 'bot1', type: 'Bot' }, | ||
{ login: 'bob', type: 'User' }, | ||
{ login: 'chris', type: 'User' }, | ||
{ login: 'bot2', type: 'Bot' }, | ||
{ login: 'bot3', type: 'Bot' }, | ||
{ login: 'bob', type: 'User' }, | ||
]); | ||
expect(result).toStrictEqual(['bob', 'chris']); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { type User } from './types'; | ||
|
||
function uniqueStringArray(texts: string[]): string[] { | ||
if (texts.length === 0) return []; | ||
|
||
const sorted = texts.sort(); | ||
const result = [sorted[0]]; | ||
|
||
for (let i = 0; i < sorted.length - 1; i++) { | ||
if (sorted[i + 1] !== sorted[i]) { | ||
result.push(sorted[i + 1]); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export function getLoginNames(users: User[]): string[] { | ||
const loginNameArray = users | ||
.filter((user) => user.type === 'User') | ||
.map((user) => user.login); | ||
|
||
return uniqueStringArray(loginNameArray); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { type Comment } from './types'; | ||
import { ACTION_IDENTIFY_TEXT } from './constants'; | ||
|
||
export function isAlreadyCommented(comments: Comment[]): boolean { | ||
return comments.some((comment) => | ||
comment.body?.startsWith(ACTION_IDENTIFY_TEXT), | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
iii