@@ -4,6 +4,7 @@ const base64 = require('base-64')
44const axios = require ( 'axios' )
55
66const githubAPI = 'https://api.github.com'
7+ let githubAPIrepos = null
78let opts = { headers : { } }
89let 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