From fc5a63b3267b6b0530c88186600eb1330cc204d3 Mon Sep 17 00:00:00 2001 From: ishowvel Date: Wed, 18 Dec 2024 17:43:15 +0530 Subject: [PATCH] feat: add support for priority label --- src/helpers/github.ts | 17 +++++++++++++++++ src/parser/formatting-evaluator-module.ts | 20 ++------------------ src/parser/github-comment-module.ts | 3 ++- src/parser/review-incentivizer-module.ts | 3 +++ src/types/results.ts | 1 + tests/priority.test.ts | 22 ++++------------------ 6 files changed, 29 insertions(+), 37 deletions(-) diff --git a/src/helpers/github.ts b/src/helpers/github.ts index 2cfb3eb5..48329633 100644 --- a/src/helpers/github.ts +++ b/src/helpers/github.ts @@ -1,5 +1,22 @@ import * as github from "@actions/github"; +import { GitHubIssue } from "../github-types"; export function getGithubWorkflowRunUrl() { return `${github.context.payload.repository?.html_url}/actions/runs/${github.context.runId}`; } + +export function parsePriorityLabel(labels: GitHubIssue["labels"] | undefined) { + if (!labels) return 1; + + for (const label of labels) { + const priorityLabel = typeof label === "string" ? label : (label.name ?? ""); + const matched = priorityLabel.match(/^Priority:\s*(\d+)/i); + + if (matched) { + const urgency = Number(matched[1]); + if (urgency) return urgency; + } + } + + return 1; +} diff --git a/src/parser/formatting-evaluator-module.ts b/src/parser/formatting-evaluator-module.ts index 06796d92..0e22f239 100644 --- a/src/parser/formatting-evaluator-module.ts +++ b/src/parser/formatting-evaluator-module.ts @@ -14,7 +14,7 @@ import { BaseModule } from "../types/module"; import { GithubCommentScore, Result, WordResult } from "../types/results"; import { typeReplacer } from "../helpers/result-replacer"; import { ContextPlugin } from "../types/plugin-input"; -import { GitHubIssue } from "../github-types"; +import { parsePriorityLabel } from "../helpers/github"; interface Multiplier { multiplier: number; @@ -69,7 +69,7 @@ export class FormattingEvaluatorModule extends BaseModule { const { formatting, words } = this._getFormattingScore(comment); const multiplierFactor = this._multipliers?.[comment.type] ?? { multiplier: 0 }; const formattingTotal = this._calculateFormattingTotal(formatting, words, multiplierFactor).toDecimalPlaces(2); - const priority = this._parsePriorityLabel(data.self?.labels); + const priority = parsePriorityLabel(data.self?.labels); const reward = (comment.score?.reward ? formattingTotal.add(comment.score.reward) : formattingTotal).toNumber(); comment.score = { ...comment.score, @@ -192,20 +192,4 @@ export class FormattingEvaluatorModule extends BaseModule { return { formatting, words }; } - - _parsePriorityLabel(labels: GitHubIssue["labels"] | undefined) { - if (!labels) return 1; - - for (const label of labels) { - const priorityLabel = typeof label === "string" ? label : (label.name ?? ""); - const matched = priorityLabel.match(/^Priority:\s*(\d+)/i); - - if (matched) { - const urgency = Number(matched[1]); - if (urgency) return urgency; - } - } - - return 1; - } } diff --git a/src/parser/github-comment-module.ts b/src/parser/github-comment-module.ts index 0a901682..d3b43be5 100644 --- a/src/parser/github-comment-module.ts +++ b/src/parser/github-comment-module.ts @@ -296,6 +296,7 @@ export class GithubCommentModule extends BaseModule { return ` +${review.effect.addition} -${review.effect.deletion} + ${review.priority ?? "-"} ${(review.effect.addition + review.effect.deletion) / baseRate} `; } @@ -307,8 +308,8 @@ export class GithubCommentModule extends BaseModule { - + diff --git a/src/parser/review-incentivizer-module.ts b/src/parser/review-incentivizer-module.ts index 70eec436..cf6d580c 100644 --- a/src/parser/review-incentivizer-module.ts +++ b/src/parser/review-incentivizer-module.ts @@ -10,6 +10,7 @@ import { ContextPlugin } from "../types/plugin-input"; import { collectLinkedMergedPulls } from "../data-collection/collect-linked-pulls"; import { getPullRequestReviews } from "../start"; import { GitHubPullRequestReviewState } from "../github-types"; +import { parsePriorityLabel } from "../helpers/github"; interface CommitDiff { [fileName: string]: { @@ -92,6 +93,7 @@ export class ReviewIncentivizerModule extends BaseModule { async fetchReviewDiffRewards(owner: string, repo: string, reviewsByUser: GitHubPullRequestReviewState[]) { const reviews: ReviewScore[] = []; + const priority = parsePriorityLabel(this.context.payload.issue.labels); for (let i = 0; i < reviewsByUser.length; i++) { const currentReview = reviewsByUser[i]; @@ -116,6 +118,7 @@ export class ReviewIncentivizerModule extends BaseModule { reviewId: currentReview.id, effect: reviewEffect, reward: reviewEffect.addition + reviewEffect.deletion, + priority: priority, }); } catch (e) { this.context.logger.error(`Failed to get diff between commits ${baseSha} and ${headSha}:`, { e }); diff --git a/src/types/results.ts b/src/types/results.ts index f83f7746..98adf850 100644 --- a/src/types/results.ts +++ b/src/types/results.ts @@ -26,6 +26,7 @@ export interface WordResult { } export interface ReviewScore { + priority: number; reviewId: number; effect: { addition: number; diff --git a/tests/priority.test.ts b/tests/priority.test.ts index f122cf07..9cb5fe4c 100644 --- a/tests/priority.test.ts +++ b/tests/priority.test.ts @@ -1,31 +1,17 @@ -import { ContextPlugin } from "../src/types/plugin-input"; -import { FormattingEvaluatorModule } from "../src/parser/formatting-evaluator-module"; import { GitHubIssue } from "../src/github-types"; -import { describe, expect, it, jest } from "@jest/globals"; +import { describe, expect, it } from "@jest/globals"; +import { parsePriorityLabel } from "../src/helpers/github"; describe("FormattingEvaluatorModule", () => { - const context = { - config: { - incentives: { - formattingEvaluator: null, - }, - }, - logger: { - error: jest.fn(), - }, - } as unknown as ContextPlugin; - - const module = new FormattingEvaluatorModule(context); - it("should default to priority 1 when no priority label is present", () => { const labels: GitHubIssue["labels"] = []; - const priority = module["_parsePriorityLabel"](labels); + const priority = parsePriorityLabel(labels); expect(priority).toBe(1); }); it('should return priority 3 when "Priority: 3" label is present', () => { const labels = ["Priority: 3"]; - const priority = module["_parsePriorityLabel"](labels); + const priority = parsePriorityLabel(labels); expect(priority).toBe(3); }); });
Review ID ChangesPriority Reward