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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
extern crate reqwest;

use std::default::Default;
use Error;
use Client;

use chrono::{DateTime, Utc};

pub const DEFAULT_PROJ_LIMIT: i32 = 100;

/// Represents a project type in the storyboard API
#[derive(Serialize, Deserialize, Debug)]
pub struct Project {
    /// Means that storyboard will try to create branches automatically
    pub autocreate_branch: Option<bool>,
    /// Details about the project.
    pub description: Option<String>,
    /// Tells if the project is active or has been deleted.
    pub is_active: bool,
    /// The project unique name.
    pub name: String,
    /// The repo link to the project.
    pub repo_url: Option<String>,
}

/// Represents a group of projects
#[derive(Serialize, Deserialize, Debug)]
pub struct ProjectGroup {
    /// The unique ID for the project group.
    pub id: i32,
    /// The unique name for the project group.
    pub name: String,
    /// The full name of the project group.
    pub title: String,
    /// Date when this project group was created.
    pub created_at: DateTime<Utc>,
}

impl Default for ProjectGroup {
    fn default() -> ProjectGroup {
        ProjectGroup {
            id: 0,
            name: String::new(),
            title: String::new(),
            created_at: Utc::now(),
        }
    }
}

impl Client {

    /// Retrieves all the projects from the storyboard API.
    ///
    /// # Example
    ///
    /// ```rust
    /// extern crate storyboard_client;
    ///
    /// use storyboard_client::{Client, Error};
    ///
    /// # fn main() { example().unwrap(); }
    /// fn example() -> Result<(), Error> {
    ///     let client = Client::new("https://storyboard.openstack.org/api/v1");
    ///     let projects = client.get_all_projects()?;
    ///     assert_ne!(projects.len(), 0);
    ///     Ok(())
    /// }
    /// ```
    pub fn get_all_projects(&self)
                            -> Result<Vec<Project>, Error> {
        let url = format!("{}/projects?limit={}", self.uri, DEFAULT_PROJ_LIMIT);
        let projects: Vec<Project> = self.fetch_url(&url)?;
        Ok(projects)
    }

    /// Search projects with the given search string
    ///
    /// # Example
    ///
    /// ```rust
    /// extern crate storyboard_client;
    ///
    /// use storyboard_client::{Client, Error};
    ///
    /// # fn main() { example().unwrap(); }
    /// fn example() -> Result<(), Error> {
    ///     let client = Client::new("https://storyboard.openstack.org/api/v1");
    ///     let projects = client.search_projects("stx")?;
    ///     assert_ne!(projects.len(), 0);
    ///     Ok(())
    /// }
    /// ```
    pub fn search_projects(&self, s: &str)
                           -> Result<Vec<Project>, Error> {
        let url = format!("{}/projects/search?q={}", self.uri, s);
        let projects: Vec<Project> = self.fetch_url(&url)?;
        Ok(projects)
    }

    /// Get all registered project groups.
    pub fn get_project_groups(&self)
                              -> Result<Vec<ProjectGroup>, Error> {
        let url = format!("{}/project_groups", self.uri);
        let groups: Vec<ProjectGroup> = self.fetch_url(&url)?;
        Ok(groups)
    }

    /// Get a project group by it's name.
    pub fn get_project_groups_by_name(&self, name: &str)
                                      -> Result<Vec<ProjectGroup>, Error> {
        let url = format!("{}/project_groups?name={}", self.uri, name);
        let groups: Vec<ProjectGroup> = self.fetch_url(&url)?;
        Ok(groups)
    }

    /// Retrieves all projects in a project group.
    ///
    /// # Example
    ///
    /// ```rust
    /// extern crate storyboard_client;
    ///
    /// use storyboard_client::{Client, Error, ProjectGroup};
    ///
    /// # fn main() { example().unwrap(); }
    /// fn example() -> Result<(), Error> {
    ///     let client = Client::new("https://storyboard.openstack.org/api/v1");
    ///     let group = ProjectGroup { id: 86, ..Default::default() };
    ///     let projects = client.get_projects_in_group(&group)?;
    ///     assert_ne!(projects.len(), 0);
    ///     Ok(())
    /// }
    /// ```
    pub fn get_projects_in_group(&self, g: &ProjectGroup)
                                 -> Result<Vec<Project>, Error> {
        let url = format!("{}/project_groups/{}/projects", self.uri, g.id);
        let projects: Vec<Project> = self.fetch_url(&url)?;
        Ok(projects)
    }

}