OXYGENHUB
# 31e4f3755627 oxygenhub-android / app / src / main / java / io / oxygenhub / android / api / Models.kt
11.0 KiB · last changed Issues on the phone: read, comment, close and reopen, labels, and new issues (refs #4) 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 comment on an issue or proposal. */
81
@Serializable
82
data class Comment(
83
    val id: Long = 0,
84
    val author: String = "",
85
    val body: String = "",
86
    @SerialName("created_ms") val createdMs: Long = 0,
87
)
88
89
/** Which issues to list: the hub's `?state=`. */
90
enum class IssueState(val query: String) { Open("open"), Closed("closed"), All("all") }
91
92
/**
93
 * A proposal (`GET /api/v1/repos/{owner}/{name}/proposals`, `…/proposals/{n}`):
94
 * changes asking to join a target bookmark. The hub decides whether it may
95
 * land, and says why not in [blockers], in its own words.
96
 */
97
@Serializable
98
data class Proposal(
99
    val number: Long,
100
    val title: String,
101
    val body: String = "",
102
    val author: String = "",
103
    /** `draft`, `open`, `landed` or `closed`. */
104
    val state: String = "open",
105
    /** The bookmark it lands on. */
106
    val target: String = "main",
107
    /** The changes it adds, in dependency order, newest last. */
108
    val changes: List<ProposalChange> = emptyList(),
109
    val conflicted: Boolean = false,
110
    val approvals: List<Approval> = emptyList(),
111
    val landable: Boolean = false,
112
    val blockers: List<String> = emptyList(),
113
    @SerialName("created_ms") val createdMs: Long = 0,
114
    @SerialName("updated_ms") val updatedMs: Long = 0,
115
    val url: String = "",
116
    val landed: Landed? = null,
117
    /** Who set it to land by itself when its checks pass, while set. */
118
    @SerialName("auto_land") val autoLand: String? = null,
119
    @SerialName("requested_reviewers") val requestedReviewers: List<String> = emptyList(),
120
    val labels: List<Label> = emptyList(),
121
) {
122
    val isOpen: Boolean get() = state == "open" || state == "draft"
123
    val isDraft: Boolean get() = state == "draft"
124
}
125
126
/** `GET /api/v1/user/proposals`: the proposals to act on, as the hub's dashboard lists them. */
127
@Serializable
128
data class UserProposals(
129
    /** Open ones by others in the caller's repositories, not yet reviewed by them. */
130
    val review: List<ProposalBrief> = emptyList(),
131
    /** The caller's own open ones and drafts. */
132
    val authored: List<ProposalBrief> = emptyList(),
133
    /** Asked for with `landable=true`: open ones the caller can land now. */
134
    val landable: List<ProposalBrief> = emptyList(),
135
)
136
137
/** A proposal in a list across repositories. */
138
@Serializable
139
data class ProposalBrief(
140
    /** `owner/name`. */
141
    val repository: String,
142
    val number: Long,
143
    val title: String = "",
144
    val author: String = "",
145
    val state: String = "open",
146
    val conflicted: Boolean = false,
147
    @SerialName("updated_ms") val updatedMs: Long = 0,
148
    val url: String = "",
149
) {
150
    val owner: String get() = repository.substringBefore('/')
151
    val name: String get() = repository.substringAfter('/')
152
}
153
154
/** `POST /api/v1/user/runner-token`: a one-time token that registers a runner for [owner]'s repositories. */
155
@Serializable
156
data class RunnerToken(val token: String, val owner: String = "") {
157
    // The token is a secret until it is used.
158
    override fun toString(): String = "RunnerToken(owner=$owner, token=<redacted>)"
159
}
160
161
/** A change a proposal adds. */
162
@Serializable
163
data class ProposalChange(
164
    val hash: String,
165
    val message: String = "",
166
    /** One that nothing else in the proposal depends on. */
167
    val head: Boolean = false,
168
) {
169
    /** The message's first line. */
170
    val subject: String get() = message.lineSequence().firstOrNull().orEmpty()
171
}
172
173
/** A file or directory of a repository's tree at some reference (the code browser, #3). */
174
@Serializable
175
data class TreeEntry(
176
    val name: String,
177
    val path: String,
178
    /** `file`, `dir`, `symlink` or `subrepo`. */
179
    val kind: String = "file",
180
    val exec: Boolean = false,
181
    val size: Long? = null,
182
    val conflicted: Boolean = false,
183
) {
184
    val isDir: Boolean get() = kind == "dir"
185
}
186
187
/** Who made a change, as the hub knows them. */
188
@Serializable
189
data class ChangeAuthor(val name: String = "", val email: String = "", val account: String? = null)
190
191
/** A change in a history or a proposal. */
192
@Serializable
193
data class ChangeSummary(
194
    val hash: String,
195
    val message: String = "",
196
    val author: ChangeAuthor = ChangeAuthor(),
197
    @SerialName("when_ms") val whenMs: Long = 0,
198
) {
199
    val subject: String get() = message.lineSequence().firstOrNull().orEmpty()
200
    val short: String get() = hash.take(12)
201
}
202
203
/** A page of a reference's history (`GET …/history/{r}`), newest first. */
204
@Serializable
205
data class History(
206
    val reference: String = "",
207
    val total: Int = 0,
208
    val offset: Int = 0,
209
    val changes: List<ChangeSummary> = emptyList(),
210
)
211
212
/** A file a change touched, and how. */
213
@Serializable
214
data class FileChange(
215
    val path: String,
216
    /** `added`, `modified`, `removed` or `renamed`. */
217
    val kind: String = "modified",
218
    val from: String? = null,
219
    val added: Int = 0,
220
    val removed: Int = 0,
221
)
222
223
/** One change, whole (`GET …/changes/{hash}`). */
224
@Serializable
225
data class ChangeDetail(
226
    val change: ChangeSummary,
227
    val body: String = "",
228
    @SerialName("in_bookmarks") val inBookmarks: List<String> = emptyList(),
229
    @SerialName("in_tags") val inTags: List<String> = emptyList(),
230
    val files: List<FileChange> = emptyList(),
231
)
232
233
/** A bookmark or tag to browse at. */
234
@Serializable
235
data class NamedRef(val name: String)
236
237
/** A reviewer's approval; it counts only when [sealed] (a seal over every change, by a key linked to them). */
238
@Serializable
239
data class Approval(val account: String, val sealed: Boolean = false)
240
241
/** Who landed a proposal, how (`web`, or `api (token NAME)`), and when. */
242
@Serializable
243
data class Landed(val by: String, val via: String = "", @SerialName("when_ms") val whenMs: Long = 0)
244
245
/** Which proposals to list. `Open` includes drafts, as the hub's own list does. */
246
enum class ProposalState(val query: String) { Open("open"), Landed("landed"), Closed("closed"), All("all") }
247
248
/** A check on a state (`GET …/proposals/{n}/checks`): a Flow job, or another reporter's. */
249
@Serializable
250
data class Check(
251
    val name: String,
252
    /** `queued`, `running`, `passed`, `failed`, `cancelled` or `skipped`. */
253
    val status: String = "queued",
254
    val summary: String = "",
255
    val url: String = "",
256
    @SerialName("started_ms") val startedMs: Long = 0,
257
    @SerialName("finished_ms") val finishedMs: Long? = null,
258
)
259
260
/** One entry in the caller's inbox. */
261
@Serializable
262
data class Notification(
263
    val id: Long,
264
    @SerialName("at_ms") val atMs: Long = 0,
265
    /** `issue.opened`, `proposal.comment`, `mention`, `review`, … */
266
    val kind: String = "",
267
    val title: String = "",
268
    /** The page on the hub, absolute. */
269
    val url: String = "",
270
    val actor: String = "",
271
    val read: Boolean = false,
272
)
273
274
/** `GET /api/v1/notifications`: the unread count and the newest entries, unread first. */
275
@Serializable
276
data class Inbox(
277
    val unread: Long = 0,
278
    val items: List<Notification> = emptyList(),
279
)
280
281
/** An RFC 7807 problem: what the API answers with on an error. */
282
@Serializable
283
data class Problem(
284
    val status: Int = 0,
285
    val title: String = "",
286
    val detail: String = "",
287
)
288
289
/** The device flow's errors, RFC 8628's shape: `{"error": …, "error_description": …}`. */
290
@Serializable
291
data class OAuthError(
292
    val error: String = "",
293
    @SerialName("error_description") val description: String = "",
294
)
295
296
/** `POST /api/v1/device/code`'s answer. */
297
@Serializable
298
data class DeviceCode(
299
    @SerialName("device_code") val deviceCode: String,
300
    @SerialName("user_code") val userCode: String,
301
    @SerialName("verification_uri") val verificationUri: String,
302
    @SerialName("verification_uri_complete") val verificationUriComplete: String = "",
303
    @SerialName("expires_in") val expiresIn: Long = 900,
304
    val interval: Long = 5,
305
) {
306
    /** The page to open: with the code filled in when the hub offers it. */
307
    val pageToOpen: String get() = verificationUriComplete.ifBlank { verificationUri }
308
309
    // The device code is a secret until it is used: never in logs.
310
    override fun toString(): String = "DeviceCode(userCode=$userCode, verificationUri=$verificationUri)"
311
}
312
313
/** `POST /api/v1/device/token`'s answer once the user approved. */
314
@Serializable
315
data class TokenGrant(
316
    @SerialName("access_token") val accessToken: String,
317
    @SerialName("token_type") val tokenType: String = "bearer",
318
    /** The scopes the user allowed, space separated (they may untick some). */
319
    val scope: String = "",
320
    val username: String = "",
321
    @SerialName("expires_in") val expiresIn: Long = 0,
322
) {
323
    val scopes: List<String> get() = scope.split(' ').filter { it.isNotBlank() }
324
325
    override fun toString(): String = "TokenGrant(username=$username, scope=$scope, accessToken=<redacted>)"
326
}