generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add permit-generator script [WIP]
- Loading branch information
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
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 "../src/adapters"; | ||
import { Database } from "../src/adapters/supabase/types/database"; | ||
import { generatePayoutPermit } from "../src/handlers"; | ||
import { Context } from "../src/types/context"; | ||
import { envSchema } from "../src/types/env"; | ||
import { permitGenerationSettingsSchema, PluginInputs } from "../src/types/plugin-input"; | ||
|
||
/** | ||
* Generates all the permits based on the current github workflow dispatch. | ||
*/ | ||
export async function generatePermitsFromGithubWorkflowDispatch() { | ||
const webhookPayload = github.context.payload.inputs; | ||
|
||
const env = Value.Decode(envSchema, process.env); | ||
const settings = Value.Decode(permitGenerationSettingsSchema, JSON.parse(webhookPayload.settings)); | ||
|
||
const inputs: PluginInputs = { | ||
stateId: webhookPayload.stateId, | ||
eventName: webhookPayload.eventName, | ||
eventPayload: JSON.parse(webhookPayload.eventPayload), | ||
settings: settings, | ||
authToken: webhookPayload.authToken, | ||
ref: webhookPayload.ref, | ||
}; | ||
const octokit = new Octokit({ auth: inputs.authToken }); | ||
const supabaseClient = createClient<Database>(env.SUPABASE_URL, env.SUPABASE_KEY); | ||
|
||
const context: Context = { | ||
eventName: inputs.eventName, | ||
payload: inputs.eventPayload, | ||
config: inputs.settings, | ||
octokit, | ||
env, | ||
logger: { | ||
debug(message: unknown, ...optionalParams: unknown[]) { | ||
console.debug(message, ...optionalParams); | ||
}, | ||
info(message: unknown, ...optionalParams: unknown[]) { | ||
console.log(message, ...optionalParams); | ||
}, | ||
warn(message: unknown, ...optionalParams: unknown[]) { | ||
console.warn(message, ...optionalParams); | ||
}, | ||
error(message: unknown, ...optionalParams: unknown[]) { | ||
console.error(message, ...optionalParams); | ||
}, | ||
fatal(message: unknown, ...optionalParams: unknown[]) { | ||
console.error(message, ...optionalParams); | ||
}, | ||
}, | ||
adapters: {} as ReturnType<typeof createAdapters>, | ||
}; | ||
|
||
context.adapters = createAdapters(supabaseClient, context); | ||
|
||
const permits = await generatePayoutPermit(context, settings.permitRequests); | ||
await returnDataToKernel(env.GITHUB_TOKEN, inputs.stateId, permits); | ||
return JSON.stringify(permits); | ||
} | ||
|
||
async function returnDataToKernel(repoToken: string, stateId: string, output: object) { | ||
const octokit = new Octokit({ auth: repoToken }); | ||
await octokit.repos.createDispatchEvent({ | ||
owner: github.context.repo.owner, | ||
repo: github.context.repo.repo, | ||
event_type: "return_data_to_ubiquibot_kernel", | ||
client_payload: { | ||
state_id: stateId, | ||
output: JSON.stringify(output), | ||
}, | ||
}); | ||
} |