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
//! Gists interface
extern crate serde_json;

use self::super::{Iter, MediaType, Github, Result};
use std::collections::BTreeMap;

fn identity<T>(x: T) -> T {
    x
}

/// reference to gists associated with a github user
pub struct Branches<'a> {
    github: &'a Github,
    owner: String,
    repo: String,
}

impl<'a> Branches<'a> {
    /// create a new instance of branches
    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(),
        }
    }

    /// list of branches for this repo
    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"))
    }

    /// provides an iterator over branches for this repo
    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"))
    }

    /// gets a branch for this repo by name
    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"))
    }

    /// update branch production for a given branch
    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"))
    }
}


// representations

#[derive(Debug, Deserialize)]
pub struct Branch {
    pub name: String,
    pub protected: bool,
    pub protection_url: String,
    pub protection: Protection, // todo: pub commit: CommitRef
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Protection {
    pub enabled: bool,
    /// when set to None in branch updates, github will apply a set of defaults
    #[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>,
}