OXYGENHUB
# ff65ea456ff1 oxygenhub-android / app / src / main / java / io / oxygenhub / android / api / Models.kt
8.9 KiB · last changed Notifications when a proposal can be landed; the runner comes back after an update or restart (closes #20, 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
    /** 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 reviewer's approval; it counts only when [sealed] (a seal over every change, by a key linked to them). */
165
@Serializable
166
data class Approval(val account: String, val sealed: Boolean = false)
167
168
/** Who landed a proposal, how (`web`, or `api (token NAME)`), and when. */
169
@Serializable
170
data class Landed(val by: String, val via: String = "", @SerialName("when_ms") val whenMs: Long = 0)
171
172
/** Which proposals to list. `Open` includes drafts, as the hub's own list does. */
173
enum class ProposalState(val query: String) { Open("open"), Landed("landed"), Closed("closed"), All("all") }
174
175
/** A check on a state (`GET …/proposals/{n}/checks`): a Flow job, or another reporter's. */
176
@Serializable
177
data class Check(
178
    val name: String,
179
    /** `queued`, `running`, `passed`, `failed`, `cancelled` or `skipped`. */
180
    val status: String = "queued",
181
    val summary: String = "",
182
    val url: String = "",
183
    @SerialName("started_ms") val startedMs: Long = 0,
184
    @SerialName("finished_ms") val finishedMs: Long? = null,
185
)
186
187
/** One entry in the caller's inbox. */
188
@Serializable
189
data class Notification(
190
    val id: Long,
191
    @SerialName("at_ms") val atMs: Long = 0,
192
    /** `issue.opened`, `proposal.comment`, `mention`, `review`, … */
193
    val kind: String = "",
194
    val title: String = "",
195
    /** The page on the hub, absolute. */
196
    val url: String = "",
197
    val actor: String = "",
198
    val read: Boolean = false,
199
)
200
201
/** `GET /api/v1/notifications`: the unread count and the newest entries, unread first. */
202
@Serializable
203
data class Inbox(
204
    val unread: Long = 0,
205
    val items: List<Notification> = emptyList(),
206
)
207
208
/** An RFC 7807 problem: what the API answers with on an error. */
209
@Serializable
210
data class Problem(
211
    val status: Int = 0,
212
    val title: String = "",
213
    val detail: String = "",
214
)
215
216
/** The device flow's errors, RFC 8628's shape: `{"error": …, "error_description": …}`. */
217
@Serializable
218
data class OAuthError(
219
    val error: String = "",
220
    @SerialName("error_description") val description: String = "",
221
)
222
223
/** `POST /api/v1/device/code`'s answer. */
224
@Serializable
225
data class DeviceCode(
226
    @SerialName("device_code") val deviceCode: String,
227
    @SerialName("user_code") val userCode: String,
228
    @SerialName("verification_uri") val verificationUri: String,
229
    @SerialName("verification_uri_complete") val verificationUriComplete: String = "",
230
    @SerialName("expires_in") val expiresIn: Long = 900,
231
    val interval: Long = 5,
232
) {
233
    /** The page to open: with the code filled in when the hub offers it. */
234
    val pageToOpen: String get() = verificationUriComplete.ifBlank { verificationUri }
235
236
    // The device code is a secret until it is used: never in logs.
237
    override fun toString(): String = "DeviceCode(userCode=$userCode, verificationUri=$verificationUri)"
238
}
239
240
/** `POST /api/v1/device/token`'s answer once the user approved. */
241
@Serializable
242
data class TokenGrant(
243
    @SerialName("access_token") val accessToken: String,
244
    @SerialName("token_type") val tokenType: String = "bearer",
245
    /** The scopes the user allowed, space separated (they may untick some). */
246
    val scope: String = "",
247
    val username: String = "",
248
    @SerialName("expires_in") val expiresIn: Long = 0,
249
) {
250
    val scopes: List<String> get() = scope.split(' ').filter { it.isNotBlank() }
251
252
    override fun toString(): String = "TokenGrant(username=$username, scope=$scope, accessToken=<redacted>)"
253
}