-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgithub-extended.js
More file actions
259 lines (229 loc) · 9.01 KB
/
github-extended.js
File metadata and controls
259 lines (229 loc) · 9.01 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
254
255
256
257
258
259
'use strict';
import GithubApi from 'github-api';
/**
* The class that extends Github.js
*
* @extends GithubApi
*/
export
default class Github extends GithubApi {
/**
* @constructor
* @param {Object} options The object containing the information to work with the GitHub API
* @param {string} options.username The username used on GitHub
* @param {string} options.password The password of the GitHub account
* @param {string} options.auth The type of authentication to use. It can be either `basic` or `oauth`
* @param {string} options.token The token to access the GitHub API
*/
constructor(options) {
super(options);
let superGetRepo = this.getRepo;
let request = this.request || this._request; // jscs:ignore disallowDanglingUnderscores
/**
* Returns an object representing a specific repository
*
* @param {string} user The username that possesses the repository
* @param {string} repo The name of the repository to work on
*
* @returns {Object}
*/
this.getRepo = (user, repo) => {
let repository = superGetRepo(user, repo);
let superRemove = repository.remove;
let superFork = repository.fork;
function getRepositoryInfo(repository) {
return new Promise((resolve, reject) => {
repository.show((error, repo) => {
if (error) {
reject(error);
}
resolve(repo);
});
});
}
/**
* Searches files and folders
*
* @param {string} string The string to search
* @param {Object} [options={}] Possible options
* @param {string} [options.branch] The name of the branch in which the search must be performed
* @param {boolean} [options.caseSensitive=false] If the search must be case sensitive
* @param {boolean} [options.excludeFiles=false] If the result must exclude files
* @param {boolean} [options.excludeFolders=false] If the result must exclude folders
*
* @returns {Promise}
*/
repository.search = (string, options = {}) => {
const FILE = 'blob';
const FOLDER = 'tree';
options = Object.assign({
branch: 'master',
caseSensitive: false,
excludeFiles: false,
excludeFolders: false
}, options);
return new Promise((resolve, reject) => {
repository.getSha(options.branch, '', (error, sha) => {
if (error) {
reject(error);
}
resolve(sha);
});
})
.then(sha => {
return new Promise((resolve, reject) => {
repository.getTree(`${sha}?recursive=true`, (error, list) => {
if (error) {
// No matches
if (error.error === 404) {
resolve([]);
} else {
reject(error);
}
}
resolve(list);
});
});
})
.then(list => {
let regex = new RegExp(string, options.caseSensitive ? '' : 'i');
return list.filter(content => {
let fileCondition = options.excludeFiles ? content.type !== FILE : true;
let folderCondition = options.excludeFolders ? content.type !== FOLDER : true;
let extractName = (path) => path.substring(path.lastIndexOf('/') + 1);
return fileCondition && folderCondition && regex.test(extractName(content.path));
});
});
};
/**
* Merges a pull request
*
* @param {Object} pullRequest The pull request to merge
* @param {Object} [options={}] Possible options
* @param {string} [options.commitMessage] The commit message for the merge
*
* @returns {Promise}
*/
repository.mergePullRequest = (pullRequest, options = {}) => {
options = Object.assign(
{
commitMessage: `Merged pull request gh-${pullRequest.number}`
},
options
);
return getRepositoryInfo(repository)
.then(repositoryInfo => {
return new Promise((resolve, reject) => {
request(
'PUT',
`/repos/${repositoryInfo.full_name}/pulls/${pullRequest.number}/merge`, // jscs:ignore
{
commit_message: options.commitMessage, // jscs:ignore
sha: pullRequest.head.sha
},
(error, mergeInfo) => {
if (error) {
reject(error);
}
resolve(mergeInfo);
}
);
});
});
};
/**
* Deletes a file or a folder and all of its content from a given branch
*
* @param {string} [branchName='master'] The name of the branch in which the deletion must be performed
* @param {string} [path=''] The path of the file or the folder to delete
*
* @returns {Promise}
*/
repository.remove = (branchName = 'master', path = '') => {
function removeFile(branchName, path) {
return new Promise((resolve, reject) => {
superRemove(branchName, path, error => {
if (error) {
reject(error);
}
resolve();
});
});
}
function removeFolder() {
return new Promise((resolve, reject) => {
repository.getRef(`heads/${branchName}`, (error, sha) => {
if (error) {
reject(error);
}
resolve(sha);
});
})
.then(sha => {
return new Promise((resolve, reject) => {
repository.getTree(`${sha}?recursive=true`, (error, tree) => {
if (error) {
reject(error);
}
resolve(tree);
});
});
})
.then(tree => {
let filesPromises = Promise.resolve();
// Filters all items that aren't in the path of interest and aren't files
// and delete them.
tree
.filter(item => item.path.indexOf(path) === 0 && item.type === 'blob')
.map(item => item.path)
.forEach(path => {
filesPromises = filesPromises.then(() => removeFile(branchName, path));
});
return filesPromises;
});
}
// Remove any trailing slash from the path.
// GitHub does not accept it even when dealing with folders.
path = path.replace(/\/$/, '');
let removeFilePromise = removeFile(branchName, path);
return removeFilePromise
.then(
() => removeFilePromise,
error => {
// If the operation fails because the path specified is that of a folder
// keep going to retrieve the files recursively
if (error.error !== 422) {
throw error;
}
return removeFolder();
});
};
/**
* Creates a fork of the repository
*
* @returns {Promise}
*/
repository.fork = () => {
return new Promise((resolve, reject) => {
superFork((err, forkInfo) => {
function pollFork(fork) {
fork.contents('master', '', (err, contents) => {
if (contents) {
resolve(forkInfo);
} else {
setTimeout(pollFork.bind(null, fork), 250);
}
});
}
if (err) {
reject(err);
} else {
pollFork(superGetRepo(options.username, repo));
}
});
});
};
return repository;
};
}
}