feat: git init + commands

pull/760/head
Nick 6 years ago
parent 830ddd9d2c
commit d5028b1bce

@ -29,7 +29,7 @@ FROM node:10.15-alpine
LABEL maintainer="requarks.io"
RUN apk update && \
apk add bash curl git openssh supervisor --no-cache && \
apk add bash curl git openssh gnupg supervisor --no-cache && \
rm -rf /var/cache/apk/* && \
mkdir -p /wiki && \
mkdir -p /logs

@ -5,7 +5,7 @@ FROM node:10-alpine
LABEL maintainer "requarks.io"
RUN apk update && \
apk add bash curl git python make g++ nano --no-cache && \
apk add bash curl git python make g++ nano openssh gnupg --no-cache && \
mkdir -p /wiki
WORKDIR /wiki

@ -293,7 +293,9 @@
"zxcvbn": "4.4.2"
},
"resolutions": {
"terser": "3.14.1"
"terser": "3.14.1",
"apollo-server/**/core-js": "3.0.0-beta.11",
"apollo-server-express/**/core-js": "3.0.0-beta.11"
},
"browserslist": [
"> 1%",

@ -48,6 +48,7 @@ module.exports = {
}, {})
}).where('key', tgt.key)
}
await WIKI.models.storage.initTargets()
return {
responseResult: graphHelper.generateSuccess('Storage targets updated successfully')
}

