summary refs log tree commit diff stats
path: root/pkgs/by-name/ba/back/src/git_bug/issue/mod.rs
blob: f27bfec4b96966cd06d10ccc0afc1f2cf483d870 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Back - An extremely simple git issue tracking system. Inspired by tvix's
// panettone
//
// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This file is part of Back.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/agpl.txt>.

use std::fmt::Display;

use entity::{Entity, Id};
use identity::Author;
use label::Label;
use operation::Operation;
use serde_json::Value;

use super::format::{MarkDown, TimeStamp};

pub mod entity;
pub mod identity;
pub mod label;
pub mod operation;

#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum Status {
    Open,
    Closed,
}
impl From<&Value> for Status {
    fn from(value: &Value) -> Self {
        match value.as_u64().expect("This should be a integer") {
            1 => Self::Open,
            2 => Self::Closed,
            other => unimplemented!("Invalid status string: '{other}'"),
        }
    }
}
impl Display for Status {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Status::Open => f.write_str("Open"),
            Status::Closed => f.write_str("Closed"),
        }
    }
}

#[derive(Debug)]
pub struct CollapsedIssue {
    pub id: Id,
    pub author: Author,
    pub timestamp: TimeStamp,
    pub title: MarkDown,
    pub message: MarkDown,
    pub comments: Vec<Comment>,
    pub status: Status,
    pub last_status_change: TimeStamp,
    pub labels: Vec<Label>,
}
impl From<RawCollapsedIssue> for CollapsedIssue {
    fn from(r: RawCollapsedIssue) -> Self {
        macro_rules! get {
            ($name:ident) => {
                r.$name.expect(concat!(
                    "'",
                    stringify!($name),
                    "' is unset, when trying to collapes an issue! (This is likely a bug)"
                ))
            };
        }

        Self {
            id: get! {id},
            author: get! {author},
            timestamp: get! {timestamp},
            title: get! {title},
            message: get! {message},
            comments: r.comments,
            status: get! {status},
            last_status_change: get! {last_status_change},
            labels: r.labels,
        }
    }
}

#[derive(Debug)]
pub struct Comment {
    pub id: Id,
    pub author: Author,
    pub timestamp: TimeStamp,
    pub message: MarkDown,
}

#[derive(Debug, Default)]
pub struct RawCollapsedIssue {
    pub id: Option<Id>,
    pub author: Option<Author>,
    pub timestamp: Option<TimeStamp>,
    pub title: Option<MarkDown>,
    pub message: Option<MarkDown>,
    pub status: Option<Status>,
    pub last_status_change: Option<TimeStamp>,

    // NOTE(@bpeetz): These values set here already, because an issue without these
    // would be perfectly valid. <2024-12-26>
    pub labels: Vec<Label>,
    pub comments: Vec<Comment>,
}

impl RawCollapsedIssue {
    pub fn append_entity(&mut self, entity: Entity) {
        for op in entity.operations {
            match op {
                Operation::AddComment { timestamp, message } => {
                    self.comments.push(Comment {
                        id: entity.id.clone(),
                        author: entity.author.clone(),
                        timestamp,
                        message,
                    });
                }
                Operation::Create {
                    timestamp,
                    title,
                    message,
                } => {
                    self.id = Some(entity.id.clone());
                    self.author = Some(entity.author.clone());
                    self.timestamp = Some(timestamp.clone());
                    self.title = Some(title);
                    self.message = Some(message);
                    self.status = Some(Status::Open); // This is the default in git_bug
                    self.last_status_change = Some(timestamp);
                }
                Operation::EditComment {
                    timestamp,
                    target,
                    message,
                } => {
                    let comments = &mut self.comments;

                    let target_comment = comments
                        .iter_mut()
                        .find(|comment| comment.id == target)
                        .expect("The target must be a valid comment");

                    // TODO(@bpeetz): We should probably set a `edited = true` flag here. <2024-12-26>
                    // TODO(@bpeetz): Should we also change the author? <2024-12-26>

                    target_comment.timestamp = timestamp;
                    target_comment.message = message;
                }
                Operation::LabelChange {
                    timestamp: _,
                    added,
                    removed,
                } => {
                    let labels = self.labels.clone();

                    self.labels = labels
                        .into_iter()
                        .filter(|val| !removed.contains(val))
                        .chain(added.into_iter())
                        .collect();
                }
                Operation::SetStatus { timestamp, status } => {
                    self.status = Some(status);
                    self.last_status_change = Some(timestamp);
                }
                Operation::SetTitle {
                    timestamp: _,
                    title,
                    was: _,
                } => {
                    self.title = Some(title);
                }

                Operation::NoOp {} => unimplemented!(),
                Operation::SetMetadata {} => unimplemented!(),
            }
        }
    }
}