Allow importing from GitLab snippet

This commit is contained in:
Jason Croft 2016-05-11 17:05:25 -04:00
parent 17daf32239
commit 3dd3e6bc35
1 changed files with 48 additions and 21 deletions

View File

@ -243,6 +243,15 @@ var lastInfo = {
}; };
var personalInfo = {}; var personalInfo = {};
var onlineUsers = []; var onlineUsers = [];
var fileTypes = {
"pl": "perl",
"cgi": "perl",
"js": "javascript",
"php": "php",
"sh": "bash",
"rb": "ruby",
"html": "html"
}
//editor settings //editor settings
var textit = document.getElementById("textit"); var textit = document.getElementById("textit");
@ -1192,7 +1201,22 @@ ui.toolbar.import.gist.click(function () {
}); });
//import from snippet //import from snippet
ui.toolbar.import.snippet.click(function () { ui.toolbar.import.snippet.click(function () {
//na $.get(serverurl + '/gitlab')
.success(function (data) {
$("#snippetImportModalAccessToken").val(data.accesstoken);
$("#snippetImportModalBaseURL").val(data.baseURL);
$("#snippetImportModalContent").prop('disabled', false);
$("#snippetImportModalConfirm").prop('disabled', false);
$("#snippetImportModalLoading").hide();
$("#snippetImportModal").modal('toggle');
})
.error(function (data) {
showMessageModal('<i class="fa fa-gitlab"></i> Import from Snippet', 'Unable to fetch gitlab parameters :(', '', '', false);
})
.complete(function () {
//na
});
return false;
}); });
//import from clipboard //import from clipboard
ui.toolbar.import.clipboard.click(function () { ui.toolbar.import.clipboard.click(function () {
@ -1370,32 +1394,35 @@ $("#snippetImportModalConfirm").click(function () {
if (!snippeturl) return; if (!snippeturl) return;
$('#snippetImportModal').modal('hide'); $('#snippetImportModal').modal('hide');
$("#snippetImportModalContent").val(''); $("#snippetImportModalContent").val('');
if (!isValidURL(snippeturl)) { if (!/^.+\/snippets\/.+$/.test(snippeturl)) {
showMessageModal('<i class="fa fa-gitlab"></i> Import from Snippet', 'Not a valid URL :(', '', '', false); showMessageModal('<i class="fa fa-github"></i> Import from Snippet', 'Not a valid Snippet URL :(', '', '', false);
return;
} else { } else {
// TODO: Validate against config.gitlab.baseURL
ui.spinner.show(); ui.spinner.show();
$.get(snippeturl) var accessToken = '?access_token=' + $("#snippetImportModalAccessToken").val();
.success(function (data) { var fullURL = $("#snippetImportModalBaseURL").val() + '/api/v3' + snippeturl;
if (data.files) { $.get(fullURL + accessToken)
var contents = ""; .success(function(data) {
Object.keys(data.files).forEach(function (key) { var content = '# ' + (data.title || "Snippet Import");
contents += key; var fileInfo = data.file_name.split('.');
contents += '\n---\n'; $.get(fullURL + '/raw' + accessToken)
contents += data.files[key].content; .success(function (raw) {
contents += '\n\n'; if (raw) {
content += "\n\n```";
content += fileTypes[fileInfo[1]] + "=\n";
content += raw;
content += "\n```";
replaceAll(content);
}
})
.error(function (data) {
showMessageModal('<i class="fa fa-gitlab"></i> Import from Snippet', 'Not a valid Snippet URL :(', '', JSON.stringify(data), false);
})
.complete(function () {
ui.spinner.hide();
}); });
replaceAll(contents);
} else {
showMessageModal('<i class="fa fa-gitlab"></i> Import from Snippet', 'Unable to fetch snippet files :(', '', '', false);
}
}) })
.error(function (data) { .error(function (data) {
showMessageModal('<i class="fa fa-gitlab"></i> Import from Snippet', 'Not a valid Snippet URL :(', '', JSON.stringify(data), false); showMessageModal('<i class="fa fa-gitlab"></i> Import from Snippet', 'Not a valid Snippet URL :(', '', JSON.stringify(data), false);
})
.complete(function () {
ui.spinner.hide();
}); });
} }
}); });