OXYGENHUB
# a49fd826e10b oxygenhub-android / app / src / main / java / io / oxygenhub / android / api / Models.kt
11.2 KiB · last changed Diffs on a change, natively 10 days ago source blame history raw
1
package io.oxygenhub.android.api
2
3
import kotlinx.serialization.SerialName
4
import kotlinx.serialization.Serializable
5
6
// The hub's JSON, by hand from oxygenhub's crates/hub-api/src/lib.rs (and
7
// the device flow's answers in crates/hub/src/api/device.rs). Field names
8
// are the hub's own, snake case; times are milliseconds since the epoch.
9
// Every field has a default, so an added or missing field never breaks the
10
// app: the hub's types may still change before 1.0.
11
12
/** `GET /api/v1/user`. */
13
@Serializable
14
data class User(
15
    val username: String,
16
    @SerialName("display_name") val displayName: String = "",
17
    @SerialName("is_admin") val isAdmin: Boolean = false,
18
    val keys: List<String> = emptyList(),
19
    @SerialName("created_ms") val createdMs: Long = 0,
20
) {
21
    /** The name to greet: the display name, or the username without one. */
22
    val shownName: String get() = displayName.ifBlank { username }
23
}
24
25
@Serializable
26
enum class Visibility {
27
    @SerialName("public") Public,
28
    @SerialName("internal") Internal,
29
    @SerialName("private") Private,
30
}
31
32
/** A repository as `GET /api/v1/repos` lists it. */
33
@Serializable
34
data class Repository(
35
    val owner: String,
36
    val name: String,
37
    val description: String = "",
38
    @SerialName("default_bookmark") val defaultBookmark: String = "main",
39
    val visibility: Visibility = Visibility.Public,
40
    @SerialName("created_ms") val createdMs: Long = 0,
41
    @SerialName("updated_ms") val updatedMs: Long = 0,
42
    @SerialName("clone_url") val cloneUrl: String = "",
43
) {
44
    val slug: String get() = "$owner/$name"
45
}
46
47
@Serializable
48
data class Label(
49
    val name: String,
50
    /** `#rrggbb`. */
51
    val colour: String = "",
52
    val description: String = "",
53
)
54
55
/** `GET /api/v1/repos/{owner}/{name}/issues`. */
56
@Serializable
57
data class Issue(
58
    val number: Long,
59
    val title: String,
60
    val body: String = "",
61
    val author: String = "",
62
    /** `open` or `closed`. */
63
    val state: String = "open",
64
    /** Why it closed: `completed`, `not_planned`, … (empty while open). */
65
    val reason: String = "",
66
    val labels: List<Label> = emptyList(),
67
    val assignees: List<String> = emptyList(),
68
    val milestone: String? = null,
69
    val comments: Long = 0,
70
    val locked: Boolean = false,
71
    val pinned: Boolean = false,
72
    @SerialName("created_ms") val createdMs: Long = 0,
73
    @SerialName("updated_ms") val updatedMs: Long = 0,
74
    @SerialName("closed_ms") val closedMs: Long? = null,
75
    val url: String = "",
76
) {
77
    val isOpen: Boolean get() = state == "open"
78
}
79
80
/** Which issues to list: the hub's `?state=`. */
81
enum class IssueState(val query: String) { Open("open"), Closed("closed"), All("all") }
82
83
/**
84
 * A proposal (`GET /api/v1/repos/{owner}/{name}/proposals`, `…/proposals/{n}`):
85
 * changes asking to join a target bookmark. The hub decides whether it may
86
 * land, and says why not in [blockers], in its own words.
87
 */
