diff --git a/.github/scripts/auto-approval/index.ts b/.github/scripts/auto-approval/index.ts index b4c6e37fb..dd25f62f7 100644 --- a/.github/scripts/auto-approval/index.ts +++ b/.github/scripts/auto-approval/index.ts @@ -1,6 +1,6 @@ import type { RestEndpointMethodTypes } from "@octokit/rest"; -import { Octokit, RequestError } from "octokit"; +import { Octokit } from "octokit"; import yargs from "yargs"; import { hideBin } from "yargs/helpers"; @@ -52,12 +52,7 @@ async function main() { pull_number: prId, }); } catch (e) { - let s: string; - if (e instanceof RequestError) { - s = JSON.stringify(e.response?.data); - } else { - s = String(e); - } + const s = JSON.stringify(e); throw new Error(`GitHub API Error\n\n${s}`); } @@ -76,12 +71,7 @@ async function main() { event: "APPROVE", }); } catch (e) { - let s: string; - if (e instanceof RequestError) { - s = JSON.stringify(e.response?.data); - } else { - s = String(e); - } + const s = JSON.stringify(e); throw new Error(`GitHub API Error\n\n${s}`); } } diff --git a/.github/scripts/copy-prod-db/index.ts b/.github/scripts/copy-prod-db/index.ts index f32c7ef7d..4f99f3e63 100644 --- a/.github/scripts/copy-prod-db/index.ts +++ b/.github/scripts/copy-prod-db/index.ts @@ -81,60 +81,8 @@ async function main() { --port="$DATABASE_PORT" \ --username="$DATABASE_USER" \ --dbname="$STAGING_DATABASE_NAME" \ - --command=" - DELETE FROM \"ApiKey\"; - DELETE FROM \"Auth\"; - DELETE FROM \"Club\"; - DELETE FROM \"Session\"; - - -- Scramble non-admin user data - UPDATE \"User\" - SET - \"nickname\" = CASE - WHEN \"nickname\" IS NOT NULL THEN 'user_' || encode(gen_random_bytes(8), 'hex') - ELSE NULL - END, - \"verifyKey\" = encode(gen_random_bytes(16), 'hex'), - \"schoolEmail\" = CASE - WHEN \"schoolEmail\" IS NOT NULL THEN 'test_' || encode(gen_random_bytes(4), 'hex') || '@example.com' - ELSE NULL - END, - \"profileUrl\" = 'https://via.placeholder.com/150' - WHERE \"admin\" IS NOT TRUE; - - -- Scramble admin user data - UPDATE \"User\" - SET - \"leetcodeUsername\" = NULL - WHERE \"admin\" IS TRUE; - - -- Update DiscordClubMetadata for 'Patina Network' - UPDATE \"DiscordClubMetadata\" m - SET - \"guildId\" = '1389762654452580373', - \"leaderboardChannelId\" = '1417161536014778388' - FROM \"DiscordClub\" c - WHERE c.\"id\" = m.\"discordClubId\" - AND c.\"name\" = 'Patina Network'; - - -- Update DiscordClubMetadata for 'MHC++' - UPDATE \"DiscordClubMetadata\" m - SET - \"guildId\" = '1389762654452580373', - \"leaderboardChannelId\" = '1417161536014778388' - FROM \"DiscordClub\" c - WHERE c.\"id\" = m.\"discordClubId\" - AND c.\"name\" = 'MHC++'; - - -- Update DiscordClubMetadata for 'GWC - Hunter College' - UPDATE \"DiscordClubMetadata\" m - SET - \"guildId\" = '1389762654452580373', - \"leaderboardChannelId\" = '1417161536014778388' - FROM \"DiscordClub\" c - WHERE c.\"id\" = m.\"discordClubId\" - AND c.\"name\" = 'GWC - Hunter College'; - "`; + --set ON_ERROR_STOP=on \ + --file ./infra/clean-stg-db.SQL`; await updateCommitStatus({ sha, diff --git a/.github/scripts/utils/send-message/index.ts b/.github/scripts/utils/send-message/index.ts index 7572db8bd..8db2db5e3 100644 --- a/.github/scripts/utils/send-message/index.ts +++ b/.github/scripts/utils/send-message/index.ts @@ -1,5 +1,4 @@ import { Octokit } from "@octokit/rest"; -import { RequestError } from "octokit"; const githubToken = (() => { const v = process.env.GH_TOKEN; @@ -36,12 +35,7 @@ export async function sendMessage( body: message, }); } catch (e) { - let s: string; - if (e instanceof RequestError) { - s = JSON.stringify(e.response?.data); - } else { - s = String(e); - } + const s = JSON.stringify(e); throw new Error(`GitHub API Error\n\n${s}`); } } catch (e) { diff --git a/.github/scripts/utils/update-commit-status/index.ts b/.github/scripts/utils/update-commit-status/index.ts index bbf5f6f6d..da20724b4 100644 --- a/.github/scripts/utils/update-commit-status/index.ts +++ b/.github/scripts/utils/update-commit-status/index.ts @@ -1,5 +1,4 @@ import { Octokit } from "@octokit/rest"; -import { RequestError } from "octokit"; const githubToken = (() => { const v = process.env.GH_TOKEN; @@ -44,12 +43,7 @@ export async function updateCommitStatus(options: Options) { description: description, }); } catch (e) { - let s: string; - if (e instanceof RequestError) { - s = JSON.stringify(e.response?.data); - } else { - s = String(e); - } + const s = JSON.stringify(e); throw new Error(`GitHub API Error\n\n${s}`); } } catch (e) { diff --git a/.github/scripts/utils/update-pr-description.ts b/.github/scripts/utils/update-pr-description.ts index 305d580ea..47352e40c 100644 --- a/.github/scripts/utils/update-pr-description.ts +++ b/.github/scripts/utils/update-pr-description.ts @@ -1,4 +1,4 @@ -import { Octokit, RequestError } from "octokit"; +import { Octokit } from "octokit"; const githubToken = (() => { const v = process.env.GH_TOKEN; @@ -37,12 +37,7 @@ export async function updatePrDescriptionWithTicket( }); body = pr.data.body; } catch (e) { - let s: string; - if (e instanceof RequestError) { - s = JSON.stringify(e.response?.data); - } else { - s = String(e); - } + const s = JSON.stringify(e); throw new Error(`GitHub API Error\n\n${s}`); } @@ -70,12 +65,7 @@ export async function updatePrDescriptionWithTicket( body: newBody, }); } catch (e) { - let s: string; - if (e instanceof RequestError) { - s = JSON.stringify(e.response?.data); - } else { - s = String(e); - } + const s = JSON.stringify(e); throw new Error(`GitHub API Error\n\n${s}`); } } catch (e) { diff --git a/infra/README.md b/infra/README.md index 9d49a4b35..1e55ef8ee 100644 --- a/infra/README.md +++ b/infra/README.md @@ -1,10 +1,12 @@ # `infra/` > [!NOTE] -> Our staging to production migration script has been moved [here](https://github.com/tahminator/codebloom/tree/main/.github/scripts/copy-prod-db/index.ts#L28). +> Our staging to production migration script is located [here](https://github.com/tahminator/codebloom/tree/main/.github/scripts/copy-prod-db/index.ts#L28). + +[`clean-stg-db.SQL`](./clean-stg-db.SQL) is a SQL script used to clean and scramble staging data after copying the production database. This directory contains the Dockerfile used to build the main Codebloom image, which is then uploaded to [hub.docker.com/r/tahminator/codebloom](https://hub.docker.com/r/tahminator/codebloom). The image is then deployed to the [DigitalOcean App Platform](https://www.digitalocean.com/products/app-platform) see [`.do/`](../.do/) to view the DigitalOcean app spec and more detailed documentation regarding DigitalOcean and deployments. -There is a Bun Shell script which helps us manage the workflow for deployments across production and staging, which can be found at [`.github/scripts/redeploy/index.ts`](../.github/scripts/redeploy/index.ts). +There is a Bun Shell script which helps us manage the workflow for deployments across production and staging, which can be found at [`.github/scripts/redeploy/index.ts`](../.github/scripts/redeploy/index.ts). \ No newline at end of file diff --git a/infra/clean-stg-db.SQL b/infra/clean-stg-db.SQL new file mode 100644 index 000000000..4357eea54 --- /dev/null +++ b/infra/clean-stg-db.SQL @@ -0,0 +1,59 @@ +BEGIN; + +CREATE EXTENSION IF NOT EXISTS pgcrypto; + +-- Cleaning unnecesary data... +DELETE FROM "ApiKey"; +DELETE FROM "Auth"; +DELETE FROM "Club"; +DELETE FROM "Session"; + +-- Scramble non-admin user data +UPDATE "User" +SET + "nickname" = CASE + WHEN "nickname" IS NOT NULL THEN 'user_' || encode(gen_random_bytes(8), 'hex') + ELSE NULL + END, + "verifyKey" = encode(gen_random_bytes(16), 'hex'), + "schoolEmail" = CASE + WHEN "schoolEmail" IS NOT NULL THEN 'test_' || encode(gen_random_bytes(4), 'hex') || '@example.com' + ELSE NULL + END, + "profileUrl" = 'https://via.placeholder.com/150' +WHERE "admin" IS NOT TRUE; + +-- Scramble admin user data +UPDATE "User" +SET + "leetcodeUsername" = NULL +WHERE "admin" IS TRUE; + +-- Update DiscordClubMetadata for 'Patina Network' +UPDATE "DiscordClubMetadata" m +SET + "guildId" = '1389762654452580373', + "leaderboardChannelId" = '1417161536014778388' +FROM "DiscordClub" c +WHERE c."id" = m."discordClubId" + AND c."name" = 'Patina Network'; + +-- Update DiscordClubMetadata for 'MHC++' +UPDATE "DiscordClubMetadata" m +SET + "guildId" = '1389762654452580373', + "leaderboardChannelId" = '1417161536014778388' +FROM "DiscordClub" c +WHERE c."id" = m."discordClubId" + AND c."name" = 'MHC++'; + +-- Update DiscordClubMetadata for 'GWC - Hunter College' +UPDATE "DiscordClubMetadata" m +SET + "guildId" = '1389762654452580373', + "leaderboardChannelId" = '1417161536014778388' +FROM "DiscordClub" c +WHERE c."id" = m."discordClubId" + AND c."name" = 'GWC - Hunter College'; + +COMMIT; \ No newline at end of file