Skip to content

Commit d5028b1

Browse files
committed
feat: git init + commands
1 parent 830ddd9 commit d5028b1

File tree

8 files changed

+13991
-19
lines changed

8 files changed

+13991
-19
lines changed

dev/build/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ FROM node:10.15-alpine
2929
LABEL maintainer="requarks.io"
3030

3131
RUN apk update && \
32-
apk add bash curl git openssh supervisor --no-cache && \
32+
apk add bash curl git openssh gnupg supervisor --no-cache && \
3333
rm -rf /var/cache/apk/* && \
3434
mkdir -p /wiki && \
3535
mkdir -p /logs

dev/docker/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ FROM node:10-alpine
55
LABEL maintainer "requarks.io"
66

77
RUN apk update && \
8-
apk add bash curl git python make g++ nano --no-cache && \
8+
apk add bash curl git python make g++ nano openssh gnupg --no-cache && \
99
mkdir -p /wiki
1010

1111
WORKDIR /wiki

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@
293293
"zxcvbn": "4.4.2"
294294
},
295295
"resolutions": {
296-
"terser": "3.14.1"
296+
"terser": "3.14.1",
297+
"apollo-server/**/core-js": "3.0.0-beta.11",
298+
"apollo-server-express/**/core-js": "3.0.0-beta.11"
297299
},
298300
"browserslist": [
299301
"> 1%",

server/graph/resolvers/storage.js

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module.exports = {
4848
}, {})
4949
}).where('key', tgt.key)
5050
}
51+
await WIKI.models.storage.initTargets()
5152
return {
5253
responseResult: graphHelper.generateSuccess('Storage targets updated successfully')
5354
}

server/models/storage.js

+27-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const commonHelper = require('../helpers/common')
77

88
/* global WIKI */
99

10+
let targets = []
11+
1012
/**
1113
* Storage model
1214
*/
@@ -93,18 +95,34 @@ module.exports = class Storage extends Model {
9395
}
9496
}
9597

