Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(packages/sui-mono): allow to release dependabot upgrades #1667

Merged
merged 6 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
50 changes: 45 additions & 5 deletions packages/sui-mono/src/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
Expand All @@ -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)
}

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

Expand All @@ -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`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤩

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

Expand All @@ -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,
Expand Down
Loading