forked from adamogle/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-helper.js
105 lines (95 loc) · 2.23 KB
/
git-helper.js
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
var getRepoInfo = require('git-repo-info');
var GitHubApi = require('github');
var Promise = require('bluebird');
var fs = require('fs');
var semver = require('semver');
var github;
/**
* Setup the Github API helper objects and authenticate them.
*
* @param {String} githubToken The Github access token.
*/
exports.setupGitApi = function setupGitApi(githubToken) {
if (github) {
return;
}
github = new GitHubApi({
version: '3.0.0',
timeout: 5000,
headers: {
'user-agent': 'Handsontable'
}
});
github.authenticate({
type: 'token',
token: githubToken
});
};
/**
* Get information about local repository.
*
* @returns {Object}
*/
exports.getLocalInfo = function getLocalInfo() {
return getRepoInfo();
};
/**
* Get latest Handsontable release version based on passed semver range.
*
* @param {String} [range=false] Semver range version
* @returns {Promise}
*/
exports.getHotLatestRelease = function getHotLatestRelease(range) {
return new Promise(function(resolve, reject) {
github.releases.listReleases({
owner: 'handsontable',
repo: 'handsontable',
page: 1,
per_page: 100
}, function(err, resp) {
if (err) {
return reject(err);
}
if (range) {
resp = resp.filter(function(release) {
return semver.satisfies(release.tag_name, range);
});
}
resolve(resp.length ? resp[0] : null);
});
});
};
/**
* Get all availables docs version
*
* @returns {Promise}
*/
exports.getDocsVersions = function getDocsVersions() {
return new Promise(function(resolve, reject) {
github.repos.getBranches({
user: 'handsontable',
repo: 'docs',
page: 1,
per_page: 100
}, function(err, resp) {
if (err) {
return reject(err);
}
var branches;
branches = resp.filter(function(branch) {
return branch.name.match(/^\d{1,5}\.\d{1,5}\.\d{1,5}(\-(beta|alpha)(\d+)?)?$/) ? true : false;
}).map(function(branch) {
return branch.name;
}).sort(function(a, b) {
if (semver.gt(a, b)) {
return 1;
}
if (semver.lt(a, b)) {
return -1;
}
return 0;
});
resolve(branches);
});
});
};