Skip to content

Commit

Permalink
refactor: streamline comment skipping logic in data purge
Browse files Browse the repository at this point in the history
Consolidate comment skipping checks into a single method for clarity.
  • Loading branch information
gentlementlegen committed Nov 21, 2024
1 parent 6b2ad91 commit 44309a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
7 changes: 2 additions & 5 deletions src/helpers/user-assigned-timespans.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GitHubIssue, GitHubIssueComment, GitHubPullRequest, GitHubPullRequestReviewComment } from "../github-types";
import { IssueParams } from "../start";
import { ContextPlugin } from "../types/plugin-input";
import { IssueActivity } from "../issue-activity";

export interface AssignmentPeriod {
assignedAt: string;
Expand Down Expand Up @@ -54,10 +54,7 @@ export async function getAssignmentPeriods(octokit: ContextPlugin["octokit"], is
return userAssignments;
}

export function isCommentDuringAssignment(
comment: GitHubIssueComment | GitHubPullRequestReviewComment | GitHubIssue | GitHubPullRequest,
assignments: AssignmentPeriod[]
) {
export function isCommentDuringAssignment(comment: IssueActivity["allComments"][0], assignments: AssignmentPeriod[]) {
const commentDate = new Date(comment.created_at);
if (!assignments?.length) {
return false;
Expand Down
35 changes: 22 additions & 13 deletions src/parser/data-purge-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { GitHubPullRequestReviewComment } from "../github-types";
import { IssueActivity } from "../issue-activity";
import { BaseModule } from "../types/module";
import { Result } from "../types/results";
import { getAssignmentPeriods, isCommentDuringAssignment } from "../helpers/user-assigned-timespans";
import { getAssignmentPeriods, isCommentDuringAssignment, UserAssignments } from "../helpers/user-assigned-timespans";
import { parseGitHubUrl } from "../start";

/**
* Removes the data in the comments that we do not want to be processed.
*/
export class DataPurgeModule extends BaseModule {
readonly _configuration: DataPurgeConfiguration | null = this.context.config.incentives.dataPurge;
_assignmentPeriods: UserAssignments = {};

get enabled(): boolean {
if (!this._configuration) {
Expand All @@ -20,25 +21,33 @@ export class DataPurgeModule extends BaseModule {
return true;
}

async _shouldSkipComment(comment: IssueActivity["allComments"][0]) {
if ("isMinimized" in comment && comment.isMinimized) {
this.context.logger.debug("Skipping hidden comment", { comment });
return true;
}
if (
this._configuration?.skipCommentsWhileAssigned &&
comment.user?.login &&
isCommentDuringAssignment(comment, this._assignmentPeriods[comment.user?.login])
) {
this.context.logger.debug("Skipping comment during assignment", {
comment,
});
return true;
}
return false;
}

async transform(data: Readonly<IssueActivity>, result: Result) {
const assignmentPeriods = await getAssignmentPeriods(
this._assignmentPeriods = await getAssignmentPeriods(
this.context.octokit,
parseGitHubUrl(this.context.payload.issue.html_url)
);
for (const comment of data.allComments) {
// Skips comments if they are minimized
if ("isMinimized" in comment && comment.isMinimized) {
this.context.logger.debug("Skipping hidden comment", { comment });
if (await this._shouldSkipComment(comment)) {
continue;
}
if (this._configuration?.skipCommentsWhileAssigned) {
if (comment.user?.login && isCommentDuringAssignment(comment, assignmentPeriods[comment.user?.login])) {
this.context.logger.debug("Skipping comment during assignment", {
comment,
});
continue;
}
}
if (comment.body && comment.user?.login && result[comment.user.login]) {
const newContent = comment.body
// Remove quoted text
Expand Down

0 comments on commit 44309a8

Please sign in to comment.