Skeletons for GitLab actions.

This commit is contained in:
Jason Croft 2016-05-09 17:07:23 -04:00
parent 476cabd109
commit 521f96fb11
1 changed files with 83 additions and 0 deletions

View File

@ -321,6 +321,17 @@ function actionGist(req, res, note) {
res.redirect("https://github.com/login/oauth/authorize?" + query); res.redirect("https://github.com/login/oauth/authorize?" + query);
} }
function actionSnippet(req, res, note) {
var data = {
client_id: config.gitlab.clientID,
redirect_uri: config.serverurl + '/auth/github/callback/' + LZString.compressToBase64(note.id) + '/gist',
scope: "snippet",
state: shortId.generate()
};
var query = querystring.stringify(data);
res.redirect(config.gitlab.baseURL + "/login/oauth/authorize?" + query);
}
function noteActions(req, res, next) { function noteActions(req, res, next) {
var noteId = req.params.noteId; var noteId = req.params.noteId;
findNote(req, res, function (note) { findNote(req, res, function (note) {
@ -378,6 +389,21 @@ function githubActions(req, res, next) {
}); });
} }
function gitlabActions(req, res, next) {
var noteId = req.params.noteId;
findNote(req, res, function (note) {
var action = req.params.action;
switch (action) {
case "gist":
gitlabActionSnippet(req, res, note);
break;
default:
res.redirect(config.serverurl + '/' + noteId);
break;
}
});
}
function githubActionGist(req, res, note) { function githubActionGist(req, res, note) {
var code = req.query.code; var code = req.query.code;
var state = req.query.state; var state = req.query.state;
@ -435,6 +461,63 @@ function githubActionGist(req, res, note) {
} }
} }
function gitlabActionSnippet(req, res, note) {
var code = req.query.code;
var state = req.query.state;
if (!code || !state) {
return response.errorForbidden(res);
} else {
var data = {
client_id: config.gitlab.clientID,
client_secret: config.gitlab.clientSecret,
code: code,
state: state
}
var auth_url = config.gitlab.baseURL + '/login/oauth/access_token';
request({
url: auth_url,
method: "POST",
json: data
}, function (error, httpResponse, body) {
if (!error && httpResponse.statusCode == 200) {
var access_token = body.access_token;
if (access_token) {
var content = LZString.decompressFromBase64(note.content);
var title = models.Note.decodeTitle(note.title);
var filename = title.replace('/', ' ') + '.md';
var gist = {
"files": {}
};
gist.files[filename] = {
"content": content
};
var gist_url = "https://api.gitlab.com/snippets";
request({
url: gist_url,
headers: {
'User-Agent': 'HackMD',
'Authorization': 'token ' + access_token
},
method: "POST",
json: gist
}, function (error, httpResponse, body) {
if (!error && httpResponse.statusCode == 201) {
res.setHeader('referer', '');
res.redirect(body.html_url);
} else {
return response.errorForbidden(res);
}
});
} else {
return response.errorForbidden(res);
}
} else {
return response.errorForbidden(res);
}
})
}
}
function showPublishSlide(req, res, next) { function showPublishSlide(req, res, next) {
findNote(req, res, function (note) { findNote(req, res, function (note) {
note.increment('viewcount').then(function (note) { note.increment('viewcount').then(function (note) {