-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add handling for comments during issue assignment
Implement functionality to skip comments during user assignment periods.
- Loading branch information
1 parent
b64d54a
commit 3623fd8
Showing
3 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { Type, Static } from "@sinclair/typebox"; | ||
|
||
export const dataPurgeConfigurationType = Type.Object({}); | ||
export const dataPurgeConfigurationType = Type.Object({ | ||
skipCommentsWhileAssigned: Type.Boolean({ | ||
default: true, | ||
description: "Skip comments posted by a user while it is assigned to the issue", | ||
}), | ||
}); | ||
|
||
export type DataPurgeConfiguration = Static<typeof dataPurgeConfigurationType>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { GitHubIssue, GitHubIssueComment, GitHubPullRequest, GitHubPullRequestReviewComment } from "../github-types"; | ||
import { IssueParams } from "../start"; | ||
import { ContextPlugin } from "../types/plugin-input"; | ||
|
||
export interface AssignmentPeriod { | ||
assignedAt: string; | ||
unassignedAt: string | null; | ||
} | ||
|
||
export interface UserAssignments { | ||
[username: string]: AssignmentPeriod[]; | ||
} | ||
|
||
/* | ||
* Returns the list of assignment periods per user for a given issue. | ||
*/ | ||
export async function getAssignmentPeriods(octokit: ContextPlugin["octokit"], issueParams: IssueParams) { | ||
const events = await octokit.paginate(octokit.rest.issues.listEvents, { | ||
...issueParams, | ||
per_page: 100, | ||
}); | ||
|
||
const userAssignments: UserAssignments = {}; | ||
|
||
events | ||
.filter((event) => ["assigned", "unassigned"].includes(event.event)) | ||
.forEach((event) => { | ||
const username = "assignee" in event ? event.assignee?.login : null; | ||
if (!username) return; | ||
|
||
if (!userAssignments[username]) { | ||
userAssignments[username] = []; | ||
} | ||
|
||
const lastPeriod = userAssignments[username][userAssignments[username].length - 1]; | ||
|
||
if (event.event === "assigned") { | ||
userAssignments[username].push({ | ||
assignedAt: event.created_at, | ||
unassignedAt: null, | ||
}); | ||
} else if (event.event === "unassigned" && lastPeriod && lastPeriod.unassignedAt === null) { | ||
lastPeriod.unassignedAt = event.created_at; | ||
} | ||
}); | ||
|
||
Object.values(userAssignments).forEach((periods) => { | ||
const lastPeriod = periods[periods.length - 1]; | ||
if (lastPeriod && lastPeriod.unassignedAt === null) { | ||
lastPeriod.unassignedAt = new Date().toISOString(); | ||
} | ||
}); | ||
|
||
return userAssignments; | ||
} | ||
|
||
export function isCommentDuringAssignment( | ||
comment: GitHubIssueComment | GitHubPullRequestReviewComment | GitHubIssue | GitHubPullRequest, | ||
assignments: AssignmentPeriod[] | ||
) { | ||
const commentDate = new Date(comment.created_at); | ||
if (!assignments?.length) { | ||
return false; | ||
} | ||
return assignments.some((period) => { | ||
const assignedAt = new Date(period.assignedAt); | ||
const unassignedAt = period.unassignedAt ? new Date(period.unassignedAt) : new Date(); | ||
return commentDate >= assignedAt && commentDate <= unassignedAt; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters