codealong_github/
analyzed_pull_request.rs

1use chrono::prelude::*;
2use chrono::DateTime;
3use std::borrow::Cow;
4use std::collections::HashSet;
5use std::iter::FromIterator;
6
7use codealong::{AnalyzedDiff, Event, Person};
8
9use crate::pull_request::PullRequest;
10
11#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
12pub struct AnalyzedPullRequest {
13    timestamp: DateTime<Utc>,
14
15    normalized_author: Person,
16
17    #[serde(flatten)]
18    pr: PullRequest,
19
20    #[serde(flatten)]
21    pub diff: Option<AnalyzedDiff>,
22
23    pub time_to_resolve: Option<i64>,
24}
25
26impl AnalyzedPullRequest {
27    pub fn new(
28        pr: PullRequest,
29        diff: Option<AnalyzedDiff>,
30        normalized_author: Person,
31    ) -> AnalyzedPullRequest {
32        AnalyzedPullRequest {
33            timestamp: pr.merged_at.unwrap_or(pr.updated_at),
34            normalized_author,
35            diff,
36            time_to_resolve: pr
37                .merged_at
38                .as_ref()
39                .map(|ma| (ma.clone() - pr.created_at.clone()).num_seconds()),
40            pr,
41        }
42    }
43}
44
45impl Event for AnalyzedPullRequest {
46    fn timestamp(&self) -> &DateTime<Utc> {
47        &self.timestamp
48    }
49
50    fn id(&self) -> Cow<str> {
51        self.pr.id.to_string().into()
52    }
53
54    fn event_type(&self) -> &str {
55        "pull_request"
56    }
57
58    fn tags(&self) -> HashSet<String> {
59        if let Some(ref diff) = self.diff {
60            HashSet::from_iter(diff.tag_stats.keys().map(|s| s.to_owned()))
61        } else {
62            HashSet::new()
63        }
64    }
65}