Skip to content

Commit

Permalink
fix: some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
EtherealGlow committed Dec 30, 2023
1 parent 56fb4de commit a78d5e4
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 54 deletions.
48 changes: 42 additions & 6 deletions src/helpers/calculateReward.ts
Original file line number Diff line number Diff line change
@@ -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<AssigneeRewardMap> {
const rewardMap: AssigneeRewardMap = {};
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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<AssigneeRewardMap> {
return await parseRewards(owner, repo, issueNumber);
}

export async function calculateRepoReward(owner: string, repo: string) {
const issues = await getCompletedIssues(owner, repo);
let totalReward: AssigneeRewardMap = {};
Expand All @@ -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<AssigneeRewardMap> {
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;
}
27 changes: 0 additions & 27 deletions src/helpers/excel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createWriteStream } from "fs";
import { AssigneeRewardMap } from "../types/miscellaneous";

function getMaxPermits(data: Record<string, { reward: number; permit: string[] }>): number {
let maxPermits = 0;
Expand Down Expand Up @@ -39,29 +38,3 @@ export async function convertToCSV(data: Record<string, { reward: number; permit
stream.write(csvString);
stream.end();
}

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;
}
12 changes: 12 additions & 0 deletions src/helpers/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ export async function checkIfRepoExists(owner: string, repo: string): Promise<bo
return false;
}
}

export async function userExists(username: string) {
try {
const response = await octokit.rest.users.getByUsername({
username: username,
});
return response.status === 200;
} catch (error) {
console.log("Couldnt find user");
return false;
}
}
35 changes: 32 additions & 3 deletions src/helpers/issue.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Octokit } from "octokit";
import { Comment } from "../types/github";

const octokit = new Octokit();

export async function getCompletedIssues(owner: string, repo: string) {
const octokit = new Octokit();
try {
let page = 1;
const completedIssues = [];
Expand Down Expand Up @@ -40,13 +41,13 @@ export async function getCompletedIssues(owner: string, repo: string) {
}
}

// eslint-disable-next-line func-style
export const getAllIssueComments = async (
owner: string,
repo: string,
issueNumber: number,
format: "raw" | "html" | "text" | "full" = "raw"
): Promise<Comment[]> => {
const octokit = new Octokit();
const result: Comment[] = [];
let shouldFetch = true;
let pageNumber = 1;
Expand Down Expand Up @@ -79,7 +80,6 @@ export const getAllIssueComments = async (
};

export async function getIssueAssignee(owner: string, repo: string, issueNumber: number): Promise<string | undefined> {
const octokit = new Octokit();
try {
const response = await octokit.rest.issues.get({
owner,
Expand All @@ -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 [];
}
}
51 changes: 33 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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));

0 comments on commit a78d5e4

Please sign in to comment.