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
//! Trait definition related to Git Data on Github

use serde_json;
use json::{Commit,Ref}; //Blob, BlobCreate
use requests::*;
use github::Client;
use error::*;

/// Trait used to define access to endpoints grouped under `Users` in the Github API
/// specification
pub trait GitData {

    // /// ### Request Type:
    // /// `GET`
    // /// ### Endpoint:
    // /// /repos/:owner/:repo/git/blobs/:sha
    // /// ### Description
    // /// Returns a `Blob` Struct for the requested owner's repo and sha.
    // fn get_blob(&self, owner: &str, repo: &str, sha: &str) -> Result<Blob>;

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /repos/:owner/:repo/git/commits/:sha
    /// ### Description
    /// Returns a `Blob` Struct for the requested owner's repo and sha.
    fn get_commit(&self, owner: &str, repo: &str, sha: &str) -> Result<Commit>;

    // This requires a repo so get to it at some point
    // /// ### Request Type:
    // /// `POST`
    // /// ### Endpoint:
    // /// /repos/:owner/:repo/git/commits
    // /// ### Description
    // /// Creates a commit for an owner's repo and returns the commit if it
    // /// succeeded.
    // fn post_commit(&self, owner: &str, repo: &str) -> Result<Commit>;

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /repos/:owner/:repo/git/refs/:ref
    /// ### Description
    /// Returns a `Ref` Struct for the requested owner's repo and sha.
    fn get_ref(&self, owner: &str, repo: &str, _ref: &str) -> Result<Ref>;

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /repos/:owner/:repo/git/refs/:ref
    /// ### Description
    /// Returns an array of `Ref` Struct for the requested owner's repo and sha.
    /// This should be used if there are multiple refs. If you have two branches
    /// projectA and projectB and use "project" as the input to `_ref` then you'll
    /// need this as opposed to get_ref since you'll get the ref for both projectA
    /// and projectB.
    fn get_ref_mult(&self, owner: &str, repo: &str, _ref: &str) -> Result<Vec<Ref>>;

    /// ### Request Type:
    /// `GET`
    /// ### Endpoint:
    /// /repos/:owner/:repo/git/refs
    /// ### Description
    /// Returns a vector containing all `Ref` Struct for the requested owner's repo and sha.
    fn get_refs(&self, owner: &str, repo: &str) -> Result<Vec<Ref>>;
}

impl GitData for Client {

    // /// This gets 301 errors right now and shouldn't be used yet
    // fn get_blob(&self, owner: &str, repo: &str, sha: &str) -> Result<Blob> {
    //     let url = format!(
    //             "https://api.github.com/repos/{}/{}/git/blobs/{}",
    //             owner, repo, sha
    //         );
    //     let data = get(&url, self.get_headers().clone())?;
    //     try_serde!(serde_json::from_str(&data))
    // }

    /// Retrieves a specific commit from an owner's repo given the commit hash
    fn get_commit(&self, owner: &str, repo: &str, sha: &str) -> Result<Commit> {
        let url = format!(
                "https://api.github.com/repos/{}/{}/git/commits/{}",
                owner, repo, sha
            );
        let data = get(&url, self.get_headers().clone())?;
        try_serde!(serde_json::from_str(&data))
    }

    /// Retrieves a specific ref from an owner's repo given the ref
    fn get_ref(&self, owner: &str, repo: &str, _ref: &str) -> Result<Ref> {
        let url = format!(
                "https://api.github.com/repos/{}/{}/git/refs/{}",
                owner, repo, _ref
            );
        let data = get(&url, self.get_headers().clone())?;

        if data.contains("Not Found") {
            Err(GithubError::Github404)
        } else {
            try_serde!(serde_json::from_str(&data))
        }
    }

    /// Retrieves multiple refs based on a partial match from an owner's repo given the ref
    fn get_ref_mult(&self, owner: &str, repo: &str, _ref: &str) -> Result<Vec<Ref>> {
        let url = format!(
                "https://api.github.com/repos/{}/{}/git/refs/{}",
                owner, repo, _ref
            );
        let data = get(&url, self.get_headers().clone())?;
        if data.contains("Not Found") {
            Err(GithubError::Github404)
        } else {
            try_serde!(serde_json::from_str(&data))
        }
    }

    /// Retrieves all refs an owner's repo given the co
    fn get_refs(&self, owner: &str, repo: &str) -> Result<Vec<Ref>> {
        let url = format!(
                "https://api.github.com/repos/{}/{}/git/refs",
                owner, repo
            );
        let data = get(&url, self.get_headers().clone())?;
        if data.contains("Not Found") {
            Err(GithubError::Github404)
        } else {
            try_serde!(serde_json::from_str(&data))
        }
    }
}