bugbite 0.0.15

library for bug, issue, and ticket mangling
Documentation
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::str::FromStr;

use byte_unit::Byte;
use chrono::prelude::*;
use indexmap::{IndexMap, IndexSet};
use itertools::{Either, Itertools};
use serde::{Deserialize, Deserializer, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use serde_with::{
    BoolFromInt, DefaultOnError, DefaultOnNull, DeserializeFromStr, SerializeDisplay, serde_as,
    skip_serializing_none,
};
use strum::{Display, EnumString};

use crate::Error;
use crate::serde::{byte_object, non_empty_str};
use crate::service::bugzilla::{BugField, FilterField, GroupField, UNSET_VALUES};
use crate::traits::RenderSearch;

use super::{Base64, stringify};

/// A file attachment on a bug.
#[serde_as]
#[derive(Deserialize, Debug, PartialEq, Eq, Hash)]
pub struct Attachment {
    /// Unique attachment identifier.
    pub id: u64,

    /// Bug identifier the attachment is related to.
    pub bug_id: u64,

    /// File name of the attachment.
    pub file_name: String,

    /// Description of the attachment.
    pub summary: String,

    /// Size of the attachment in bytes.
    #[serde(deserialize_with = "byte_object")]
    pub size: Byte,

    /// Login identifier of the attachment's creator.
    pub creator: String,

    /// MIME type of the attachment.
    pub content_type: String,

    /// Attachment is private.
    #[serde_as(as = "BoolFromInt")]
    pub is_private: bool,

    /// Attachment is obsolete.
    #[serde_as(as = "BoolFromInt")]
    pub is_obsolete: bool,

    /// Attachment is a patch.
    #[serde_as(as = "BoolFromInt")]
    pub is_patch: bool,

    /// Creation time of the attachment.
    #[serde(rename = "creation_time")]
    pub created: DateTime<Utc>,

    /// Last update time of the attachment.
    #[serde(rename = "last_change_time")]
    pub updated: DateTime<Utc>,

    /// Flags of the attachment.
    #[serde_as(deserialize_as = "DefaultOnNull")]
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub flags: Vec<BugFlag>,

    /// Attachment data.
    #[serde(default)]
    data: Base64,
}

impl AsRef<[u8]> for Attachment {
    fn as_ref(&self) -> &[u8] {
        self.data.as_ref()
    }
}

// TODO: support auto-decompressing standard archive formats
impl Attachment {
    /// Return true if the attachment has no data, otherwise false.
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Return true if the attachment has been deleted, otherwise false.
    pub fn is_deleted(&self) -> bool {
        self.size == 0_u64
    }
}

#[derive(Debug, PartialEq, Eq)]
pub enum BugUpdate<'a> {
    Comment(&'a Comment),
    Event(&'a Event),
}

impl BugUpdate<'_> {
    fn date(&self) -> &DateTime<Utc> {
        match self {
            Self::Comment(comment) => &comment.created,
            Self::Event(event) => &event.when,
        }
    }
}

impl Ord for BugUpdate<'_> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.date().cmp(other.date())
    }
}

impl PartialOrd for BugUpdate<'_> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
pub struct Comment {
    /// Globally unique ID for the comment.
    pub id: u64,

    /// Bug ID the comment is on.
    pub bug_id: u64,

    /// Attachment ID related to the comment.
    pub attachment_id: Option<u64>,

    /// The number of the comment local to the bug.
    ///
    /// The description is 0, comments start at 1.
    pub count: usize,

    pub text: String,
    pub creator: String,
    #[serde(rename = "creation_time")]
    pub created: DateTime<Utc>,
    pub is_private: bool,
    #[serde(default)]
    pub tags: IndexSet<String>,
}

impl Comment {
    /// Format a comment into a reply string.
    pub fn reply(&self) -> String {
        // TODO: pull real name for creator?
        let creator = self
            .creator
            .split_once('@')
            .map(|x| x.0)
            .unwrap_or(&self.creator);
        let mut data = vec![format!(
            "(In reply to {} from comment #{})",
            creator, self.count
        )];
        for line in self.text.lines() {
            data.push(format!("> {line}"));
        }
        data.iter().join("\n")
    }
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Hash, Clone)]
pub struct Event {
    pub who: String,
    pub when: DateTime<Utc>,
    pub changes: Vec<Change>,
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Hash, Clone)]
pub struct Change {
    pub field_name: String,
    #[serde(deserialize_with = "non_empty_str")]
    pub removed: Option<String>,
    #[serde(deserialize_with = "non_empty_str")]
    pub added: Option<String>,
    pub attachment_id: Option<u64>,
}

