226 lines
7.9 KiB
Rust
226 lines
7.9 KiB
Rust
use uuid::Uuid;
|
|
|
|
use crate::error::AppError;
|
|
use crate::models::common::Role;
|
|
use crate::models::prs::{PrLabel, PrLabelRelation};
|
|
use crate::service::PrService;
|
|
use crate::session::Session;
|
|
|
|
use super::util::{clamp_limit_offset, ensure_affected, required_text};
|
|
|
|
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, utoipa::ToSchema)]
|
|
pub struct CreatePrLabelParams {
|
|
pub name: String,
|
|
pub color: String,
|
|
pub description: Option<String>,
|
|
}
|
|
|
|
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, utoipa::ToSchema)]
|
|
pub struct UpdatePrLabelParams {
|
|
pub name: Option<String>,
|
|
pub color: Option<String>,
|
|
pub description: Option<String>,
|
|
}
|
|
|
|
impl PrService {
|
|
pub async fn pr_labels(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
) -> Result<Vec<PrLabel>, AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
|
self.ensure_repo_readable(user_uid, &repo).await?;
|
|
sqlx::query_as::<_, PrLabel>(
|
|
"SELECT id, repo_id, name, color, description, created_by, created_at, updated_at \
|
|
FROM pr_label WHERE repo_id = $1 ORDER BY name ASC",
|
|
)
|
|
.bind(repo.id)
|
|
.fetch_all(self.ctx.db.reader())
|
|
.await
|
|
.map_err(AppError::Database)
|
|
}
|
|
|
|
pub async fn pr_create_label(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
params: CreatePrLabelParams,
|
|
) -> Result<PrLabel, AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
|
self.ensure_repo_role_at_least(user_uid, &repo, Role::Member)
|
|
.await?;
|
|
let name = required_text(params.name, "name")?;
|
|
let color = required_text(params.color, "color")?;
|
|
let now = chrono::Utc::now();
|
|
sqlx::query_as::<_, PrLabel>(
|
|
"INSERT INTO pr_label (id, repo_id, name, color, description, created_by, created_at, updated_at) \
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $7) \
|
|
RETURNING id, repo_id, name, color, description, created_by, created_at, updated_at",
|
|
)
|
|
.bind(Uuid::now_v7()).bind(repo.id).bind(&name).bind(&color)
|
|
.bind(params.description.as_deref()).bind(user_uid).bind(now)
|
|
.fetch_one(self.ctx.db.writer()).await.map_err(AppError::Database)
|
|
}
|
|
|
|
pub async fn pr_update_label(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
label_id: Uuid,
|
|
params: UpdatePrLabelParams,
|
|
) -> Result<PrLabel, AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
|
self.ensure_repo_role_at_least(user_uid, &repo, Role::Admin)
|
|
.await?;
|
|
let current = sqlx::query_as::<_, PrLabel>(
|
|
"SELECT id, repo_id, name, color, description, created_by, created_at, updated_at \
|
|
FROM pr_label WHERE id = $1 AND repo_id = $2",
|
|
)
|
|
.bind(label_id)
|
|
.bind(repo.id)
|
|
.fetch_optional(self.ctx.db.reader())
|
|
.await
|
|
.map_err(AppError::Database)?
|
|
.ok_or(AppError::NotFound("label not found".into()))?;
|
|
|
|
let name = params.name.unwrap_or(current.name);
|
|
let color = params.color.unwrap_or(current.color);
|
|
let description = super::util::merge_optional_text(params.description, current.description);
|
|
let now = chrono::Utc::now();
|
|
sqlx::query_as::<_, PrLabel>(
|
|
"UPDATE pr_label SET name = $1, color = $2, description = $3, updated_at = $4 \
|
|
WHERE id = $5 RETURNING id, repo_id, name, color, description, created_by, created_at, updated_at",
|
|
)
|
|
.bind(&name).bind(&color).bind(&description).bind(now).bind(label_id)
|
|
.fetch_one(self.ctx.db.writer()).await.map_err(AppError::Database)
|
|
}
|
|
|
|
pub async fn pr_delete_label(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
label_id: Uuid,
|
|
) -> Result<(), AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
|
self.ensure_repo_role_at_least(user_uid, &repo, Role::Admin)
|
|
.await?;
|
|
let result = sqlx::query("DELETE FROM pr_label WHERE id = $1 AND repo_id = $2")
|
|
.bind(label_id)
|
|
.bind(repo.id)
|
|
.execute(self.ctx.db.writer())
|
|
.await
|
|
.map_err(AppError::Database)?;
|
|
ensure_affected(result.rows_affected(), "label not found")
|
|
}
|
|
|
|
pub async fn pr_assign_label(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
number: i64,
|
|
label_id: Uuid,
|
|
) -> Result<PrLabelRelation, AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
let pr = self.resolve_pr(wk_name, repo_name, number).await?;
|
|
self.ensure_pr_editable(user_uid, &pr).await?;
|
|
let now = chrono::Utc::now();
|
|
let mut txn = self
|
|
.ctx
|
|
.db
|
|
.writer()
|
|
.begin()
|
|
.await
|
|
.map_err(|_| AppError::TxnError)?;
|
|
sqlx::query("SET LOCAL app.current_user_id = $1")
|
|
.bind(user_uid)
|
|
.execute(&mut *txn)
|
|
.await
|
|
.map_err(AppError::Database)?;
|
|
|
|
let rel = sqlx::query_as::<_, PrLabelRelation>(
|
|
"INSERT INTO pr_label_relation (id, pull_request_id, label_id, created_by, created_at) \
|
|
VALUES ($1, $2, $3, $4, $5) \
|
|
RETURNING id, pull_request_id, label_id, created_by, created_at",
|
|
)
|
|
.bind(Uuid::now_v7())
|
|
.bind(pr.id)
|
|
.bind(label_id)
|
|
.bind(user_uid)
|
|
.bind(now)
|
|
.fetch_one(&mut *txn)
|
|
.await
|
|
.map_err(AppError::Database)?;
|
|
|
|
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
|
Ok(rel)
|
|
}
|
|
|
|
pub async fn pr_unassign_label(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
number: i64,
|
|
label_id: Uuid,
|
|
) -> Result<(), AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
let pr = self.resolve_pr(wk_name, repo_name, number).await?;
|
|
self.ensure_pr_editable(user_uid, &pr).await?;
|
|
let mut txn = self
|
|
.ctx
|
|
.db
|
|
.writer()
|
|
.begin()
|
|
.await
|
|
.map_err(|_| AppError::TxnError)?;
|
|
sqlx::query("SET LOCAL app.current_user_id = $1")
|
|
.bind(user_uid)
|
|
.execute(&mut *txn)
|
|
.await
|
|
.map_err(AppError::Database)?;
|
|
|
|
let result = sqlx::query(
|
|
"DELETE FROM pr_label_relation WHERE pull_request_id = $1 AND label_id = $2",
|
|
)
|
|
.bind(pr.id)
|
|
.bind(label_id)
|
|
.execute(&mut *txn)
|
|
.await
|
|
.map_err(AppError::Database)?;
|
|
ensure_affected(result.rows_affected(), "label not assigned")?;
|
|
|
|
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn pr_label_relations(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
number: i64,
|
|
limit: i64,
|
|
offset: i64,
|
|
) -> Result<Vec<PrLabelRelation>, AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
let pr = self.resolve_pr(wk_name, repo_name, number).await?;
|
|
self.ensure_pr_readable(user_uid, &pr).await?;
|
|
let (limit, offset) = clamp_limit_offset(limit, offset);
|
|
sqlx::query_as::<_, PrLabelRelation>(
|
|
"SELECT id, pull_request_id, label_id, created_by, created_at \
|
|
FROM pr_label_relation WHERE pull_request_id = $1 ORDER BY created_at ASC LIMIT $2 OFFSET $3",
|
|
)
|
|
.bind(pr.id).bind(limit).bind(offset)
|
|
.fetch_all(self.ctx.db.reader()).await.map_err(AppError::Database)
|
|
}
|
|
}
|