@ -7,6 +7,8 @@ const commonHelper = require('../helpers/common')
/* global WIKI */
let targets = []
/**
* Storage model
*/
@ -93,18 +95,34 @@ module.exports = class Storage extends Model {
}
}
static async initTargets() {
targets = await WIKI.models.storage.query().where('isEnabled', true)
try {
for(let target of targets) {
target.fn = require(`../modules/storage/${target.key}/storage`)
await target.fn.init.call({
config: target.config,
mode: target.mode
})
}
} catch (err) {
WIKI.logger.warn(err)
throw err
}
}
static async pageEvent({ event, page }) {
const targets = await WIKI.models.storage.query().where('isEnabled', true)
if (targets && targets.length > 0) {
_.forEach(targets, target => {
WIKI.queue.job.syncStorage.add({
event,
target,
try {
for(let target of targets) {
await target.fn[event].call({
config: target.config,
mode: target.mode,
page
}, {
removeOnComplete: true
})
})
}
} catch (err) {
WIKI.logger.warn(err)
throw err
}
}
}

@ -41,3 +41,18 @@ props:
type: String
title: Password / PAT
hint: Basic Authentication Only
localRepoPath:
type: String
title: Local Repository Path
default: './data/repo'
hint: 'Path where the local git repository will be created.'
defaultEmail:
type: String
title: Default Author Email
default: 'name@company.com'
hint: 'Used as fallback in case the author of the change is not present.'
defaultName:
type: String
title: Default Author Name
default: 'John Smith'
hint: 'Used as fallback in case the author of the change is not present.'

@ -3,7 +3,7 @@ const sgit = require('simple-git/promise')
const fs = require('fs-extra')
const _ = require('lodash')
const repoPath = path.join(WIKI.ROOTPATH, 'data/repo')
let repoPath = path.join(process.cwd(), 'data/repo')
/**
* Get file extension based on content type
@ -50,29 +50,78 @@ module.exports = {
},
async init() {
WIKI.logger.info('(STORAGE/GIT) Initializing...')
repoPath = path.resolve(WIKI.ROOTPATH, this.config.localRepoPath)
await fs.ensureDir(repoPath)
const git = sgit(repoPath)
// Initialize repo (if needed)
WIKI.logger.info('(STORAGE/GIT) Checking repository state...')
const isRepo = await git.checkIsRepo()
if (!isRepo) {
WIKI.logger.info('(STORAGE/GIT) Initializing local repository...')
await git.init()
}
// Checkout branch
await git.checkout(this.config.branch)
// Set default author
await git.raw(['config', '--local', 'user.email', this.config.defaultEmail])
await git.raw(['config', '--local', 'user.name', this.config.defaultName])
// Purge existing remotes
WIKI.logger.info('(STORAGE/GIT) Listing existing remotes...')
const remotes = await git.getRemotes()
if (remotes.length > 0) {
WIKI.logger.info('(STORAGE/GIT) Purging existing remotes...')
for(let remote of remotes) {
await git.removeRemote(remote.name)
}
}
// Add remote
WIKI.logger.info('(STORAGE/GIT) Setting SSL Verification config...')
await git.raw(['config', '--local', '--bool', 'http.sslVerify', _.toString(this.config.verifySSL)])
switch (this.config.authType) {
case 'ssh':
WIKI.logger.info('(STORAGE/GIT) Setting SSH Command config...')
await git.addConfig('core.sshCommand', `ssh -i "${this.config.sshPrivateKeyPath}" -o StrictHostKeyChecking=no`)
WIKI.logger.info('(STORAGE/GIT) Adding origin remote via SSH...')
await git.addRemote('origin', this.config.repoUrl)
break
default:
WIKI.logger.info('(STORAGE/GIT) Adding origin remote via HTTPS...')
await git.addRemote('origin', `https://${this.config.basicUsername}:${this.config.basicPassword}@${this.config.repoUrl}`)
break
}
// Fetch updates for remote
WIKI.logger.info('(STORAGE/GIT) Fetch updates from remote...')
await git.raw(['remote', 'update', 'origin'])
// Checkout branch
const branches = await git.branch()
if (!_.includes(branches.all, this.config.branch) && !_.includes(branches.all, `remotes/origin/${this.config.branch}`)) {
throw new Error('Invalid branch! Make sure it exists on the remote first.')
}
WIKI.logger.info(`(STORAGE/GIT) Checking out branch ${this.config.branch}...`)
await git.checkout(this.config.branch)
// Pull rebase
if (_.includes(['sync', 'pull'], this.mode)) {
WIKI.logger.info(`(STORAGE/GIT) Performing pull rebase from origin on branch ${this.config.branch}...`)
await git.pull('origin', this.config.branch, ['--rebase'])
}
// Push
if (_.includes(['sync', 'push'], this.mode)) {
WIKI.logger.info(`(STORAGE/GIT) Performing push to origin on branch ${this.config.branch}...`)
let pushOpts = ['--signed=if-asked']
if (this.mode === 'push') {
pushOpts.push('--force')
}
await git.push('origin', this.config.branch, pushOpts)
}
WIKI.logger.info('(STORAGE/GIT) Initialization completed.')
},
async created() {
const fileName = `${this.page.path}.${getFileExtension(this.page.contentType)}`
@ -80,7 +129,8 @@ module.exports = {
await fs.outputFile(filePath, injectMetadata(this.page), 'utf8')
const git = sgit(repoPath)
await git.add(`./${fileName}`).commit(`docs: create ${this.page.path}`, fileName, {
await git.add(`./${fileName}`)
await git.commit(`docs: create ${this.page.path}`, fileName, {
'--author': `"${this.page.authorName} <${this.page.authorEmail}>"`
})
},
@ -90,7 +140,8 @@ module.exports = {
await fs.outputFile(filePath, injectMetadata(this.page), 'utf8')
const git = sgit(repoPath)
await git.add(`./${fileName}`).commit(`docs: update ${this.page.path}`, fileName, {
await git.add(`./${fileName}`)
await git.commit(`docs: update ${this.page.path}`, fileName, {
'--author': `"${this.page.authorName} <${this.page.authorEmail}>"`
})
},
@ -98,7 +149,8 @@ module.exports = {
const fileName = `${this.page.path}.${getFileExtension(this.page.contentType)}`
const git = sgit(repoPath)
await git.rm(`./${fileName}`).commit(`docs: delete ${this.page.path}`, fileName, {
await git.rm(`./${fileName}`)
await git.commit(`docs: delete ${this.page.path}`, fileName, {
'--author': `"${this.page.authorName} <${this.page.authorEmail}>"`
})
},
@ -107,7 +159,8 @@ module.exports = {
const destinationFilePath = `${this.page.destinationPath}.${getFileExtension(this.page.contentType)}`
const git = sgit(repoPath)
await git.mv(`./${sourceFilePath}`, `./${destinationFilePath}`).commit(`docs: rename ${this.page.sourcePath} to ${destinationFilePath}`, destinationFilePath, {
await git.mv(`./${sourceFilePath}`, `./${destinationFilePath}`)
await git.commit(`docs: rename ${this.page.sourcePath} to ${destinationFilePath}`, destinationFilePath, {
'--author': `"${this.page.authorName} <${this.page.authorEmail}>"`
})
}

13883
yarn.lock

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save