#[derive(
    Display,
    EnumString,
    DeserializeFromStr,
    SerializeDisplay,
    Debug,
    PartialEq,
    Eq,
    Hash,
    Clone,
    Copy,
)]
pub enum FlagStatus {
    #[strum(serialize = "+")]
    Granted,
    #[strum(serialize = "-")]
    Denied,
    #[strum(serialize = "?")]
    Requested,
    #[strum(serialize = "X")]
    Remove,
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Hash, Clone)]
pub struct Flag {
    pub name: String,
    pub status: FlagStatus,
}

impl FromStr for Flag {
    type Err = Error;

    fn from_str(s: &str) -> crate::Result<Self> {
        if s.is_empty() {
            return Err(Error::InvalidValue("empty flag".to_string()));
        }

        let name = &s[..s.len() - 1];
        let status = &s[s.len() - 1..];
        let status = status
            .parse()
            .map_err(|_| Error::InvalidValue(format!("invalid flag status: {status}")))?;

        Ok(Self {
            name: name.to_string(),
            status,
        })
    }
}

impl fmt::Display for Flag {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}{}", self.name, self.status)
    }
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Hash)]
pub struct BugFlag {
    #[serde(flatten)]
    pub flag: Flag,
    pub setter: String,
    #[serde(rename = "creation_date")]
    pub created: DateTime<Utc>,
    #[serde(rename = "modification_date")]
    pub updated: DateTime<Utc>,
}

impl fmt::Display for BugFlag {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.flag)
    }
}

// Support deserializing alias field from string or array, the current Bugzilla 5.0
// webservice API returns alias arrays while Mozilla upstream uses string values which
// is what Bugzilla is moving to in the future (see
// https://bugzilla.mozilla.org/show_bug.cgi?id=1534305).
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq)]
#[serde(untagged)]
enum Alias {
    List(IndexSet<String>),
    String(String),
}

/// Deserialize a string field value setting common unset values to None.
pub(crate) fn unset_value_str<'de, D: Deserializer<'de>>(d: D) -> Result<Option<String>, D::Error> {
    non_empty_str(d).map(|o| o.filter(|s| !UNSET_VALUES.contains(s)))
}

/// Deserialize an alias as an ordered set of strings.
pub(crate) fn alias_to_set<'de, D: Deserializer<'de>>(d: D) -> Result<IndexSet<String>, D::Error> {
    Option::<Alias>::deserialize(d).map(|o| match o {
        Some(Alias::List(values)) => values,
        Some(Alias::String(value)) => [value].into_iter().collect(),
        None => Default::default(),
    })
}

