From 492a644f569e6b1b418d7fc3a8a8548dfef80d4c Mon Sep 17 00:00:00 2001 From: Micha Albert Date: Tue, 3 Sep 2024 13:57:33 +0000 Subject: [PATCH] slack modal work --- onboard_live_backend/main.py | 30 ++++++++++++++++++- .../migration.sql | 14 +++++++++ .../migration.sql | 26 ++++++++++++++++ .../migration.sql | 2 ++ .../migration.sql | 8 +++++ onboard_live_backend/schema.prisma | 18 +++++++++-- 6 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 onboard_live_backend/migrations/20240831153915_add_session_model/migration.sql create mode 100644 onboard_live_backend/migrations/20240831155249_relate_session_model_to_pr_not_user/migration.sql create mode 100644 onboard_live_backend/migrations/20240831222739_remove_unique_constraint/migration.sql create mode 100644 onboard_live_backend/migrations/20240831222937_add_different_unique_constraint/migration.sql diff --git a/onboard_live_backend/main.py b/onboard_live_backend/main.py index 75cf63b..6ab7243 100644 --- a/onboard_live_backend/main.py +++ b/onboard_live_backend/main.py @@ -264,6 +264,7 @@ async def github_callback(request: Request): }, }, { + "block_id": "session-checks", "type": "section", "text": { "type": "mrkdwn", @@ -376,8 +377,35 @@ async def handle_app_home_opened_events(body, logger, event, client): @bolt.action("submit_sessions") -async def submit_sessions(ack, body): +async def submit_sessions(ack: AsyncAck, body): await ack() + selected_sessions_ts: List[str] = [ + i["text"]["text"].split("session on ")[1] + for i in body["state"]["values"]["session-checks"]["checkboxes"][ + "selected_options" + ] + ] + pr_id = int( + body["message"]["blocks"][1]["text"]["text"].split("#")[1].split(":")[0] + ) # don't tell my mom she raised a monster + db_pr = await db.pullrequest.find_first_or_raise(where={"github_id": pr_id}) + if db_pr.user_id: + stream_key = ( + await db.stream.find_first_or_raise(where={"user_id": db_pr.user_id}) + ).key + for session in selected_sessions_ts: + await db.session.create( + { + "pull": {"connect": {"id": db_pr.id}}, + "timestamp": session, + "filename": f"/home/onboard/recordings/{stream_key}/{datetime.strptime(session, '%Y-%m-%dT%H:%M:%S.%fZ').strftime('%Y-%m-%d_%H-%M-%S-%f')}.mp4", + "duration": get_recording_duration(session, stream_key), + } + ) + await bolt.client.chat_delete( + channel=body["container"]["channel_id"], ts=body["message"]["ts"] + ) + print(pr_id, selected_sessions_ts) @bolt.action("deny") diff --git a/onboard_live_backend/migrations/20240831153915_add_session_model/migration.sql b/onboard_live_backend/migrations/20240831153915_add_session_model/migration.sql new file mode 100644 index 0000000..8fece81 --- /dev/null +++ b/onboard_live_backend/migrations/20240831153915_add_session_model/migration.sql @@ -0,0 +1,14 @@ +-- CreateTable +CREATE TABLE "Session" ( + "id" TEXT NOT NULL PRIMARY KEY, + "user_id" TEXT NOT NULL, + "timestamp" TEXT NOT NULL, + "filename" TEXT NOT NULL, + "duration" INTEGER NOT NULL, + "reviewed" BOOLEAN NOT NULL DEFAULT false, + "approved" BOOLEAN NOT NULL DEFAULT false, + CONSTRAINT "Session_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE +); + +-- CreateIndex +CREATE UNIQUE INDEX "Session_user_id_key" ON "Session"("user_id"); diff --git a/onboard_live_backend/migrations/20240831155249_relate_session_model_to_pr_not_user/migration.sql b/onboard_live_backend/migrations/20240831155249_relate_session_model_to_pr_not_user/migration.sql new file mode 100644 index 0000000..64e8757 --- /dev/null +++ b/onboard_live_backend/migrations/20240831155249_relate_session_model_to_pr_not_user/migration.sql @@ -0,0 +1,26 @@ +/* + Warnings: + + - You are about to drop the column `user_id` on the `Session` table. All the data in the column will be lost. + - Added the required column `pr_id` to the `Session` table without a default value. This is not possible if the table is not empty. + +*/ +-- RedefineTables +PRAGMA defer_foreign_keys=ON; +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_Session" ( + "id" TEXT NOT NULL PRIMARY KEY, + "pr_id" INTEGER NOT NULL, + "timestamp" TEXT NOT NULL, + "filename" TEXT NOT NULL, + "duration" INTEGER NOT NULL, + "reviewed" BOOLEAN NOT NULL DEFAULT false, + "approved" BOOLEAN NOT NULL DEFAULT false, + CONSTRAINT "Session_pr_id_fkey" FOREIGN KEY ("pr_id") REFERENCES "PullRequest" ("id") ON DELETE RESTRICT ON UPDATE CASCADE +); +INSERT INTO "new_Session" ("approved", "duration", "filename", "id", "reviewed", "timestamp") SELECT "approved", "duration", "filename", "id", "reviewed", "timestamp" FROM "Session"; +DROP TABLE "Session"; +ALTER TABLE "new_Session" RENAME TO "Session"; +CREATE UNIQUE INDEX "Session_pr_id_key" ON "Session"("pr_id"); +PRAGMA foreign_keys=ON; +PRAGMA defer_foreign_keys=OFF; diff --git a/onboard_live_backend/migrations/20240831222739_remove_unique_constraint/migration.sql b/onboard_live_backend/migrations/20240831222739_remove_unique_constraint/migration.sql new file mode 100644 index 0000000..cf824ee --- /dev/null +++ b/onboard_live_backend/migrations/20240831222739_remove_unique_constraint/migration.sql @@ -0,0 +1,2 @@ +-- DropIndex +DROP INDEX "Session_pr_id_key"; diff --git a/onboard_live_backend/migrations/20240831222937_add_different_unique_constraint/migration.sql b/onboard_live_backend/migrations/20240831222937_add_different_unique_constraint/migration.sql new file mode 100644 index 0000000..25d4f10 --- /dev/null +++ b/onboard_live_backend/migrations/20240831222937_add_different_unique_constraint/migration.sql @@ -0,0 +1,8 @@ +/* + Warnings: + + - A unique constraint covering the columns `[filename]` on the table `Session` will be added. If there are existing duplicate values, this will fail. + +*/ +-- CreateIndex +CREATE UNIQUE INDEX "Session_filename_key" ON "Session"("filename"); diff --git a/onboard_live_backend/schema.prisma b/onboard_live_backend/schema.prisma index 59ff94a..3ac9bf4 100644 --- a/onboard_live_backend/schema.prisma +++ b/onboard_live_backend/schema.prisma @@ -29,9 +29,21 @@ model Stream { } model PullRequest { - id Int @id @default(autoincrement()) - github_id Int @unique + id Int @id @default(autoincrement()) + github_id Int @unique user_id String? gh_user_id Int - user User? @relation("PullRequestToUser", fields: [user_id], references: [id]) + user User? @relation("PullRequestToUser", fields: [user_id], references: [id]) + sessions Session[] +} + +model Session { + id String @id @default(cuid()) + pr_id Int + pull PullRequest @relation(fields: [pr_id], references: [id]) + timestamp String + filename String @unique + duration Int // in minutes + reviewed Boolean @default(false) + approved Boolean @default(false) }