120 lines
4.0 KiB
Rust
120 lines
4.0 KiB
Rust
use uuid::Uuid;
|
|
|
|
use crate::error::AppError;
|
|
use crate::models::prs::PrAssignee;
|
|
use crate::service::PrService;
|
|
use crate::session::Session;
|
|
|
|
use super::util::{clamp_limit_offset, ensure_affected};
|
|
|
|
impl PrService {
|
|
pub async fn pr_assignees(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
number: i64,
|
|
limit: i64,
|
|
offset: i64,
|
|
) -> Result<Vec<PrAssignee>, 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::<_, PrAssignee>(
|
|
"SELECT id, pull_request_id, assignee_id, assigned_by, created_at \
|
|
FROM pr_assignee 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)
|
|
}
|
|
|
|
pub async fn pr_assign(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
number: i64,
|
|
assignee_id: Uuid,
|
|
) -> Result<PrAssignee, 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 assignee = sqlx::query_as::<_, PrAssignee>(
|
|
"INSERT INTO pr_assignee (id, pull_request_id, assignee_id, assigned_by, created_at) \
|
|
VALUES ($1, $2, $3, $4, $5) ON CONFLICT (pull_request_id, assignee_id) DO NOTHING \
|
|
RETURNING id, pull_request_id, assignee_id, assigned_by, created_at",
|
|
)
|
|
.bind(Uuid::now_v7())
|
|
.bind(pr.id)
|
|
.bind(assignee_id)
|
|
.bind(user_uid)
|
|
.bind(now)
|
|
.fetch_optional(&mut *txn)
|
|
.await
|
|
.map_err(AppError::Database)?
|
|
.ok_or(AppError::Conflict("user already assigned".into()))?;
|
|
|
|
sqlx::query(
|
|
"INSERT INTO pr_subscription (id, pull_request_id, user_id, reason, muted, created_at, updated_at) \
|
|
VALUES ($1, $2, $3, 'assignee', false, $4, $4) ON CONFLICT DO NOTHING",
|
|
)
|
|
.bind(Uuid::now_v7()).bind(pr.id).bind(assignee_id).bind(now)
|
|
.execute(&mut *txn).await.map_err(AppError::Database)?;
|
|
|
|
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
|
Ok(assignee)
|
|
}
|
|
|
|
pub async fn pr_unassign(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
number: i64,
|
|
assignee_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_assignee WHERE pull_request_id = $1 AND assignee_id = $2")
|
|
.bind(pr.id)
|
|
.bind(assignee_id)
|
|
.execute(&mut *txn)
|
|
.await
|
|
.map_err(AppError::Database)?;
|
|
ensure_affected(result.rows_affected(), "assignee not found")?;
|
|
|
|
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
|
Ok(())
|
|
}
|
|
}
|