#[serde_as]
#[skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, Default, PartialEq, Eq)]
#[serde(default)]
pub struct Bug {
    pub id: u64,
    #[serde(deserialize_with = "alias_to_set")]
    #[serde(skip_serializing_if = "IndexSet::is_empty")]
    pub alias: IndexSet<String>,
    #[serde(deserialize_with = "non_empty_str")]
    pub assigned_to: Option<String>,
    #[serde(deserialize_with = "non_empty_str")]
    pub qa_contact: Option<String>,
    #[serde(deserialize_with = "non_empty_str")]
    pub creator: Option<String>,
    #[serde(rename = "creation_time")]
    pub created: Option<DateTime<Utc>>,
    #[serde(rename = "last_change_time")]
    pub updated: Option<DateTime<Utc>>,
    pub deadline: Option<NaiveDate>,
    #[serde(deserialize_with = "non_empty_str")]
    pub summary: Option<String>,
    #[serde(deserialize_with = "non_empty_str")]
    pub status: Option<String>,
    #[serde(deserialize_with = "non_empty_str")]
    pub resolution: Option<String>,
    #[serde(deserialize_with = "non_empty_str")]
    pub whiteboard: Option<String>,
    #[serde(deserialize_with = "non_empty_str")]
    pub product: Option<String>,
    #[serde(deserialize_with = "non_empty_str")]
    pub component: Option<String>,
    #[serde(deserialize_with = "unset_value_str")]
    pub version: Option<String>,
    #[serde(deserialize_with = "unset_value_str")]
    pub platform: Option<String>,
    #[serde(deserialize_with = "unset_value_str")]
    pub op_sys: Option<String>,
    #[serde(rename = "target_milestone", deserialize_with = "unset_value_str")]
    pub target: Option<String>,
    #[serde(deserialize_with = "unset_value_str")]
    pub priority: Option<String>,
    #[serde(deserialize_with = "unset_value_str")]
    pub severity: Option<String>,
    #[serde_as(deserialize_as = "DefaultOnNull")]
    #[serde(skip_serializing_if = "IndexSet::is_empty")]
    pub groups: IndexSet<String>,
    #[serde_as(deserialize_as = "DefaultOnNull")]
    #[serde(skip_serializing_if = "IndexSet::is_empty")]
    pub keywords: IndexSet<String>,
    #[serde_as(deserialize_as = "DefaultOnNull")]
    #[serde(skip_serializing_if = "IndexSet::is_empty")]
    pub cc: IndexSet<String>,
    #[serde_as(deserialize_as = "DefaultOnNull")]
    #[serde(skip_serializing_if = "IndexSet::is_empty")]
    pub blocks: IndexSet<u64>,
    #[serde_as(deserialize_as = "DefaultOnNull")]
    #[serde(skip_serializing_if = "IndexSet::is_empty")]
    pub depends_on: IndexSet<u64>,
    #[serde(rename = "dupe_of")]
    pub duplicate_of: Option<u64>,
    #[serde_as(deserialize_as = "DefaultOnNull")]
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub flags: Vec<BugFlag>,
    #[serde_as(deserialize_as = "DefaultOnNull")]
    #[serde(skip_serializing_if = "IndexSet::is_empty")]
    pub tags: IndexSet<String>,
    #[serde_as(deserialize_as = "DefaultOnNull")]
    #[serde(skip_serializing_if = "IndexSet::is_empty")]
    pub see_also: IndexSet<String>,
    #[serde(deserialize_with = "non_empty_str")]
    pub url: Option<String>,
    #[serde(skip)]
    pub comments: Vec<Comment>,
    #[serde(skip)]
    pub attachments: Vec<Attachment>,
    #[serde(skip)]
    pub history: Vec<Event>,
    #[serde(skip)]
    pub custom_fields: IndexMap<BugzillaFieldName, String>,
}

impl Bug {
    pub fn updates(&self) -> impl Iterator<Item = BugUpdate<'_>> {
        let comments = self.comments.iter().map(BugUpdate::Comment);
        let history = self.history.iter().map(BugUpdate::Event);
        comments.chain(history).sorted()
    }
}

impl RenderSearch<BugField> for Bug {
    fn render(&self, fields: &[BugField]) -> String {
        let field_to_string = |field: &BugField| -> String {
            match field {
                BugField::Alias => format!("{:<20}", self.alias.iter().join(",")),
                BugField::Assignee => format!("{:<20}", stringify!(self.assigned_to)),
                BugField::Blocks => format!("{:<20}", self.blocks.iter().join(",")),
                BugField::Cc => format!("{:<20}", self.cc.iter().join(",")),
                BugField::Component => format!("{:<20}", stringify!(self.component)),
                BugField::Created => stringify!(self.created),
                BugField::Creator => format!("{:<20}", stringify!(self.creator)),
                BugField::Deadline => stringify!(self.deadline),
                BugField::Depends => format!("{:<20}", self.depends_on.iter().join(",")),
                BugField::DuplicateOf => format!("{:<9}", stringify!(self.duplicate_of)),
                BugField::Flags => format!("{:<20}", self.flags.iter().join(",")),
                BugField::Id => format!("{:<9}", self.id),
                BugField::Keywords => format!("{:<20}", self.keywords.iter().join(",")),
                BugField::Os => format!("{:<20}", stringify!(self.op_sys)),
                BugField::Platform => format!("{:<20}", stringify!(self.platform)),
                BugField::Priority => format!("{:<12}", stringify!(self.priority)),
                BugField::Product => format!("{:<20}", stringify!(self.product)),
                BugField::Qa => format!("{:<20}", stringify!(self.qa_contact)),
                BugField::Resolution => format!("{:<20}", stringify!(self.resolution)),
                BugField::SeeAlso => format!("{:<20}", self.see_also.iter().join(",")),
                BugField::Severity => format!("{:<12}", stringify!(self.severity)),
                BugField::Status => format!("{:<20}", stringify!(self.status)),
                BugField::Summary => stringify!(self.summary),
                BugField::Tags => format!("{:<20}", self.tags.iter().join(",")),
                BugField::Target => format!("{:<20}", stringify!(self.target)),
                BugField::Updated => stringify!(self.updated),
                BugField::Url => format!("{:<20}", stringify!(self.url)),
                BugField::Version => format!("{:<20}", stringify!(self.version)),
                BugField::Whiteboard => format!("{:<20}", stringify!(self.whiteboard)),
            }
        };

        match fields {
            [] => panic!("no fields defined"),
            [field] => field_to_string(field).trim().to_string(),
            fields => fields.iter().map(field_to_string).join(" "),
        }
    }
}

