g1t/services/work/src/rows.rs

192 lines5,095 bytesCodeBlame
1//! Rows as they come out of D1, and their conversion to contract types.
2
3use g1t_contracts::User;
4use g1t_contracts::repos::RepoPath;
5use g1t_contracts::work::{
6 CheckStatus, Comment, Issue, IssueReason, Pull, PullStatus, Runtime, SessionEntry,
7 SessionEntryKind, State, Verdict,
8};
9use serde::Deserialize;
10
11fn user(id: String, username: String) -> User {
12 User {
13 id,
14 username,
15 ..User::default()
16 }
17}
18
19#[derive(Deserialize)]
20pub struct IssueRow {
21 pub id: String,
22 pub repo_id: String,
23 pub number: u32,
24 pub title: String,
25 pub body: String,
26 /// JSON array of label names.
27 pub labels: String,
28 /// JSON array of commands.
29 pub checks: String,
30 pub state: State,
31 pub reason: Option<IssueReason>,
32 pub resolved_by: Option<u32>,
33 pub author_id: String,
34 pub author_name: String,
35 pub created_at: String,
36 pub updated_at: String,
37 pub closed_at: Option<String>,
38 pub pull_count: u32,
39 pub comment_count: u32,
40}
41
42impl From<IssueRow> for Issue {
43 fn from(row: IssueRow) -> Self {
44 Issue {
45 id: row.id,
46 repo_id: row.repo_id,
47 number: row.number,
48 title: row.title,
49 body: row.body,
50 labels: serde_json::from_str(&row.labels).unwrap_or_default(),
51 checks: serde_json::from_str(&row.checks).unwrap_or_default(),
52 state: row.state,
53 reason: row.reason,
54 resolved_by: row.resolved_by,
55 author: user(row.author_id, row.author_name),
56 created_at: row.created_at,
57 updated_at: row.updated_at,
58 closed_at: row.closed_at,
59 pull_count: row.pull_count,
60 comment_count: row.comment_count,
61 }
62 }
63}
64
65#[derive(Deserialize)]
66pub struct PullRow {
67 pub id: String,
68 pub repo_id: String,
69 pub number: u32,
70 pub issue_number: Option<u32>,
71 pub title: String,
72 pub body: Option<String>,
73 pub agent: String,
74 pub runtime: Runtime,
75 pub status: PullStatus,
76 pub fork_repo_id: Option<String>,
77 pub fork_namespace: Option<String>,
78 pub fork_name: Option<String>,
79 pub source_branch: Option<String>,
80 pub head_commit: Option<String>,
81 pub merge_base: Option<String>,
82 pub merged_by: Option<String>,
83 pub merged_at: Option<String>,
84 pub superseded_by: Option<u32>,
85 pub check_status: Option<CheckStatus>,
86 pub author_id: String,
87 pub author_name: String,
88 pub created_at: String,
89 pub updated_at: String,
90}
91
92impl From<PullRow> for Pull {
93 fn from(row: PullRow) -> Self {
94 Pull {
95 id: row.id,
96 repo_id: row.repo_id,
97 number: row.number,
98 issue: row.issue_number,
99 title: row.title,
100 body: row.body,
101 agent: row.agent,
102 runtime: row.runtime,
103 status: row.status,
104 fork: row
105 .fork_namespace
106 .zip(row.fork_name)
107 .map(|(namespace, name)| RepoPath { namespace, name }),
108 fork_repo_id: row.fork_repo_id,
109 branch: row.source_branch,
110 head_commit: row.head_commit,
111 merge_base: row.merge_base,
112 merged_by: row.merged_by,
113 merged_at: row.merged_at,
114 superseded_by: row.superseded_by,
115 check_status: row.check_status,
116 author: user(row.author_id, row.author_name),
117 created_at: row.created_at,
118 updated_at: row.updated_at,
119 }
120 }
121}
122
123#[derive(Deserialize)]
124pub struct CommentRow {
125 pub id: String,
126 pub author_id: String,
127 pub author_name: String,
128 pub body: String,
129 pub path: Option<String>,
130 pub line: Option<u32>,
131 pub verdict: Option<Verdict>,
132 pub created_at: String,
133}
134
135impl From<CommentRow> for Comment {
136 fn from(row: CommentRow) -> Self {
137 Comment {
138 id: row.id,
139 author: user(row.author_id, row.author_name),
140 body: row.body,
141 path: row.path,
142 line: row.line,
143 verdict: row.verdict,
144 created_at: row.created_at,
145 }
146 }
147}
148
149#[derive(Deserialize)]
150pub struct SessionRow {
151 pub seq: u32,
152 pub kind: SessionEntryKind,
153 pub text: String,
154 pub tool: Option<String>,
155 pub commit: Option<String>,
156 pub at: String,
157}
158
159impl From<SessionRow> for SessionEntry {
160 fn from(row: SessionRow) -> Self {
161 SessionEntry {
162 seq: row.seq,
163 kind: row.kind,
164 text: row.text,
165 tool: row.tool,
166 commit: row.commit,
167 at: row.at,
168 }
169 }
170}
171
172/// A pull request whose head a push moved.
173#[derive(Deserialize)]
174pub struct MovedRow {
175 pub id: String,
176 pub repo_id: String,
177 pub number: u32,
178 pub issue_number: Option<u32>,
179 pub status: PullStatus,
180}
181
182/// A single number selected as `n`.
183#[derive(Deserialize)]
184pub struct NumberRow {
185 pub n: u32,
186}
187
188/// A single string selected as `value`.
189#[derive(Deserialize)]
190pub struct ValueRow {
191 pub value: String,
192}