From 420ef73c5417c691c72d6065bdd550900f8b9e55 Mon Sep 17 00:00:00 2001 From: Kiko Ruiz Lloret Date: Mon, 4 Dec 2023 17:40:37 +0100 Subject: [PATCH] feat(packages/sui-mono): make check function print the new commit push to be released --- packages/sui-mono/bin/sui-mono-check.js | 5 ++-- packages/sui-mono/src/check.js | 37 +++++++++++++++++-------- 2 files changed, 28 insertions(+), 14 deletions(-) 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 eb1e1bd89..60190e759 100644 --- a/packages/sui-mono/src/check.js +++ b/packages/sui-mono/src/check.js @@ -17,8 +17,9 @@ const PACKAGE_VERSION_INCREMENT = { 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_yan/' +const DEPS_UPGRADE_BRANCH_PREFIX = 'dependabot/npm_and_yarn/' const SCOPE_REGEX = /packages\/[a-z]+-[a-z]+/ const isCommitBreakingChange = commit => { @@ -29,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) } @@ -38,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) @@ -57,29 +60,38 @@ const getOverride = ({overrides, header}) => { const getTransform = ({status, packages, overrides = getOverrides()} = {}) => async (commit, cb) => { - const {scope, header, type} = commit + const {scope, header, type, subject} = commit const [pkgToOverride] = getOverride({overrides, header}) ?? [] - const pkg = pkgToOverride ?? getPkgFromScope(scope) - const isDepsUpdate = type === DEPS_UPGRADE_COMMIT_TYPE && DEPS_UPGRADE_PACKAGES.includes(pkg) + let pkg = pkgToOverride ?? getPkgFromScope(scope) + const isDepsUpgrade = type === DEPS_UPGRADE_COMMIT_TYPE && DEPS_UPGRADE_PACKAGES.includes(pkg) let toPush = null - if (isDepsUpdate) { - const {stdout: rawUpdateHash} = await exec( + if (isDepsUpgrade) { + const {stdout: rawUpgradeHash} = await exec( `git log --oneline --grep=${DEPS_UPGRADE_BRANCH_PREFIX} | awk '{print $1}' | head -n 1` ) - const updateHash = rawUpdateHash.trim() + const upgradeHash = rawUpgradeHash.trim() - if (!updateHash) return cb() + if (!upgradeHash) return cb() - const {stdout: rawChangedFiles} = await exec(`git diff --name-only ${updateHash} master`) + 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 + toPush = { + ...commit, + header: `${DEPS_UPGRADE_COMMIT_TYPE_TO_PUSH}(${pkgToUpdate}): ${subject}` + } } if (!packages.includes(pkg)) return cb() @@ -100,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() } @@ -121,8 +134,8 @@ const check = () => return privateField !== true }) - const status = {} + packagesWithChangelog.forEach(pkg => { status[pkg] = { increment: PACKAGE_VERSION_INCREMENT.NOTHING,