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