feat: init
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::models::common::{Role, Status};
|
||||
use crate::models::prs::PrCheckRun;
|
||||
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 CreateCheckRunParams {
|
||||
pub commit_sha: String,
|
||||
pub name: String,
|
||||
pub status: String,
|
||||
pub conclusion: Option<String>,
|
||||
pub details_url: Option<String>,
|
||||
pub external_id: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct UpdateCheckRunParams {
|
||||
pub status: Option<String>,
|
||||
pub conclusion: Option<String>,
|
||||
pub details_url: Option<String>,
|
||||
}
|
||||
|
||||
impl PrService {
|
||||
pub async fn pr_check_runs(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
number: i64,
|
||||
limit: i64,
|
||||
offset: i64,
|
||||
) -> Result<Vec<PrCheckRun>, 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::<_, PrCheckRun>(
|
||||
"SELECT id, pull_request_id, commit_sha, name, status, conclusion, details_url, \
|
||||
external_id, started_at, completed_at, created_at, updated_at \
|
||||
FROM pr_check_run WHERE pull_request_id = $1 ORDER BY created_at DESC 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_create_check_run(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
number: i64,
|
||||
params: CreateCheckRunParams,
|
||||
) -> Result<PrCheckRun, 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 pr = self.resolve_pr(wk_name, repo_name, number).await?;
|
||||
|
||||
let name = required_text(params.name, "name")?;
|
||||
let status = params
|
||||
.status
|
||||
.trim()
|
||||
.parse::<Status>()
|
||||
.map_err(|_| AppError::BadRequest("invalid status".into()))?;
|
||||
if status == Status::Unknown {
|
||||
return Err(AppError::BadRequest("invalid status".into()));
|
||||
}
|
||||
let conclusion = params
|
||||
.conclusion
|
||||
.as_deref()
|
||||
.and_then(|s| s.parse::<Status>().ok())
|
||||
.filter(|s| *s != Status::Unknown);
|
||||
|
||||
let now = chrono::Utc::now();
|
||||
sqlx::query_as::<_, PrCheckRun>(
|
||||
"INSERT INTO pr_check_run (id, pull_request_id, commit_sha, name, status, conclusion, \
|
||||
details_url, external_id, started_at, completed_at, created_at, updated_at) \
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $11) \
|
||||
RETURNING id, pull_request_id, commit_sha, name, status, conclusion, details_url, \
|
||||
external_id, started_at, completed_at, created_at, updated_at",
|
||||
)
|
||||
.bind(Uuid::now_v7())
|
||||
.bind(pr.id)
|
||||
.bind(¶ms.commit_sha)
|
||||
.bind(&name)
|
||||
.bind(status)
|
||||
.bind(conclusion)
|
||||
.bind(params.details_url.as_deref())
|
||||
.bind(params.external_id.as_deref())
|
||||
.bind(if status == Status::Running {
|
||||
Some(now)
|
||||
} else {
|
||||
None
|
||||
})
|
||||
.bind(
|
||||
if matches!(status, Status::Completed | Status::Success | Status::Failed) {
|
||||
Some(now)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
)
|
||||
.bind(now)
|
||||
.fetch_one(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
|
||||
pub async fn pr_update_check_run(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
number: i64,
|
||||
check_run_id: Uuid,
|
||||
params: UpdateCheckRunParams,
|
||||
) -> Result<PrCheckRun, 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 pr = self.resolve_pr(wk_name, repo_name, number).await?;
|
||||
|
||||
let current = sqlx::query_as::<_, PrCheckRun>(
|
||||
"SELECT id, pull_request_id, commit_sha, name, status, conclusion, details_url, \
|
||||
external_id, started_at, completed_at, created_at, updated_at \
|
||||
FROM pr_check_run WHERE id = $1 AND pull_request_id = $2",
|
||||
)
|
||||
.bind(check_run_id)
|
||||
.bind(pr.id)
|
||||
.fetch_optional(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
.ok_or(AppError::NotFound("check run not found".into()))?;
|
||||
|
||||
let status = match params.status {
|
||||
Some(ref v) => v
|
||||
.trim()
|
||||
.parse::<Status>()
|
||||
.map_err(|_| AppError::BadRequest("invalid status".into()))?,
|
||||
None => current.status,
|
||||
};
|
||||
let conclusion = params
|
||||
.conclusion
|
||||
.as_deref()
|
||||
.and_then(|s| s.parse::<Status>().ok())
|
||||
.filter(|s| *s != Status::Unknown)
|
||||
.or(current.conclusion);
|
||||
let details_url = params.details_url.or(current.details_url);
|
||||
let now = chrono::Utc::now();
|
||||
|
||||
sqlx::query_as::<_, PrCheckRun>(
|
||||
"UPDATE pr_check_run SET status = $1, conclusion = $2, details_url = $3, updated_at = $4 \
|
||||
WHERE id = $5 \
|
||||
RETURNING id, pull_request_id, commit_sha, name, status, conclusion, details_url, \
|
||||
external_id, started_at, completed_at, created_at, updated_at",
|
||||
)
|
||||
.bind(status).bind(conclusion).bind(details_url.as_deref()).bind(now).bind(check_run_id)
|
||||
.fetch_one(self.ctx.db.writer()).await.map_err(AppError::Database)
|
||||
}
|
||||
|
||||
pub async fn pr_delete_check_run(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
number: i64,
|
||||
check_run_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 pr = self.resolve_pr(wk_name, repo_name, number).await?;
|
||||
|
||||
let result = sqlx::query("DELETE FROM pr_check_run WHERE id = $1 AND pull_request_id = $2")
|
||||
.bind(check_run_id)
|
||||
.bind(pr.id)
|
||||
.execute(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
ensure_affected(result.rows_affected(), "check run not found")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user