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
//! Single issue
//!
//! https://docs.gitlab.com/ce/api/issues.html#single-issue
//!
//! # Single issue
//!
//! Get a single project issue.
//!
//! ```text
//! GET /projects/ID/issues/ISSUE_ID
//! ```
//!
//! | Attribute | Type | Required | Description |
//! | --------- | ---- | -------- | ----------- |
//! | `id`      | integer | yes   | The ID of a project |
//! | `issue_id`| integer | yes   | The ID of a project's issue |
//!
//!


use BuildQuery;

use ::errors::*;


#[derive(Debug, Clone)]
pub struct IssueLister<'a> {
    gl: &'a ::GitLab,
    /// The ID of a project
    id: i64,
    /// The ID of a project's issue
    issue_id: i64,
}


impl<'a> IssueLister<'a> {
    pub fn new(gl: &'a ::GitLab, id: i64, issue_id: i64) -> IssueLister {
        IssueLister {
            gl: gl,
            id: id,
            issue_id: issue_id,
        }
    }

    /// Commit the lister: Query GitLab and return a list of projects.
    pub fn list(&self) -> Result<::issues::Issue> {
        let query = self.build_query();
        debug!("query: {:?}", query);

        self.gl.get(&query, None, None).chain_err(|| format!("cannot get query {}", query))
    }
}


impl<'a> BuildQuery for IssueLister<'a> {
    fn build_query(&self) -> String {
        format!("projects/{}/issues/{}", self.id, self.issue_id)
    }
}


#[cfg(test)]
mod tests {
    use BuildQuery;

    const TEST_PROJECT_ID: i64 = 123;
    const TEST_ISSUE_ID: i64 = 456;


    #[test]
    fn build_query_default() {
        let gl = ::GitLab::new(&"localhost", "XXXXXXXXXXXXXXXXXXXX").unwrap();
        // let gl: ::GitLab = Default::default();

        let expected_string = format!("projects/{}/issues/{}", TEST_PROJECT_ID, TEST_ISSUE_ID);

        let lister = gl.issues();
        let lister = lister.single(TEST_PROJECT_ID, TEST_ISSUE_ID);
        let query = lister.build_query();
        assert_eq!(query, expected_string);

        let lister = gl.issues().single(TEST_PROJECT_ID, TEST_ISSUE_ID);
        let query = lister.build_query();
        assert_eq!(query, expected_string);

        let query = gl.issues().single(TEST_PROJECT_ID, TEST_ISSUE_ID).build_query();
        assert_eq!(query, expected_string);
    }
}