refactor(repo): remove pull_request types (inlined)
zhenyi committed
Deleted lib/repo/features/pull_request/types.rs lib/repo/features/pull_request/types.rs +0 −268
@@ -1,268 +0,0 @@
1use db::pull_request::pull_request_comment as comment_entity;
2use db::pull_request::pull_request_event_line as event_entity;
3use db::pull_request::pull_request_review as review_entity;
4use db::pull_request::pull_requests as pr_entity;
5use serde::{Deserialize, Serialize};
6use time::OffsetDateTime;
7use time::format_description::well_known::Rfc3339;
8use utoipa::{IntoParams, ToSchema};
9use uuid::Uuid;
10
11fn fmt_ts(ts: OffsetDateTime) -> String {
12 ts.format(&Rfc3339).unwrap_or_default()
13}
14
15fn fmt_ts_opt(ts: Option<OffsetDateTime>) -> Option<String> {
16 ts.map(fmt_ts)
17}
18
19fn fmt_uuid_opt(id: Option<Uuid>) -> Option<String> {
20 id.map(|v| v.to_string())
21}
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
24#[serde(rename_all = "lowercase")]
25pub enum PrState {
26 Open,
27 Closed,
28 Merged,
29}
30
31impl PrState {
32 pub fn from_model(has_merged: bool, closed_at: Option<OffsetDateTime>) -> Self {
33 if has_merged {
34 Self::Merged
35 } else if closed_at.is_some() {
36 Self::Closed
37 } else {
38 Self::Open
39 }
40 }
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
44pub struct PullRequestView {
45 pub id: Uuid,
46 pub repo_id: Uuid,
47 pub number: i64,
48 pub title: String,
49 pub body: Option<String>,
50 pub head_branch: String,
51 pub head_sha: Option<String>,
52 pub base_branch: String,
53 pub base_sha: Option<String>,
54 pub author_id: Uuid,
55 pub state: PrState,
56 pub is_locked: bool,
57 pub has_merged: bool,
58 pub merged_at: Option<String>,
59 pub merger_id: Option<String>,
60 pub merged_commit_sha: Option<String>,
61 pub closed_at: Option<String>,
62 pub created_at: String,
63 pub updated_at: Option<String>,
64 pub mergeable: Option<bool>,
65 pub mergeable_state: Option<String>,
66 pub comments_count: i64,
67 pub reviews_count: i64,
68}
69
70impl From<pr_entity::Model> for PullRequestView {
71 fn from(m: pr_entity::Model) -> Self {
72 Self {
73 id: m.id,
74 repo_id: m.repo_id,
75 number: m.autoincrement_id as i64,
76 title: m.title,
77 body: (!m.body.is_empty()).then_some(m.body),
78 head_branch: m.head_branch,
79 head_sha: (!m.head_sha.is_empty()).then_some(m.head_sha),
80 base_branch: m.base_branch,
81 base_sha: (!m.base_sha.is_empty()).then_some(m.base_sha),
82 author_id: m.author_id,
83 state: PrState::from_model(m.has_merged, m.closed_at),
84 is_locked: m.is_locked,
85 has_merged: m.has_merged,
86 merged_at: fmt_ts_opt(m.merged_at),
87 merger_id: fmt_uuid_opt(m.merger_id),
88 merged_commit_sha: m.merged_commit_sha,
89 closed_at: fmt_ts_opt(m.closed_at),
90 created_at: fmt_ts(m.created_at),
91 updated_at: fmt_ts_opt(m.updated_at),
92 mergeable: None,
93 mergeable_state: None,
94 comments_count: 0,
95 reviews_count: 0,
96 }
97 }
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
101pub struct PullRequestPage {
102 pub items: Vec<PullRequestView>,
103 pub total: i64,
104}
105
106#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
107pub struct CreatePullRequestOption {
108 pub title: String,
109 pub body: Option<String>,
110 pub head: String,
111 pub base: String,
112}
113
114#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
115pub struct UpdatePullRequestOption {
116 pub title: Option<String>,
117 pub body: Option<String>,
118 pub base: Option<String>,
119 pub head: Option<String>,
120}
121
122#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
123#[serde(rename_all = "snake_case")]
124pub enum MergeStyle {
125 FastForward,
126 Rebase,
127 RebaseMerge,
128 Squash,
129 Merge,
130 ManuallyMerged,
131}
132
133#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
134pub struct MergePullRequestOption {
135 pub style: MergeStyle,
136 #[serde(default)]
137 pub delete_branch_after_merge: bool,
138 pub commit_title: Option<String>,
139 pub commit_message: Option<String>,
140}
141
142#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
143pub struct MergeView {
144 pub sha: String,
145 pub merged: bool,
146 pub message: Option<String>,
147}
148
149#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
150pub struct PullRequestCommentView {
151 pub id: Uuid,
152 pub pull_request_id: Uuid,
153 pub user_id: Uuid,
154 pub review_id: Option<String>,
155 pub commit_sha: Option<String>,
156 pub path: Option<String>,
157 pub line: Option<i32>,
158 pub body: String,
159 pub created_at: String,
160 pub updated_at: Option<String>,
161}
162
163#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
164pub struct CreatePullRequestCommentOption {
165 pub body: String,
166 pub commit_sha: Option<String>,
167 pub path: Option<String>,
168 pub line: Option<i32>,
169}
170
171#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
172#[serde(rename_all = "snake_case")]
173pub enum ReviewState {
174 Approved,
175 ChangesRequested,
176 Commented,
177}
178
179#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
180pub struct PullRequestReviewView {
181 pub id: Uuid,
182 pub pull_request_id: Uuid,
183 pub user_id: Uuid,
184 pub commit_sha: Option<String>,
185 pub official: bool,
186 pub content: Option<String>,
187 pub state: ReviewState,
188 pub created_at: String,
189 pub updated_at: Option<String>,
190}
191
192#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
193pub struct SubmitPullRequestReviewOption {
194 pub state: ReviewState,
195 pub content: Option<String>,
196 pub commit_sha: Option<String>,
197}
198
199#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
200pub struct PullRequestEventView {
201 pub id: Uuid,
202 pub pull_request_id: Uuid,
203 pub user_id: Uuid,
204 pub event_type: String,
205 pub commit_sha: Option<String>,
206 pub content: Option<String>,
207 pub created_at: String,
208}
209
210#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, IntoParams)]
211pub struct ListPullRequestQuery {
212 pub page: Option<u64>,
213 pub limit: Option<u64>,
214 pub state: Option<String>,
215 pub keyword: Option<String>,
216}
217
218impl From<comment_entity::Model> for PullRequestCommentView {
219 fn from(m: comment_entity::Model) -> Self {
220 Self {
221 id: m.id,
222 pull_request_id: m.pull_request_id,
223 user_id: m.user_id,
224 review_id: fmt_uuid_opt(m.review_id),
225 commit_sha: (!m.commit_sha.is_empty()).then_some(m.commit_sha),
226 path: (!m.path.is_empty()).then_some(m.path),
227 line: m.line,
228 body: m.body,
229 created_at: fmt_ts(m.created_at),
230 updated_at: fmt_ts_opt(m.updated_at),
231 }
232 }
233}
234
235impl From<review_entity::Model> for PullRequestReviewView {
236 fn from(m: review_entity::Model) -> Self {
237 let state = match m.state {
238 review_entity::ReviewState::Approved => ReviewState::Approved,
239 review_entity::ReviewState::ChangesRequested => ReviewState::ChangesRequested,
240 review_entity::ReviewState::Commented => ReviewState::Commented,
241 };
242 Self {
243 id: m.id,
244 pull_request_id: m.pull_request_id,
245 user_id: m.user_id,
246 commit_sha: (!m.commit_sha.is_empty()).then_some(m.commit_sha),
247 official: m.official,
248 content: m.content,
249 state,
250 created_at: fmt_ts(m.created_at),
251 updated_at: fmt_ts_opt(m.updated_at),
252 }
253 }
254}
255
256impl From<event_entity::Model> for PullRequestEventView {
257 fn from(m: event_entity::Model) -> Self {
258 Self {
259 id: m.id,
260 pull_request_id: m.pull_request_id,
261 user_id: m.user_id,
262 event_type: m.event_type,
263 commit_sha: m.commit_sha,
264 content: m.content,
265 created_at: fmt_ts(m.created_at),
266 }
267 }
268}