| 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 |
/** An organisation (`GET /orgs/{name}`). */ |
| 224 |
@Serializable |
| 225 |
data class Organisation( |
| 226 |
val name: String, |
| 227 |
@SerialName("display_name") val displayName: String = "", |
| 228 |
val description: String = "", |
| 229 |
val members: List<String> = emptyList(), |
| 230 |
) |
| 231 |
|
| 232 |
/** An account or an organisation, as its page shows it. */ |
| 233 |
sealed interface Owner { |
| 234 |
val name: String |
| 235 |
val shownName: String |
| 236 |
|
| 237 |
data class Person(val user: User) : Owner { |
| 238 |
override val name get() = user.username |
| 239 |
override val shownName get() = user.shownName |
| 240 |
} |
| 241 |
|
| 242 |
data class Org(val org: Organisation) : Owner { |
| 243 |
override val name get() = org.name |
| 244 |
override val shownName get() = org.displayName.ifBlank { org.name } |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
/** A proposal in a list across repositories. */ |
| 249 |
@Serializable |
| 250 |
data class ProposalBrief( |
| 251 |
/** `owner/name`. */ |
| 252 |
val repository: String, |
| 253 |
val number: Long, |
| 254 |
val title: String = "", |
| 255 |
val author: String = "", |
| 256 |
val state: String = "open", |
| 257 |
val conflicted: Boolean = false, |
| 258 |
@SerialName("updated_ms") val updatedMs: Long = 0, |
| 259 |
val url: String = "", |
| 260 |
) { |
| 261 |
val owner: String get() = repository.substringBefore('/') |
| 262 |
val name: String get() = repository.substringAfter('/') |
| 263 |
} |
| 264 |
|
| 265 |
/** `POST /api/v1/user/runner-token`: a one-time token that registers a runner for [owner]'s repositories. */ |
| 266 |
@Serializable |
| 267 |
data class RunnerToken(val token: String, val owner: String = "") { |
| 268 |
// The token is a secret until it is used. |
| 269 |
override fun toString(): String = "RunnerToken(owner=$owner, token=<redacted>)" |
| 270 |
} |
| 271 |
|
| 272 |
/** A change a proposal adds. */ |
| 273 |
@Serializable |
| 274 |
data class ProposalChange( |
| 275 |
val hash: String, |
| 276 |
val message: String = "", |
| 277 |
/** One that nothing else in the proposal depends on. */ |
| 278 |
val head: Boolean = false, |
| 279 |
) { |
| 280 |
/** The message's first line. */ |
| 281 |
val subject: String get() = message.lineSequence().firstOrNull().orEmpty() |
| 282 |
} |
| 283 |
|
| 284 |
/** A file or directory of a repository's tree at some reference (the code browser, #3). */ |
| 285 |
@Serializable |
| 286 |
data class TreeEntry( |
| 287 |
val name: String, |
| 288 |
val path: String, |
| 289 |
/** `file`, `dir`, `symlink` or `subrepo`. */ |
| 290 |
val kind: String = "file", |
| 291 |
val exec: Boolean = false, |
| 292 |
val size: Long? = null, |
| 293 |
val conflicted: Boolean = false, |
| 294 |
) { |
| 295 |
val isDir: Boolean get() = kind == "dir" |
| 296 |
} |
| 297 |
|
| 298 |
/** Who made a change, as the hub knows them. */ |
| 299 |
@Serializable |
| 300 |
data class ChangeAuthor(val name: String = "", val email: String = "", val account: String? = null) |
| 301 |
|
| 302 |
/** A change in a history or a proposal. */ |
| 303 |
@Serializable |
| 304 |
data class ChangeSummary( |
| 305 |
val hash: String, |
| 306 |
val message: String = "", |
| 307 |
val author: ChangeAuthor = ChangeAuthor(), |
| 308 |
@SerialName("when_ms") val whenMs: Long = 0, |
| 309 |
) { |
| 310 |
val subject: String get() = message.lineSequence().firstOrNull().orEmpty() |
| 311 |
val short: String get() = hash.take(12) |
| 312 |
} |
| 313 |
|
| 314 |
/** A page of a reference's history (`GET …/history/{r}`), newest first. */ |
| 315 |
@Serializable |
| 316 |
data class History( |
| 317 |
val reference: String = "", |
| 318 |
val total: Int = 0, |
| 319 |
val offset: Int = 0, |
| 320 |
val changes: List<ChangeSummary> = emptyList(), |
| 321 |
) |
| 322 |
|
| 323 |
/** A file a change touched, and how. */ |
| 324 |
@Serializable |
| 325 |
data class FileChange( |
| 326 |
val path: String, |
| 327 |
/** `added`, `modified`, `removed` or `renamed`. */ |
| 328 |
val kind: String = "modified", |
| 329 |
val from: String? = null, |
| 330 |
val added: Int = 0, |
| 331 |
val removed: Int = 0, |
| 332 |
/** The diff with three lines of context (hub #320), cut at 2000 lines. */ |
| 333 |
val lines: List<DiffLine> = emptyList(), |
| 334 |
val truncated: Boolean = false, |
| 335 |
) |
| 336 |
|
| 337 |
/** A line of a file's diff: `context`, `add`, `del`, or `gap` for lines left out between hunks. */ |
| 338 |
@Serializable |
| 339 |
data class DiffLine( |
| 340 |
val kind: String = "context", |
| 341 |
val old: Int? = null, |
| 342 |
val new: Int? = null, |
| 343 |
val text: String = "", |
| 344 |
) |
| 345 |
|
| 346 |
/** One change, whole (`GET …/changes/{hash}`). */ |
| 347 |
@Serializable |
| 348 |
data class ChangeDetail( |
| 349 |
val change: ChangeSummary, |
| 350 |
val body: String = "", |
| 351 |
@SerialName("in_bookmarks") val inBookmarks: List<String> = emptyList(), |
| 352 |
@SerialName("in_tags") val inTags: List<String> = emptyList(), |
| 353 |
val files: List<FileChange> = emptyList(), |
| 354 |
) |
| 355 |
|
| 356 |
/** A bookmark or tag to browse at. */ |
| 357 |
@Serializable |
| 358 |
data class NamedRef(val name: String) |
| 359 |
|
| 360 |
/** A reviewer's approval; it counts only when [sealed] (a seal over every change, by a key linked to them). */ |
| 361 |
@Serializable |
| 362 |
data class Approval(val account: String, val sealed: Boolean = false) |
| 363 |
|
| 364 |
/** Who landed a proposal, how (`web`, or `api (token NAME)`), and when. */ |
| 365 |
@Serializable |
| 366 |
data class Landed(val by: String, val via: String = "", @SerialName("when_ms") val whenMs: Long = 0) |
| 367 |
|
| 368 |
/** Which proposals to list. `Open` includes drafts, as the hub's own list does. */ |
| 369 |
enum class ProposalState(val query: String) { Open("open"), Landed("landed"), Closed("closed"), All("all") } |
| 370 |
|
| 371 |
/** A check on a state (`GET …/proposals/{n}/checks`): a Flow job, or another reporter's. */ |
| 372 |
@Serializable |
| 373 |
data class Check( |
| 374 |
val name: String, |
| 375 |
/** `queued`, `running`, `passed`, `failed`, `cancelled` or `skipped`. */ |
| 376 |
val status: String = "queued", |
| 377 |
val summary: String = "", |
| 378 |
val url: String = "", |
| 379 |
@SerialName("started_ms") val startedMs: Long = 0, |
| 380 |
@SerialName("finished_ms") val finishedMs: Long? = null, |
| 381 |
) |
| 382 |
|
| 383 |
/** One entry in the caller's inbox. */ |
| 384 |
@Serializable |
| 385 |
data class Notification( |
| 386 |
val id: Long, |
| 387 |
@SerialName("at_ms") val atMs: Long = 0, |
| 388 |
/** `issue.opened`, `proposal.comment`, `mention`, `review`, … */ |
| 389 |
val kind: String = "", |
| 390 |
val title: String = "", |
| 391 |
/** The page on the hub, absolute. */ |
| 392 |
val url: String = "", |
| 393 |
val actor: String = "", |
| 394 |
val read: Boolean = false, |
| 395 |
) |
| 396 |
|
| 397 |
/** `GET /api/v1/notifications`: the unread count and the newest entries, unread first. */ |
| 398 |
@Serializable |
| 399 |
data class Inbox( |
| 400 |
val unread: Long = 0, |
| 401 |
val items: List<Notification> = emptyList(), |
| 402 |
) |
| 403 |
|
| 404 |
/** An RFC 7807 problem: what the API answers with on an error. */ |
| 405 |
@Serializable |
| 406 |
data class Problem( |
| 407 |
val status: Int = 0, |
| 408 |
val title: String = "", |
| 409 |
val detail: String = "", |
| 410 |
) |
| 411 |
|
| 412 |
/** The device flow's errors, RFC 8628's shape: `{"error": …, "error_description": …}`. */ |
| 413 |
@Serializable |
| 414 |
data class OAuthError( |
| 415 |
val error: String = "", |
| 416 |
@SerialName("error_description") val description: String = "", |
| 417 |
) |
| 418 |
|
| 419 |
/** `POST /api/v1/device/code`'s answer. */ |
| 420 |
@Serializable |
| 421 |
data class DeviceCode( |
| 422 |
@SerialName("device_code") val deviceCode: String, |
| 423 |
@SerialName("user_code") val userCode: String, |
| 424 |
@SerialName("verification_uri") val verificationUri: String, |
| 425 |
@SerialName("verification_uri_complete") val verificationUriComplete: String = "", |
| 426 |
@SerialName("expires_in") val expiresIn: Long = 900, |
| 427 |
val interval: Long = 5, |
| 428 |
) { |
| 429 |
/** The page to open: with the code filled in when the hub offers it. */ |
| 430 |
val pageToOpen: String get() = verificationUriComplete.ifBlank { verificationUri } |
| 431 |
|
| 432 |
// The device code is a secret until it is used: never in logs. |
| 433 |
override fun toString(): String = "DeviceCode(userCode=$userCode, verificationUri=$verificationUri)" |
| 434 |
} |
| 435 |
|
| 436 |
/** `POST /api/v1/device/token`'s answer once the user approved. */ |
| 437 |
@Serializable |
| 438 |
data class TokenGrant( |
| 439 |
@SerialName("access_token") val accessToken: String, |
| 440 |
@SerialName("token_type") val tokenType: String = "bearer", |
| 441 |
/** The scopes the user allowed, space separated (they may untick some). */ |
| 442 |
val scope: String = "", |
| 443 |
val username: String = "", |
| 444 |
@SerialName("expires_in") val expiresIn: Long = 0, |
| 445 |
) { |
| 446 |
val scopes: List<String> get() = scope.split(' ').filter { it.isNotBlank() } |
| 447 |
|
| 448 |
override fun toString(): String = "TokenGrant(username=$username, scope=$scope, accessToken=<redacted>)" |
| 449 |
} |