Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
Merge branch 'feat/contributor-incentives-total-scoring-duplicate-fix…
Browse files Browse the repository at this point in the history
…' into refactor/type-locations
  • Loading branch information
0x4007 committed Nov 26, 2023
2 parents 55c68b7 + d4a8794 commit 5cd3d6d
Show file tree
Hide file tree
Showing 5 changed files with 448 additions and 431 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"prettier": "^2.7.1",
"probot": "^12.2.4",
"tsx": "^3.12.7",
"yaml": "^2.2.2"
"yaml": "^2.2.2",
"zlib": "^1.0.5"
},
"devDependencies": {
"@types/dotenv": "^8.2.0",
Expand Down
23 changes: 20 additions & 3 deletions src/bindings/event.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import OpenAI from "openai";
import { Context as ProbotContext } from "probot";
import zlib from "zlib";
import { createAdapters, supabaseClient } from "../adapters/adapters";
import { LogMessage, LogReturn, Logs } from "../adapters/supabase/helpers/tables/logs";
import { processors, wildcardProcessors } from "../handlers/processors";
import { validateConfigChange } from "../handlers/push/push";
import structuredMetadata from "../handlers/shared/structured-metadata";
import { BotConfig } from "../types/configuration-types";

import { addCommentToIssue } from "../helpers/issue";
import { shouldSkip } from "../helpers/shared";
import { Context } from "../types/context";
import {
HandlerReturnValuesNoVoid,
Expand All @@ -20,8 +23,6 @@ import { ajv } from "../utils/ajv";
import { generateConfiguration } from "../utils/generate-configuration";
import Runtime from "./bot-runtime";
import { env } from "./env";
import { shouldSkip } from "../helpers/shared";
import { addCommentToIssue } from "../helpers/issue";

const allowedEvents = Object.values(GitHubEvent) as string[];

Expand All @@ -46,7 +47,7 @@ export async function bindEvents(eventContext: ProbotContext) {

logger.info("Event received", { id: eventContext.id, name: eventName });

if (!allowedEvents.includes(eventName)) {
if (!allowedEvents.includes(eventName) && eventContext.name !== "repository_dispatch") {
// just check if its on the watch list
return logger.info(`Skipping the event. reason: not configured`);
}
Expand Down Expand Up @@ -93,6 +94,22 @@ export async function bindEvents(eventContext: ProbotContext) {
throw new Error("Failed to create logger");
}

if (eventContext.name === GitHubEvent.REPOSITORY_DISPATCH) {
const dispatchPayload = payload as any;
if (payload.action === "issueClosed") {
//This is response for issueClosed request
const response = dispatchPayload.client_payload.result;
if (response.comment as Comment) {
const uncompressedComment = zlib.gunzipSync(Buffer.from(response.comment));
await addCommentToIssue(
context,
uncompressedComment.toString(),
parseInt(dispatchPayload.client_payload.issueNumber)
);
}
}
}

// Get the handlers for the action
const handlers = processors[eventName];

Expand Down
56 changes: 39 additions & 17 deletions src/handlers/comment/handlers/issue/issue-closed.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import Runtime from "../../../../bindings/bot-runtime";
import { env } from "../../../../bindings/env";
import { checkUserPermissionForRepoAndOrg, getAllIssueComments } from "../../../../helpers/issue";
import { Context } from "../../../../types/context";

import { Comment, Issue, Payload, StateReason } from "../../../../types/payload";
import structuredMetadata from "../../../shared/structured-metadata";
import { generatePermits } from "./generate-permits";
import { aggregateAndScoreContributions } from "./scoreSources";
import { sumTotalScores } from "./sumTotalScoresPerContributor";
import { getCollaboratorsForRepo } from "./get-collaborator-ids-for-repo";
import { getPullRequestComments } from "./getPullRequestComments";

export async function issueClosed(context: Context) {
// TODO: delegate permit calculation to GitHub Action
Expand All @@ -19,21 +18,44 @@ export async function issueClosed(context: Context) {

// === Calculate Permit === //

// 1. score sources will credit every contributor for every one of their contributions
const sourceScores = await aggregateAndScoreContributions({
context,
issue,
issueComments,
const pullRequestComments = await getPullRequestComments(context, owner, repository, issueNumber);
const repoCollaborators = await getCollaboratorsForRepo(context);

await dispatchWorkflow(owner, "ubiquibot-config", "compute.yml", {
eventName: "issueClosed",
secretToken: process.env.GITHUB_TOKEN,
owner,
repository,
issueNumber,
repo: repository,
issueNumber: `${issueNumber}`,
payload: JSON.stringify({
issue,
issueComments,
openAiKey: context.config.keys.openAi,
pullRequestComments,
botConfig: context.config,
repoCollaborators,
X25519_PRIVATE_KEY: env.X25519_PRIVATE_KEY,
supabaseUrl: env.SUPABASE_URL,
supabaseKey: env.SUPABASE_KEY,
}),
});
// 2. sum total scores will sum the scores of every contribution, and organize them by contributor
const contributorTotalScores = sumTotalScores(sourceScores);
// 3. generate permits will generate a payment for every contributor
const permitComment = await generatePermits(context, contributorTotalScores);
// 4. return the permit comment
return permitComment;

return "Please wait until we get the result.";
}

export async function dispatchWorkflow(owner: string, repo: string, workflowId: string, inputs: any) {
const res = await fetch(`https://api.github.com/repos/${owner}/${repo}/actions/workflows/${workflowId}/dispatches`, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
Accept: "application/vnd.github.v3+json",
},
body: JSON.stringify({ ref: "master", inputs }),
});
if (res.status !== 204) {
const errorMessage = await res.text();
console.error(errorMessage);
}
}

async function getEssentials(context: Context) {
Expand Down
2 changes: 2 additions & 0 deletions src/types/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export enum GitHubEvent {

// label
LABEL_EDITED = "label.edited",

REPOSITORY_DISPATCH = "repository_dispatch",
}

export enum UserType {
Expand Down
Loading

0 comments on commit 5cd3d6d

Please sign in to comment.