extern crate serde_json;
use self::super::{Iter, MediaType, Github, Result};
use std::collections::BTreeMap;
fn identity<T>(x: T) -> T {
x
}
pub struct Branches<'a> {
github: &'a Github,
owner: String,
repo: String,
}
impl<'a> Branches<'a> {
pub fn new<U, R>(github: &'a Github, owner: U, repo: R) -> Self
where U: Into<String>,
R: Into<String>
{
Branches {
github: github,
owner: owner.into(),
repo: repo.into(),
}
}
pub fn list(&self) -> Result<Vec<Branch>> {
self.github
.get_media::<Vec<Branch>>(&format!("/repos/{owner}/{repo}/branches",
owner = self.owner,
repo = self.repo),
MediaType::Preview("loki"))
}
pub fn iter(&self) -> Result<Iter<Vec<Branch>, Branch>> {
self.github
.iter_media(format!("/repos/{owner}/{repo}/branches",
owner = self.owner,
repo = self.repo),
identity,
MediaType::Preview("loki"))
}
pub fn get<B>(&self, branch: B) -> Result<Branch>
where B: Into<String>
{
self.github
.get_media(&format!("/repos/{owner}/{repo}/branches/{branch}",
owner = self.owner,
repo = self.repo,
branch = branch.into()),
MediaType::Preview("loki"))
}
pub fn protection<B>(&self, branch: B, pro: &Protection) -> Result<Branch>
where B: Into<String>
{
let mut payload = BTreeMap::new();
payload.insert("protection", pro);
let data = serde_json::to_string(&payload)?;
self.github
.patch_media::<Branch>(&format!("/repos/{owner}/{repo}/branches/{branch}",
owner = self.owner,
repo = self.repo,
branch = branch.into()),
data.as_bytes(),
MediaType::Preview("loki"))
}
}
#[derive(Debug, Deserialize)]
pub struct Branch {
pub name: String,
pub protected: bool,
pub protection_url: String,
pub protection: Protection, }
#[derive(Debug, Deserialize, Serialize)]
pub struct Protection {
pub enabled: bool,
#[serde(skip_serializing_if="Option::is_none")]
pub required_status_checks: Option<StatusChecks>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct StatusChecks {
pub enforcement_level: String,
pub contexts: Vec<String>,
}