OXYGENHUB
# 1c42a70fcba3 oxygenhub-android / app / src / main / java / io / oxygenhub / android / api / Models.kt
14.0 KiB · last changed Flow on the phone: a repository's runs, a run's jobs, live logs, rerun and cancel (refs #5) 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
/** A Flow run of a repository's pipeline (#5). */
81
@Serializable
82
data class FlowRun(
83
    val number: Long,
84
    val bookmark: String = "",
85
    /** `queued`, `running`, `passed`, `failed`, `error` or `cancelled`. */
86
    val status: String = "queued",
87
    /** `push`, `manual`, `schedule`, `proposal` or `rerun:N`. */
88
    val trigger: String = "",
89
    val actor: String = "",
90
    val error: String = "",
91
    @SerialName("created_ms") val createdMs: Long = 0,
92
    @SerialName("finished_ms") val finishedMs: Long? = null,
93
    val url: String = "",
94
    @SerialName("queue_position") val queuePosition: Long? = null,
95
    val jobs: List<FlowJob> = emptyList(),
96
) {
97
    val live: Boolean get() = status == "queued" || status == "running"
98
}
99
100
/** A job of a run. */
101
@Serializable
102
data class FlowJob(
103
    val name: String,
104
    val status: String = "queued",
105
    @SerialName("depends_on") val dependsOn: List<String> = emptyList(),
106
    val runner: String = "",
107
    @SerialName("exit_code") val exitCode: Long? = null,
108
    val error: String = "",
109
    @SerialName("started_ms") val startedMs: Long? = null,
110
    @SerialName("finished_ms") val finishedMs: Long? = null,
111
    @SerialName("log_lines") val logLines: Long = 0,
112
    val waiting: String = "",
113
) {
114
    val live: Boolean get() = status == "queued" || status == "running"
115
116
    /** How long it ran, or has been running, in seconds. */
117
    fun seconds(now: Long = System.currentTimeMillis()): Long? = startedMs?.let { ((finishedMs ?: now) - it) / 1000 }
118
}
119
120
/** A line of a job's log: `stdout`, `stderr` or `flow` (the runner's own). */
121
@Serializable
122
data class LogLine(val seq: Long, val t: Long = 0, val stream: String = "stdout", val text: String = "")
123
124
/** A project board's summary (`GET /projects/{owner}`). */
125
@Serializable
126
data class Project(
127
    val owner: String,
128
    val number: Long,
129
    val title: String = "",
130
    val description: String = "",
131
    val public: Boolean = true,
132
    val closed: Boolean = false,
133
    @SerialName("updated_ms") val updatedMs: Long = 0,
134
    val url: String = "",
135
)
136
137
/** A card on a board: an issue, a proposal or a note. */
138
@Serializable
139
data class ProjectCard(
140
    val id: Long,
141
    /** `issue`, `proposal` or `note`. */
142
    val kind: String = "note",
143
    /** `owner/name#N`; empty for a note. */
144
    val reference: String = "",
145
    val title: String = "",
146
    val state: String = "",
147
    val labels: List<String> = emptyList(),
148
    val assignees: List<String> = emptyList(),
149
) {
150
    /** The repository and number a card refers to, or null for a note. */
151
    val target: Triple<String, String, Long>? get() {
152
        val slug = reference.substringBefore('#', "")
153
        val n = reference.substringAfter('#', "").toLongOrNull() ?: return null
154
        val (owner, name) = slug.split('/').takeIf { it.size == 2 } ?: return null
155
        return Triple(owner, name, n)
156
    }
157
}
158
159
@Serializable
160
data class ProjectColumn(val id: Long, val name: String, val cards: List<ProjectCard> = emptyList())
161
162
/** A board, its columns in order (`GET /projects/{owner}/{n}`). */
163
@Serializable
164
data class Board(val project: Project, val columns: List<ProjectColumn> = emptyList())
165
166
/** A comment on an issue or proposal. */
167
@Serializable
168
data class Comment(
169
    val id: Long = 0,
170
    val author: String = "",
171
    val body: String = "",
172
    @SerialName("created_ms") val createdMs: Long = 0,
173
)
174
175
/** Which issues to list: the hub's `?state=`. */
176
enum class IssueState(val query: String) { Open("open"), Closed("closed"), All("all") }
177
178
/**
179
 * A proposal (`GET /api/v1/repos/{owner}/{name}/proposals`, `…/proposals/{n}`):
180
 * changes asking to join a target bookmark. The hub decides whether it may
181
 * land, and says why not in [blockers], in its own words.
182
 */
183
@Serializable
184
data class Proposal(
185
    val number: Long,
186
    val title: String,
187
    val body: String = "",
188
    val author: String = "",
189
    /** `draft`, `open`, `landed` or `closed`. */
190
    val state: String = "open",
191
    /** The bookmark it lands on. */
192
    val target: String = "main",
193
    /** The changes it adds, in dependency order, newest last. */
194
    val changes: List<ProposalChange> = emptyList(),
195
    val conflicted: Boolean = false,
196
    val approvals: List<Approval> = emptyList(),
197
    val landable: Boolean = false,
198
    val blockers: List<String> = emptyList(),
199
    @SerialName("created_ms") val createdMs: Long = 0,
200
    @SerialName("updated_ms") val updatedMs: Long = 0,
201
    val url: String = "",
202
    val landed: Landed? = null,
203
    /** Who set it to land by itself when its checks pass, while set. */
204
    @SerialName("auto_land") val autoLand: String? = null,
205
    @SerialName("requested_reviewers") val requestedReviewers: List<String> = emptyList(),
206
    val labels: List<Label> = emptyList(),
207
) {
208
    val isOpen: Boolean get() = state == "open" || state == "draft"
209
    val isDraft: Boolean get() = state == "draft"
210
}
211
212
/** `GET /api/v1/user/proposals`: the proposals to act on, as the hub's dashboard lists them. */
213
@Serializable
214
data class UserProposals(
215
    /** Open ones by others in the caller's repositories, not yet reviewed by them. */
216
    val review: List<ProposalBrief> = emptyList(),
217
    /** The caller's own open ones and drafts. */
218
    val authored: List<ProposalBrief> = emptyList(),
219
    /** Asked for with `landable=true`: open ones the caller can land now. */
220
    val landable: List<ProposalBrief> = emptyList(),
221
)
222
223
/** A proposal in a list across repositories. */
224
@Serializable
225
data class ProposalBrief(
226
    /** `owner/name`. */
227
    val repository: String,
228
    val number: Long,
229
    val title: String = "",
230
    val author: String = "",
231
    val state: String = "open",
232
    val conflicted: Boolean = false,
233
    @SerialName("updated_ms") val updatedMs: Long = 0,
234
    val url: String = "",
235
) {
236
    val owner: String get() = repository.substringBefore('/')
237
    val name: String get() = repository.substringAfter('/')
238
}
239
240
/** `POST /api/v1/user/runner-token`: a one-time token that registers a runner for [owner]'s repositories. */
241
@Serializable
242
data class RunnerToken(val token: String, val owner: String = "") {
243
    // The token is a secret until it is used.
244
    override fun toString(): String = "RunnerToken(owner=$owner, token=<redacted>)"
245
}
246
247
/** A change a proposal adds. */
248
@Serializable
249
data class ProposalChange(
250
    val hash: String,
251
    val message: String = "",
252
    /** One that nothing else in the proposal depends on. */
253
    val head: Boolean = false,
254
) {
255
    /** The message's first line. */
256
    val subject: String get() = message.lineSequence().firstOrNull().orEmpty()
257
}
258
259
/** A file or directory of a repository's tree at some reference (the code browser, #3). */
260
@Serializable
261
data class TreeEntry(
262
    val name: String,
263
    val path: String,
264
    /** `file`, `dir`, `symlink` or `subrepo`. */
265
    val kind: String = "file",
266
    val exec: Boolean = false,
267
    val size: Long? = null,
268
    val conflicted: Boolean = false,
269
) {
270
    val isDir: Boolean get() = kind == "dir"
271
}
272
273
/** Who made a change, as the hub knows them. */
274
@Serializable
275
data class ChangeAuthor(val name: String = "", val email: String = "", val account: String? = null)
276
277
/** A change in a history or a proposal. */
278
@Serializable
279
data class ChangeSummary(
280
    val hash: String,
281
    val message: String = "",
282
    val author: ChangeAuthor = ChangeAuthor(),
283
    @SerialName("when_ms") val whenMs: Long = 0,
284
) {
285
    val subject: String get() = message.lineSequence().firstOrNull().orEmpty()
286
    val short: String get() = hash.take(12)
287
}
288
289
/** A page of a reference's history (`GET …/history/{r}`), newest first. */
290
@Serializable
291
data class History(
292
    val reference: String = "",
293
    val total: Int = 0,
294
    val offset: Int = 0,
295
    val changes: List<ChangeSummary> = emptyList(),
296
)
297
298
/** A file a change touched, and how. */
299
@Serializable
300
data class FileChange(
301
    val path: String,
302
    /** `added`, `modified`, `removed` or `renamed`. */
303
    val kind: String = "modified",
304
    val from: String? = null,
305
    val added: Int = 0,
306
    val removed: Int = 0,
307
)
308
309
/** One change, whole (`GET …/changes/{hash}`). */
310
@Serializable
311
data class ChangeDetail(
312
    val change: ChangeSummary,
313
    val body: String = "",
314
    @SerialName("in_bookmarks") val inBookmarks: List<String> = emptyList(),
315
    @SerialName("in_tags") val inTags: List<String> = emptyList(),
316
    val files: List<FileChange> = emptyList(),
317
)
318
319
/** A bookmark or tag to browse at. */
320
@Serializable
321
data class NamedRef(val name: String)
322
323
/** A reviewer's approval; it counts only when [sealed] (a seal over every change, by a key linked to them). */
324
@Serializable
325
data class Approval(val account: String, val sealed: Boolean = false)
326
327
/** Who landed a proposal, how (`web`, or `api (token NAME)`), and when. */
328
@Serializable
329
data class Landed(val by: String, val via: String = "", @SerialName("when_ms") val whenMs: Long = 0)
330
331
/** Which proposals to list. `Open` includes drafts, as the hub's own list does. */
332
enum class ProposalState(val query: String) { Open("open"), Landed("landed"), Closed("closed"), All("all") }
333
334
/** A check on a state (`GET …/proposals/{n}/checks`): a Flow job, or another reporter's. */
335
@Serializable
336
data class Check(
337
    val name: String,
338
    /** `queued`, `running`, `passed`, `failed`, `cancelled` or `skipped`. */
339
    val status: String = "queued",
340
    val summary: String = "",
341
    val url: String = "",
342
    @SerialName("started_ms") val startedMs: Long = 0,
343
    @SerialName("finished_ms") val finishedMs: Long? = null,
344
)
345
346
/** One entry in the caller's inbox. */
347
@Serializable
348
data class Notification(
349
    val id: Long,
350
    @SerialName("at_ms") val atMs: Long = 0,
351
    /** `issue.opened`, `proposal.comment`, `mention`, `review`, … */
352
    val kind: String = "",
353
    val title: String = "",
354
    /** The page on the hub, absolute. */
355
    val url: String = "",
356
    val actor: String = "",
357
    val read: Boolean = false,
358
)
359
360
/** `GET /api/v1/notifications`: the unread count and the newest entries, unread first. */
361
@Serializable
362
data class Inbox(
363
    val unread: Long = 0,
364
    val items: List<Notification> = emptyList(),
365
)
366
367
/** An RFC 7807 problem: what the API answers with on an error. */
368
@Serializable
369
data class Problem(
370
    val status: Int = 0,
371
    val title: String = "",
372
    val detail: String = "",
373
)
374
375
/** The device flow's errors, RFC 8628's shape: `{"error": …, "error_description": …}`. */
376
@Serializable
377
data class OAuthError(
378
    val error: String = "",
379
    @SerialName("error_description") val description: String = "",
380
)
381
382
/** `POST /api/v1/device/code`'s answer. */
383
@Serializable
384
data class DeviceCode(
385
    @SerialName("device_code") val deviceCode: String,
386
    @SerialName("user_code") val userCode: String,
387
    @SerialName("verification_uri") val verificationUri: String,
388
    @SerialName("verification_uri_complete") val verificationUriComplete: String = "",
389
    @SerialName("expires_in") val expiresIn: Long = 900,
390
    val interval: Long = 5,
391
) {
392
    /** The page to open: with the code filled in when the hub offers it. */
393
    val pageToOpen: String get() = verificationUriComplete.ifBlank { verificationUri }
394
395
    // The device code is a secret until it is used: never in logs.
396
    override fun toString(): String = "DeviceCode(userCode=$userCode, verificationUri=$verificationUri)"
397
}
398
399
/** `POST /api/v1/device/token`'s answer once the user approved. */
400
@Serializable
401
data class TokenGrant(
402
    @SerialName("access_token") val accessToken: String,
403
    @SerialName("token_type") val tokenType: String = "bearer",
404
    /** The scopes the user allowed, space separated (they may untick some). */
405
    val scope: String = "",
406
    val username: String = "",
407
    @SerialName("expires_in") val expiresIn: Long = 0,
408
) {
409
    val scopes: List<String> get() = scope.split(' ').filter { it.isNotBlank() }
410
411
    override fun toString(): String = "TokenGrant(username=$username, scope=$scope, accessToken=<redacted>)"
412
}