Skip to content

Commit ec1fe24

Browse files
committed
fix: duplicate proposal guard clause
1 parent de87ca7 commit ec1fe24

2 files changed

Lines changed: 67 additions & 17 deletions

File tree

.github/actions/javascript/proposalPoliceComment/index.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11596,6 +11596,28 @@ function isCommentCreatedEvent(payload) {
1159611596
function isCommentEditedEvent(payload) {
1159711597
return payload.action === CONST_1.default.ACTIONS.EDITED;
1159811598
}
11599+
/**
11600+
* Checks if a comment body matches the criteria for a Proposal.
11601+
*/
11602+
function getIsProposal(body) {
11603+
if (!body) {
11604+
return false;
11605+
}
11606+
const lowerCaseBody = body.toLowerCase();
11607+
return body.includes(CONST_1.default.PROPOSAL_KEYWORD) && lowerCaseBody.includes(CONST_1.default.PROPOSAL_HEADER_A) && lowerCaseBody.includes(CONST_1.default.PROPOSAL_HEADER_B);
11608+
}
11609+
/**
11610+
* Determines if a comment author is a known bot or a bot-type account.
11611+
*/
11612+
function getIsBotAuthor(user) {
11613+
if (!user) {
11614+
return false;
11615+
}
11616+
const knownBotLogins = [CONST_1.default.COMMENT.NAME_MELVIN_A, CONST_1.default.COMMENT.NAME_MELVIN_B, CONST_1.default.COMMENT.NAME_CODEX, CONST_1.default.COMMENT.NAME_GITHUB_ACTIONS];
11617+
const isBotType = user.type === CONST_1.default.COMMENT.TYPE_BOT;
11618+
const isKnownLogin = knownBotLogins.includes(user.login ?? '');
11619+
return isBotType || isKnownLogin;
11620+
}
1159911621
// Main function to process the workflow event
1160011622
async function run() {
1160111623
// Capture the timestamp immediately at the start of the run
@@ -11650,22 +11672,22 @@ async function run() {
1165011672
core.endGroup();
1165111673
let didFindDuplicate = false;
1165211674
let originalProposal;
11675+
const isNewCommentAProposal = getIsProposal(newProposalBody);
11676+
if (!isNewCommentAProposal) {
11677+
console.log('New comment is not a proposal. Skipping duplicate check.');
11678+
return;
11679+
}
1165311680
for (const previousProposal of commentsResponse) {
1165411681
const body = previousProposal.body ?? '';
11655-
const lowerCaseBody = body.toLowerCase() ?? '';
11656-
const isProposal = !!body.includes(CONST_1.default.PROPOSAL_KEYWORD) && !!lowerCaseBody.includes(CONST_1.default.PROPOSAL_HEADER_A) && !!lowerCaseBody.includes(CONST_1.default.PROPOSAL_HEADER_B);
11682+
const isProposal = getIsProposal(body);
1165711683
const previousProposalCreatedAt = new Date(previousProposal.created_at).getTime();
1165811684
// Early continue if not a proposal or previous comment is newer than current one
1165911685
if (!isProposal || previousProposalCreatedAt >= newProposalCreatedAt) {
1166011686
continue;
1166111687
}
11662-
const isAuthorBot = previousProposal.user?.login === CONST_1.default.COMMENT.NAME_MELVIN_A ||
11663-
previousProposal.user?.login === CONST_1.default.COMMENT.NAME_MELVIN_B ||
11664-
previousProposal.user?.login === CONST_1.default.COMMENT.NAME_CODEX ||
11665-
previousProposal.user?.login === CONST_1.default.COMMENT.NAME_GITHUB_ACTIONS ||
11666-
previousProposal.user?.type === CONST_1.default.COMMENT.TYPE_BOT;
11688+
const isBotAuthor = getIsBotAuthor(previousProposal.user);
1166711689
// Skip prompting if comment author is the GH bot
11668-
if (isAuthorBot) {
11690+
if (isBotAuthor) {
1166911691
continue;
1167011692
}
1167111693
const duplicateCheckPrompt = proposalPolice_1.default.getPromptForNewProposalDuplicateCheck(previousProposal.body, newProposalBody);

.github/actions/javascript/proposalPoliceComment/proposalPoliceComment.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,33 @@ function isCommentEditedEvent(payload: IssueCommentEvent): payload is IssueComme
2828
return payload.action === CONST.ACTIONS.EDITED;
2929
}
3030

31+
/**
32+
* Checks if a comment body matches the criteria for a Proposal.
33+
*/
34+
function getIsProposal(body: string | null | undefined): boolean {
35+
if (!body) {
36+
return false;
37+
}
38+
const lowerCaseBody = body.toLowerCase();
39+
return body.includes(CONST.PROPOSAL_KEYWORD) && lowerCaseBody.includes(CONST.PROPOSAL_HEADER_A) && lowerCaseBody.includes(CONST.PROPOSAL_HEADER_B);
40+
}
41+
42+
/**
43+
* Determines if a comment author is a known bot or a bot-type account.
44+
*/
45+
function getIsBotAuthor(user: {login?: string; type?: string} | null | undefined): boolean {
46+
if (!user) {
47+
return false;
48+
}
49+
50+
const knownBotLogins: string[] = [CONST.COMMENT.NAME_MELVIN_A, CONST.COMMENT.NAME_MELVIN_B, CONST.COMMENT.NAME_CODEX, CONST.COMMENT.NAME_GITHUB_ACTIONS];
51+
52+
const isBotType = user.type === CONST.COMMENT.TYPE_BOT;
53+
const isKnownLogin = knownBotLogins.includes(user.login ?? '');
54+
55+
return isBotType || isKnownLogin;
56+
}
57+
3158
// Main function to process the workflow event
3259
async function run() {
3360
// Capture the timestamp immediately at the start of the run
@@ -93,23 +120,24 @@ async function run() {
93120

94121
let didFindDuplicate = false;
95122
let originalProposal: TupleToUnion<typeof commentsResponse> | undefined;
123+
124+
const isNewCommentAProposal = getIsProposal(newProposalBody);
125+
if (!isNewCommentAProposal) {
126+
console.log('New comment is not a proposal. Skipping duplicate check.');
127+
return;
128+
}
129+
96130
for (const previousProposal of commentsResponse) {
97131
const body = previousProposal.body ?? '';
98-
const lowerCaseBody = body.toLowerCase() ?? '';
99-
const isProposal = !!body.includes(CONST.PROPOSAL_KEYWORD) && !!lowerCaseBody.includes(CONST.PROPOSAL_HEADER_A) && !!lowerCaseBody.includes(CONST.PROPOSAL_HEADER_B);
132+
const isProposal = getIsProposal(body);
100133
const previousProposalCreatedAt = new Date(previousProposal.created_at).getTime();
101134
// Early continue if not a proposal or previous comment is newer than current one
102135
if (!isProposal || previousProposalCreatedAt >= newProposalCreatedAt) {
103136
continue;
104137
}
105-
const isAuthorBot =
106-
previousProposal.user?.login === CONST.COMMENT.NAME_MELVIN_A ||
107-
previousProposal.user?.login === CONST.COMMENT.NAME_MELVIN_B ||
108-
previousProposal.user?.login === CONST.COMMENT.NAME_CODEX ||
109-
previousProposal.user?.login === CONST.COMMENT.NAME_GITHUB_ACTIONS ||
110-
previousProposal.user?.type === CONST.COMMENT.TYPE_BOT;
138+
const isBotAuthor = getIsBotAuthor(previousProposal.user);
111139
// Skip prompting if comment author is the GH bot
112-
if (isAuthorBot) {
140+
if (isBotAuthor) {
113141
continue;
114142
}
115143

0 commit comments

Comments
 (0)