Skip to content

Commit

Permalink
feat(config): add pullTagsBranch customization (#83)
Browse files Browse the repository at this point in the history
Relates: #83 #48
  • Loading branch information
HeshamMeneisi authored Apr 25, 2020
1 parent aaceda4 commit e919066
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 15 deletions.
15 changes: 10 additions & 5 deletions src/main/ts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { castArray, omit } from 'lodash'
import readPkg from 'read-pkg'
import request from 'sync-request'
import { IGhpagesPluginConfig, TAnyMap, TContext } from './interface'
import { catchToSmth } from './util'
import { anyDefined, catchToSmth } from './util'
import {
DEFAULT_BRANCH,
DEFAULT_DST,
DEFAULT_MSG,
DEFAULT_SRC,
PLUGIN_PATH,
DEFAULT_ENTERPRISE
DEFAULT_ENTERPRISE,
DEFAULT_PULL_TAGS_BRANCH
} from './defaults'

export {
Expand All @@ -21,7 +22,8 @@ export {
DEFAULT_MSG,
DEFAULT_DST,
DEFAULT_ENTERPRISE,
PLUGIN_PATH
PLUGIN_PATH,
DEFAULT_PULL_TAGS_BRANCH
}

const gitUrlParse = catchToSmth(gitParse, {})
Expand Down Expand Up @@ -138,13 +140,15 @@ export const resolveConfig = (pluginConfig: TAnyMap, context: TContext, path = P
const repo = getRepo(pluginConfig, context, enterprise)
const repoUrl = getRepoUrl(pluginConfig, context)
const token = getToken(env, repoUrl)
const pullTagsBranch = anyDefined(opts.pullTagsBranch, opts._branch, DEFAULT_PULL_TAGS_BRANCH)

if (process.env.DEBUG) {
logger.log('resolveConfig args:')
logger.log('context=', JSON.stringify(omit(context, 'env.GH_TOKEN', 'env.GITHUB_TOKEN'), null, 2))
logger.log('pluginConfig=', JSON.stringify(pluginConfig, null, 2))
logger.log('path=', path)
logger.log('step=', step)
logger.log('pullTagsBranch=', pullTagsBranch)
}

return {
Expand All @@ -154,7 +158,8 @@ export const resolveConfig = (pluginConfig: TAnyMap, context: TContext, path = P
branch: opts.branch || DEFAULT_BRANCH,
enterprise,
token,
repo
repo,
pullTagsBranch
}
}

Expand All @@ -176,5 +181,5 @@ export const resolveOptions = (pluginConfig: TAnyMap, context: TContext, path =
})
.find(config => config?.path === path) || {}

return { ...base, ...extra }
return { ...base, ...extra, _branch: pluginConfig.branch }
}
1 change: 1 addition & 0 deletions src/main/ts/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export const DEFAULT_SRC: string = 'docs'
export const DEFAULT_DST: string = '.'
export const DEFAULT_MSG: string = 'docs updated {{=it.nextRelease.gitTag}}'
export const DEFAULT_ENTERPRISE: boolean = false
export const DEFAULT_PULL_TAGS_BRANCH = 'master'
7 changes: 6 additions & 1 deletion src/main/ts/ghpages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ export const OK = { status: 'OK' }
* @private
*/
export const pullTags = (opts: IPushOpts): Promise<any> => {
if (opts.pullTagsBranch === '') {
return Promise.resolve()
}

const repo = '' + opts.repo
const pullTagsBranch = '' + opts.pullTagsBranch
const execaOpts = {
env: opts.env,
cwd: opts.cwd
}

return execa('git', ['pull', '--tags', '--force', repo], execaOpts)
return execa('git', ['pull', '--tags', '--force', repo, pullTagsBranch], execaOpts)
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export interface IGhpagesPluginConfig {
msg: string,
token?: string,
repo?: string,
enterprise?: boolean
enterprise?: boolean,
pullTagsBranch?: string
}

export interface IPushOpts extends IGhpagesPluginConfig {
Expand Down
2 changes: 2 additions & 0 deletions src/main/ts/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export const catchToSmth = (fn: Function, smth?: any) => {
}
}
}

export const anyDefined = (...args: any[]) => args.find(item => typeof item !== 'undefined')
16 changes: 11 additions & 5 deletions src/test/ts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DEFAULT_MSG,
DEFAULT_SRC,
DEFAULT_ENTERPRISE,
DEFAULT_PULL_TAGS_BRANCH,
resolveOptions,
resolveConfig,
extractRepoName,
Expand Down Expand Up @@ -36,7 +37,8 @@ describe('config', () => {
PLUGIN_PATH,
DEFAULT_DST,
DEFAULT_MSG,
DEFAULT_SRC
DEFAULT_SRC,
DEFAULT_PULL_TAGS_BRANCH
]).forEach(v => expect(v).toEqual(expect.any(String)))
})

