Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
saqqdy committed May 28, 2020
2 parents 83dde83 + bae2fa6 commit 8526095
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 37 deletions.
7 changes: 0 additions & 7 deletions .gitmarsrc

This file was deleted.

14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
一个定制化的git工作流操作工具
每一个子命令都带了help功能,可输入 gitm command-name --help 获取对应子指令的帮助信息

> 注意:<type>意思是type必传;[type]意思是type选填;[-a --app [app]]其中-a是--app的简写,后面[app]指的是-a后面的传值
## 安装
```
# 通过npm安装
Expand Down Expand Up @@ -84,7 +86,7 @@ gitm merge 20001

### gitm copy
#### 短指令:gitm cp
复制其他分支上的提交记录到当前分支(注意关键词必须是4位以上的数字,任务号或者bug编号),gitm copy一共有两种使用方式
复制其他分支上的提交记录到当前分支(为确保copy准确,请尽量完整填写关键词),gitm copy一共有两种使用方式
1. 传入commit-id,把其他分支上的commit-id复制过来,执行下面指令
```
# 形式:gitm copy <from> [commitid...]
Expand Down Expand Up @@ -190,14 +192,22 @@ gitm admin create release
### gitm admin publish
发版操作
```
# 形式:gitm admin publish <type> [-c --combine] [--use-rebase] [-p --prod] [-p --postmsg]
# 形式:gitm admin publish <type> [-c --combine] [--use-rebase] [-p --prod] [-b --build [build]] [-p --postmsg]
# 传入combine时合并release之后会把release同步到bugfix
# 传入rebase使用release方法合并
# 当需要发布bugfix时,传入prod会把bugfix同步到master,不传则不合并
# postmsg:传入时有配置postmsg的指令处理消息会推送到云之家
gitm admin publish release
```

```
# 发布并执行构建全部
gitm admin publish release --build
# 发布并执行构建cloud-ui
gitm admin publish release --build cloud-ui
```


