UNPKG

tranz-commit-icafe-close

Version:

根据 commit message 自动关闭 icafe 卡片

137 lines (124 loc) 5.07 kB
'use strict' const commitParse = require('conventional-commits-parser') const readPkgUp = require('read-pkg-up') const changelog = require('conventional-changelog-befe') const { normalizeIcafeByPkg } = require('normalize-icafe-pkg') const isPlainObj = require('is-plain-obj') module.exports = tranzCommitIcafeClose // CardUpdate.fetch({ // spaceId: 'icafe-api', // u: 'abc', // pwd: 'abc', // sequence: '106', // fields: ['所属计划=12345', '估时=1'] // }) function tranzCommitIcafeClose({ silent, debug, stateName = '开发中', fields = [], ...icafe } = {}) { if (debug && !silent) { process.env.DEBUG = [process.env.DEBUG || '', 'icafe-api:*'].filter(Boolean).join(';') } const { CardUpdate, Card, FetchAble } = require('icafe-api') const debugLog = (...argv) => !silent && debug && console.log('[tranz-commit-icafe-close]', ...argv) const log = (...argv) => !silent && console.log('commit-icafe-close >', ...argv) const error = (...argv) => !silent && console.error('commit-icafe-close >', ...argv) debugLog('FetchAble.defaultData', FetchAble.defaultData) return Promise.resolve(changelog).then(({ parserOpts }) => { return function(commit) { return readPkgUp({ normalize: false, cwd: this && this.cwd }) .then(({ pkg, path }) => { if (pkg) { icafe = { ...normalizeIcafeByPkg(pkg), ...icafe } } }) .then(() => { const obj = commitParse.sync(commit, parserOpts) const issues = [] if (obj.references && obj.references.length) { obj.references.forEach(({ action, issue, prefix }) => { if (!action) return if (prefix === '#') { icafe && icafe.spaceId && issues.push([prefix, issue]) } else { issues.push([prefix, issue]) } }) } debugLog('icafe', icafe) debugLog('commitParse.sync', obj) debugLog('issues', issues) if (issues.length) { return Promise.all( issues.map(([spaceId, issue]) => { const normalizedSpaceId = spaceId === '#' ? icafe.spaceId : spaceId.slice(0, -1) const catchError = error => [ spaceId + issue, false, error.res && error.res.body && error.res.body.message ? error.res.body.message : error.message ] const fetch = stateName => CardUpdate.fetch({ ...icafe, spaceId: normalizedSpaceId, sequence: issue, fields: ['流程状态=' + stateName].concat(fields) }) .then(res => { if (res.body.code == 200) { return [spaceId + issue, true] } return [spaceId + issue, false, res.body.message] }) .catch(catchError) if (isPlainObj(stateName)) { return Card.fetch({ ...icafe, spaceId: normalizedSpaceId, sequence: issue, showAssociations: false }) .then(res => { if (res.body.code == 200 && res.body.cards && res.body.cards.length) { const typeName = res.body.cards[0].type.name const mappedStateName = stateName[typeName] || stateName['$fallback'] if (mappedStateName) { return fetch(mappedStateName) } else { return [ spaceId + issue, false, `未找到对应卡片类型 (${typeName}) 修改的状态,可设置 $fallback 为未匹配的默认状态` ] } } return [spaceId + issue, false, res.body.message] }) .catch(catchError) return } return fetch(stateName) }) ).then(list => { let successList = [] let failList = [] list.map(([name, isPassed, reason]) => { if (isPassed) { successList.push(name) } else { failList.push([name, reason]) } }) if (successList.length) { log(`成功更改 ${successList} 卡片状态`) } if (failList.length) { failList.forEach(fail => { error(`更改 ${fail[0]} 卡片状态失败`, fail[1] ? ',原因: ' + fail[1] : '') }) } return commit }) } return commit }) } }) }