Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Statistics rewards #1129

Merged
merged 17 commits into from Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/sync-issues.yml
Expand Up @@ -6,7 +6,7 @@ on:
branches:
- development
paths-ignore:
- "**/total-rewards.txt"
- "**/total-rewards.json"
schedule:
- cron: "15 * * * *" # every hour at minute 15 (github recommends to set such cron jobs not at the start of an hour)

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,4 +11,5 @@ node_modules
coverage
coverage.txt
total-rewards.txt
total-rewards.json
junit.xml
65 changes: 56 additions & 9 deletions helpers/github.ts
Expand Up @@ -2,6 +2,7 @@ import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";
import { Octokit } from "@octokit/rest";
import _projects from "../projects.json";
import opt from "../opt.json";
import { Statistics } from "../types/statistics";

export type GitHubIssue = RestEndpointMethodTypes["issues"]["get"]["response"]["data"];
export type GitHubLabel = RestEndpointMethodTypes["issues"]["listLabelsOnIssue"]["response"]["data"][0];
Expand Down Expand Up @@ -251,28 +252,74 @@ export async function getProjectUrls() {
return projectUrls;
}

// Function to calculate total rewards from open issues
export async function calculateTotalRewards(issues: GitHubIssue[]) {
let totalRewards = 0;
// Function to calculate total rewards and tasks statistics
export async function calculateStatistics(issues: GitHubIssue[]) {
const rewards = {
notAssigned: 0,
assigned: 0,
completed: 0,
total: 0,
};

const tasks = {
notAssigned: 0,
assigned: 0,
completed: 0,
total: 0,
};

await issues.forEach((issue) => {
const labels = issue.labels as GitHubLabel[];
if (issue.state === "open" && labels.some((label) => label.name as string)) {
const isAssigned = labels.find((label) => (label.name as string).includes(LABELS.UNAVAILABLE));
const isCompleted = issue.state === "closed";

// Increment tasks statistics
tasks.total++;
if (isAssigned) {
tasks.assigned++;
} else {
tasks.notAssigned++;
}

if (labels.some((label) => label.name as string)) {
const priceLabel = labels.find((label) => (label.name as string).includes("Pricing"));
if (priceLabel) {
// ignore pricing not set
if (priceLabel.name === "Pricing: not set") return;

const price = parseInt((priceLabel.name as string).split(":")[1].trim(), 10);
totalRewards += price;

// Increment rewards statistics, if it is assigned but not completed
if (isAssigned && !isCompleted) {
rewards.assigned += price;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have proper error handling for NaN etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added an error handling now, although i had a check for price not set earlier but NaN check would make it better

} else if (!isAssigned && !isCompleted) {
rewards.notAssigned += price;
}

// Increment completed rewards statistics
if (isCompleted) {
rewards.completed += price;
}

rewards.total += price;
}
}

// Increment completed tasks statistics
if (isCompleted) {
tasks.completed++;
}
});
return totalRewards;

return { rewards, tasks };
}

export async function writeTotalRewardsToGithub(totalRewards: number) {
export async function writeTotalRewardsToGithub(statistics: Statistics) {
try {
const owner = DEVPOOL_OWNER_NAME;
const repo = DEVPOOL_REPO_NAME;
const filePath = "total-rewards.txt";
const content = totalRewards.toString();
const filePath = "total-rewards.json";
const content = JSON.stringify(statistics, null, 2);

let sha: string | undefined; // Initialize sha to undefined

Expand Down
8 changes: 5 additions & 3 deletions index.ts
Expand Up @@ -15,10 +15,11 @@ import {
checkIfForked,
LABELS,
octokit,
calculateTotalRewards,
calculateStatistics,
writeTotalRewardsToGithub,
} from "./helpers/github";
import { readFile, writeFile } from "fs/promises";
import { Statistics } from "./types/statistics";
// init octokit
dotenv.config();

Expand All @@ -41,9 +42,10 @@ async function main() {
const devpoolIssues: GitHubIssue[] = await getAllIssues(DEVPOOL_OWNER_NAME, DEVPOOL_REPO_NAME);

// Calculate total rewards from open issues
const totalRewards = await calculateTotalRewards(devpoolIssues);
const { rewards, tasks } = await calculateStatistics(devpoolIssues);
const statistics: Statistics = { rewards, tasks };

await writeTotalRewardsToGithub(totalRewards);
await writeTotalRewardsToGithub(statistics);

// aggregate projects.urls and opt settings
const projectUrls = await getProjectUrls();
Expand Down
1 change: 0 additions & 1 deletion total-rewards.txt

This file was deleted.

14 changes: 14 additions & 0 deletions types/statistics.ts
@@ -0,0 +1,14 @@
export type Statistics = {
rewards: {
notAssigned: number;
assigned: number;
completed: number;
total: number;
};
tasks: {
notAssigned: number;
assigned: number;
completed: number;
total: number;
};
};