generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,132 additions
and
38 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,19 @@ | ||
import { SupabaseClient } from "@supabase/supabase-js"; | ||
import { Context } from "../types/context"; | ||
import { Access } from "./supabase/helpers/tables/access"; | ||
import { User } from "./supabase/helpers/tables/user"; | ||
import { Label } from "./supabase/helpers/tables/label"; | ||
import { Locations } from "./supabase/helpers/tables/locations"; | ||
import { Super } from "./supabase/helpers/tables/super"; | ||
|
||
export function createAdapters(supabaseClient: SupabaseClient, context: Context) { | ||
return { | ||
supabase: { | ||
access: new Access(supabaseClient, context), | ||
user: new User(supabaseClient, context), | ||
label: new Label(supabaseClient, context), | ||
locations: new Locations(supabaseClient, context), | ||
super: new Super(supabaseClient, context), | ||
}, | ||
}; | ||
} |
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,75 @@ | ||
import { SupabaseClient } from "@supabase/supabase-js"; | ||
import { Database } from "../../types/database"; | ||
import { GitHubNode } from "../../types/github"; | ||
import { Super } from "./super"; | ||
import { UserRow } from "./user"; | ||
import { Context } from "../../../../types/context"; | ||
import { Comment } from "../../../../types/github"; | ||
|
||
type AccessRow = Database["public"]["Tables"]["access"]["Row"]; | ||
type AccessInsert = Database["public"]["Tables"]["access"]["Insert"]; | ||
type UserWithAccess = UserRow & { access: AccessRow[] }; | ||
|
||
type AccessData = { | ||
user_id: number; | ||
multiplier: number; | ||
multiplier_reason: string; | ||
node_id: string; | ||
node_type: string; | ||
node_url: string; | ||
}; | ||
|
||
export class Access extends Super { | ||
constructor(supabase: SupabaseClient, context: Context) { | ||
super(supabase, context); | ||
} | ||
|
||
private async _getUserWithAccess(id: number): Promise<UserWithAccess> { | ||
const { data, error } = await this.supabase.from("users").select("*, access(*)").filter("id", "eq", id).single(); | ||
|
||
if (error) { | ||
this.context.logger.fatal(error.message, error); | ||
throw new Error(error.message); | ||
} | ||
return data; | ||
} | ||
|
||
public async getAccess(id: number): Promise<AccessRow | null> { | ||
const userWithAccess = await this._getUserWithAccess(id); | ||
if (userWithAccess.access.length === 0) { | ||
this.context.logger.debug("No access found for user", { id }); | ||
return null; | ||
} | ||
return userWithAccess.access[0]; | ||
} | ||
|
||
public async setAccess(labels: string[], node: GitHubNode, userId?: number): Promise<null> { | ||
const { data, error } = await this.supabase.from("access").upsert({ | ||
labels: labels, | ||
...node, | ||
user_id: userId, | ||
} as AccessInsert); | ||
if (error) throw new Error(error.message); | ||
return data; | ||
} | ||
|
||
async upsertMultiplier(userId: number, multiplier: number, reason: string, comment: Comment) { | ||
try { | ||
const accessData: AccessData = { | ||
user_id: userId, | ||
multiplier: multiplier, | ||
multiplier_reason: reason, | ||
node_id: comment.node_id, | ||
node_type: "IssueComment", | ||
node_url: comment.html_url, | ||
}; | ||
|
||
const { data, error } = await this.supabase.from("access").upsert(accessData, { onConflict: "location_id" }); | ||
|
||
if (error) throw new Error(error.message); | ||
if (!data) throw new Error("Multiplier not upserted"); | ||
} catch (error) { | ||
console.error("An error occurred while upserting multiplier:", error); | ||
} | ||
} | ||
} |
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,74 @@ | ||
import { SupabaseClient } from "@supabase/supabase-js"; | ||
|
||
import { Database } from "../../types/database"; | ||
import { Super } from "./super"; | ||
import { Context } from "../../../../types/context"; | ||
import { WebhookEvent } from "../../../../types/github"; | ||
|
||
type LabelRow = Database["public"]["Tables"]["labels"]["Row"]; | ||
|
||
export class Label extends Super { | ||
constructor(supabase: SupabaseClient, context: Context) { | ||
super(supabase, context); | ||
} | ||
|
||
async saveLabelChange({ | ||
previousLabel, | ||
currentLabel, | ||
authorized, | ||
repository, | ||
}: { | ||
previousLabel: string; | ||
currentLabel: string; | ||
authorized: boolean; | ||
repository: WebhookEvent<"issues">["payload"]["repository"]; | ||
}): Promise<null> { | ||
const { data, error } = await this.supabase.from("labels").insert({ | ||
label_from: previousLabel, | ||
label_to: currentLabel, | ||
authorized: authorized, | ||
node_id: repository.node_id, | ||
node_type: "Repository", | ||
node_url: repository.html_url, | ||
}); | ||
|
||
if (error) throw new Error(error.message); | ||
return data; | ||
} | ||
|
||
async getLabelChanges(repositoryNodeId: string) { | ||
const locationId = await this._getRepositoryLocationId(repositoryNodeId); | ||
if (!locationId) { | ||
return null; | ||
} | ||
return await this._getUnauthorizedLabelChanges(locationId); | ||
} | ||
|
||
async approveLabelChange(id: number): Promise<null> { | ||
const { data, error } = await this.supabase.from("labels").update({ authorized: true }).eq("id", id); | ||
if (error) throw new Error(error.message); | ||
return data; | ||
} | ||
|
||
private async _getUnauthorizedLabelChanges(locationId: number): Promise<LabelRow[]> { | ||
// Get label changes that are not authorized in the repository | ||
const { data, error } = await this.supabase.from("labels").select("*").eq("location_id", locationId).eq("authorized", false); | ||
|
||
if (error) throw new Error(error.message); | ||
|
||
return data; | ||
} | ||
|
||
private async _getRepositoryLocationId(nodeId: string) { | ||
// Get the location_id for the repository from the locations table | ||
const { data: locationData, error: locationError } = await this.supabase.from("locations").select("id").eq("node_id", nodeId).maybeSingle(); | ||
|
||
if (locationError) throw new Error(locationError.message); | ||
if (!locationData) { | ||
this.context.logger.error("Repository location ID not found in database."); | ||
return null; | ||
} | ||
|
||
return locationData.id; | ||
} | ||
} |
Oops, something went wrong.