This repository has been archived by the owner on Oct 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
github.js
162 lines (150 loc) · 4.47 KB
/
github.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
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
var Client = new require('github');
var request = require('request');
var Q = require('q');
module.exports = GitHub;
function GitHub(settings) {
this.settings = settings;
this.client = new Client({version: '3.0.0', debug: true});
this.client.authenticate(settings.user);
this.createdRepos = {};
}
GitHub.prototype.createRepo = function (repo) {
if (this.createdRepos[repo]) return this.createdRepos[repo];
var def = Q.defer();
var self = this;
var oldLog = require('github/util').log;
require('github/util').log = function () {};
function create() {
console.log('========Create Repo=========');
return Q.nfcall(self.client.repos.createFromOrg, {
org: self.settings.organisation,
name: repo,
auto_init: true,
team_id: self.settings.team
})
.fail(function (err) {
if (err && !(err.message && /already exists/.test(err.message))) {
throw err;
}
});
}
return this.createdRepos[repo] = retry(create, 5)
.then(function () {
require('github/util').log = oldLog;
}, function (err) {
require('github/util').log = oldLog;
self.createdRepos[repo] = false;
throw err;
});
};
GitHub.prototype.createCommit = function (repo) {
return new Commit(this.client, this.settings, repo);
};
function Commit(client, settings, repo) {
this.completed = false;
this.repo = repo;
this.client = client;
this.settings = settings;
console.log('========Get Sha Latest Commit==========');
this.shaLatestCommit = retry(Q.nfbind(client.gitdata.getReference, ({
user: settings.organisation,
repo: repo,
ref: 'heads/master'
})), 3).get('object').get('sha');
this.shaBaseTree = this.shaLatestCommit
.then(function (shaLatestCommit) {
console.log('=========Get Latest Commit=========');
return retry(Q.nfbind(client.gitdata.getCommit, {
user: settings.organisation,
repo: repo,
sha: shaLatestCommit
}), 3);
}).get('tree').get('sha');
this.shaNewTree = this.shaBaseTree;
}
Commit.prototype.addFile = function (path, content) {
return this.addFiles([{
path: path,
content: content
}]);
};
Commit.prototype.addFiles = function (files) {
this.assertLive();
var settings = this.settings;
var repo = this.repo;
var client = this.client;
var res = this.shaNewTree.then(function (shaBaseTree) {
console.log('==========Create New Tree===========');
return retry(Q.nfbind(client.gitdata.createTree, {
user: settings.organisation,
repo: repo,
tree: files.map(function (file) {
return {
path: file.path,
mode: '100644',
type: 'blob',
content: file.content,
sha: sha1(file.content)
}
}),
base_tree: shaBaseTree
}), 3);
});
this.shaNewTree = res.get('sha');
return res;
};
function sha1(str) {
var crypto = require('crypto')
, shasum = crypto.createHash('sha1');
shasum.update(str);
return shasum.digest('hex');
}
Commit.prototype.complete = function (message) {
this.assertLive();
this.completed = true;
var settings = this.settings;
var repo = this.repo;
var client = this.client;
return Q.all([this.shaLatestCommit, this.shaNewTree])
.spread(function (shaLatestCommit, shaNewTree) {
console.log('=============================');
console.log('creating commit');
return retry(Q.nfbind(client.gitdata.createCommit, {
user: settings.organisation,
repo: repo,
message: message,
tree: shaNewTree,
parents: [shaLatestCommit]
}), 3);
})
.get('sha')
.then(function (shaNewCommit) {
console.log('=============================');
console.log('pushing commit');
return retry(Q.nfbind(client.gitdata.createReference, {
user: settings.organisation,
repo: repo,
ref: 'heads/master',
sha: shaNewCommit
}), 3)
.fail(function (err) {
if (err instanceof Error) throw err;
else throw new Error(err);
});
});
};
Commit.prototype.assertLive = function () {
if (this.completed)
throw new Error('You can\'t interact with the commit after it\'s completed');
};
function retry(fn, attempts, delay) {
delay = delay || 0;
return attempts < 2 ? fn() : fn()
.fail(function () {
console.log('retrying: ' + (delay + 1));
return Q.delay(Math.pow(2, delay) * 1000)
.then(function () {
return retry(fn, attempts - 1, delay + 1);
});
})
}