Skip to content

Commit

Permalink
fix: some cases slipping and output dir not being found
Browse files Browse the repository at this point in the history
  • Loading branch information
EtherealGlow committed Jan 2, 2024
1 parent f6d513a commit acffea5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
54 changes: 28 additions & 26 deletions src/helpers/calculateReward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,34 @@ export async function parseRewards(owner: string, repo: string, issueNumber: num
const assignee = await getIssueAssignee(owner, repo, issueNumber);

payComments.forEach((comment) => {
if (comment.body.includes("#### Task Assignee Reward")) {
// for conv or task creator rewards
if (comment.body.includes("#### Conversation Rewards") || comment.body.includes("#### Task Creator Reward")) {
const regex = /(?:\*\*([^:]+):\s*\[ CLAIM ([\d.]+)\s*(\w+) \])/;
const match = comment.body.match(regex);

if (match) {
const username = match[1].trim();
const reward = match[2].trim();
const permitComment = comment.html_url;

console.log(`Processing reward for user ${username}`);
if (rewardMap[username]) {
rewardMap[username].reward = (rewardMap[username].reward ?? 0) + parseFloat(reward);

if (!rewardMap[username].permit) {
rewardMap[username].permit = [];
}

rewardMap[username].permit.push(permitComment);
} else {
rewardMap[username] = {
reward: parseFloat(reward),
permit: [permitComment],
};
}
}
} else {
// assignee rewards
const match = comment.body.match(/CLAIM (\d+(\.\d+)?)/);
const reward = match?.[1] ? parseFloat(match[1]) : 0;

Expand All @@ -70,31 +97,6 @@ export async function parseRewards(owner: string, repo: string, issueNumber: num
permit: [permitComment],
};
}
} else {
const regex = /(?:\*\*([^:]+):\s*\[ CLAIM ([\d.]+)\s*(\w+) \])/;
const match = comment.body.match(regex);

if (match) {
const username = match[1].trim();
const reward = match[2].trim();
const permitComment = comment.html_url;

console.log(`Processing reward for user ${username}`);
if (rewardMap[username]) {
rewardMap[username].reward = (rewardMap[username].reward ?? 0) + parseFloat(reward);

if (!rewardMap[username].permit) {
rewardMap[username].permit = [];
}

rewardMap[username].permit.push(permitComment);
} else {
rewardMap[username] = {
reward: parseFloat(reward),
permit: [permitComment],
};
}
}
}
});

Expand Down
5 changes: 4 additions & 1 deletion src/helpers/excel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createWriteStream } from "fs";
import { createDirectoryIfNotExists } from "./file";

function getMaxPermits(data: Record<string, { reward: number; permit: string[] }>): number {
let maxPermits = 0;
Expand All @@ -13,8 +14,10 @@ function getMaxPermits(data: Record<string, { reward: number; permit: string[] }
return maxPermits;
}

export async function convertToCSV(data: Record<string, { reward: number; permit: string[] }>, outputPath: string = "output/output.csv"): Promise<void> {
export async function convertToCSV(data: Record<string, { reward: number; permit: string[] }>): Promise<void> {
const csvData: string[] = [];
const outputPath = "output/output.csv";
createDirectoryIfNotExists("output");

// Push the header row
csvData.push(["assignee", "reward", ...Array.from({ length: getMaxPermits(data) }, (a, i) => `permit${i + 1}`)].join(","));
Expand Down
10 changes: 10 additions & 0 deletions src/helpers/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import fs from "fs";

export function createDirectoryIfNotExists(directoryPath: string): void {
if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath);
console.log(`Directory "${directoryPath}" created.`);
} else {
console.log(`Directory "${directoryPath}" already exists.`);
}
}

0 comments on commit acffea5

Please sign in to comment.