Skip to content

Commit

Permalink
Update generate-permits-from-context.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
surafeldev authored Dec 21, 2024
1 parent 8dfb96f commit c4590fa
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions src/generate-permits-from-context.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// src/generate-permits-from-context.ts

import * as github from "@actions/github";
import { Octokit } from "@octokit/rest";
import { Value } from "@sinclair/typebox/value";
import { createClient } from "@supabase/supabase-js";
import { createAdapters } from "./adapters";
import { Database } from "./adapters/supabase/types/database";
import { generatePayoutPermit } from "./handlers";
import { generateErc20Permit } from "./handlers/generate-erc20-permit";
import { registerWallet } from "./handlers/register-wallet";
import { Context } from "./types/context";
import { envSchema } from "./types/env";
import { permitGenerationSettingsSchema, PluginInputs } from "./types/plugin-input";
import { PluginInput, PluginOutput } from "./types/plugin-interfaces";

/**
* Generates all the permits based on the currently populated context.
Expand All @@ -27,6 +30,7 @@ export async function generatePermitsFromContext() {
authToken: webhookPayload.authToken,
ref: webhookPayload.ref,
};

const octokit = new Octokit({ auth: inputs.authToken });
const supabaseClient = createClient<Database>(env.SUPABASE_URL, env.SUPABASE_KEY);

Expand Down Expand Up @@ -61,14 +65,50 @@ export async function generatePermitsFromContext() {
if (context.eventName === "issue_comment.created") {
await handleSlashCommands(context, octokit);
} else {
const permits = await generatePayoutPermit(context, settings.permitRequests);
await returnDataToKernel(env.GITHUB_TOKEN, inputs.stateId, permits);
return JSON.stringify(permits);
const pluginInput: PluginInput = {
eventContext: {
eventName: inputs.eventName,
payload: inputs.eventPayload,
config: inputs.settings,
env,
},
inputValue: JSON.stringify(inputs.settings.permitRequests),
};

const erc20Output = await generateErc20Permit(pluginInput);

const pluginOutputs: PluginOutput[] = [erc20Output];

await aggregatePluginOutputs(pluginOutputs);
await returnDataToKernel(env.GITHUB_TOKEN, inputs.stateId, pluginOutputs);
return JSON.stringify(pluginOutputs);
}

return null;
}

async function aggregatePluginOutputs(pluginOutputs: PluginOutput[]): Promise<void> {
const totalRewards = pluginOutputs.flatMap(output => output.rewards);
const commentOutputs = pluginOutputs.map(output => output.commentOutput).join("\n");

// Sum the rewards and post a single comment
const totalRewardAmount = totalRewards.reduce((sum, reward) => sum + reward.amount, 0);
const finalComment = `<div>${commentOutputs}</div><p>Total Reward: ${totalRewardAmount}</p>`;

// Post the final comment
await postComment(finalComment);
}

async function postComment(comment: string): Promise<void> { //
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });
await octokit.rest.issues.createComment({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: github.context.issue.number,
body: comment,
});
}

async function returnDataToKernel(repoToken: string, stateId: string, output: object) {
const octokit = new Octokit({ auth: repoToken });
await octokit.rest.repos.createDispatchEvent({
Expand Down

0 comments on commit c4590fa

Please sign in to comment.