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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//! Git interface

use hyper::client::Connect;

use {Github, Future};

/// reference to git operations associated with a github repo
pub struct Git<C>
where
    C: Clone + Connect,
{
    github: Github<C>,
    owner: String,
    repo: String,
}

impl<C: Clone + Connect> Git<C> {
    #[doc(hidden)]
    pub fn new<O, R>(github: Github<C>, owner: O, repo: R) -> Self
    where
        O: Into<String>,
        R: Into<String>,
    {
        Git {
            github: github,
            owner: owner.into(),
            repo: repo.into(),
        }
    }

    fn path(&self, more: String) -> String {
        format!("/repos/{}/{}/git{}", self.owner, self.repo, more)
    }

    /// list a git tree of files for this repo at a given sha
    /// https://developer.github.com/v3/git/trees/#get-a-tree
    /// https://developer.github.com/v3/git/trees/#get-a-tree-recursively
    pub fn tree<S>(&self, sha: S, recursive: bool) -> Future<TreeData>
    where
        S: Into<String>,
    {
        self.github.get(&self.path(format!(
            "/trees/{}?recursive={}",
            sha.into(),
            if recursive { "1" } else { "0" }
        )))
    }

    /// get the blob contents of a given sha
    /// https://developer.github.com/v3/git/blobs/#get-a-blob
    pub fn blob<S>(&self, sha: S) -> Future<Blob>
    where
        S: Into<String>,
    {
        self.github.get(
            &self.path(format!("/blobs/{}", sha.into())),
        )
    }

    /// get the git reference data of a given ref
    /// the specified reference must be formatted as as "heads/branch", not just "branch"
    /// https://developer.github.com/v3/git/refs/#get-a-reference
    pub fn reference<S>(&self, reference: S) -> Future<GetReferenceResponse>
    where
        S: Into<String>,
    {
        self.github.get(
            &self.path(format!("/refs/{}", reference.into())),
        )
    }
}


// representations

#[derive(Debug, Deserialize)]
pub struct TreeData {
    pub sha: String,
    pub url: String,
    pub tree: Vec<GitFile>,
    pub truncated: bool,
}

#[derive(Debug, Deserialize)]
pub struct GitFile {
    pub path: String,
    pub mode: String,
    /// typically tree or blob
    #[serde(rename = "type")]
    pub content_type: String,
    /// size will be None for directories
    pub size: Option<usize>,
    pub sha: String,
    /// url will be None for commits
    pub url: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct Blob {
    pub content: String,
    pub encoding: String,
    pub url: String,
    pub sha: String,
    /// sizes will be None for directories
    pub size: Option<usize>,
}

#[derive(Debug, Deserialize, PartialEq)]
#[serde(untagged)]
/// The response for getting a git reference
pub enum GetReferenceResponse {
    /// The reference data matching the specified reference
    Exact(Reference),
    /// If the reference doesn't exist in the repository
    /// but existing refs start with ref they will be returned as an array.
    /// For example, a call to get the data for a branch named feature,
    /// which doesn't exist, would return head refs including featureA and featureB which do.
    StartWith(Vec<Reference>),
}

#[derive(Debug, Deserialize, PartialEq)]
pub struct Reference {
    #[serde(rename = "ref")]
    pub reference: String,
    pub url: String,
    pub object: Object,
}

#[derive(Debug, Deserialize, PartialEq)]
pub struct Object {
    #[serde(rename = "type")]
    pub object_type: String,
    pub sha: String,
    pub url: String,
}

#[cfg(test)]
mod tests {
    use std::fmt::Debug;
    use serde::Deserialize;
    use serde_json;
    use super::*;

    fn test_deserializing<'de, T>(payload: &'static str, expected: T)
    where
        T: Debug + PartialEq + Deserialize<'de>,
    {
        let incoming: T = serde_json::from_str(payload).unwrap();
        assert_eq!(incoming, expected)
    }

    #[test]
    fn deserialize_get_ref_exact() {
        let payload = r#"{
  "ref": "refs/heads/featureA",
  "url": "https://api.github.com/repos/octocat/Hello-World/git/refs/heads/featureA",
  "object": {
    "type": "commit",
    "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
    "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
  }
}"#;
        let expected = GetReferenceResponse::Exact(Reference {
            reference: "refs/heads/featureA".to_string(),
            url: "https://api.github.com/repos/octocat/Hello-World/git/refs/heads/featureA".to_string(),
            object: Object {
                object_type: "commit".to_string(),
                sha: "aa218f56b14c9653891f9e74264a383fa43fefbd".to_string(),
                url: "https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd".to_string(),
            },
        });
        test_deserializing(payload, expected)
    }

    #[test]
    fn deserialize_get_ref_starts_with() {
        let payload = r#"[
  {
    "ref": "refs/heads/feature-a",
    "url": "https://api.github.com/repos/octocat/Hello-World/git/refs/heads/feature-a",
    "object": {
      "type": "commit",
      "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
      "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
    }
  },
  {
    "ref": "refs/heads/feature-b",
    "url": "https://api.github.com/repos/octocat/Hello-World/git/refs/heads/feature-b",
    "object": {
      "type": "commit",
      "sha": "612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac",
      "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac"
    }
  }
]"#;
        let expected = GetReferenceResponse::StartWith(vec![
            Reference {
                reference: "refs/heads/feature-a".to_string(),
                url: "https://api.github.com/repos/octocat/Hello-World/git/refs/heads/feature-a".to_string(),
                object: Object {
                    object_type: "commit".to_string(),
                    sha: "aa218f56b14c9653891f9e74264a383fa43fefbd".to_string(),
                    url: "https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd".to_string(),
                },
            },
            Reference {
                reference: "refs/heads/feature-b".to_string(),
                url: "https://api.github.com/repos/octocat/Hello-World/git/refs/heads/feature-b".to_string(),
                object: Object {
                    object_type: "commit".to_string(),
                    sha: "612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac".to_string(),
                    url: "https://api.github.com/repos/octocat/Hello-World/git/commits/612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac".to_string(),
                },
            },
        ]);
        test_deserializing(payload, expected)
    }
}