|
| 1 | +const axios = require('axios') |
| 2 | +const HttpStatus = require('http-status-codes') |
| 3 | +const WikiHelper = require('../utils/wikis-helper') |
| 4 | +const HANDLER = require('../utils/response-helper') |
| 5 | +const Organization = require('../models/Organisation') |
| 6 | +const { changeFileOnRemote, addPageToIndex, fetchPagesIndex, updatePagesIndex, getOpts, getOrgId } = WikiHelper |
| 7 | + |
| 8 | +const clientId = process.env.GITHUB_OAUTH_APP_CLIENTID |
| 9 | +const clientSecret = process.env.GITHUB_OAUTH_APP_CLIENTSECRET |
| 10 | + |
| 11 | +const githubAPI = 'https://api.github.com' |
| 12 | +let accessToken = null |
| 13 | +let orgId = null |
| 14 | + |
| 15 | +module.exports = { |
| 16 | + |
| 17 | + getWikis: async (req, res, next) => { |
| 18 | + try { |
| 19 | + if (!accessToken || !orgId) { |
| 20 | + const Org = await Organization.find({}).lean().exec() |
| 21 | + if (Org[0].wikis.accessToken && Org[0].wikis.orgId) { |
| 22 | + accessToken = Org[0].wikis.accessToken |
| 23 | + orgId = Org[0].wikis.orgId |
| 24 | + WikiHelper.setOrgId(orgId) |
| 25 | + WikiHelper.setOpts(accessToken) |
| 26 | + } |
| 27 | + } |
| 28 | + if (!accessToken || !orgId) { |
| 29 | + res.status(HttpStatus.OK).json({ wikis: 'NO_ACCESS_TOKEN' }) |
| 30 | + } else { |
| 31 | + const data = await addPageToIndex(await fetchPagesIndex(), 'Home') |
| 32 | + res.status(HttpStatus.OK).json({ wikis: data }) |
| 33 | + } |
| 34 | + } catch (error) { |
| 35 | + HANDLER.handleError(res, error) |
| 36 | + } |
| 37 | + }, |
| 38 | + |
| 39 | + getPage: async (req, res, next) => { |
| 40 | + try { |
| 41 | + let { title, ref } = req.query |
| 42 | + if (!ref) { |
| 43 | + ref = 'master' |
| 44 | + } |
| 45 | + const data = await addPageToIndex(await fetchPagesIndex(), title, ref) |
| 46 | + res.status(HttpStatus.OK).json({ wikis: data }) |
| 47 | + } catch (err) { |
| 48 | + res.status(HttpStatus.BAD_REQUEST).json({ Error: err.message }) |
| 49 | + } |
| 50 | + }, |
| 51 | + |
| 52 | + editPage: async (req, res, next) => { |
| 53 | + const { title, content, comments } = req.body |
| 54 | + try { |
| 55 | + await changeFileOnRemote(title, content, `${title} changes - ${comments}`) |
| 56 | + if (title !== '_Sidebar') { |
| 57 | + const data = await addPageToIndex(await fetchPagesIndex(), title) |
| 58 | + res.status(HttpStatus.OK).json({ wikis: data }) |
| 59 | + } else { |
| 60 | + await updatePagesIndex() |
| 61 | + const data = await addPageToIndex(await fetchPagesIndex(), 'Home') |
| 62 | + res.status(HttpStatus.OK).json({ wikis: data }) |
| 63 | + } |
| 64 | + } catch (err) { |
| 65 | + res.status(HttpStatus.BAD_REQUEST).json({ Error: err.message }) |
| 66 | + } |
| 67 | + }, |
| 68 | + |
| 69 | + deletePage: async (req, res, next) => { |
| 70 | + const { title } = req.body |
| 71 | + try { |
| 72 | + const baseUrl = `${githubAPI}/repos/${getOrgId()}/Donut-wikis-backup` |
| 73 | + const data = { |
| 74 | + message: `${title} deleted`, |
| 75 | + sha: (await axios.get(`${baseUrl}/contents/${title}.md`, getOpts())).data.sha |
| 76 | + } |
| 77 | + const deleteCommit = (await axios.delete(`${baseUrl}/contents/${title}.md`, { |
| 78 | + data: data, |
| 79 | + headers: getOpts().headers |
| 80 | + })).data.commit.sha |
| 81 | + const issueNumber = await WikiHelper.getFileIssueNumber(title) |
| 82 | + await axios.post(`${baseUrl}/issues/${issueNumber}/comments`, { body: deleteCommit }, getOpts()) |
| 83 | + const newIssueTitle = `${title}-deleted-${deleteCommit.substring(0, 8)}` |
| 84 | + await axios.patch(`${baseUrl}/issues/${issueNumber}`, { title: newIssueTitle }, getOpts()) |
| 85 | + await updatePagesIndex() |
| 86 | + await WikiHelper.clearPageFromCache(title) |
| 87 | + res.status(HttpStatus.OK).json({ wikis: await addPageToIndex(await fetchPagesIndex(), 'Home') }) |
| 88 | + } catch (err) { |
| 89 | + res.status(HttpStatus.BAD_REQUEST).json({ Error: err.message }) |
| 90 | + } |
| 91 | + }, |
| 92 | + |
| 93 | + newPage: async (req, res, next) => { |
| 94 | + const { title, content, comments } = req.body |
| 95 | + try { |
| 96 | + await changeFileOnRemote(title, content, `${title} initial commit - ${comments}`, true) |
| 97 | + await updatePagesIndex() |
| 98 | + const data = await addPageToIndex(await fetchPagesIndex(), title) |
| 99 | + res.status(HttpStatus.OK).json({ wikis: data }) |
| 100 | + } catch (err) { |
| 101 | + res.status(HttpStatus.BAD_REQUEST).json({ Error: err.message }) |
| 102 | + } |
| 103 | + }, |
| 104 | + |
| 105 | + oauthCheck: async (req, res, next) => { |
| 106 | + if (!accessToken) { |
| 107 | + console.log('redirected to github auth') |
| 108 | + res.status(HttpStatus.OK).json({ |
| 109 | + redirect: true, |
| 110 | + redirect_url: `https://github.com/login/oauth/authorize?client_id=${clientId}&scope=repo` |
| 111 | + }) |
| 112 | + } else { |
| 113 | + res.status(HttpStatus.OK).json({ |
| 114 | + redirect: false |
| 115 | + }) |
| 116 | + } |
| 117 | + }, |
| 118 | + |
| 119 | + oauthCallback: async (req, res, next) => { |
| 120 | + const body = { |
| 121 | + client_id: clientId, |
| 122 | + client_secret: clientSecret, |
| 123 | + code: req.query.code |
| 124 | + } |
| 125 | + const opts = { headers: { accept: 'application/json' } } |
| 126 | + try { |
| 127 | + const resp = await axios.post('https://github.com/login/oauth/access_token', body, opts) |
| 128 | + accessToken = resp.data.access_token |
| 129 | + WikiHelper.setOpts(accessToken) |
| 130 | + const Org = await Organization.find({}).exec() |
| 131 | + orgId = await WikiHelper.getOrg() |
| 132 | + Org[0].wikis.accessToken = accessToken |
| 133 | + Org[0].wikis.orgId = orgId |
| 134 | + await Org[0].save() |
| 135 | + await WikiHelper.createRepo() |
| 136 | + await updatePagesIndex() |
| 137 | + res.redirect(`${process.env.clientbaseurl}/wikis`) |
| 138 | + } catch (err) { |
| 139 | + res.status(500).json({ message: err.message }) |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments