diff --git a/packages/sui-mono/bin/sui-mono-check.js b/packages/sui-mono/bin/sui-mono-check.js index 106d09a29..1c663a063 100644 --- a/packages/sui-mono/bin/sui-mono-check.js +++ b/packages/sui-mono/bin/sui-mono-check.js @@ -39,6 +39,7 @@ const showReportHeaderNegative = () => { const showReport = status => { let headerShown = false + for (const pkg in status) { if (status.hasOwnProperty(pkg) && status[pkg].increment > 0) { if (!headerShown) { @@ -52,9 +53,9 @@ const showReport = status => { status[pkg].commits.forEach(commit => { const messagePrefix = checker.isCommitBreakingChange(commit) ? `› ${colors.red('BREAKING CHANGE')} -` : '›' - console.log(` ${messagePrefix} ${commit.header}`) + + console.log(` ${messagePrefix} ${commit.header}\n`) }) - console.log('') } } if (!headerShown) showReportHeaderNegative() diff --git a/packages/sui-mono/src/check.js b/packages/sui-mono/src/check.js index a11e3291e..60190e759 100644 --- a/packages/sui-mono/src/check.js +++ b/packages/sui-mono/src/check.js @@ -3,8 +3,11 @@ const conventionalChangelog = require('conventional-changelog') const {readJsonSync} = require('fs-extra') +const {promisify} = require('util') + const {checkIsMonoPackage, getProjectName, getWorkspaces, getOverrides} = require('./config.js') +const exec = promisify(require('child_process').exec) const gitRawCommitsOpts = {reverse: true, topoOrder: true} const PACKAGE_VERSION_INCREMENT = { @@ -13,6 +16,11 @@ const PACKAGE_VERSION_INCREMENT = { MINOR: 2, MAJOR: 3 } +const DEPS_UPGRADE_COMMIT_TYPE = 'upgrade' +const DEPS_UPGRADE_COMMIT_TYPE_TO_PUSH = 'feat' +const DEPS_UPGRADE_PACKAGES = ['deps', 'deps-dev'] +const DEPS_UPGRADE_BRANCH_PREFIX = 'dependabot/npm_and_yarn/' +const SCOPE_REGEX = /packages\/[a-z]+-[a-z]+/ const isCommitBreakingChange = commit => { const {body, footer} = commit @@ -22,6 +30,7 @@ const isCommitBreakingChange = commit => { const isCommitReleaseTrigger = commit => { const COMMIT_TYPES_WITH_RELEASE = ['fix', 'feat', 'perf', 'refactor'] + return COMMIT_TYPES_WITH_RELEASE.includes(commit.type) } @@ -31,6 +40,7 @@ const flatten = status => Object.keys(status).reduce( (acc, scope) => { const scopeStatus = status[scope] + acc.increment = Math.max(scopeStatus.increment, acc.increment) acc.commits = acc.commits.concat(scopeStatus.commits) @@ -49,13 +59,41 @@ const getOverride = ({overrides, header}) => { const getTransform = ({status, packages, overrides = getOverrides()} = {}) => - (commit, cb) => { - const {scope, header} = commit + async (commit, cb) => { + const {scope, header, type, subject} = commit const [pkgToOverride] = getOverride({overrides, header}) ?? [] - const pkg = pkgToOverride ?? getPkgFromScope(scope) + let pkg = pkgToOverride ?? getPkgFromScope(scope) + const isDepsUpgrade = type === DEPS_UPGRADE_COMMIT_TYPE && DEPS_UPGRADE_PACKAGES.includes(pkg) let toPush = null + if (isDepsUpgrade) { + const {stdout: rawUpgradeHash} = await exec( + `git log --oneline --grep=${DEPS_UPGRADE_BRANCH_PREFIX} | awk '{print $1}' | head -n 1` + ) + const upgradeHash = rawUpgradeHash.trim() + + if (!upgradeHash) return cb() + + const {stdout: rawParentHash} = await exec(`git rev-parse ${upgradeHash}^`) + const parentHash = rawParentHash.trim() + const {stdout: rawChangedFiles} = await exec( + `git --no-pager diff --name-only ${upgradeHash} $(git merge-base ${parentHash} master)` + ) + const changedFiles = rawChangedFiles.split('\n').filter(Boolean) + const pkgToUpdate = changedFiles.find(file => file.match(SCOPE_REGEX))?.match(SCOPE_REGEX)[0] + + pkg = pkgToUpdate + + if (!pkgToUpdate) return cb() + + status[pkgToUpdate].increment = Math.max(status[pkgToUpdate].increment, PACKAGE_VERSION_INCREMENT.MINOR) + toPush = { + ...commit, + header: `${DEPS_UPGRADE_COMMIT_TYPE_TO_PUSH}(${pkgToUpdate}): ${subject}` + } + } + if (!packages.includes(pkg)) return cb() if (pkgToOverride) { @@ -74,13 +112,14 @@ const getTransform = } if (toPush) { - status[pkg].commits.push(commit) + status[pkg].commits.push(toPush) } if (commit.type === 'release') { status[pkg].increment = PACKAGE_VERSION_INCREMENT.NOTHING status[pkg].commits = [] } + cb() } @@ -92,10 +131,11 @@ const check = () => */ const packagesWithChangelog = getWorkspaces().filter(pkg => { const {private: privateField} = readJsonSync(`${pkg}/package.json`) + return privateField !== true }) - const status = {} + packagesWithChangelog.forEach(pkg => { status[pkg] = { increment: PACKAGE_VERSION_INCREMENT.NOTHING,