generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from gentlementlegen/feat/filter-comments-dur…
…ing-assignment feat: filter comments during assignment
- Loading branch information
Showing
8 changed files
with
146 additions
and
8 deletions.
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
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 @@ | ||
dist/** linguist-generated |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,15 @@ | ||
import { Type, Static } from "@sinclair/typebox"; | ||
|
||
export const dataPurgeConfigurationType = Type.Object({}); | ||
export const dataPurgeConfigurationType = Type.Object({ | ||
skipCommentsWhileAssigned: Type.Union([Type.Literal("all"), Type.Literal("exact"), Type.Literal("none")], { | ||
default: "all", | ||
description: | ||
"Configures how user comments are included in the rewards calculation when they are assigned to a GitHub issue:\n\n" + | ||
"- 'all': Excludes all comments made between the first assignment start and the last assignment end, discouraging gaming by un-assigning and commenting for rewards.\n" + | ||
"- 'exact': Excludes only comments made during precise assignment periods, targeting times when the user is actively assigned.\n" + | ||
"- 'none': Includes all comments, regardless of assignment status or timing.", | ||
examples: ["all", "exact", "none"], | ||
}), | ||
}); | ||
|
||
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,77 @@ | ||
import { IssueParams } from "../start"; | ||
import { ContextPlugin } from "../types/plugin-input"; | ||
import { IssueActivity } from "../issue-activity"; | ||
|
||
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: IssueActivity["allComments"][0], | ||
assignments: AssignmentPeriod[], | ||
isExact: boolean | ||
) { | ||
const commentDate = new Date(comment.created_at); | ||
if (!assignments?.length) { | ||
return false; | ||
} | ||
if (!isExact) { | ||
const assignedAt = new Date(assignments[0].assignedAt); | ||
const lastAssignment = assignments[assignments.length - 1].unassignedAt; | ||
const unassignedAt = lastAssignment ? new Date(lastAssignment) : new Date(); | ||
return commentDate >= assignedAt && commentDate <= unassignedAt; | ||
} | ||
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