### gitm admin update
更新release、bugfix、support分支代码,默认走merge方法
```
Expand Down
24 changes: 23 additions & 1 deletion bin/gitm-admin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
const program = require('commander')
const sh = require('shelljs')
const { error, success, config, queue, getStatus, checkBranch } = require('./index')
const { error, success, config, queue, getStatus, checkBranch, appName } = require('./index')
/**
* gitm admin create
* gitm admin publish
Expand Down Expand Up @@ -49,6 +49,7 @@ program
.option('-c, --combine', '是否把release代码同步到bug', false)
.option('--use-rebase', '是否使用rebase方式更新,默认merge', false)
.option('-p, --prod', '发布bug分支时,是否合并bug到master', false)
.option('-b, --build [build]', '需要构建的应用')
.option('--postmsg', '发送消息', false)
.action(async (type, opt) => {
const opts = ['bugfix', 'release', 'support'] // 允许执行的指令
Expand Down Expand Up @@ -118,6 +119,7 @@ program
}
]
}
// 发布bug分支且同步到master
if (type === 'bugfix' && opt.prod) {
cmd[type] = cmd[type].concat([
`git checkout ${config.master}`,
Expand All @@ -131,8 +133,27 @@ program
config: { slient: false, again: true, success: '推送成功', fail: '推送失败,请根据提示处理' }
}
])
if (opt.build) {
cmd[type] = cmd[type].concat([
{
cmd: `gitm build ${appName} --env bug --app ${opt.build === true ? 'all' : opt.build}`,
config: { slient: true, again: false, success: '调起构建成功', fail: '调起构建失败' }
}
])
}
}
// 发布release
if (type === 'release' && opt.build) {
cmd[type] = cmd[type].concat([
{
cmd: `gitm build ${appName} --env prod --app ${opt.build === true ? 'all' : opt.build}`,
config: { slient: true, again: false, success: '调起构建成功', fail: '调起构建失败' }
}
])
}
// 发布release分支且同步release代码到bug线
if (type === 'release' && opt.combine) {
// 使用rebase
if (opt.useRebase) {
cmd[type] = cmd[type].concat([
`git checkout ${config.release}`,
Expand Down Expand Up @@ -170,6 +191,7 @@ program
])
}
}

queue(cmd[type])
} else {
sh.echo(error('type只允许输入:' + opts.join(',')))
Expand Down
2 changes: 1 addition & 1 deletion bin/gitm-clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ program
.description('清理gitmars缓存')
.option('-f, --force', '强制清理', false)
.action(opt => {
sh.rm(gitDir + '/.gitmarscommands', gitDir + '/.gitmarslog')
sh.rm(gitDir + '/.gitmarscommands', gitDir + '/.gitmarslog', gitDir + '/buildConfig.json', gitDir + '/buildConfig.txt')
if (opt.force) {
sh.echo(warning('您输入了--force,将同时清理本地gitmars配置文件'))
sh.rm(pwd + '/gitmarsconfig.json', pwd + '/.gitmarsrc')
Expand Down
8 changes: 4 additions & 4 deletions bin/gitm-combine.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ program
if (opt.build) {
cmd = cmd.concat([
{
cmd: `gitm build jenkins --env dev --project ${appName} --app ${opt.build === true ? 'all' : opt.build}`,
cmd: `gitm build ${appName} --env dev --app ${opt.build === true ? 'all' : opt.build}`,
config: { slient: true, again: false, success: '调起构建成功', fail: '调起构建失败' }
}
])
Expand Down Expand Up @@ -104,16 +104,16 @@ program
if (type === 'bugfix') {
cmd = cmd.concat([
{
cmd: `gitm build jenkins --env bug --project ${appName} --app ${opt.build}`,
config: { slient: true, again: false, success: '调起构建成功', fail: '调起构建失败' }
cmd: `gitm build ${appName} --env bug --app ${opt.build === true ? 'all' : opt.build}`,
config: { slient: true, again: false, success: '调起构建成功', fail: '调起构建失败' }
}
])
}
// support分支要构建bug和release
if (type === 'support' && opt.bugfix) {
cmd = cmd.concat([
{
cmd: `gitm build jenkins --env bug --project ${appName} --app ${opt.build}`,
cmd: `gitm build ${appName} --env bug --app ${opt.build === true ? 'all' : opt.build}`,
config: { slient: true, again: false, success: '调起构建成功', fail: '调起构建失败' }
}
])
Expand Down
50 changes: 28 additions & 22 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const apolloConfig = require('../lib/apollo')

let fs = require('fs'),
path = require('path')
const sh = require('shelljs')
const colors = require('colors')
let pwd = sh.exec('git rev-parse --show-toplevel', { silent: true }).stdout.replace(/[\n\s]*$/g, ''),
gitDir = sh.exec('git rev-parse --git-dir', { silent: true }).stdout.replace(/[\n\s]*$/g, ''),
// gitDir = sh.exec('git rev-parse --git-dir', { silent: true }).stdout.replace(/[\n\s]*$/g, ''),
gitDir = pwd + '/.git',
gitUrl = sh.exec('git config --local --get remote.origin.url', { silent: true }).stdout.replace(/[\n\s]*$/g, ''),
appName = gitUrl.replace(/^[\s\S]+\/([a-z0-9A-Z]+)\.git$/, '$1'),
system = sh.exec('uname -s', { silent: true }).stdout || 'MINGW64_NT',
Expand All @@ -27,8 +32,7 @@ const defaults = {
email: '',
msgTemplate: '${message};项目:${project};路径:${pwd}',
msgUrl: '',
jenkinsUrlTemplate: '',
crpUrlTemplate: ''
apolloConfig: ''
}
const getConfigFrom = () => {
if (sh.test('-f', pwd + '/.gitmarsrc')) {
Expand Down Expand Up @@ -69,17 +73,22 @@ function getConfig() {
config = { ...defaults, ...getConfig() }

/**
* getBuildConfig
* @description 读取构建配置
* @returns {Object} arr 返回配置对象
* writeFile
* @description 写文件
*/
function getBuildConfig() {
if (sh.test('-f', pwd + '/buildConfig.json')) {
return require(pwd + '/buildConfig.json')
}
return null
const writeFile = (url, data) => {
return new Promise((resolve, reject) => {
fs.writeFile(url, data, err => {
if (err) {
reject(new Error('文件写入错误'))
} else {
resolve()
}
})
})
}
buildConfig = getBuildConfig()

buildConfig = apolloConfig({ config, pwd, gitDir, writeFile })

/**
* mapTemplate
Expand Down Expand Up @@ -291,8 +300,7 @@ const postMessage = msg => {
*/
const getMessage = type => {
let str = '',
d = new Date(),
name = pwd.match(/\/([a-zA-Z0-9-_]+)\/?$/)
d = new Date()
switch (type) {
case 'time':
str = d
Expand All @@ -304,7 +312,7 @@ const getMessage = type => {
str = pwd
break
case 'project':
str = name ? name[1] : ''
str = appName
break
case 'user':
str = config.user
Expand Down Expand Up @@ -380,11 +388,9 @@ const handleConfigOutput = name => {
} else if (name === 'msgUrl') {
return '请输入云之家消息推送地址'
} else if (name === 'msgTemplate') {
return '请输入消息模板, 默认为:' + defaults[msgTemplate]
} else if (name === 'jenkinsUrlTemplate') {
return '请输入Jenkins构建地址模板'
} else if (name === 'crpUrlTemplate') {
return '请输入CRP构建地址模板'
return '请输入消息模板, 默认为:' + defaults[name]
} else if (name === 'apolloConfig') {
return '请配置apollo'
}
return '请输入' + name + '分支名称,默认为:' + defaults[name]
}
Expand Down Expand Up @@ -413,12 +419,12 @@ const runJenkins = ({ env, project, app = 'all' }) => {
sh.exit(1)
return
}
if (!config.jenkinsUrlTemplate) {
if (!buildConfig.template) {
sh.echo(error('请配置Jenkins构建地址模板'))
sh.exit(1)
return
}
url = mapTemplate(config.jenkinsUrlTemplate, {
url = mapTemplate(buildConfig.template, {
line: cfg.line,
project: p.project,
token: cfg.token,
Expand Down
45 changes: 45 additions & 0 deletions lib/apollo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
let apollo = require('node-apollo'),
path = require('path'),
fs = require('fs'),
sh = require('shelljs')

/**
* apolloConfig
* @description 读取构建配置
* @returns {Object} arr 返回配置对象
*/
module.exports = function apolloConfig({ config, pwd, gitDir, writeFile }) {
let date = new Date().getTime(),
apolloConfig
if (sh.test('-f', pwd + '/gitmarsconfig.json')) {
let fileDate = parseInt(sh.cat(gitDir + '/buildConfig.txt').stdout)
if (date - fileDate < 24 * 60 * 60 * 1000) return require(gitDir + '/buildConfig.json')
}
if (!config.apolloConfig) {
sh.echo(error('请配置apollo'))
sh.exit(1)
return
}
// 如果传入的是json字符串,转json
try {
apolloConfig = JSON.parse(config.apolloConfig)
} catch (err) {
apolloConfig = config.apolloConfig
}
apollo
.remoteConfigService(apolloConfig)
.then(result => {
return new Promise((resolve, reject) => {
Promise.all([writeFile(gitDir + '/buildConfig.txt', date), writeFile(gitDir + '/buildConfig.json', JSON.stringify(result.content))])
.then(() => {
resolve(result.content)
})
.catch(err => {
reject(err)
})
})
})
.catch(err => {
console.error(err)
}).done
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"colors": "^1.4.0",
"commander": "5.1.0",
"node-apollo": "^1.2.1",
"shelljs": "0.8.4",
"tracer": "1.1.1"
},
Expand Down

0 comments on commit 8526095

Please sign in to comment.