aboutsummaryrefslogtreecommitdiff
path: root/ticket/src/actions.rs
blob: 3e237ffe007234981e3e0148964759e660ce69f6 (plain)
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
use crate::{
  Ticket,
  TicketV0,
};
use anyhow::{
  bail,
  Result,
};
use log::*;
use shared::find_root;
use std::{
  fs,
  path::PathBuf,
};

pub fn get_open_tickets() -> Result<Vec<Ticket>> {
  get_tickets(open_tickets()?)
}

pub fn get_closed_tickets() -> Result<Vec<Ticket>> {
  get_tickets(closed_tickets()?)
}

fn get_tickets(path: PathBuf) -> Result<Vec<Ticket>> {
  let mut out = Vec::new();
  debug!("Looking for ticket.");
  for entry in fs::read_dir(&path)? {
    let entry = entry?;
    let path = entry.path();
    trace!("Looking at entry {}.", path.display());
    if path.is_file() {
      trace!("Entry is a file.");
      match toml::from_slice::<Ticket>(&fs::read(&path)?) {
        Ok(ticket) => out.push(ticket),
        Err(e) => {
          error!("Failed to parse ticket {}", path.canonicalize()?.display());
          error!("Is the file an old ticket format? You might need to run `ticket migrate`.");
          bail!("Underlying error was {}", e);
        }
      }
    }
  }
  out.sort_by(|a, b| a.id.cmp(&b.id));
  Ok(out)
}

pub fn ticket_root() -> Result<PathBuf> {
  Ok(find_root()?.join(".dev-suite").join("ticket"))
}

pub fn closed_tickets() -> Result<PathBuf> {
  Ok(ticket_root()?.join("closed"))
}

pub fn open_tickets() -> Result<PathBuf> {
  Ok(ticket_root()?.join("open"))
}

// Old version ticket code to handle grabbing code
pub fn get_all_ticketsv0() -> Result<Vec<TicketV0>> {
  let mut tickets = get_open_ticketsv0()?;
  tickets.extend(get_closed_ticketsv0()?);
  Ok(tickets)
}
pub fn get_open_ticketsv0() -> Result<Vec<TicketV0>> {
  get_ticketsv0(open_tickets()?)
}

pub fn get_closed_ticketsv0() -> Result<Vec<TicketV0>> {
  get_ticketsv0(closed_tickets()?)
}

fn get_ticketsv0(path: PathBuf) -> Result<Vec<TicketV0>> {
  let mut out = Vec::new();
  debug!("Looking for ticket.");
  for entry in fs::read_dir(&path)? {
    let entry = entry?;
    let path = entry.path();
    trace!("Looking at entry {}.", path.display());
    if path.is_file() {
      trace!("Entry is a file.");
      if let Ok(ticket) = toml::from_slice::<TicketV0>(&fs::read(&path)?) {
        out.push(ticket);
      }
    }
  }
  out.sort_by(|a, b| a.number.cmp(&b.number));
  Ok(out)
}