Expand Down Expand Up @@ -101,7 +103,8 @@ describe('config', () => {
baz: 'qux',
msg: 'doc update',
branch: 'master', // NOTE must be omitted,
repositoryUrl: 'https://enterprise.com/org/repo.git'
repositoryUrl: 'https://enterprise.com/org/repo.git',
pullTagsBranch: 'dev'
}
const extra = {
enterprise: true,
Expand Down Expand Up @@ -129,7 +132,8 @@ describe('config', () => {
branch: DEFAULT_BRANCH,
msg: 'doc update',
token,
repo: `https://${token}@enterprise.com/org/repo.git`
repo: `https://${token}@enterprise.com/org/repo.git`,
pullTagsBranch: 'dev'
})
})

Expand Down Expand Up @@ -163,7 +167,8 @@ describe('config', () => {
msg: DEFAULT_MSG,
src: DEFAULT_SRC,
token,
repo: `https://${token}@github.com/${repoName}.git`
repo: `https://${token}@github.com/${repoName}.git`,
pullTagsBranch: DEFAULT_PULL_TAGS_BRANCH
})
})

Expand Down Expand Up @@ -234,7 +239,8 @@ describe('config', () => {
msg: DEFAULT_MSG,
src: 'dist/web',
token: 'secure',
repo: `https://secure@github-enterprise-repo-url.com/foo/bar.git`
repo: `https://secure@github-enterprise-repo-url.com/foo/bar.git`,
pullTagsBranch: DEFAULT_PULL_TAGS_BRANCH
})
})
})
Expand Down
9 changes: 6 additions & 3 deletions src/test/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DEFAULT_MSG,
DEFAULT_BRANCH,
DEFAULT_ENTERPRISE,
DEFAULT_PULL_TAGS_BRANCH,
PLUGIN_PATH
} from '../../main/ts'
import { getUrlFromPackage } from '../../main/ts/config'
Expand Down Expand Up @@ -82,7 +83,8 @@ describe('index', () => {
src: DEFAULT_SRC,
enterprise: DEFAULT_ENTERPRISE,
repo: getRepo(pluginConfig, context),
token
token,
pullTagsBranch: DEFAULT_PULL_TAGS_BRANCH
})
expect(result).toBeUndefined()
})
Expand Down Expand Up @@ -171,7 +173,7 @@ describe('index', () => {
it('performs commit to docs branch via gh-pages util', async () => {
const { publish: ghpagesPublish } = require('gh-pages')
const { publish } = require('../../main/ts')
const { getRepo } = require('../../main/ts/config')
const { getRepo, resolveConfig } = require('../../main/ts/config')
const { OK } = require('../../main/ts/ghpages')
const pluginConfig = {
foo: 'bar',
Expand Down Expand Up @@ -213,7 +215,8 @@ describe('index', () => {
'pull',
'--tags',
'--force',
expectedOpts.repo
expectedOpts.repo,
resolveConfig(pluginConfig, context).pullTagsBranch
],
execaOpts
)
Expand Down

0 comments on commit e919066

Please sign in to comment.