OXYGENHUB
# 74bbbc259910 oxygenhub-android / app / src / main / java / io / oxygenhub / android / api / Models.kt
8.8 KiB · last changed The phone as an OxygenFlow runner, first part (refs #8) 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
)
125
126
/** A proposal in a list across repositories. */
127
@Serializable
128
data class ProposalBrief(
129
    /** `owner/name`. */
130
    val repository: String,
131
    val number: Long,
132
    val title: String = "",
133
    val author: String = "",
134
    val state: String = "open",
135
    val conflicted: Boolean = false,
136
    @SerialName("updated_ms") val updatedMs: Long = 0,
137
    val url: String = "",
138
) {
139
    val owner: String get() = repository.substringBefore('/')
140
    val name: String get() = repository.substringAfter('/')
141
}
142
143
/** `POST /api/v1/user/runner-token`: a one-time token that registers a runner for [owner]'s repositories. */
144
@Serializable
145
data class RunnerToken(val token: String, val owner: String = "") {
146
    // The token is a secret until it is used.
147
    override fun toString(): String = "RunnerToken(owner=$owner, token=<redacted>)"
148
}
149
150
/** A change a proposal adds. */
151
@Serializable
152
data class ProposalChange(
153
    val hash: String,
154
    val message: String = "",
155
    /** One that nothing else in the proposal depends on. */
156
    val head: Boolean = false,
157
) {
158
    /** The message's first line. */
159
    val subject: String get() = message.lineSequence().firstOrNull().orEmpty()
160
}
161
162
/** A reviewer's approval; it counts only when [sealed] (a seal over every change, by a key linked to them). */
163
@Serializable
164
data class Approval(val account: String, val sealed: Boolean = false)
165
166
/** Who landed a proposal, how (`web`, or `api (token NAME)`), and when. */
167
@Serializable
168
data class Landed(val by: String, val via: String = "", @SerialName("when_ms") val whenMs: Long = 0)
169
170
/** Which proposals to list. `Open` includes drafts, as the hub's own list does. */
171
enum class ProposalState(val query: String) { Open("open"), Landed("landed"), Closed("closed"), All("all") }
172
173
/** A check on a state (`GET …/proposals/{n}/checks`): a Flow job, or another reporter's. */
174
@Serializable
175
data class Check(
176
    val name: String,
177
    /** `queued`, `running`, `passed`, `failed`, `cancelled` or `skipped`. */
178
    val status: String = "queued",
179
    val summary: String = "",
180
    val url: String = "",
181
    @SerialName("started_ms") val startedMs: Long = 0,
182
    @SerialName("finished_ms") val finishedMs: Long? = null,
183
)
184
185
/** One entry in the caller's inbox. */
186
@Serializable
187
data class Notification(
188
    val id: Long,
189
    @SerialName("at_ms") val atMs: Long = 0,
190
    /** `issue.opened`, `proposal.comment`, `mention`, `review`, … */
191
    val kind: String = "",
192
    val title: String = "",
193
    /** The page on the hub, absolute. */
194
    val url: String = "",
195
    val actor: String = "",
196
    val read: Boolean = false,
197
)
198
199
/** `GET /api/v1/notifications`: the unread count and the newest entries, unread first. */
200
@Serializable
201
data class Inbox(
202
    val unread: Long = 0,
203
    val items: List<Notification> = emptyList(),
204
)
205
206
/** An RFC 7807 problem: what the API answers with on an error. */
207
@Serializable
208
data class Problem(
209
    val status: Int = 0,
210
    val title: String = "",
211
    val detail: String = "",
212
)
213
214
/** The device flow's errors, RFC 8628's shape: `{"error": …, "error_description": …}`. */
215
@Serializable
216
data class OAuthError(
217
    val error: String = "",
218
    @SerialName("error_description") val description: String = "",
219
)
220
221
/** `POST /api/v1/device/code`'s answer. */
222
@Serializable
223
data class DeviceCode(
224
    @SerialName("device_code") val deviceCode: String,
225
    @SerialName("user_code") val userCode: String,
226
    @SerialName("verification_uri") val verificationUri: String,
227
    @SerialName("verification_uri_complete") val verificationUriComplete: String = "",
228
    @SerialName("expires_in") val expiresIn: Long = 900,
229
    val interval: Long = 5,
230
) {
231
    /** The page to open: with the code filled in when the hub offers it. */
232
    val pageToOpen: String get() = verificationUriComplete.ifBlank { verificationUri }
233
234
    // The device code is a secret until it is used: never in logs.
235
    override fun toString(): String = "DeviceCode(userCode=$userCode, verificationUri=$verificationUri)"
236
}
237
238
/** `POST /api/v1/device/token`'s answer once the user approved. */
239
@Serializable
240
data class TokenGrant(
241
    @SerialName("access_token") val accessToken: String,
242
    @SerialName("token_type") val tokenType: String = "bearer",
243
    /** The scopes the user allowed, space separated (they may untick some). */
244
    val scope: String = "",
245
    val username: String = "",
246
    @SerialName("expires_in") val expiresIn: Long = 0,
247
) {
248
    val scopes: List<String> get() = scope.split(' ').filter { it.isNotBlank() }
249
250
    override fun toString(): String = "TokenGrant(username=$username, scope=$scope, accessToken=<redacted>)"
251
}