impl RenderSearch<FilterField> for Bug {
    fn render(&self, fields: &[FilterField]) -> String {
        let (bug_fields, group_fields): (Vec<_>, Vec<_>) =
            fields.iter().partition_map(|x| match x {
                FilterField::Bug(x) => Either::Left(*x),
                FilterField::Group(x) => Either::Right(*x),
            });
        match (&bug_fields[..], &group_fields[..]) {
            (fields @ [_, ..], _) => self.render(fields),
            ([], x) if x.contains(&GroupField::All) || x.contains(&GroupField::Default) => {
                self.render(&[BugField::Id, BugField::Summary])
            }
            _ => self.render(&[BugField::Id]),
        }
    }
}

/// Bugzilla field variants.
#[derive(Serialize_repr, Deserialize_repr, Default, Debug)]
#[repr(u8)]
pub enum BugzillaFieldKind {
    /// Unknown field type
    #[default]
    Unknown,
    /// Single-line string
    String,
    /// Single value
    Value,
    /// Multiple value
    MultiValue,
    /// Multi-line text
    Text,
    DateTime,
    BugId,
    SeeAlso,
    Keywords,
    Date,
    Integer,
}

/// Bugzilla field name.
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct BugzillaFieldName {
    /// Unique ID used internally by bugzilla.
    #[serde(rename = "name")]
    pub(crate) id: String,

    /// Name shown in the user interface.
    #[serde(rename = "display_name")]
    display: String,
}

impl PartialEq for BugzillaFieldName {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for BugzillaFieldName {}

impl Hash for BugzillaFieldName {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}

impl Ord for BugzillaFieldName {
    fn cmp(&self, other: &Self) -> Ordering {
        self.id.cmp(&other.id)
    }
}

impl PartialOrd for BugzillaFieldName {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl fmt::Display for BugzillaFieldName {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.display)
    }
}

/// Bugzilla field metadata.
#[serde_as]
#[derive(Deserialize, Serialize, Debug)]
pub struct BugzillaField {
    id: u64,
    #[serde_as(deserialize_as = "DefaultOnError")]
    #[serde(default, rename = "type")]
    pub kind: BugzillaFieldKind,
    is_custom: bool,
    #[serde(flatten)]
    pub name: BugzillaFieldName,
    is_mandatory: bool,
    #[serde(default)]
    values: Vec<BugzillaFieldValue>,
}

impl PartialEq for BugzillaField {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
    }
}

impl Eq for BugzillaField {}

impl Hash for BugzillaField {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.name.hash(state);
    }
}

impl fmt::Display for BugzillaField {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "Name: {}", self.name.id)?;
        write!(f, "Display name: {}", self.name.display)?;
        if !self.values.is_empty() {
            let values = self
                .values
                .iter()
                .filter(|x| !x.name.is_empty())
                .map(|x| x.name.as_str())
                .collect::<IndexSet<_>>();
            let values = values.iter().join(", ");
            write!(f, "\nValues: {values}")?;
        }
        Ok(())
    }
}

/// Legal values for relevant field types.
#[serde_as]
#[derive(Deserialize, Serialize, Debug)]
pub struct BugzillaFieldValue {
    #[serde_as(deserialize_as = "DefaultOnNull")]
    name: String,
    sort_key: Option<i64>,
    description: Option<String>,
    is_open: Option<bool>,
}

/// Bugzilla user.
#[serde_as]
#[derive(Deserialize, Debug, PartialEq, Eq, Hash)]
pub struct User {
    /// Unique user identifier.
    pub id: u64,

    /// Actual name of the user.
    #[serde(deserialize_with = "non_empty_str")]
    pub real_name: Option<String>,

    /// Login name of the user.
    pub name: String,

    /// Email address of the user.
    pub email: Option<String>,
}

impl fmt::Display for User {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(real_name) = &self.real_name {
            write!(f, "{real_name} ({})", self.name)
        } else {
            write!(f, "{}", self.name)
        }
    }
}