-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
93 lines (82 loc) · 2.55 KB
/
github.js
File metadata and controls
93 lines (82 loc) · 2.55 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
/**
* Thanks to Caolan McMahon's work on Jam on which this file is based.
* https://github.com/caolan/jam/
*/
var url = require('url'),
request = require('request'),
logger = require('./logger'),
utils = require('./'),
_ = require('underscore');
var uc = encodeURIComponent;
exports.GITHUB_URL = 'https://api.github.com';
exports.GITHUB_RAW_URL = 'https://raw.github.com';
exports.getRaw = function (user, repo, ref, path, callback) {
var req = {
json: true,
url: url.resolve(
exports.GITHUB_RAW_URL,
_([user, repo, ref, path]).compact().map(uc).join('/')
),
proxy: utils.getHttpProxy(exports.GITHUB_RAW_URL)
};
request(req, function (err, res, data) {
if (data.error) {
return callback(data.error);
}
callback(err, data);
});
};
exports.repos = {};
exports.repos.getArchiveLink = function (user, repo, format, ref, callback) {
var req = {
json: true,
followRedirect: false,
headers: {
'Accept': 'application/vnd.github.beta+json'
},
url: url.resolve(
exports.GITHUB_URL,
_.map(['repos', user, repo, format, ref], uc).join('/')
),
proxy: utils.getHttpProxy(exports.GITHUB_URL)
};
logger.debug('getting github archive link', req.url);
request(req, function (err, res, data) {
if (err) {
return callback(err);
}
if (data && data.error) {
return callback(res.error);
}
if (res.statusCode === 404) {
return callback('GitHub repository or tag not found');
}
if (!res.headers || !res.headers.location) {
return callback('Failed to get archive link');
}
callback(null, res.headers.location);
});
};
exports.repos.search = function (q, callback) {
var req = {
json: true,
headers: {
'Accept': 'application/vnd.github.beta+json'
},
url: url.resolve(
exports.GITHUB_URL,
['legacy', 'repos', 'search', uc(q)].join('/')
),
proxy: utils.getHttpProxy(exports.GITHUB_URL)
};
logger.debug('searching github repositories', req.url);
request.get(req, function (err, res, data) {
if (err) {
return callback(err);
}
if (data && data.error) {
return callback(res.error);
}
callback(null, data);
});
};