Skip to content

Commit

Permalink
Merge pull request #1129 from devpanther/statistics-rewards
Browse files Browse the repository at this point in the history
Feat: Statistics rewards
  • Loading branch information
0x4007 committed Mar 12, 2024
2 parents f0780fd + 8a07b59 commit d81380c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 14 deletions.
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
69 changes: 60 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,78 @@ 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;

if (!isNaN(price)) {
// Increment rewards statistics, if it is assigned but not completed
if (isAssigned && !isCompleted) {
rewards.assigned += price;
} else if (!isAssigned && !isCompleted) {
rewards.notAssigned += price;
}

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

rewards.total += price;
} else {
console.error(`Price '${priceLabel.name}' is not a valid number in issue: ${issue.number}`);
}
}
}

// 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;
};
};

1 comment on commit d81380c

@github-actions
Copy link

Choose a reason for hiding this comment

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

Lines Statements Branches Functions
Coverage: 86%
85.29% (203/238) 68.91% (51/74) 94.44% (34/36)

JUnit

Tests Skipped Failures Errors Time
15 0 💤 0 ❌ 0 🔥 10.243s ⏱️
Coverage Report (86%)
File% Stmts% Branch% Funcs% LinesUncovered Line #s
All files85.2968.9194.4486.57 
devpool-directory72.7254.1683.3375.4 
   index.ts72.7254.1683.3375.474–77, 129–153, 161, 169
devpool-directory/helpers90.117696.6690.96 
   github.ts92.6676.7410093.98101, 112, 118, 279, 295, 307, 344, 359
   twitter.ts72.7271.425072.7239–47

Please sign in to comment.