88
@Serializable
89
data class Proposal(
90
    val number: Long,
91
    val title: String,
92
    val body: String = "",
93
    val author: String = "",
94
    /** `draft`, `open`, `landed` or `closed`. */
95
    val state: String = "open",
96
    /** The bookmark it lands on. */
97
    val target: String = "main",
98
    /** The changes it adds, in dependency order, newest last. */
99
    val changes: List<ProposalChange> = emptyList(),
100
    val conflicted: Boolean = false,
101
    val approvals: List<Approval> = emptyList(),
102
    val landable: Boolean = false,
103
    val blockers: List<String> = emptyList(),
104
    @SerialName("created_ms") val createdMs: Long = 0,
105
    @SerialName("updated_ms") val updatedMs: Long = 0,
106
    val url: String = "",
107
    val landed: Landed? = null,
108
    /** Who set it to land by itself when its checks pass, while set. */
109
    @SerialName("auto_land") val autoLand: String? = null,
110
    @SerialName("requested_reviewers") val requestedReviewers: List<String> = emptyList(),
111
    val labels: List<Label> = emptyList(),
112
) {
113
    val isOpen: Boolean get() = state == "open" || state == "draft"
114
    val isDraft: Boolean get() = state == "draft"
115
}
116
117
/** `GET /api/v1/user/proposals`: the proposals to act on, as the hub's dashboard lists them. */
118
@Serializable
119
data class UserProposals(
120
    /** Open ones by others in the caller's repositories, not yet reviewed by them. */
121
    val review: List<ProposalBrief> = emptyList(),
122
    /** The caller's own open ones and drafts. */
123
    val authored: List<ProposalBrief> = emptyList(),
124
    /** Asked for with `landable=true`: open ones the caller can land now. */
125
    val landable: List<ProposalBrief> = emptyList(),
126
)
127
128
/** A proposal in a list across repositories. */
129
@Serializable
130
data class ProposalBrief(
131
    /** `owner/name`. */
132
    val repository: String,
133
    val number: Long,
134
    val title: String = "",
135
    val author: String = "",
136
    val state: String = "open",
137
    val conflicted: Boolean = false,
138
    @SerialName("updated_ms") val updatedMs: Long = 0,
139
    val url: String = "",
140
) {
141
    val owner: String get() = repository.substringBefore('/')
142
    val name: String get() = repository.substringAfter('/')
143
}
144
145
/** `POST /api/v1/user/runner-token`: a one-time token that registers a runner for [owner]'s repositories. */
146
@Serializable
147
data class RunnerToken(val token: String, val owner: String = "") {
148
    // The token is a secret until it is used.
149
    override fun toString(): String = "RunnerToken(owner=$owner, token=<redacted>)"
150
}
151
152
/** A change a proposal adds. */
153
@Serializable
154
data class ProposalChange(
155
    val hash: String,
156
    val message: String = "",
157
    /** One that nothing else in the proposal depends on. */
158
    val head: Boolean = false,
159
) {
160
    /** The message's first line. */
161
    val subject: String get() = message.lineSequence().firstOrNull().orEmpty()
162
}
163
164
/** A file or directory of a repository's tree at some reference (the code browser, #3). */
165
@Serializable
166
data class TreeEntry(
167
    val name: String,
168
    val path: String,
169
    /** `file`, `dir`, `symlink` or `subrepo`. */
170
    val kind: String = "file",
171
    val exec: Boolean = false,
172
    val size: Long? = null,
173
    val conflicted: Boolean = false,
174
) {
175
    val isDir: Boolean get() = kind == "dir"
176
}
177
178
/** Who made a change, as the hub knows them. */
179
@Serializable
180
data class ChangeAuthor(val name: String = "", val email: String = "", val account: String? = null)
181
182
/** A change in a history or a proposal. */
183
@Serializable
184
data class ChangeSummary(
185
    val hash: String,
186
    val message: String = "",
187
    val author: ChangeAuthor = ChangeAuthor(),
188
    @SerialName("when_ms") val whenMs: Long = 0,
189
) {
190
    val subject: String get() = message.lineSequence().firstOrNull().orEmpty()
191
    val short: String get() = hash.take(12)
192
}
193
194
/** A page of a reference's history (`GET …/history/{r}`), newest first. */
195
@Serializable
196
data class History(
197
    val reference: String = "",
198
    val total: Int = 0,
199
    val offset: Int = 0,
200
    val changes: List<ChangeSummary> = emptyList(),
201
)
202
203
/** A file a change touched, and how. */
204
@Serializable
205
data class FileChange(
206
    val path: String,
207
    /** `added`, `modified`, `removed` or `renamed`. */
208
    val kind: String = "modified",
209
    val from: String? = null,
210
    val added: Int = 0,
211
    val removed: Int = 0,
212
    /** The diff with three lines of context (hub #320), cut at 2000 lines. */
213
    val lines: List<DiffLine> = emptyList(),
214
    val truncated: Boolean = false,
215
)
216
217
/** A line of a file's diff: `context`, `add`, `del`, or `gap` for lines left out between hunks. */
218
@Serializable
219
data class DiffLine(
220
    val kind: String = "context",
221
    val old: Int? = null,
222
    val new: Int? = null,
223
    val text: String = "",
224
)
225
226
/** One change, whole (`GET …/changes/{hash}`). */
227
@Serializable
228
data class ChangeDetail(
229
    val change: ChangeSummary,
230
    val body: String = "",
231
    @SerialName("in_bookmarks") val inBookmarks: List<String> = emptyList(),
232
    @SerialName("in_tags") val inTags: List<String> = emptyList(),
233
    val files: List<FileChange> = emptyList(),
234
)
235
236
/** A bookmark or tag to browse at. */
237
@Serializable
238
data class NamedRef(val name: String)
239
240
/** A reviewer's approval; it counts only when [sealed] (a seal over every change, by a key linked to them). */
241
@Serializable
242
data class Approval(val account: String, val sealed: Boolean = false)
243
244
/** Who landed a proposal, how (`web`, or `api (token NAME)`), and when. */
245
@Serializable
246
data class Landed(val by: String, val via: String = "", @SerialName("when_ms") val whenMs: Long = 0)
247
248
/** Which proposals to list. `Open` includes drafts, as the hub's own list does. */
249
enum class ProposalState(val query: String) { Open("open"), Landed("landed"), Closed("closed"), All("all") }
250
251
/** A check on a state (`GET …/proposals/{n}/checks`): a Flow job, or another reporter's. */
252
@Serializable
253
data class Check(
254
    val name: String,
255
    /** `queued`, `running`, `passed`, `failed`, `cancelled` or `skipped`. */
256
    val status: String = "queued",
257
    val summary: String = "",
258
    val url: String = "",
259
    @SerialName("started_ms") val startedMs: Long = 0,
260
    @SerialName("finished_ms") val finishedMs: Long? = null,
261
)
262
263
/** One entry in the caller's inbox. */
264
@Serializable
265
data class Notification(
266
    val id: Long,
267
    @SerialName("at_ms") val atMs: Long = 0,
268
    /** `issue.opened`, `proposal.comment`, `mention`, `review`, … */
269
    val kind: String = "",
270
    val title: String = "",
271
    /** The page on the hub, absolute. */
272
    val url: String = "",
273
    val actor: String = "",
274
    val read: Boolean = false,
275
)
276
277
/** `GET /api/v1/notifications`: the unread count and the newest entries, unread first. */
278
@Serializable
279
data class Inbox(
280
    val unread: Long = 0,
281
    val items: List<Notification> = emptyList(),
282
)
283
284
/** An RFC 7807 problem: what the API answers with on an error. */
285
@Serializable
286
data class Problem(
287
    val status: Int = 0,
288
    val title: String = "",
289
    val detail: String = "",
290
)
291
292
/** The device flow's errors, RFC 8628's shape: `{"error": …, "error_description": …}`. */
293
@Serializable
294
data class OAuthError(
295
    val error: String = "",
296
    @SerialName("error_description") val description: String = "",
297
)
298
299
/** `POST /api/v1/device/code`'s answer. */
300
@Serializable
301
data class DeviceCode(
302
    @SerialName("device_code") val deviceCode: String,
303
    @SerialName("user_code") val userCode: String,
304
    @SerialName("verification_uri") val verificationUri: String,
305
    @SerialName("verification_uri_complete") val verificationUriComplete: String = "",
306
    @SerialName("expires_in") val expiresIn: Long = 900,
307
    val interval: Long = 5,
308
) {
309
    /** The page to open: with the code filled in when the hub offers it. */
310
    val pageToOpen: String get() = verificationUriComplete.ifBlank { verificationUri }
311
312
    // The device code is a secret until it is used: never in logs.
313
    override fun toString(): String = "DeviceCode(userCode=$userCode, verificationUri=$verificationUri)"
314
}
315
316
/** `POST /api/v1/device/token`'s answer once the user approved. */
317
@Serializable
318
data class TokenGrant(
319
    @SerialName("access_token") val accessToken: String,
320
    @SerialName("token_type") val tokenType: String = "bearer",
321
    /** The scopes the user allowed, space separated (they may untick some). */
322
    val scope: String = "",
323
    val username: String = "",
324
    @SerialName("expires_in") val expiresIn: Long = 0,
325
) {
326
    val scopes: List<String> get() = scope.split(' ').filter { it.isNotBlank() }
327
328
    override fun toString(): String = "TokenGrant(username=$username, scope=$scope, accessToken=<redacted>)"
329
}