automatons_github/resource/check_run/
output.rs

1use std::fmt::{Display, Formatter};
2
3use serde::{Deserialize, Serialize};
4use url::Url;
5
6use crate::name;
7
8name!(
9    /// Check run output title
10    CheckRunOutputTitle
11);
12
13name!(
14    /// Check out output summary
15    CheckRunOutputSummary
16);
17
18/// Check run output
19///
20/// Integrations can provide additional context for a completed check run in the check run's output.
21/// The output has a title and a summary, which supports Markdown. It can optionally have a text
22/// with more details, and annotations.
23#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
24pub struct CheckRunOutput {
25    title: CheckRunOutputTitle,
26    summary: CheckRunOutputSummary,
27    text: Option<String>,
28    annotations_count: u64,
29    annotations_url: Url,
30}
31
32impl CheckRunOutput {
33    /// Returns the check run output's title.
34    #[cfg_attr(feature = "tracing", tracing::instrument)]
35    pub fn title(&self) -> &CheckRunOutputTitle {
36        &self.title
37    }
38
39    /// Returns the check run output's summary.
40    #[cfg_attr(feature = "tracing", tracing::instrument)]
41    pub fn summary(&self) -> &CheckRunOutputSummary {
42        &self.summary
43    }
44
45    /// Returns the check run output's text.
46    #[cfg_attr(feature = "tracing", tracing::instrument)]
47    pub fn text(&self) -> &Option<String> {
48        &self.text
49    }
50
51    /// Returns the check run output's annotations count.
52    #[cfg_attr(feature = "tracing", tracing::instrument)]
53    pub fn annotations_count(&self) -> u64 {
54        self.annotations_count
55    }
56
57    /// Returns the API endpoint to query the check run output's annotations.
58    #[cfg_attr(feature = "tracing", tracing::instrument)]
59    pub fn annotations_url(&self) -> &Url {
60        &self.annotations_url
61    }
62}
63
64impl Display for CheckRunOutput {
65    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
66        write!(f, "{}", self.title)
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use url::Url;
73
74    use super::{CheckRunOutput, CheckRunOutputSummary, CheckRunOutputTitle};
75
76    const JSON: &str = r#"
77    {
78        "title": "2/2 checks succeeded",
79        "summary": "... long Markdown content ...",
80        "text": null,
81        "annotations_count": 0,
82        "annotations_url": "https://api.github.com/repos/devxbots/automatons/check-runs/7668626402/annotations"
83    }
84    "#;
85
86    #[test]
87    fn trait_deserialize() {
88        let output: CheckRunOutput = serde_json::from_str(JSON).unwrap();
89
90        assert_eq!("2/2 checks succeeded", output.title().get());
91    }
92
93    #[test]
94    fn trait_display() {
95        let output = CheckRunOutput {
96            title: CheckRunOutputTitle::new("2/2 checks succeeded"),
97            summary: CheckRunOutputSummary::new("... long Markdown content ..."),
98            text: None,
99            annotations_count: 0,
100            annotations_url: Url::parse("https://api.github.com/repos/devxbots/automatons/check-runs/7669942377/annotations").unwrap()
101        };
102
103        assert_eq!("2/2 checks succeeded", output.to_string());
104    }
105
106    #[test]
107    fn trait_send() {
108        fn assert_send<T: Send>() {}
109        assert_send::<CheckRunOutput>();
110    }
111
112    #[test]
113    fn trait_sync() {
114        fn assert_sync<T: Sync>() {}
115        assert_sync::<CheckRunOutput>();
116    }
117}