diff --git a/src/rep.rs.in b/src/rep.rs.in index 52b4c89f..f462fae4 100644 --- a/src/rep.rs.in +++ b/src/rep.rs.in @@ -374,7 +374,7 @@ pub struct Repo { pub name: String, pub full_name: String, pub description: String, - // private (keyword) + pub private: bool, pub fork: bool, pub url: String, pub html_url: String, @@ -450,6 +450,172 @@ impl Repo { } } + +#[derive(Debug, Serialize)] +pub struct RepoOptions { + pub name: String, + #[serde(skip_serializing_if="Option::is_none")] + pub description: Option, + #[serde(skip_serializing_if="Option::is_none")] + pub homepage: Option, + #[serde(skip_serializing_if="Option::is_none")] + pub private: Option, + #[serde(skip_serializing_if="Option::is_none")] + pub has_issues: Option, + #[serde(skip_serializing_if="Option::is_none")] + pub has_wiki: Option, + #[serde(skip_serializing_if="Option::is_none")] + pub has_downloads: Option, + #[serde(skip_serializing_if="Option::is_none")] + pub team_id: Option, + #[serde(skip_serializing_if="Option::is_none")] + pub auto_init: Option, + #[serde(skip_serializing_if="Option::is_none")] + pub gitignore_template: Option, + #[serde(skip_serializing_if="Option::is_none")] + pub license_template: Option, +} + +#[derive(Default)] +pub struct RepoOptionsBuilder { + name: String, + description: Option, + homepage: Option, + private: Option, + has_issues: Option, + has_wiki: Option, + has_downloads: Option, + team_id: Option, + auto_init: Option, + gitignore_template: Option, + license_template: Option, +} + +impl RepoOptionsBuilder { + pub fn new(name: N) -> RepoOptionsBuilder + where N: Into + { + RepoOptionsBuilder { name: name.into(), ..Default::default() } + } + + pub fn description(&mut self, description: D) -> &mut RepoOptionsBuilder + where D: Into + { + self.description = Some(description.into()); + self + } + + pub fn homepage(&mut self, homepage: H) -> &mut RepoOptionsBuilder + where H: Into + { + self.homepage = Some(homepage.into()); + self + } + + pub fn private(&mut self, private: bool) -> &mut RepoOptionsBuilder + { + self.private = Some(private); + self + } + + pub fn has_issues(&mut self, has_issues: bool) -> &mut RepoOptionsBuilder + { + self.has_issues = Some(has_issues); + self + } + + pub fn has_wiki(&mut self, has_wiki: bool) -> &mut RepoOptionsBuilder + { + self.has_wiki = Some(has_wiki); + self + } + + pub fn has_downloads(&mut self, has_downloads: bool) -> &mut RepoOptionsBuilder + { + self.has_downloads = Some(has_downloads); + self + } + + pub fn team_id(&mut self, team_id: i32) -> &mut RepoOptionsBuilder + { + self.team_id = Some(team_id); + self + } + + pub fn auto_init(&mut self, auto_init: bool) -> &mut RepoOptionsBuilder + { + self.auto_init = Some(auto_init); + self + } + + pub fn gitignore_template(&mut self, gitignore_template: GI) -> &mut RepoOptionsBuilder + where GI: Into + { + self.gitignore_template = Some(gitignore_template.into()); + self + } + + pub fn license_template(&mut self, license_template: L) -> &mut RepoOptionsBuilder + where L: Into + { + self.license_template = Some(license_template.into()); + self + } + + pub fn build(&self) -> RepoOptions { + RepoOptions::new(self.name.as_ref(), + self.description.clone(), + self.homepage.clone(), + self.private, + self.has_issues, + self.has_wiki, + self.has_downloads, + self.team_id, + self.auto_init, + self.gitignore_template.clone(), + self.license_template.clone()) + } +} + +impl RepoOptions { + pub fn new(name: N, + description: Option, + homepage: Option, + private: Option, + has_issues: Option, + has_wiki: Option, + has_downloads: Option, + team_id: Option, + auto_init: Option, + gitignore_template: Option, + license_template: Option) + -> RepoOptions + where N: Into, + D: Into, + H: Into, + GI: Into, + L: Into + { + RepoOptions { + name: name.into(), + description: description.map(|h| h.into()), + homepage: homepage.map(|h| h.into()), + private: private, + has_issues: has_issues, + has_wiki: has_wiki, + has_downloads: has_downloads, + team_id: team_id, + auto_init: auto_init, + gitignore_template: gitignore_template.map(|gi| gi.into()), + license_template: license_template.map(|l| l.into()), + } + } + + pub fn builder>(name: N) -> RepoOptionsBuilder { + RepoOptionsBuilder::new(name) + } +} + #[derive(Debug, Deserialize)] pub struct RepoDetails { pub id: u64, diff --git a/src/repositories.rs b/src/repositories.rs index 85a56e13..f95f13f5 100644 --- a/src/repositories.rs +++ b/src/repositories.rs @@ -1,4 +1,5 @@ //! Repository interface +extern crate serde_json; use self::super::{Github, Result}; use deployments::Deployments; @@ -7,7 +8,7 @@ use issues::{IssueRef, Issues}; use labels::Labels; use pulls::PullRequests; use releases::Releases; -use rep::{Repo, RepoListOptions, UserRepoListOptions, OrganizationRepoListOptions}; +use rep::{Repo, RepoOptions, RepoListOptions, UserRepoListOptions, OrganizationRepoListOptions}; use statuses::Statuses; use std::fmt; @@ -136,6 +137,13 @@ impl<'a> Repositories<'a> { format!("/user/repos{}", more) } + /// Create a new repository + /// https://developer.github.com/v3/repos/#create + pub fn create(&self, repo: &RepoOptions) -> Result { + let data = try!(serde_json::to_string(&repo)); + self.github.post::(&self.path(""), data.as_bytes()) + } + /// list the authenticated users repositories /// https://developer.github.com/v3/repos/#list-your-repositories pub fn list(&self, options: &RepoListOptions) -> Result> {