98+
static async initTargets() {
99+
targets = await WIKI.models.storage.query().where('isEnabled', true)
100+
try {
101+
for(let target of targets) {
102+
target.fn = require(`../modules/storage/${target.key}/storage`)
103+
await target.fn.init.call({
104+
config: target.config,
105+
mode: target.mode
106+
})
107+
}
108+
} catch (err) {
109+
WIKI.logger.warn(err)
110+
throw err
111+
}
112+
}
113+
96114
static async pageEvent({ event, page }) {
97-
const targets = await WIKI.models.storage.query().where('isEnabled', true)
98-
if (targets && targets.length > 0) {
99-
_.forEach(targets, target => {
100-
WIKI.queue.job.syncStorage.add({
101-
event,
102-
target,
115+
try {
116+
for(let target of targets) {
117+
await target.fn[event].call({
118+
config: target.config,
119+
mode: target.mode,
103120
page
104-
}, {
105-
removeOnComplete: true
106121
})
107-
})
122+
}
123+
} catch (err) {
124+
WIKI.logger.warn(err)
125+
throw err
108126
}
109127
}
110128
}

server/modules/storage/git/definition.yml

+15
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,18 @@ props:
4141
type: String
4242
title: Password / PAT
4343
hint: Basic Authentication Only
44+
localRepoPath:
45+
type: String
46+
title: Local Repository Path
47+
default: './data/repo'
48+
hint: 'Path where the local git repository will be created.'
49+
defaultEmail:
50+
type: String
51+
title: Default Author Email
52+
default: '[email protected]'
53+
hint: 'Used as fallback in case the author of the change is not present.'
54+
defaultName:
55+
type: String
56+
title: Default Author Name
57+
default: 'John Smith'
58+
hint: 'Used as fallback in case the author of the change is not present.'

server/modules/storage/git/storage.js

+60-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const sgit = require('simple-git/promise')
33
const fs = require('fs-extra')
44
const _ = require('lodash')
55

6-
const repoPath = path.join(WIKI.ROOTPATH, 'data/repo')
6+
let repoPath = path.join(process.cwd(), 'data/repo')
77

88
/**
99
* Get file extension based on content type
@@ -50,37 +50,87 @@ module.exports = {
5050

5151
},
5252
async init() {
53+
WIKI.logger.info('(STORAGE/GIT) Initializing...')
54+
repoPath = path.resolve(WIKI.ROOTPATH, this.config.localRepoPath)
5355
await fs.ensureDir(repoPath)
5456
const git = sgit(repoPath)
5557

5658
// Initialize repo (if needed)
59+
WIKI.logger.info('(STORAGE/GIT) Checking repository state...')
5760
const isRepo = await git.checkIsRepo()
5861
if (!isRepo) {
62+
WIKI.logger.info('(STORAGE/GIT) Initializing local repository...')
5963
await git.init()
6064
}
6165

62-
// Checkout branch
63-
await git.checkout(this.config.branch)
66+
// Set default author
67+
await git.raw(['config', '--local', 'user.email', this.config.defaultEmail])
68+
await git.raw(['config', '--local', 'user.name', this.config.defaultName])
69+
70+
// Purge existing remotes
71+
WIKI.logger.info('(STORAGE/GIT) Listing existing remotes...')
72+
const remotes = await git.getRemotes()
73+
if (remotes.length > 0) {
74+
WIKI.logger.info('(STORAGE/GIT) Purging existing remotes...')
75+
for(let remote of remotes) {
76+
await git.removeRemote(remote.name)
77+
}
78+
}
6479

6580
// Add remote
81+
WIKI.logger.info('(STORAGE/GIT) Setting SSL Verification config...')
6682
await git.raw(['config', '--local', '--bool', 'http.sslVerify', _.toString(this.config.verifySSL)])
6783
switch (this.config.authType) {
6884
case 'ssh':
85+
WIKI.logger.info('(STORAGE/GIT) Setting SSH Command config...')
6986
await git.addConfig('core.sshCommand', `ssh -i "${this.config.sshPrivateKeyPath}" -o StrictHostKeyChecking=no`)
87+
WIKI.logger.info('(STORAGE/GIT) Adding origin remote via SSH...')
7088
await git.addRemote('origin', this.config.repoUrl)
7189
break
7290
default:
91+
WIKI.logger.info('(STORAGE/GIT) Adding origin remote via HTTPS...')
7392
await git.addRemote('origin', `https://${this.config.basicUsername}:${this.config.basicPassword}@${this.config.repoUrl}`)
7493
break
7594
}
95+
96+
// Fetch updates for remote
97+
WIKI.logger.info('(STORAGE/GIT) Fetch updates from remote...')
98+
await git.raw(['remote', 'update', 'origin'])
99+
100+
// Checkout branch
101+
const branches = await git.branch()
102+
if (!_.includes(branches.all, this.config.branch) && !_.includes(branches.all, `remotes/origin/${this.config.branch}`)) {
103+
throw new Error('Invalid branch! Make sure it exists on the remote first.')
104+
}
105+
WIKI.logger.info(`(STORAGE/GIT) Checking out branch ${this.config.branch}...`)
106+
await git.checkout(this.config.branch)
107+
108+
// Pull rebase
109+
if (_.includes(['sync', 'pull'], this.mode)) {
110+
WIKI.logger.info(`(STORAGE/GIT) Performing pull rebase from origin on branch ${this.config.branch}...`)
111+
await git.pull('origin', this.config.branch, ['--rebase'])
112+
}
113+
114+
// Push
115+
if (_.includes(['sync', 'push'], this.mode)) {
116+
WIKI.logger.info(`(STORAGE/GIT) Performing push to origin on branch ${this.config.branch}...`)
117+
let pushOpts = ['--signed=if-asked']
118+
if (this.mode === 'push') {
119+
pushOpts.push('--force')
120+
}
121+
await git.push('origin', this.config.branch, pushOpts)
122+
}
123+
124+
WIKI.logger.info('(STORAGE/GIT) Initialization completed.')
76125
},
77126
async created() {
78127
const fileName = `${this.page.path}.${getFileExtension(this.page.contentType)}`
79128
const filePath = path.join(repoPath, fileName)
80129
await fs.outputFile(filePath, injectMetadata(this.page), 'utf8')
81130

82131
const git = sgit(repoPath)
83-
await git.add(`./${fileName}`).commit(`docs: create ${this.page.path}`, fileName, {
132+
await git.add(`./${fileName}`)
133+
await git.commit(`docs: create ${this.page.path}`, fileName, {
84134
'--author': `"${this.page.authorName} <${this.page.authorEmail}>"`
85135
})
86136
},
@@ -90,15 +140,17 @@ module.exports = {
90140
await fs.outputFile(filePath, injectMetadata(this.page), 'utf8')
91141

92142
const git = sgit(repoPath)
93-
await git.add(`./${fileName}`).commit(`docs: update ${this.page.path}`, fileName, {
143+
await git.add(`./${fileName}`)
144+
await git.commit(`docs: update ${this.page.path}`, fileName, {
94145
'--author': `"${this.page.authorName} <${this.page.authorEmail}>"`
95146
})
96147
},
97148
async deleted() {
98149
const fileName = `${this.page.path}.${getFileExtension(this.page.contentType)}`
99150

100151
const git = sgit(repoPath)
101-
await git.rm(`./${fileName}`).commit(`docs: delete ${this.page.path}`, fileName, {
152+
await git.rm(`./${fileName}`)
153+
await git.commit(`docs: delete ${this.page.path}`, fileName, {
102154
'--author': `"${this.page.authorName} <${this.page.authorEmail}>"`
103155
})
104156
},
@@ -107,7 +159,8 @@ module.exports = {
107159
const destinationFilePath = `${this.page.destinationPath}.${getFileExtension(this.page.contentType)}`
108160

109161
const git = sgit(repoPath)
110-
await git.mv(`./${sourceFilePath}`, `./${destinationFilePath}`).commit(`docs: rename ${this.page.sourcePath} to ${destinationFilePath}`, destinationFilePath, {
162+
await git.mv(`./${sourceFilePath}`, `./${destinationFilePath}`)
163+
await git.commit(`docs: rename ${this.page.sourcePath} to ${destinationFilePath}`, destinationFilePath, {
111164
'--author': `"${this.page.authorName} <${this.page.authorEmail}>"`
112165
})
113166
}

0 commit comments

Comments
 (0)