diff options
author | Jason Croft | 2016-05-09 17:07:23 -0400 |
---|---|---|
committer | Jason Croft | 2016-05-09 17:07:23 -0400 |
commit | 521f96fb11af9b2669c6d7002f983d3e7cc99e79 (patch) | |
tree | 0de534a5ae931ef60097a8582fe932551ce1e510 /lib | |
parent | 476cabd10952f026f331754baf9846b1aad917cd (diff) |
Skeletons for GitLab actions.
Diffstat (limited to '')
-rw-r--r-- | lib/response.js | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/lib/response.js b/lib/response.js index 2114c99b..63099b8d 100644 --- a/lib/response.js +++ b/lib/response.js @@ -321,6 +321,17 @@ function actionGist(req, res, note) { 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) { var noteId = req.params.noteId; 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) { var code = req.query.code; 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) { findNote(req, res, function (note) { note.increment('viewcount').then(function (note) { |