forked from github-tools/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
253 lines (207 loc) · 6.79 KB
/
github.js
File metadata and controls
253 lines (207 loc) · 6.79 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Github.js 0.2.0
// (c) 2012 Michael Aufreiter, Development Seed
// Github.js is freely distributable under the MIT license.
// For all details and documentation:
// http://substance.io/michael/github
(function() {
var API_URL = 'https://api.github.com';
var Github = function(options) {
var username = options.username;
var password = options.password;
// Util
// =======
function _request(method, path, data, cb) {
$.ajax({
type: method,
url: API_URL + path,
data: JSON.stringify(data),
dataType: 'json',
contentType: 'application/x-www-form-urlencoded',
success: function(res) { cb(null, res); },
error: function(err) { cb(err); },
headers : { Authorization : 'Basic ' + Base64.encode(username + ':' + password) }
});
}
// USER API
// =======
Github.User = function() {
this.repos = function(cb) {
_request("GET", "/user/repos?type=all", null, function(err, res) {
cb(err, res);
});
}
};
// Repository API
// =======
Github.Repository = function(options) {
var repo = options.name;
var branch = options.branch;
var user = options.user;
var that = this;
var repoPath = "/repos/" + user + "/" + repo;
// Get a particular reference
// -------
this.getRef = function(ref, cb) {
_request("GET", repoPath + "/git/refs/heads/" + ref, null, function(err, res) {
if (err) return cb(err);
cb(null, res.object.sha);
});
};
// List all branches of a repository
// -------
this.listBranches = function(cb) {
_request("GET", repoPath + "/git/refs/heads", null, function(err, heads) {
if (err) return cb(err);
cb(null, _.map(heads, function(head) { return _.last(head.ref.split('/')); }));
});
};
// Retrieve the contents of a blob
// -------
this.getBlob = function(sha, cb) {
_request("GET", repoPath + "/git/blobs/" + sha, null, function(err, res) {
cb(err, res);
});
};
// Retrieve the tree a commit points to
// -------
this.getTree = function(commit, cb) {
_request("GET", repoPath + "/git/trees/"+commit, null, function(err, res) {
if (err) return cb(err);
cb(null, res.sha);
});
};
// Post a new blob object, getting a blob SHA back
// -------
this.postBlob = function(content, cb) {
var data = {
"content": content,
"encoding": "utf-8"
};
_request("POST", repoPath + "/git/blobs", data, function(err, res) {
if (err) return cb(err);
cb(null, res.sha);
});
};
// Post a new tree object having a file path pointer replaced
// with a new blob SHA getting a tree SHA back
// -------
this.postTree = function(baseTree, path, blob, cb) {
var data = {
"base_tree": baseTree,
"tree": [
{
"path": path,
"mode": "100644",
"type": "blob",
"sha": blob
}
]
};
_request("POST", repoPath + "/git/trees", data, function(err, res) {
if (err) return cb(err);
cb(null, res.sha);
});
};
// Create a new commit object with the current commit SHA as the parent
// and the new tree SHA, getting a commit SHA back
// -------
this.commit = function(parent, tree, message, cb) {
var data = {
"message": message,
"author": {
"name": username
},
"parents": [
parent
],
"tree": tree
};
_request("POST", repoPath + "/git/commits", data, function(err, res) {
if (err) return cb(err);
cb(null, res.sha);
});
};
// Update the reference of your head to point to the new commit SHA
// -------
this.updateHead = function(head, commit, cb) {
_request("PATCH", repoPath + "/git/refs/heads/" + head, { "sha": commit }, function(err, res) {
cb(err);
});
};
// Show repository information
// -------
this.show = function(cb) {
_request("GET", repoPath, null, function(err, res) {
cb();
});
};
// List all files of a branch
// -------
this.list = function(branch, cb) {
_request("GET", repoPath + "/git/trees/" + branch + "?recursive=1", null, function(err, res) {
cb(err, res ? res.tree : null);
});
};
// Read file at given path
// -------
this.read = function(branch, path, cb) {
that.list(branch, function(err, tree) {
var file = _.select(tree, function(file) {
return file.path === path;
})[0];
if (!file) return cb("not found", null);
that.getBlob(file.sha, function(err, blob) {
function decode(blob) {
if (blob.content) {
var data = blob.encoding == 'base64' ?
atob(blob.content.replace(/\s/g, '')) :
blob.content;
return data;
} else {
return "";
}
}
cb(null, decode(blob));
});
});
};
// Write file contents to a given branch and path
// -------
this.write = function(branch, path, content, message, cb) {
that.getRef(branch, function(err, latestCommit) {
that.getTree(latestCommit, function(err, tree) {
that.postBlob(content, function(err, blob) {
that.postTree(tree, path, blob, function(err, tree) {
that.commit(latestCommit, tree, message, function(err, commit) {
that.updateHead(branch, commit, function(err) {
cb(err);
});
});
});
});
});
});
};
};
// Top Level API
// -------
this.getRepo = function(user, repo, branch) {
return new Github.Repository({user: user, name: repo, branch: branch || "master"});
};
this.getUser = function() {
return new Github.User();
};
};
// Export the Github object for Node.js, with
// backwards-compatibility for the old `require()` API. If we're in
// the browser, add `Github` as a global object via a string identifier,
// for Closure Compiler "advanced" mode.
if (typeof exports !== "undefined") {
if (typeof module !== "undefined" && module.exports) {
exports = module.exports = Github;
}
exports.Github = Github;
} else {
this['Github'] = Github;
}
}).call(this);