From a78d5e4a5c439d0d65e559200f5aaa8edb21c4d2 Mon Sep 17 00:00:00 2001 From: EtherealGlow <139999816+EtherealGlow@users.noreply.github.com> Date: Sat, 30 Dec 2023 07:34:33 +0000 Subject: [PATCH] fix: some issues --- src/helpers/calculateReward.ts | 48 ++++++++++++++++++++++++++++---- src/helpers/excel.ts | 27 ------------------ src/helpers/github.ts | 12 ++++++++ src/helpers/issue.ts | 35 +++++++++++++++++++++-- src/index.ts | 51 ++++++++++++++++++++++------------ 5 files changed, 119 insertions(+), 54 deletions(-) diff --git a/src/helpers/calculateReward.ts b/src/helpers/calculateReward.ts index b099e27..375c2e4 100644 --- a/src/helpers/calculateReward.ts +++ b/src/helpers/calculateReward.ts @@ -1,7 +1,32 @@ import { UserType } from "../types/github"; -import { getAllIssueComments, getCompletedIssues, getIssueAssignee, parsePermit } from "./issue"; +import { getAllIssueComments, getCompletedIssues, getIssueAssignee, getOrgRepositories } from "./issue"; import { AssigneeRewardMap } from "../types/miscellaneous"; -import { mergeRewards } from "./excel"; + +export function mergeRewards(map1: AssigneeRewardMap, map2: AssigneeRewardMap): AssigneeRewardMap { + const result: AssigneeRewardMap = {}; + + // Merge map1 into the result + for (const assignee in map1) { + if (map1.hasOwnProperty(assignee)) { + result[assignee] = { + reward: (result[assignee]?.reward || 0) + map1[assignee].reward, + permit: (result[assignee]?.permit || []).concat(map1[assignee].permit), + }; + } + } + + // Merge map2 into the result + for (const assignee in map2) { + if (map2.hasOwnProperty(assignee)) { + result[assignee] = { + reward: (result[assignee]?.reward || 0) + map2[assignee].reward, + permit: (result[assignee]?.permit || []).concat(map2[assignee].permit), + }; + } + } + + return result; +} export async function parseRewards(owner: string, repo: string, issueNumber: number): Promise { const rewardMap: AssigneeRewardMap = {}; @@ -19,7 +44,7 @@ export async function parseRewards(owner: string, repo: string, issueNumber: num const assigneeOrDefault = assignee || "null"; const finalReward = isNaN(reward) ? 0 : reward; - const permitComment = parsePermit(comment.body); + const permitComment = comment.html_url; if (rewardMap[assigneeOrDefault]) { if (rewardMap[assigneeOrDefault].reward) { @@ -46,7 +71,7 @@ export async function parseRewards(owner: string, repo: string, issueNumber: num if (match) { const username = match[1].trim(); const reward = match[2].trim(); - const permitComment = parsePermit(comment.body); + const permitComment = comment.html_url; if (rewardMap[username]) { rewardMap[username].reward = (rewardMap[username].reward ?? 0) + parseFloat(reward); @@ -69,6 +94,10 @@ export async function parseRewards(owner: string, repo: string, issueNumber: num return rewardMap; } +export async function calculateIssueReward(owner: string, repo: string, issueNumber: number): Promise { + return await parseRewards(owner, repo, issueNumber); +} + export async function calculateRepoReward(owner: string, repo: string) { const issues = await getCompletedIssues(owner, repo); let totalReward: AssigneeRewardMap = {}; @@ -80,6 +109,13 @@ export async function calculateRepoReward(owner: string, repo: string) { return totalReward; } -export async function calculateIssueReward(owner: string, repo: string, issueNumber: number): Promise { - return await parseRewards(owner, repo, issueNumber); +export async function calculateOrgReward(owner: string) { + const repos = await getOrgRepositories(owner); + let totalReward: AssigneeRewardMap = {}; + for (let i = 0; i < repos.length; i++) { + console.log(repos[i]); + const reward = await calculateRepoReward(owner, repos[i]); + totalReward = mergeRewards(totalReward, reward); + } + return totalReward; } diff --git a/src/helpers/excel.ts b/src/helpers/excel.ts index 48d9921..adabc41 100644 --- a/src/helpers/excel.ts +++ b/src/helpers/excel.ts @@ -1,5 +1,4 @@ import { createWriteStream } from "fs"; -import { AssigneeRewardMap } from "../types/miscellaneous"; function getMaxPermits(data: Record): number { let maxPermits = 0; @@ -39,29 +38,3 @@ export async function convertToCSV(data: Record => { - const octokit = new Octokit(); const result: Comment[] = []; let shouldFetch = true; let pageNumber = 1; @@ -79,7 +80,6 @@ export const getAllIssueComments = async ( }; export async function getIssueAssignee(owner: string, repo: string, issueNumber: number): Promise { - const octokit = new Octokit(); try { const response = await octokit.rest.issues.get({ owner, @@ -104,3 +104,32 @@ export function parsePermit(comment: string) { return ""; } } + +export async function getOrgRepositories(org: string) { + try { + const response = await octokit.request("GET /orgs/{org}/repos", { + org: org, + headers: { + "X-GitHub-Api-Version": "2022-11-28", + }, + }); + const repositories = response.data.map((repo) => repo.name); + return repositories; + } catch (error) { + console.error("Error fetching repositories:", error); + return []; + } +} + +export async function getUserRepositories(username: string) { + try { + const response = await octokit.rest.repos.listForUser({ + username, + }); + const repositories = response.data.map((repo) => repo.name); + return repositories; + } catch (error) { + console.error("Error fetching repositories:", error); + return []; + } +} diff --git a/src/index.ts b/src/index.ts index c555b9a..2fbdf81 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,30 +1,45 @@ -import { calculateIssueReward, calculateRepoReward } from "./helpers/calculateReward"; +import { calculateIssueReward, calculateOrgReward, calculateRepoReward } from "./helpers/calculateReward"; import { convertToCSV } from "./helpers/excel"; -import { checkIfRepoExists } from "./helpers/github"; +import { checkIfRepoExists, userExists } from "./helpers/github"; if (process.argv[2] == "--help") { console.log( - "Usage: 'npx tsx src/index.ts ubiquity/ubiquibot' for the entire repo\n\nUsage: 'npx tsx src/index.ts ubiquity/ubiquibot' 223\nThe above command outputs the total reward for the issue with the number 223" + "Usage: 'npx tsx src/index.ts ubiquity/ubiquibot' for the entire repo\n\nUsage: 'npx tsx src/index.ts ubiquity/ubiquibot' 223\nThe above command outputs the total reward for the issue with the number 223\n" ); + console.log("Usage: 'npx tsx src/index.ts ubiquity' for the entire user"); process.exit(); } -const regexPattern = /^([\w-]+)\/([\w-]+)$/; -const match = process.argv[2].match(regexPattern); -const owner = match ? match[1] : null; -const repo = match ? match[2] : null; - -if (!repo || !owner) { - console.log("The repository you have provided does not match the pattern owner/repo"); +if (!process.argv[2]) { + console.log("Please provide argumet in terms of owner/repo or just owner"); process.exit(); } -// eslint-disable-next-line @typescript-eslint/naming-convention -const exists = await checkIfRepoExists(owner, repo); -if (!exists) { - console.log("The repository you have provided does not exist"); - process.exit(); + +const match = process.argv[2].split("/"); + +if (match.length > 2) { + console.log("The argument you have provided you have provided does not match the syntax owner/repo or owner"); +} + +const owner = match[0]; +const repo = match[1] ?? null; + +if (!repo) { + if (await userExists(owner)) { + const rewardData = await calculateOrgReward(owner); + await convertToCSV(rewardData); + } +} else { + // eslint-disable-next-line @typescript-eslint/naming-convention + const exists = await checkIfRepoExists(owner, repo); + if (!exists) { + console.log("The repository you have provided does not exist"); + process.exit(); + } + + const issueNumber = Number(process.argv[3]) ?? null; + const rewardData = issueNumber ? await calculateIssueReward(owner, repo, issueNumber) : await calculateRepoReward(owner, repo); + await convertToCSV(rewardData); } -const issueNumber = Number(process.argv[3]) ?? null; -const rewardData = issueNumber ? await calculateIssueReward(owner, repo, issueNumber) : await calculateRepoReward(owner, repo); -await convertToCSV(rewardData); +console.log(await userExists(owner));