Skip to content
Clone

refactor(git): archives POST/GET by revision with real size, range diff GET query, /utils namespace

ZhenYi committed
parent e3e9d68 commit b5cb919 Browse files

8 files changed

+76 −40

File tree 8
Modified lib/git/src/handler/archives/create.rs View lib/git/src/handler/archives/create.rs
@@ -4,25 +4,53 @@
44use session::Session;
55
66use crate::errors::{ApiError, GitApiError};
7use crate::handler::{RepoPathRev, require_writer};
7use crate::handler::{RepoPath, require_writer};
88use crate::service::GitService;
99use crate::service::archives::{ArchiveOptionsDto, ArchiveResultDto};
1010
11#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize, utoipa::ToSchema)]
12#[serde(rename_all = "snake_case")]
13pub enum ArchiveFormatDto {
14 Tar,
15 TarGz,
16 TarBz2,
17 Zip,
18}
19
20impl From<ArchiveFormatDto> for gitbaby::archive::types::ArchiveFormat {
21 fn from(v: ArchiveFormatDto) -> Self {
22 match v {
23 ArchiveFormatDto::Tar => gitbaby::archive::types::ArchiveFormat::Tar,
24 ArchiveFormatDto::TarGz => gitbaby::archive::types::ArchiveFormat::TarGz,
25 ArchiveFormatDto::TarBz2 => gitbaby::archive::types::ArchiveFormat::TarBz2,
26 ArchiveFormatDto::Zip => gitbaby::archive::types::ArchiveFormat::Zip,
27 }
28 }
29}
30
31#[derive(Debug, Clone, serde::Deserialize, utoipa::ToSchema)]
32pub struct ArchiveCreateCmd {
33 pub revision: String,
34 pub format: ArchiveFormatDto,
35 #[serde(default)]
36 pub path_filter: Option<String>,
37}
38
1139#[utoipa::path(
1240 post,
13 path = "/repos/{space_name}/{repo_name}/archives/create/{revision}",
41 path = "/repos/{space_name}/{repo_name}/archives",
1442 tag = "archives",
15 params(RepoPathRev),
16 request_body = String,
43 params(RepoPath),
44 request_body = ArchiveCreateCmd,
1745 responses(
18 (status = 200, description = "create archive", body = AppResponseData<ArchiveResultDto>)
46 (status = 200, description = "build archive and return download link", body = AppResponseData<ArchiveResultDto>)
1947 )
2048)]
21pub async fn create(
49pub async fn create_archive(
2250 State(service): State<GitService>,
2351 session: Session,
24 Path(path): Path<RepoPathRev>,
25 Json(opts): Json<ArchiveOptionsDto>,
52 Path(path): Path<RepoPath>,
53 Json(cmd): Json<ArchiveCreateCmd>,
2654) -> Result<AppResponseData<ArchiveResultDto>, ApiError> {
2755 require_writer(
2856 &service,
@@ -31,8 +59,12 @@
3159 path.repo_name.clone(),
3260 )
3361 .await?;
62 let opts = ArchiveOptionsDto {
63 format: cmd.format.into(),
64 path_filter: cmd.path_filter,
65 };
3466 let out = service
35 .archive_create(path.space_name, path.repo_name, path.revision, opts)
67 .archive_create(path.space_name, path.repo_name, cmd.revision, opts)
3668 .await
3769 .map_err(GitApiError::from)?;
3870 Ok(AppResponseData::ok(out))
Modified lib/git/src/handler/archives/download.rs View lib/git/src/handler/archives/download.rs
@@ -9,7 +9,7 @@
99
1010#[utoipa::path(
1111 get,
12 path = "/repos/{space_name}/{repo_name}/archives/download/{revision}",
12 path = "/repos/{space_name}/{repo_name}/archives/{revision}",
1313 tag = "archives",
1414 params(
1515 RepoPathRev,
@@ -20,7 +20,7 @@
2020 (status = 200, description = "download archive", body = AppResponseData<ArchiveResultDto>)
2121 )
2222)]
23pub async fn download(
23pub async fn get_archive(
2424 State(service): State<GitService>,
2525 session: Session,
2626 Path(path): Path<RepoPathRev>,
Modified lib/git/src/handler/content/caller_signature.rs View lib/git/src/handler/content/caller_signature.rs
@@ -17,7 +17,7 @@
1717
1818#[utoipa::path(
1919 post,
20 path = "/repos/_/contents/signature",
20 path = "/repos/utils/commit-signature",
2121 tag = "content",
2222 request_body = CallerSignatureBody,
2323 responses(
Modified lib/git/src/handler/diffs/range_diff.rs View lib/git/src/handler/diffs/range_diff.rs
@@ -1,5 +1,4 @@
1use axum::Json;
2use axum::extract::{Path, State};
1use axum::extract::{Path, Query, State};
32use export::rest::AppResponseData;
43use gitbaby::diff::types::RangeDiffSpec;
54use session::Session;
@@ -8,12 +7,19 @@
87use crate::handler::resolve_reader;
98use crate::service::GitService;
109
10#[derive(Debug, Clone, serde::Deserialize, utoipa::IntoParams)]
11pub struct RangeDiffQuery {
12 pub from: String,
13 pub to: String,
14 #[serde(default)]
15 pub base: Option<String>,
16}
17
1118#[utoipa::path(
12 post,
19 get,
1320 path = "/repos/{space_name}/{repo_name}/diff/range",
1421 tag = "diffs",
15 params(crate::handler::RepoPath),
16 request_body = String,
22 params(crate::handler::RepoPath, RangeDiffQuery),
1723 responses(
1824 (status = 200, description = "range diff", body = AppResponseData<String>)
1925 )
@@ -22,8 +28,12 @@
2228 State(service): State<GitService>,
2329 session: Session,
2430 Path(path): Path<crate::handler::RepoPath>,
25 Json(body): Json<RangeDiffSpec>,
31 Query(q): Query<RangeDiffQuery>,
2632) -> Result<AppResponseData<String>, ApiError> {
33 let body = match q.base {
34 Some(base) => RangeDiffSpec::BaseWithRevisions(base, q.from, q.to),
35 None => RangeDiffSpec::RevisionRange(q.from, q.to),
36 };
2737 let _ = resolve_reader(
2838 &service,
2939 &session,
Modified lib/git/src/handler/patches/parse.rs View lib/git/src/handler/patches/parse.rs
@@ -10,7 +10,7 @@
1010
1111#[utoipa::path(
1212 post,
13 path = "/repos/_/patch/parse",
13 path = "/repos/utils/patch/parse",
1414 tag = "patches",
1515 request_body = String,
1616 responses(
Modified lib/git/src/service/archives.rs View lib/git/src/service/archives.rs
@@ -99,18 +99,18 @@
9999 ext
100100 );
101101 let storage: &AppStorage = &self.state.storage;
102 let exists = storage
102 let head = storage
103103 .client()
104 .exists(&key)
104 .head(&key)
105105 .await
106 .map_err(|e| GitError::Internal(format!("storage exists: {e}")))?;
107 if !exists {
106 .map_err(|e| GitError::Internal(format!("storage head: {e}")))?;
107 let Some((size, _)) = head else {
108108 return Err(GitError::NotFound);
109 }
109 };
110110 let download_url = storage.client().presign_get(&key, 3600).ok();
111111 Ok(ArchiveResultDto {
112112 format: ext.to_string(),
113 size: -1,
113 size: size as i64,
114114 cache_key: key,
115115 download_url,
116116 })
Modified lib/git/src/openapi.rs View lib/git/src/openapi.rs
@@ -12,9 +12,9 @@
1212 handler::analysis::contributors::contributors,
1313 handler::analysis::languages::languages,
1414 handler::analysis::languages_read::languages_read,
15 handler::archives::create::create,
15 handler::archives::create::create_archive,
1616 handler::archives::create_bundle::create_bundle,
17 handler::archives::download::download,
17 handler::archives::download::get_archive,
1818 handler::blame::blame,
1919 handler::blobs::content::blob_content,
2020 handler::blobs::list_lfs_pointers::list_lfs_pointers,
Modified lib/git/src/routes.rs View lib/git/src/routes.rs
@@ -2,9 +2,9 @@
22use crate::handler::analysis::contributors::contributors;
33use crate::handler::analysis::languages::languages;
44use crate::handler::analysis::languages_read::languages_read;
5use crate::handler::archives::create::create as archive_create;
5use crate::handler::archives::create::create_archive;
66use crate::handler::archives::create_bundle::create_bundle;
7use crate::handler::archives::download::download as archive_download;
7use crate::handler::archives::download::get_archive;
88use crate::handler::blame::blame as blame_handler;
99use crate::handler::blobs::content::blob_content;
1010use crate::handler::blobs::list_lfs_pointers::list_lfs_pointers;
@@ -202,12 +202,13 @@
202202 "/{space_name}/{repo_name}/diff/for-commit/{commit}",
203203 get(diff_for_commit),
204204 )
205 .route("/{space_name}/{repo_name}/diff/range", post(range_diff))
205 .route("/{space_name}/{repo_name}/diff/range", get(range_diff))
206206 .route(
207207 "/{space_name}/{repo_name}/patch/generate",
208208 get(patch_generate),
209209 )
210 .route("/_/patch/parse", post(patch_parse))
210 .route("/utils/patch/parse", post(patch_parse))
211 .route("/utils/commit-signature", post(caller_signature))
211212 .route("/{space_name}/{repo_name}/conflicts", get(get_conflicts))
212213 .route(
213214 "/{space_name}/{repo_name}/conflicts/detail",
@@ -250,7 +251,6 @@
250251 "/{space_name}/{repo_name}/pipelines/schedule",
251252 post(schedule),
252253 )
253 .route("/_/contents/signature", post(caller_signature))
254254 .route(
255255 "/{space_name}/{repo_name}/analysis/languages",
256256 get(languages_read).post(languages),
@@ -263,14 +263,8 @@
263263 "/{space_name}/{repo_name}/analysis/code-activity",
264264 get(code_activity),
265265 )
266 .route(
267 "/{space_name}/{repo_name}/archives/create/{revision}",
268 post(archive_create),
269 )
270 .route(
271 "/{space_name}/{repo_name}/archives/download/{revision}",
272 get(archive_download),
273 )
266 .route("/{space_name}/{repo_name}/archives", post(create_archive))
267 .route("/{space_name}/{repo_name}/archives/{revision}", get(get_archive))
274268 .route(
275269 "/{space_name}/{repo_name}/archives/bundle",
276270 post(create_bundle),