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
mod config;

pub use config::{Level, Mode};
use itertools::Itertools;
use std::fmt::{Display, Formatter};

pub const CONFIG_ENV: &str = "ISSUE_RS::Config::Mode";
pub const IGNORE_ENV: &str = "ISSUE_RS_IGNORE";

pub fn get_mode() -> Option<Mode> {
    if let Ok(var) = std::env::var(CONFIG_ENV) {
        serde_json::from_str(&var).expect("reading Mode from configuration")
    } else if std::env::var(IGNORE_ENV).is_ok() {
        Some(Mode::Noop)
    } else {
        Some(Default::default())
    }
}

pub const APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));

#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Issue {
    pub url: String,
}

impl Display for Issue {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "issue: {}", self.url)
    }
}

// Both Github and Gitlab use the `closed_at` field to identify closed issues.
#[derive(Default, Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct GithubIssue {
    pub closed_at: Option<String>,
}

impl Issue {
    pub fn canonicalize_url(&self) -> url::Url {
        let url = url::Url::parse(&self.url).unwrap();

        if url.host_str().unwrap().contains("github") {
            let path: String = Itertools::intersperse(url.path_segments().unwrap(), "/").collect();
            return url::Url::parse(&format!("https://api.github.com/repos/{}", path)).unwrap();
        }
        unreachable!("only github public repositories are currently supported")
    }

    pub fn is_closed(&self) -> Result<bool, anyhow::Error> {
        let client = reqwest::blocking::ClientBuilder::new()
            .user_agent(APP_USER_AGENT)
            .build()
            .unwrap();

        let response = client.get(self.canonicalize_url()).send()?;

        if !response.status().is_success() {
            anyhow::bail!(
                "failed to fetch issue: {}",
                response
                    .text()
                    .unwrap_or_else(|e| format!("no response found: {}", e))
            )
        }

        let issue: GithubIssue = response.json()?;

        Ok(issue.closed_at.is_some())
    }
}