|
1 |
| -// Github.js 0.1.0 |
| 1 | +// Github.js 0.1.1 |
2 | 2 | // (c) 2012 Michael Aufreiter, Development Seed
|
3 | 3 | // Github.js is freely distributable under the MIT license.
|
4 | 4 | // For all details and documentation:
|
|
18 | 18 | // Util
|
19 | 19 | // -------
|
20 | 20 |
|
21 |
| - function _request(method, route, data, cb) { |
| 21 | + function _request(method, path, data, cb) { |
22 | 22 | $.ajax({
|
23 | 23 | type: method,
|
24 |
| - url: API_URL + route, |
| 24 | + url: API_URL + path, |
25 | 25 | data: JSON.stringify(data),
|
26 | 26 | dataType: 'json',
|
27 | 27 | contentType: 'application/x-www-form-urlencoded',
|
|
38 | 38 | this.username = options.username;
|
39 | 39 | var userPath = "/users/" + options.username;
|
40 | 40 | this.repos = function(cb) {
|
41 |
| - _request("GET", userPath + "/repos", null, function(err, res) { |
| 41 | + _request("GET", userPath + "/repos?type=all", null, function(err, res) { |
42 | 42 | cb(err, res);
|
43 | 43 | });
|
44 | 44 | }
|
45 | 45 | };
|
46 | 46 |
|
| 47 | + |
47 | 48 | // Repository API
|
48 | 49 | // -------
|
49 | 50 |
|
50 | 51 | Github.Repository = function(options) {
|
| 52 | + var repo = options.name; |
| 53 | + var branch = options.branch; |
| 54 | + |
| 55 | + var that = this; |
| 56 | + var repoPath = "/repos/" + username + "/" + repo; |
| 57 | + |
| 58 | + // Get latest commit from master |
| 59 | + function getLatestCommit(cb) { |
| 60 | + _request("GET", repoPath + "/git/refs/heads/" + branch, null, function(err, res) { |
| 61 | + if (err) return cb(err); |
| 62 | + cb(null, res.object.sha); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + // Retrieve the contents of a blob |
| 67 | + function getBlob(sha, cb) { |
| 68 | + _request("GET", repoPath + "/git/blobs/" + sha, null, function(err, res) { |
| 69 | + cb(err, res); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + // Retrieve the tree a commit points to |
| 74 | + |
| 75 | + function getTree(commit, cb) { |
| 76 | + _request("GET", repoPath + "/git/trees/"+commit, null, function(err, res) { |
| 77 | + if (err) return cb(err); |
| 78 | + cb(null, res.sha); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + // Post a new blob object, getting a blob SHA back |
| 83 | + |
| 84 | + function postBlob(content, cb) { |
| 85 | + var data = { |
| 86 | + "content": content, |
| 87 | + "encoding": "utf-8" |
| 88 | + }; |
| 89 | + _request("POST", repoPath + "/git/blobs", data, function(err, res) { |
| 90 | + if (err) return cb(err); |
| 91 | + cb(null, res.sha); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + // Post a new tree object having a file path pointer replaced |
| 96 | + // with a new blob SHA getting a tree SHA back |
| 97 | + |
| 98 | + function postTree(baseTree, path, blob, cb) { |
| 99 | + var data = { |
| 100 | + "base_tree": baseTree, |
| 101 | + "tree": [ |
| 102 | + { |
| 103 | + "path": path, |
| 104 | + "mode": "100644", |
| 105 | + "type": "blob", |
| 106 | + "sha": blob |
| 107 | + } |
| 108 | + ] |
| 109 | + }; |
| 110 | + _request("POST", repoPath + "/git/trees", data, function(err, res) { |
| 111 | + if (err) return cb(err); |
| 112 | + cb(null, res.sha); |
| 113 | + }); |
| 114 | + }; |
| 115 | + |
| 116 | + // Create a new commit object with the current commit SHA as the parent |
| 117 | + // and the new tree SHA, getting a commit SHA back |
| 118 | + |
| 119 | + function createCommit(parent, tree, cb) { |
| 120 | + var data = { |
| 121 | + "message": "Spooky. Isn't it?", |
| 122 | + "author": { |
| 123 | + "name": "Ghost" |
| 124 | + }, |
| 125 | + "parents": [ |
| 126 | + parent |
| 127 | + ], |
| 128 | + "tree": tree |
| 129 | + }; |
| 130 | + |
| 131 | + _request("POST", repoPath + "/git/commits", data, function(err, res) { |
| 132 | + if (err) return cb(err); |
| 133 | + cb(null, res.sha); |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + // Update the reference of your head to point to the new commit SHA |
| 138 | + |
| 139 | + function updateHead(commit, cb) { |
| 140 | + _request("PATCH", repoPath + "/git/refs/heads/" + branch, { "sha": commit }, function(err, res) { |
| 141 | + cb(err); |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + |
51 | 146 |
|
52 | 147 | // Show repository information
|
53 | 148 | // -------
|
54 | 149 |
|
55 |
| - var repoPath = "/repos/" + username + "/" + options.name; |
56 |
| - |
57 | 150 | this.show = function(cb) {
|
58 | 151 | _request("GET", repoPath, null, function(err, res) {
|
59 | 152 | cb();
|
60 | 153 | });
|
61 | 154 | };
|
62 | 155 |
|
| 156 | + // List all files |
| 157 | + // ------- |
| 158 | + |
| 159 | + this.list = function(cb) { |
| 160 | + _request("GET", repoPath + "/git/trees/" + branch + "?recursive=1", null, function(err, res) { |
| 161 | + cb(err, res ? res.tree : null); |
| 162 | + }); |
| 163 | + }; |
| 164 | + |
63 | 165 |
|
64 | 166 | // Read file at given path
|
65 | 167 | // -------
|
66 | 168 |
|
67 | 169 | this.read = function(path, cb) {
|
| 170 | + that.list(function(err, tree) { |
| 171 | + var file = _.select(tree, function(file) { |
| 172 | + return file.path === path; |
| 173 | + })[0]; |
| 174 | + |
| 175 | + if (!file) return cb("not found", null); |
| 176 | + |
| 177 | + getBlob(file.sha, function(err, blob) { |
| 178 | + function decode(blob) { |
| 179 | + if (blob.content) { |
| 180 | + var data = blob.encoding == 'base64' ? |
| 181 | + atob(blob.content.replace(/\s/g, '')) : |
| 182 | + blob.content; |
| 183 | + |
| 184 | + return data; |
| 185 | + } else { |
| 186 | + return ""; |
| 187 | + } |
| 188 | + } |
68 | 189 |
|
| 190 | + cb(null, decode(blob)); |
| 191 | + }); |
| 192 | + }); |
69 | 193 | };
|
70 | 194 |
|
71 | 195 | // Write file contents on a given path
|
72 | 196 | // -------
|
73 | 197 |
|
74 | 198 | this.write = function(path, content, cb) {
|
75 |
| - |
76 |
| - // Get latest commit from master |
77 |
| - function getLatestCommit(cb) { |
78 |
| - _request("GET", repoPath + "/git/refs/heads/master", null, function(err, res) { |
79 |
| - if (err) return cb(err); |
80 |
| - cb(null, res.object.sha); |
81 |
| - }); |
82 |
| - } |
83 |
| - |
84 |
| - // Retrieve the tree a commit points to |
85 |
| - |
86 |
| - function getTree(commit, cb) { |
87 |
| - _request("GET", repoPath + "/git/trees/"+commit, null, function(err, res) { |
88 |
| - if (err) return cb(err); |
89 |
| - cb(null, res.sha); |
90 |
| - }); |
91 |
| - } |
92 |
| - |
93 |
| - // Post a new blob object, getting a blob SHA back |
94 |
| - |
95 |
| - function postBlob(content, cb) { |
96 |
| - var data = { |
97 |
| - "content": content, |
98 |
| - "encoding": "utf-8" |
99 |
| - }; |
100 |
| - _request("POST", repoPath + "/git/blobs", data, function(err, res) { |
101 |
| - if (err) return cb(err); |
102 |
| - cb(null, res.sha); |
103 |
| - }); |
104 |
| - } |
105 |
| - |
106 |
| - // Post a new tree object having a file path pointer replaced |
107 |
| - // with a new blob SHA getting a tree SHA back |
108 |
| - |
109 |
| - function postTree(baseTree, path, blob, cb) { |
110 |
| - var data = { |
111 |
| - "base_tree": baseTree, |
112 |
| - "tree": [ |
113 |
| - { |
114 |
| - "path": path, |
115 |
| - "mode": "100644", |
116 |
| - "type": "blob", |
117 |
| - "sha": blob |
118 |
| - } |
119 |
| - ] |
120 |
| - }; |
121 |
| - _request("POST", repoPath + "/git/trees", data, function(err, res) { |
122 |
| - if (err) return cb(err); |
123 |
| - cb(null, res.sha); |
124 |
| - }); |
125 |
| - }; |
126 |
| - |
127 |
| - // Create a new commit object with the current commit SHA as the parent |
128 |
| - // and the new tree SHA, getting a commit SHA back |
129 |
| - |
130 |
| - function createCommit(parent, tree, cb) { |
131 |
| - var data = { |
132 |
| - "message": "Spooky. Isn't it?", |
133 |
| - "author": { |
134 |
| - "name": "Ghost" |
135 |
| - }, |
136 |
| - "parents": [ |
137 |
| - parent |
138 |
| - ], |
139 |
| - "tree": tree |
140 |
| - }; |
141 |
| - |
142 |
| - _request("POST", repoPath + "/git/commits", data, function(err, res) { |
143 |
| - if (err) return cb(err); |
144 |
| - cb(null, res.sha); |
145 |
| - }); |
146 |
| - } |
147 |
| - |
148 |
| - // Update the reference of your head to point to the new commit SHA |
149 |
| - |
150 |
| - function updateHead(commit, cb) { |
151 |
| - _request("PATCH", repoPath + "/git/refs/heads/master", { "sha": commit }, function(err, res) { |
152 |
| - cb(err); |
153 |
| - }); |
154 |
| - } |
155 |
| - |
156 |
| - // Write it. |
157 |
| - |
158 | 199 | getLatestCommit(function(err, latestCommit) {
|
159 | 200 | getTree(latestCommit, function(err, tree) {
|
160 | 201 | postBlob(content, function(err, blob) {
|
|
174 | 215 | // Top Level API
|
175 | 216 | // -------
|
176 | 217 |
|
177 |
| - this.getRepo = function(repo) { |
178 |
| - return new Github.Repository({name: repo}); |
| 218 | + this.getRepo = function(repo, branch) { |
| 219 | + return new Github.Repository({name: repo, branch: branch || "master"}); |
179 | 220 | };
|
180 | 221 |
|
181 | 222 | this.getUser = function(user) {
|
|
0 commit comments