aboutsummaryrefslogtreecommitdiff
path: root/Hutch/Models/Patchset.swift
blob: 330bbbc9a44c809ca6eddf1d73e84edd31533af6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import Foundation

/// Review state of a patchset on lists.sr.ht.
enum PatchsetStatus: String, Codable, Sendable, CaseIterable {
    case unknown = "UNKNOWN"
    case proposed = "PROPOSED"
    case needsRevision = "NEEDS_REVISION"
    case superseded = "SUPERSEDED"
    case approved = "APPROVED"
    case rejected = "REJECTED"
    case applied = "APPLIED"

    var displayName: String {
        switch self {
        case .unknown: "Unknown"
        case .proposed: "Proposed"
        case .needsRevision: "Needs Revision"
        case .superseded: "Superseded"
        case .approved: "Approved"
        case .rejected: "Rejected"
        case .applied: "Applied"
        }
    }

    var systemImage: String {
        switch self {
        case .unknown: "questionmark.circle"
        case .proposed: "paperplane"
        case .needsRevision: "exclamationmark.arrow.circlepath"
        case .superseded: "arrow.triangle.branch"
        case .approved: "checkmark.seal"
        case .rejected: "xmark.circle"
        case .applied: "checkmark.circle.fill"
        }
    }

    /// Whether the patchset is still awaiting a decision.
    var isOpen: Bool {
        switch self {
        case .unknown, .proposed, .needsRevision: true
        case .superseded, .approved, .rejected, .applied: false
        }
    }

    /// Statuses a reviewer can set directly.
    ///
    /// `unknown` is a sentinel for patchsets sr.ht could not classify, and
    /// `superseded` is set by the server when a later version arrives, so neither
    /// is offered as a choice.
    static var assignable: [PatchsetStatus] {
        [.proposed, .needsRevision, .approved, .rejected, .applied]
    }
}

/// A patchset as it appears in a mailing list listing, derived from the thread's
/// root email rather than a dedicated patchsets query — `MailingList` exposes no
/// such field.
struct PatchsetSummary: Identifiable, Hashable, Sendable {
    let id: Int
    let subject: String
    let version: Int
    let prefix: String?
    let status: PatchsetStatus

    /// The `[PATCH v2]`-style prefix sr.ht parsed from the subject, if any.
    var versionLabel: String? {
        guard version > 1 else { return nil }
        return "v\(version)"
    }
}

/// One email within a patchset: either the cover letter or a single patch.
struct PatchsetEmail: Identifiable, Hashable, Sendable {
    let id: Int
    let subject: String
    let date: Date?
    let sender: Entity
    /// Split into commit message and diff blocks for rendering.
    let contentBlocks: [InboxMessageContentBlock]
    /// Position within the series, from the `[PATCH 2/5]` prefix.
    let index: Int?
    let count: Int?

    var seriesLabel: String? {
        guard let index, let count, count > 1 else { return nil }
        return "\(index)/\(count)"
    }
}

/// A build or check reported against a patchset.
struct PatchsetToolResult: Identifiable, Hashable, Sendable {
    let id: Int
    let icon: PatchsetToolIcon
    let details: String
}

enum PatchsetToolIcon: String, Codable, Sendable {
    case pending = "PENDING"
    case waiting = "WAITING"
    case success = "SUCCESS"
    case failed = "FAILED"
    case cancelled = "CANCELLED"

    var systemImage: String {
        switch self {
        case .pending, .waiting: "clock"
        case .success: "checkmark.circle.fill"
        case .failed: "xmark.circle.fill"
        case .cancelled: "minus.circle"
        }
    }
}

/// A patchset with its cover letter, patches, and review context.
struct PatchsetDetail: Sendable {
    let id: Int
    let created: Date
    let updated: Date
    let subject: String
    let version: Int
    let prefix: String?
    let status: PatchsetStatus
    let submitter: Entity
    let coverLetter: PatchsetEmail?
    let patches: [PatchsetEmail]
    /// Set when a newer version of this series exists.
    let supersededBy: Int?
    /// Set when this series revises an earlier one.
    let supersedes: Int?
    let tools: [PatchsetToolResult]
    let mbox: URL?
}