Skip to content

Commit

Permalink
feat(packages/sui-mono): make check function print the new commit pus…
Browse files Browse the repository at this point in the history
…h to be released
  • Loading branch information
kikoruiz committed Dec 4, 2023
1 parent b35d7be commit 420ef73
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
5 changes: 3 additions & 2 deletions packages/sui-mono/bin/sui-mono-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
Expand Down
37 changes: 25 additions & 12 deletions packages/sui-mono/src/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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)
}

Expand All @@ -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)

Expand All @@ -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()
Expand All @@ -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()
}

Expand All @@ -121,8 +134,8 @@ const check = () =>

return privateField !== true
})

const status = {}

packagesWithChangelog.forEach(pkg => {
status[pkg] = {
increment: PACKAGE_VERSION_INCREMENT.NOTHING,
Expand Down

0 comments on commit 420ef73

Please sign in to comment.