Skip to content
Clone

feat(export): add repo branch export

zhenyi committed
parent b684762 commit 125eba5 Browse files

1 file changed

+128 −0

Added lib/export/repo/branch.rs View lib/export/repo/branch.rs
@@ -0,0 +1,128 @@
1use db::git::git_branches as branch_entity;
2use serde::{Deserialize, Serialize};
3use time::OffsetDateTime;
4use time::format_description::well_known::Rfc3339;
5use utoipa::{IntoParams, ToSchema};
6use uuid::Uuid;
7
8fn fmt_ts(ts: OffsetDateTime) -> String {
9 ts.format(&Rfc3339).unwrap_or_default()
10}
11
12fn fmt_ts_opt(ts: Option<OffsetDateTime>) -> Option<String> {
13 ts.map(fmt_ts)
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
17pub struct CommitMeta {
18 pub sha: String,
19 pub message: String,
20 pub author_name: Option<String>,
21 pub author_email: Option<String>,
22 pub committer_name: Option<String>,
23 pub committer_email: Option<String>,
24 pub authored_at: Option<String>,
25 pub committed_at: Option<String>,
26 pub tree_sha: Option<String>,
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
30pub struct BranchView {
31 pub id: Uuid,
32 pub repo_id: Uuid,
33 pub name: String,
34 pub commit_sha: String,
35
36 pub is_protected: bool,
37 pub protected: bool,
38 pub required_approvals: i64,
39 pub enable_status_check: bool,
40 pub status_check_contexts: Vec<String>,
41
42 pub user_can_push: bool,
43 pub user_can_merge: bool,
44 pub effective_branch_protection_name: String,
45
46 pub is_deleted: bool,
47 pub deleted_at: Option<String>,
48 pub commit: Option<CommitMeta>,
49 pub created_at: String,
50 pub updated_at: String,
51}
52
53impl From<branch_entity::Model> for BranchView {
54 fn from(m: branch_entity::Model) -> Self {
55 Self {
56 id: m.id,
57 repo_id: m.repo_id,
58 name: m.name,
59 commit_sha: m.commit_sha.clone(),
60 is_protected: m.is_protected,
61 protected: m.is_protected,
62 required_approvals: 0,
63 enable_status_check: false,
64 status_check_contexts: Vec::new(),
65 user_can_push: false,
66 user_can_merge: false,
67 effective_branch_protection_name: String::new(),
68 is_deleted: m.is_deleted,
69 deleted_at: fmt_ts_opt(m.deleted_at),
70 commit: Some(CommitMeta {
71 sha: m.commit_sha,
72 message: String::new(),
73 author_name: None,
74 author_email: None,
75 committer_name: None,
76 committer_email: None,
77 authored_at: None,
78 committed_at: None,
79 tree_sha: None,
80 }),
81 created_at: fmt_ts(m.created_at),
82 updated_at: fmt_ts(m.updated_at),
83 }
84 }
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
88pub struct BranchPage {
89 pub items: Vec<BranchView>,
90 pub total: i64,
91}
92
93#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
94pub struct CreateBranchOption {
95 pub new_branch_name: String,
96 pub old_ref_name: Option<String>,
97 pub old_branch_name: Option<String>,
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
101pub struct UpdateBranchOption {
102 pub new_commit_id: String,
103 pub old_commit_id: Option<String>,
104 pub force: Option<bool>,
105}
106
107#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
108pub struct RenameBranchOption {
109 pub new_name: String,
110}
111
112#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
113pub struct MergeUpstreamRequest {
114 pub branch: String,
115 pub ff_only: Option<bool>,
116}
117
118#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
119pub struct MergeUpstreamResponse {
120 pub merge_type: String,
121}
122
123#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, IntoParams)]
124pub struct ListBranchQuery {
125 pub page: Option<u64>,
126 pub limit: Option<u64>,
127 pub keyword: Option<String>,
128}