Skip to content

Commit

Permalink
feat: set db to be sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Jul 3, 2024
1 parent bb001cf commit 2dbe73b
Show file tree
Hide file tree
Showing 18 changed files with 1,049 additions and 294 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ coverage
junit.xml
cypress/screenshots
script.ts
.wrangler
.wrangler
test-dashboard.md
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
"@supabase/supabase-js": "2.43.5",
"@ubiquity-dao/ubiquibot-logger": "1.1.2",
"dotenv": "16.4.5",
"typebox-validators": "0.3.5"
"reflect-metadata": "0.2.2",
"sqlite3": "5.1.7",
"typebox-validators": "0.3.5",
"typeorm": "0.3.20"
},
"devDependencies": {
"@commitlint/cli": "19.3.0",
Expand Down
45 changes: 45 additions & 0 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as github from "@actions/github";
import { Octokit } from "@octokit/rest";
import { Value } from "@sinclair/typebox/value";
import { plugin } from "./plugin";
import { envSchema, PluginInputs, pluginSettingsSchema, pluginSettingsValidator } from "./types";

/**
* How a GitHub action executes the plugin.
*/
export async function run() {
const payload = github.context.payload.inputs;

const env = Value.Decode(envSchema, payload.env);
const settings = Value.Decode(pluginSettingsSchema, Value.Default(pluginSettingsSchema, JSON.parse(payload.settings)));

if (!pluginSettingsValidator.test(settings)) {
throw new Error("Invalid settings provided");
}

const inputs: PluginInputs = {
stateId: payload.stateId,
eventName: payload.eventName,
eventPayload: JSON.parse(payload.eventPayload),
settings,
authToken: payload.authToken,
ref: payload.ref,
};

await plugin(inputs, env);

return returnDataToKernel(inputs.authToken, inputs.stateId, {});
}

async function returnDataToKernel(repoToken: string, stateId: string, output: object) {
const octokit = new Octokit({ auth: repoToken });
return 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),
},
});
}
18 changes: 6 additions & 12 deletions src/adapters/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { SupabaseClient } from "@supabase/supabase-js";
import { Context } from "../types/context";
import { Access } from "./supabase/helpers/access";
import { User } from "./supabase/helpers/user";
import { Label } from "./supabase/helpers/label";
import { Super } from "./supabase/helpers/supabase";
import { DataSource } from "typeorm";
import { Context } from "../types";
import { PullRequestAdapter } from "./sqlite/helpers/pull-request-adapter";

export function createAdapters(supabaseClient: SupabaseClient, context: Context) {
export function createAdapters(sqlClient: DataSource, context: Context) {
return {
supabase: {
access: new Access(supabaseClient, context),
user: new User(supabaseClient, context),
label: new Label(supabaseClient, context),
super: new Super(supabaseClient, context),
sqlite: {
pullRequest: new PullRequestAdapter(sqlClient, context),
},
};
}
15 changes: 15 additions & 0 deletions src/adapters/sqlite/data-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DataSource } from "typeorm";
import { PullRequest } from "./entities/pull-request";

export async function initializeDataSource(databaseUrl: string) {
const dataSource = new DataSource({
type: "sqlite",
database: databaseUrl,
synchronize: true,
logging: true,
entities: [PullRequest],
subscribers: [],
migrations: [],
});
return dataSource.initialize();
}
10 changes: 10 additions & 0 deletions src/adapters/sqlite/entities/pull-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class PullRequest extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;

@Column({ unique: true })
url: string;
}
9 changes: 9 additions & 0 deletions src/adapters/sqlite/helpers/pull-request-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { DataSource } from "typeorm";
import { Context } from "../../../types";
import { Super } from "./sqlite";

export class PullRequestAdapter extends Super {
constructor(sqlite: DataSource, context: Context) {
super(sqlite, context);
}
}
12 changes: 12 additions & 0 deletions src/adapters/sqlite/helpers/sqlite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { DataSource } from "typeorm";
import { Context } from "../../../types";

export class Super {
protected sqlite: DataSource;
protected context: Context;

constructor(sqlite: DataSource, context: Context) {
this.sqlite = sqlite;
this.context = context;
}
}
49 changes: 0 additions & 49 deletions src/adapters/supabase/helpers/access.ts

This file was deleted.

52 changes: 0 additions & 52 deletions src/adapters/supabase/helpers/label.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/adapters/supabase/helpers/supabase.ts

This file was deleted.

84 changes: 0 additions & 84 deletions src/adapters/supabase/helpers/user.ts

This file was deleted.

46 changes: 1 addition & 45 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,5 @@
import * as core from "@actions/core";
import * as github from "@actions/github";
import { Octokit } from "@octokit/rest";
import { Value } from "@sinclair/typebox/value";
import { envSchema, pluginSettingsSchema, PluginInputs, pluginSettingsValidator } from "./types";
import { plugin } from "./plugin";

/**
* How a GitHub action executes the plugin.
*/
export async function run() {
const payload = github.context.payload.inputs;

const env = Value.Decode(envSchema, payload.env);
const settings = Value.Decode(pluginSettingsSchema, Value.Default(pluginSettingsSchema, JSON.parse(payload.settings)));

if (!pluginSettingsValidator.test(settings)) {
throw new Error("Invalid settings provided");
}

const inputs: PluginInputs = {
stateId: payload.stateId,
eventName: payload.eventName,
eventPayload: JSON.parse(payload.eventPayload),
settings,
authToken: payload.authToken,
ref: payload.ref,
};

await plugin(inputs, env);

return returnDataToKernel(inputs.authToken, inputs.stateId, {});
}

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),
},
});
}
import { run } from "./action";

run()
.then((result) => {
Expand Down
Loading

0 comments on commit 2dbe73b

Please sign in to comment.