Skip to content

Commit

Permalink
feat: save creator user id when applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegec committed Mar 16, 2024
1 parent 29b8a09 commit 6aafb9f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- This file should undo anything in `up.sql`
ALTER TABLE pipelines DROP CONSTRAINT creator_user;
ALTER TABLE pipelines DROP COLUMN creator_user_id;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Your SQL goes here
ALTER TABLE pipelines ADD COLUMN creator_user_id INT;
ALTER TABLE pipelines ADD CONSTRAINT creator_user FOREIGN KEY(creator_user_id) REFERENCES users(id);
23 changes: 17 additions & 6 deletions server/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::{
github::{get_crab_github_installation, get_packages_from_pr},
models::{NewJob, NewPipeline, Pipeline, Worker},
models::{NewJob, NewPipeline, Pipeline, User, Worker},
DbPool, ABBS_REPO_LOCK, ALL_ARCH, ARGS,
};
use anyhow::anyhow;
use anyhow::Context;
use buildit_utils::github::{get_archs, update_abbs};
use diesel::{dsl::count, ExpressionMethods, QueryDsl, RunQueryDsl, SelectableHelper};
use diesel::{
dsl::count, ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl, SelectableHelper,
};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use tracing::warn;
Expand Down Expand Up @@ -100,10 +102,18 @@ pub async fn pipeline_new(
.get()
.context("Failed to get db connection from pool")?;
use crate::schema::pipelines;
let (source, github_pr, telegram_user) = match source {
JobSource::Telegram(id) => ("telegram", github_pr, Some(id)),
JobSource::Github(id) => ("github", Some(*id), None),
JobSource::Manual => ("manual", github_pr, None),
let (source, github_pr, telegram_user, creator_user_id) = match source {
JobSource::Telegram(id) => {
// lookup user id via telegram chat id
let user = crate::schema::users::dsl::users
.filter(crate::schema::users::dsl::telegram_chat_id.eq(id))
.first::<User>(&mut conn)
.optional()?;
let creator_user_id = user.map(|user| user.id);
("telegram", github_pr, Some(id), creator_user_id)
}
JobSource::Github(id) => ("github", Some(*id), None, None),
JobSource::Manual => ("manual", github_pr, None, None),
};
let new_pipeline = NewPipeline {
packages: packages.to_string(),
Expand All @@ -114,6 +124,7 @@ pub async fn pipeline_new(
source: source.to_string(),
github_pr: github_pr.map(|pr| pr as i64),
telegram_user: telegram_user.copied(),
creator_user_id: creator_user_id,
};
let pipeline = diesel::insert_into(pipelines::table)
.values(&new_pipeline)
Expand Down
2 changes: 2 additions & 0 deletions server/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Pipeline {
pub source: String,
pub github_pr: Option<i64>,
pub telegram_user: Option<i64>,
pub creator_user_id: Option<i32>,
}

#[derive(Insertable)]
Expand All @@ -28,6 +29,7 @@ pub struct NewPipeline {
pub source: String,
pub github_pr: Option<i64>,
pub telegram_user: Option<i64>,
pub creator_user_id: Option<i32>,
}

#[derive(Queryable, Selectable, Associations, Identifiable, Debug)]
Expand Down
2 changes: 2 additions & 0 deletions server/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ diesel::table! {
source -> Text,
github_pr -> Nullable<Int8>,
telegram_user -> Nullable<Int8>,
creator_user_id -> Nullable<Int4>,
}
}

Expand Down Expand Up @@ -62,5 +63,6 @@ diesel::table! {
}

diesel::joinable!(jobs -> pipelines (pipeline_id));
diesel::joinable!(pipelines -> users (creator_user_id));

diesel::allow_tables_to_appear_in_same_query!(jobs, pipelines, users, workers,);

0 comments on commit 6aafb9f

Please sign in to comment.