Skip to content

Commit 11a897b

Browse files
committed
suggested changes and some bug fixes
1 parent 81e7266 commit 11a897b

File tree

4 files changed

+94
-32
lines changed

4 files changed

+94
-32
lines changed

app/controllers/wikis.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,26 @@ const clientSecret = process.env.GITHUB_OAUTH_APP_CLIENTSECRET
1010

1111
const githubAPI = 'https://api.github.com'
1212
let accessToken = null
13+
let orgId = null
1314

1415
module.exports = {
1516

1617
getWikis: async (req, res, next) => {
1718
try {
18-
if (!accessToken) {
19+
if (!accessToken || !orgId) {
1920
const Org = await Organization.find({}).lean().exec()
20-
if (Org[0].wikis.accessToken) {
21+
if (Org[0].wikis.accessToken && Org[0].wikis.orgId) {
2122
accessToken = Org[0].wikis.accessToken
23+
orgId = Org[0].wikis.orgId
24+
WikiHelper.setOrgId(orgId)
25+
WikiHelper.setOpts(accessToken)
2226
}
2327
}
24-
if (!accessToken) {
28+
if (!accessToken || !orgId) {
2529
res.status(HttpStatus.OK).json({ wikis: 'NO_ACCESS_TOKEN' })
2630
} else {
27-
res.status(HttpStatus.OK).json({ wikis: await addPageToIndex(await fetchPagesIndex(), 'Home') })
31+
const data = await addPageToIndex(await fetchPagesIndex(), 'Home')
32+
res.status(HttpStatus.OK).json({ wikis: data })
2833
}
2934
} catch (error) {
3035
HANDLER.handleError(res, error)
@@ -37,7 +42,8 @@ module.exports = {
3742
if (!ref) {
3843
ref = 'master'
3944
}
40-
res.status(HttpStatus.OK).json({ wikis: await addPageToIndex(await fetchPagesIndex(), title, ref) })
45+
const data = await addPageToIndex(await fetchPagesIndex(), title, ref)
46+
res.status(HttpStatus.OK).json({ wikis: data })
4147
} catch (err) {
4248
res.status(HttpStatus.BAD_REQUEST).json({ Error: err.message })
4349
}
@@ -48,10 +54,12 @@ module.exports = {
4854
try {
4955
await changeFileOnRemote(title, content, `${title} changes - ${comments}`)
5056
if (title !== '_Sidebar') {
51-
res.status(HttpStatus.OK).json({ wikis: await addPageToIndex(await fetchPagesIndex(), title) })
57+
const data = await addPageToIndex(await fetchPagesIndex(), title)
58+
res.status(HttpStatus.OK).json({ wikis: data })
5259
} else {
5360
await updatePagesIndex()
54-
res.status(HttpStatus.OK).json({ wikis: await addPageToIndex(await fetchPagesIndex(), 'Home') })
61+
const data = await addPageToIndex(await fetchPagesIndex(), 'Home')
62+
res.status(HttpStatus.OK).json({ wikis: data })
5563
}
5664
} catch (err) {
5765
res.status(HttpStatus.BAD_REQUEST).json({ Error: err.message })
@@ -61,17 +69,19 @@ module.exports = {
6169
deletePage: async (req, res, next) => {
6270
const { title } = req.body
6371
try {
72+
const baseUrl = `${githubAPI}/repos/${getOrgId()}/Donut-wikis-backup`
6473
const data = {
6574
message: `${title} deleted`,
66-
sha: (await axios.get(`${githubAPI}/repos/${getOrgId()}/Donut-wikis-backup/contents/${title}.md`, getOpts())).data.sha
75+
sha: (await axios.get(`${baseUrl}/contents/${title}.md`, getOpts())).data.sha
6776
}
68-
const deleteCommit = (await axios.delete(`${githubAPI}/repos/${getOrgId()}/Donut-wikis-backup/contents/${title}.md`, {
77+
const deleteCommit = (await axios.delete(`${baseUrl}/contents/${title}.md`, {
6978
data: data,
7079
headers: getOpts().headers
7180
})).data.commit.sha
7281
const issueNumber = await WikiHelper.getFileIssueNumber(title)
73-
await axios.post(`${githubAPI}/repos/${getOrgId()}/Donut-wikis-backup/issues/${issueNumber}/comments`, { body: deleteCommit }, getOpts())
74-
await axios.patch(`${githubAPI}/repos/${getOrgId()}/Donut-wikis-backup/issues/${issueNumber}`, { title: `${title}-deleted-${deleteCommit.substring(0, 8)}` }, getOpts())
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())
7585
await updatePagesIndex()
7686
await WikiHelper.clearPageFromCache(title)
7787
res.status(HttpStatus.OK).json({ wikis: await addPageToIndex(await fetchPagesIndex(), 'Home') })
@@ -85,7 +95,8 @@ module.exports = {
8595
try {
8696
await changeFileOnRemote(title, content, `${title} initial commit - ${comments}`, true)
8797
await updatePagesIndex()
88-
res.status(HttpStatus.OK).json({ wikis: await addPageToIndex(await fetchPagesIndex(), title) })
98+
const data = await addPageToIndex(await fetchPagesIndex(), title)
99+
res.status(HttpStatus.OK).json({ wikis: data })
89100
} catch (err) {
90101
res.status(HttpStatus.BAD_REQUEST).json({ Error: err.message })
91102
}
@@ -117,9 +128,10 @@ module.exports = {
117128
accessToken = resp.data.access_token
118129
WikiHelper.setOpts(accessToken)
119130
const Org = await Organization.find({}).exec()
131+
orgId = await WikiHelper.getOrg()
120132
Org[0].wikis.accessToken = accessToken
133+
Org[0].wikis.orgId = orgId
121134
await Org[0].save()
122-
await WikiHelper.getOrg()
123135
await WikiHelper.createRepo()
124136
await updatePagesIndex()
125137
res.redirect(`${process.env.clientbaseurl}/wikis`)

app/models/Organisation.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ const orgSchema = new Schema({
196196
default: Date.now()
197197
},
198198
wikis: {
199-
accessToken: { type: String, default: null }
199+
accessToken: { type: String, default: null },
200+
orgId: { type: String, default: null }
200201
}
201202
})
202203
module.exports = mongoose.model('Organization', orgSchema)

app/routes/wikis.js

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,51 @@ const auth = require('../middleware/auth')
44
const wikisController = require('../controllers/wikis')
55
const isUnderMaintenance = require('../middleware/maintenance')
66

7-
router.get('/', isUnderMaintenance, auth, wikisController.getWikis)
8-
router.get('/oauth-callback', isUnderMaintenance, wikisController.oauthCallback)
9-
router.get('/oauth-check', isUnderMaintenance, auth, wikisController.oauthCheck)
10-
router.get('/pages', isUnderMaintenance, auth, wikisController.getPage)
11-
router.post('/pages', isUnderMaintenance, auth, wikisController.newPage)
12-
router.put('/pages', isUnderMaintenance, auth, wikisController.editPage)
13-
router.delete('/pages', isUnderMaintenance, auth, wikisController.deletePage)
7+
router.get(
8+
'/',
9+
isUnderMaintenance,
10+
auth,
11+
wikisController.getWikis
12+
)
13+
14+
router.get(
15+
'/oauth-callback',
16+
isUnderMaintenance,
17+
wikisController.oauthCallback
18+
)
19+
20+
router.get(
21+
'/oauth-check',
22+
isUnderMaintenance,
23+
auth,
24+
wikisController.oauthCheck
25+
)
26+
27+
router.get(
28+
'/pages',
29+
isUnderMaintenance,
30+
auth, wikisController.getPage
31+
)
32+
33+
router.post(
34+
'/pages',
35+
isUnderMaintenance,
36+
auth,
37+
wikisController.newPage
38+
)
39+
40+
router.put(
41+
'/pages',
42+
isUnderMaintenance,
43+
auth,
44+
wikisController.editPage
45+
)
46+
47+
router.delete(
48+
'/pages',
49+
isUnderMaintenance,
50+
auth,
51+
wikisController.deletePage
52+
)
53+
1454
module.exports = router

app/utils/wikis-helper.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const base64 = require('base-64')
44
const axios = require('axios')
55

66
const githubAPI = 'https://api.github.com'
7+
let githubAPIrepos = null
78
let opts = { headers: {} }
89
let orgId = null
910

@@ -14,6 +15,8 @@ module.exports = {
1415
getOrg: async () => {
1516
const respOrg = await axios.get(`${githubAPI}/user/orgs`, opts)
1617
orgId = respOrg.data[0].login
18+
githubAPIrepos = `${githubAPI}/repos/${orgId}/Donut-wikis-backup`
19+
return orgId
1720
},
1821

1922
getOrgId: () => orgId,
@@ -26,7 +29,7 @@ module.exports = {
2629
if (isPresentInCache) {
2730
return base64.decode(await redisClient.get(`${pageName}-${ref}`))
2831
}
29-
const page = (await axios.get(`${githubAPI}/repos/${orgId}/Donut-wikis-backup/contents/${pageName}.md?ref=${ref}`, opts)).data.content
32+
const page = (await axios.get(`${githubAPIrepos}/${pageName}.md?ref=${ref}`, opts)).data.content
3033
await redisClient.set(`${pageName}-${ref}`, page)
3134
return base64.decode(page)
3235
} catch (err) {
@@ -45,7 +48,8 @@ module.exports = {
4548

4649
updatePageHistory: async (pageName) => {
4750
try {
48-
let history = (await axios.get(`${githubAPI}/repos/${orgId}/Donut-wikis-backup/issues/${await module.exports.getFileIssueNumber(pageName)}/comments`, opts)).data
51+
const issueNumber = await module.exports.getFileIssueNumber(pageName)
52+
let history = (await axios.get(`${githubAPIrepos}/issues/${issueNumber}/comments`, opts)).data
4953
history = history.reverse()
5054
await redisClient.set(`${pageName}-history`, JSON.stringify(history))
5155
} catch (err) {
@@ -61,8 +65,9 @@ module.exports = {
6165
},
6266

6367
updatePagesIndex: async () => {
64-
const newIndex = [{ title: '_Sidebar', content: await module.exports.fetchPage('_Sidebar') }, { title: 'Home' }]
65-
const pages = (await axios.get(`${githubAPI}/repos/${orgId}/Donut-wikis-backup/contents`, opts)).data
68+
const pageCotent = await module.exports.fetchPage('_Sidebar')
69+
const newIndex = [{ title: '_Sidebar', content: pageCotent }, { title: 'Home' }]
70+
const pages = (await axios.get(`${githubAPIrepos}/contents`, opts)).data
6671
pages.forEach(ele => {
6772
const eleName = ele.name.substring(0, ele.name.indexOf('.'))
6873
if (eleName !== '_Sidebar' && eleName !== 'Home') { newIndex.push({ title: eleName }) }
@@ -97,7 +102,7 @@ module.exports = {
97102
if (await redisClient.exists(`${fileName}-IssueNumber`)) {
98103
issueNumber = await redisClient.get(`${fileName}-IssueNumber`)
99104
} else {
100-
const allIssues = (await axios.get(`${githubAPI}/repos/${orgId}/Donut-wikis-backup/issues?state=closed`, opts)).data
105+
const allIssues = (await axios.get(`${githubAPIrepos}/issues?state=closed`, opts)).data
101106
issueNumber = (allIssues.filter(issue => issue.title === fileName))[0].number
102107
await redisClient.set(`${fileName}-IssueNumber`, issueNumber)
103108
}
@@ -107,24 +112,25 @@ module.exports = {
107112
changeFileOnRemote: async (fileName, content, commitMesage, newFile = false) => {
108113
let data = { message: commitMesage, content: base64.encode(content) }
109114
if (!newFile) {
110-
data.sha = (await axios.get(`${githubAPI}/repos/${orgId}/Donut-wikis-backup/contents/${fileName}.md`, opts)).data.sha
115+
data.sha = (await axios.get(`${githubAPIrepos}/contents/${fileName}.md`, opts)).data.sha
111116
}
112117
try {
113-
const commit = (await axios.put(`${githubAPI}/repos/${orgId}/Donut-wikis-backup/contents/${fileName}.md`, data, opts)).data.commit.sha
118+
const commit = (await axios.put(`${githubAPIrepos}/contents/${fileName}.md`, data, opts)).data.commit.sha
114119
if (newFile) {
115120
// open an issue
116121
data = { title: fileName, body: 'Issue opened by Donut to keep track of commits affecting this file.' }
117-
const issueNumber = (await axios.post(`${githubAPI}/repos/${orgId}/Donut-wikis-backup/issues`, data, opts)).data.number
122+
const issueNumber = (await axios.post(`${githubAPIrepos}/issues`, data, opts)).data.number
118123
redisClient.set(`${fileName}-IssueNumber`, issueNumber)
119-
await axios.patch(`${githubAPI}/repos/${orgId}/Donut-wikis-backup/issues/${issueNumber}`, { state: 'closed' }, opts)
124+
await axios.patch(`${githubAPIrepos}/issues/${issueNumber}`, { state: 'closed' }, opts)
120125
}
121126
await redisClient.set(`${fileName}-master`, base64.encode(content))
122127
// comment the sha of the commit and comments on the issue
123128
const commentBody = {
124129
commit: commit,
125130
comment: commitMesage
126131
}
127-
await axios.post(`${githubAPI}/repos/${orgId}/Donut-wikis-backup/issues/${await module.exports.getFileIssueNumber(fileName)}/comments`, { body: JSON.stringify(commentBody) }, opts)
132+
const issueNumber = await module.exports.getFileIssueNumber(fileName)
133+
await axios.post(`${githubAPIrepos}/issues/${issueNumber}/comments`, { body: JSON.stringify(commentBody) }, opts)
128134
await module.exports.updatePageHistory(fileName)
129135
} catch (err) {
130136
console.log(err)
@@ -149,7 +155,10 @@ module.exports = {
149155
}
150156
}
151157
},
152-
setOrgId: (id) => { orgId = id },
158+
setOrgId: (id) => {
159+
orgId = id
160+
githubAPIrepos = `${githubAPI}/repos/${orgId}/Donut-wikis-backup`
161+
},
153162
setOpts: (token) => { opts.headers.Authorization = `token ${token}` },
154163
getUser: async () => (await axios.get(`${githubAPI}/user`, opts)).data.login
155164
}

0 commit comments

Comments
 (0)