242 lines
8.4 KiB
Rust
242 lines
8.4 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
|
|
use crate::error::AppError;
|
|
use crate::models::common::{Role, State};
|
|
use crate::models::repos::{RepoCommitComment, RepoCommitStatus};
|
|
use crate::service::RepoService;
|
|
use crate::session::Session;
|
|
|
|
use super::util::{clamp_limit_offset, required_text};
|
|
|
|
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
|
pub struct CreateCommitStatusParams {
|
|
pub push_commit_id: Uuid,
|
|
pub latest_commit_sha: String,
|
|
pub context: String,
|
|
pub state: String,
|
|
pub target_url: Option<String>,
|
|
pub description: Option<String>,
|
|
}
|
|
|
|
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
|
pub struct CreateCommitCommentParams {
|
|
pub push_commit_id: Uuid,
|
|
pub commit_sha: String,
|
|
pub body: String,
|
|
pub path: Option<String>,
|
|
pub line: Option<i32>,
|
|
}
|
|
|
|
impl RepoService {
|
|
pub async fn repo_commit_statuses(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
push_commit_id: Uuid,
|
|
limit: i64,
|
|
offset: i64,
|
|
) -> Result<Vec<RepoCommitStatus>, AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
|
let repo_id = repo.id;
|
|
self.ensure_repo_readable(user_uid, &repo).await?;
|
|
let (limit, offset) = clamp_limit_offset(limit, offset);
|
|
sqlx::query_as::<_, RepoCommitStatus>(
|
|
"SELECT id, repo_id, push_commit_id, latest_commit_sha, context, state, target_url, description, reported_by, reported_at, created_at, updated_at FROM repo_commit_status WHERE repo_id = $1 AND push_commit_id = $2 ORDER BY created_at DESC LIMIT $3 OFFSET $4",
|
|
)
|
|
.bind(repo_id)
|
|
.bind(push_commit_id)
|
|
.bind(limit)
|
|
.bind(offset)
|
|
.fetch_all(self.ctx.db.reader())
|
|
.await
|
|
.map_err(AppError::Database)
|
|
}
|
|
|
|
pub async fn repo_create_commit_status(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
params: CreateCommitStatusParams,
|
|
) -> Result<RepoCommitStatus, AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
|
let repo_id = repo.id;
|
|
self.ensure_repo_role_at_least(user_uid, &repo, Role::Member)
|
|
.await?;
|
|
let context = required_text(params.context, "context")?;
|
|
let state = params
|
|
.state
|
|
.trim()
|
|
.parse::<State>()
|
|
.map_err(|_| AppError::BadRequest("invalid state".into()))?;
|
|
if state == State::Unknown {
|
|
return Err(AppError::BadRequest("invalid state".into()));
|
|
}
|
|
let latest_commit_sha = required_text(params.latest_commit_sha, "latest_commit_sha")?;
|
|
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 result = sqlx::query_as::<_, RepoCommitStatus>(
|
|
"INSERT INTO repo_commit_status (id, repo_id, push_commit_id, latest_commit_sha, context, \
|
|
state, target_url, description, reported_by, reported_at, created_at, updated_at) \
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $10, $10) RETURNING id, repo_id, push_commit_id, latest_commit_sha, context, state, target_url, description, reported_by, reported_at, created_at, updated_at",
|
|
)
|
|
.bind(Uuid::now_v7())
|
|
.bind(repo_id)
|
|
.bind(params.push_commit_id)
|
|
.bind(&latest_commit_sha)
|
|
.bind(&context)
|
|
.bind(state)
|
|
.bind(¶ms.target_url)
|
|
.bind(¶ms.description)
|
|
.bind(user_uid)
|
|
.bind(now)
|
|
.fetch_one(&mut *txn)
|
|
.await
|
|
.map_err(AppError::Database)?;
|
|
|
|
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
|
Ok(result)
|
|
}
|
|
|
|
pub async fn repo_commit_comments(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
push_commit_id: Uuid,
|
|
limit: i64,
|
|
offset: i64,
|
|
) -> Result<Vec<RepoCommitComment>, AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
|
let repo_id = repo.id;
|
|
self.ensure_repo_readable(user_uid, &repo).await?;
|
|
let (limit, offset) = clamp_limit_offset(limit, offset);
|
|
sqlx::query_as::<_, RepoCommitComment>(
|
|
"SELECT id, repo_id, push_commit_id, commit_sha, author_id, body, path, line, resolved, resolved_by, resolved_at, created_at, updated_at, deleted_at FROM repo_commit_comment WHERE repo_id = $1 AND push_commit_id = $2 AND deleted_at IS NULL ORDER BY created_at DESC LIMIT $3 OFFSET $4",
|
|
)
|
|
.bind(repo_id)
|
|
.bind(push_commit_id)
|
|
.bind(limit)
|
|
.bind(offset)
|
|
.fetch_all(self.ctx.db.reader())
|
|
.await
|
|
.map_err(AppError::Database)
|
|
}
|
|
|
|
pub async fn repo_create_commit_comment(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
params: CreateCommitCommentParams,
|
|
) -> Result<RepoCommitComment, AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
|
let repo_id = repo.id;
|
|
self.ensure_repo_role_at_least(user_uid, &repo, Role::Member)
|
|
.await?;
|
|
let body = required_text(params.body, "body")?;
|
|
let commit_sha = required_text(params.commit_sha, "commit_sha")?;
|
|
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 result = sqlx::query_as::<_, RepoCommitComment>(
|
|
"INSERT INTO repo_commit_comment (id, repo_id, push_commit_id, commit_sha, author_id, body, \
|
|
path, line, resolved, created_at, updated_at) \
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, false, $9, $9) RETURNING id, repo_id, push_commit_id, commit_sha, author_id, body, path, line, resolved, resolved_by, resolved_at, created_at, updated_at, deleted_at",
|
|
)
|
|
.bind(Uuid::now_v7())
|
|
.bind(repo_id)
|
|
.bind(params.push_commit_id)
|
|
.bind(&commit_sha)
|
|
.bind(user_uid)
|
|
.bind(&body)
|
|
.bind(¶ms.path)
|
|
.bind(params.line)
|
|
.bind(now)
|
|
.fetch_one(&mut *txn)
|
|
.await
|
|
.map_err(AppError::Database)?;
|
|
|
|
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
|
Ok(result)
|
|
}
|
|
|
|
pub async fn repo_resolve_commit_comment(
|
|
&self,
|
|
ctx: &Session,
|
|
wk_name: &str,
|
|
repo_name: &str,
|
|
comment_id: Uuid,
|
|
) -> Result<(), AppError> {
|
|
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
|
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
|
let repo_id = repo.id;
|
|
self.ensure_repo_role_at_least(user_uid, &repo, Role::Member)
|
|
.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 result = sqlx::query(
|
|
"UPDATE repo_commit_comment SET resolved = true, resolved_by = $1, resolved_at = $2, updated_at = $2 \
|
|
WHERE id = $3 AND repo_id = $4 AND deleted_at IS NULL AND resolved = false",
|
|
)
|
|
.bind(user_uid)
|
|
.bind(now)
|
|
.bind(comment_id)
|
|
.bind(repo_id)
|
|
.execute(&mut *txn)
|
|
.await
|
|
.map_err(AppError::Database)?;
|
|
super::util::ensure_affected(
|
|
result.rows_affected(),
|
|
"comment not found or already resolved",
|
|
)?;
|
|
|
|
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
|
Ok(())
|
|
}
|
|
}
|