Skip to content

Commit

Permalink
feat: add support for priority label
Browse files Browse the repository at this point in the history
  • Loading branch information
ishowvel committed Dec 18, 2024
1 parent c34ee34 commit fc5a63b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 37 deletions.
17 changes: 17 additions & 0 deletions src/helpers/github.ts
Original file line number Diff line number Diff line change
@@ -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;
}
20 changes: 2 additions & 18 deletions src/parser/formatting-evaluator-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion src/parser/github-comment-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ export class GithubCommentModule extends BaseModule {
return `
<tr>
<td>+${review.effect.addition} -${review.effect.deletion}</td>
<td>${review.priority ?? "-"}</td>
<td>${(review.effect.addition + review.effect.deletion) / baseRate}</td>
</tr>`;
}
Expand All @@ -307,8 +308,8 @@ export class GithubCommentModule extends BaseModule {
<table>
<thead>
<tr>
<th>Review ID</th>
<th>Changes</th>
<th>Priority</th>
<th>Reward</th>
</tr>
</thead>
Expand Down
3 changes: 3 additions & 0 deletions src/parser/review-incentivizer-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]: {
Expand Down Expand Up @@ -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];
Expand All @@ -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 });
Expand Down
1 change: 1 addition & 0 deletions src/types/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface WordResult {
}

export interface ReviewScore {
priority: number;
reviewId: number;
effect: {
addition: number;
Expand Down
22 changes: 4 additions & 18 deletions tests/priority.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit fc5a63b

Please sign in to comment.