-
Notifications
You must be signed in to change notification settings - Fork 4
/
GitHubManager.js
254 lines (209 loc) · 9.97 KB
/
GitHubManager.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
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
/*
* Copyright (c) 2013 Bernhard Sirlinger. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint sloppy: true, vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 100 */
/*global define, $, brackets, Mustache, Bloodhound*/
define(function (require, exports, module) {
var Dialogs = brackets.getModule("widgets/Dialogs"),
StatusBar = brackets.getModule("widgets/StatusBar"),
FileSystem = brackets.getModule("filesystem/FileSystem"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
NodeDomain = brackets.getModule("utils/NodeDomain"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
_ = brackets.getModule("thirdparty/lodash"),
Octokit = require("octokit"),
cloneDialog = require("text!templates/clone-dialog.html"),
loginDialog = require("text!templates/login-dialog.html"),
cloneDialogData = require("text!json/clone-dialog.json"),
loginDialogData = require("text!json/login-dialog.json"),
prefs = PreferencesManager.getExtensionPrefs("bsirlinger.github-access");
var domain = new NodeDomain("github-access", ExtensionUtils.getModulePath(module, "node/GitHubAccessDomain")),
globalToken;
function GitHubManager() {
}
GitHubManager.prototype.cloneRepo = function (repoName, repo, branch, targetDir) {
domain.exec("cloneRepo", globalToken, repoName, branch, targetDir)
.done(function (result) {
console.log(result);
}).fail(function (err) {
console.error(err);
});
};
GitHubManager.prototype.addSelectMenueForArray = function ($element, array) {
var htmlString = "",
length = array.length,
i;
for (i = 0; i < length; i++) {
htmlString += "<option value='" + array[i] + "'>" + array[i] + "</option>";
}
$element.html(htmlString);
};
GitHubManager.prototype.buildBranchArray = function (rawBranches) {
var branches = [];
branches = _.flatten(rawBranches);
return _.difference(_.unique(branches.sort(), true), ["refs", "heads"]);
};
GitHubManager.prototype.openCloneDialog = function (gh, self) {
var templateVars,
dlg,
repo,
$dlg,
bloodhound = new Bloodhound({
local: prefs.get("rememberedRepos"),
datumTokenizer: function (d) {
return Bloodhound.tokenizers.whitespace(d);
},
queryTokenizer: Bloodhound.tokenizers.whitespace
}),
promise = bloodhound.initialize();
templateVars = JSON.parse(cloneDialogData);
dlg = Dialogs.showModalDialogUsingTemplate(Mustache.render(cloneDialog, templateVars), false);
$dlg = dlg.getElement();
promise.done(function () {
$dlg.find("input.repo").typeahead(null, {
name: "repo-hints",
source: function (query, cb) {
bloodhound.get(query, function (r) {
cb(r);
});
},
displayKey: function (o) {
return o;
},
highlight: true
});
});
$dlg.find("button.get-branches").on("click", function (event) {
$dlg.find("input.repo").typeahead("close");
var value = $dlg.find(".repo.tt-input").val().trim();
if (value !== "") {
try {
$(".errors").html("");
repo = gh.getRepo(value.split("/")[0], value.split("/")[1]);
} catch (e) {
console.error(e.message);
$(".errors").append("<div class='error'>Error: " + e.message + " Repo must be in the Format (User/Repository) </div>");
return;
}
StatusBar.showBusyIndicator(true);
repo.getBranches().then($.proxy(function (branches) {
var temp = prefs.get("rememberedRepos");
if (!_.contains(temp, value)) {
temp.push(value);
}
prefs.set("rememberedRepos", temp);
prefs.save();
branches = this.buildBranchArray(branches);
var $step = $dlg.find("div.step1"),
$input;
this.addSelectMenueForArray($step.find("select.branch-selection"), branches);
$step.css("display", "block");
$step.find("select.branch-selection").chosen();
$input = $step.find("input.target-path");
StatusBar.hideBusyIndicator();
$step.find("button.select-folder").on("click", function () {
FileSystem.showOpenDialog(false, true, "Choose target path", null, [], function (error, dir) {
if (!error && dir) {
$input.val(dir);
$input.change();
}
});
});
$input.on("keydown change", function () {
if ($(this).val().trim() !== "") {
$dlg.find(".githubaccess-clone").removeAttr("disabled");
} else {
$dlg.find(".githubaccess-clone").attr("disabled");
}
});
}, self), function (answer) {
var message;
try {
message = JSON.parse(answer.error).message;
} catch (err) {
console.error(err.message);
message = "An internal Error occured.";
}
$dlg.find(".errors").append("<div class='error'>Error: " + message + "</div>");
});
}
});
$dlg.on("buttonClick", function (event, id) {
if (id === "cancel") {
Dialogs.cancelModalDialogIfOpen("github-access", "cancel");
} else if (id === "clone") {
var value = $dlg.find(".repo.tt-input").val().trim();
self.cloneRepo(value, repo, $dlg.find(".branch-selection").val(), $dlg.find("div.step1").find("input.target-path").val());
Dialogs.cancelModalDialogIfOpen("github-access", "clone");
}
});
};
GitHubManager.prototype.checkLoginData = function ($element) {
var valid = ($element.find("input.oauth-token").val().trim().length === 40);
return valid;
};
GitHubManager.prototype.init = function () {
var gh,
self = new GitHubManager(),
repo,
templateVars,
dlg,
$dlg,
temp;
temp = prefs.get("token");
if (temp.length === 40) {
gh = new Octokit({
token: temp
});
globalToken = temp;
self.openCloneDialog(gh, self);
return;
}
templateVars = JSON.parse(loginDialogData);
dlg = Dialogs.showModalDialogUsingTemplate(Mustache.render(loginDialog, templateVars), false);
$dlg = dlg.getElement();
$dlg.on("buttonClick", function (event, id) {
if (id === "cancel") {
Dialogs.cancelModalDialogIfOpen("github-access-login", "cancel");
} else if (id === "login") {
var valid = self.checkLoginData($dlg);
if (valid) {
var token = $dlg.find("input.oauth-token").val();
globalToken = token;
gh = new Octokit({
token: token
});
if ($dlg.find("input:checked").size() === 1) {
prefs.set("token", token);
prefs.save();
}
Dialogs.cancelModalDialogIfOpen("github-access-login", "login");
self.openCloneDialog(gh, self);
}
}
});
};
prefs.definePreference("token", "string", "");
prefs.definePreference("rememberedRepos", "array", []);
/*Exporting GitHubManager*/
exports.GitHubManager = GitHubManager;
});