Skip to content

Commit 986776c

Browse files
authored
make changeset publishing more robust (#6895)
1 parent ab46b5e commit 986776c

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

scripts/create-github-release.mjs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,26 @@ import { tmpdir } from 'node:os'
88
const rootDir = path.join(import.meta.dirname, '..')
99
const ghToken = process.env.GH_TOKEN || process.env.GITHUB_TOKEN
1010

11-
// Get the previous release commit to diff against
12-
const lastReleaseHash = execSync(
13-
'git log --oneline --grep="ci: changeset release" -n 1 --format=%H HEAD~1',
11+
// Get the previous release commit to diff against.
12+
// This script runs right after the "ci: changeset release" commit is pushed,
13+
// so HEAD is the release commit. We want commits between the previous release
14+
// and this one (exclusive of both release commits).
15+
const releaseLogs = execSync(
16+
'git log --oneline --grep="ci: changeset release" --format=%H',
1417
)
1518
.toString()
1619
.trim()
20+
.split('\n')
21+
.filter(Boolean)
1722

18-
const rangeFrom = lastReleaseHash || 'HEAD~1'
23+
// Current release commit is releaseLogs[0] (HEAD), previous is releaseLogs[1]
24+
const currentRelease = releaseLogs[0] || 'HEAD'
25+
const previousRelease = releaseLogs[1]
26+
const rangeFrom = previousRelease || `${currentRelease}~1`
1927

20-
// Get commits since last release (include author email for username lookup)
28+
// Get commits between previous release and current release (exclude both)
2129
const rawLog = execSync(
22-
`git log ${rangeFrom}..HEAD~1 --pretty=format:"%h %ae %s" --no-merges`,
30+
`git log ${rangeFrom}..${currentRelease} --pretty=format:"%h %ae %s" --no-merges`,
2331
)
2432
.toString()
2533
.trim()
@@ -49,26 +57,24 @@ async function resolveUsername(email) {
4957
}
5058
}
5159

52-
// Group commits by type
60+
// Group commits by conventional commit type
5361
const groups = {}
5462
for (const line of commits) {
63+
// Format: "<hash> <email> <type>(<scope>): <subject>" or "<hash> <email> <type>: <subject>"
5564
const match = line.match(/^(\w+)\s+(\S+)\s+(\w+)(?:\(([^)]+)\))?:\s*(.+)$/)
5665
if (match) {
5766
const [, hash, email, type, scope, subject] = match
5867
const key = type.charAt(0).toUpperCase() + type.slice(1)
5968
if (!groups[key]) groups[key] = []
6069
groups[key].push({ hash, email, scope, subject })
6170
} else {
71+
// Non-conventional commits (merge commits, etc.) go to Other
6272
if (!groups['Other']) groups['Other'] = []
63-
const spaceIdx = line.indexOf(' ')
64-
const rest = line.slice(spaceIdx + 1)
65-
const spaceIdx2 = rest.indexOf(' ')
66-
groups['Other'].push({
67-
hash: line.slice(0, spaceIdx),
68-
email: rest.slice(0, spaceIdx2),
69-
scope: null,
70-
subject: rest.slice(spaceIdx2 + 1),
71-
})
73+
const parts = line.split(' ')
74+
const hash = parts[0]
75+
const email = parts[1]
76+
const subject = parts.slice(2).join(' ')
77+
groups['Other'].push({ hash, email, scope: null, subject })
7278
}
7379
}
7480

@@ -150,12 +156,13 @@ if (!tagExists) {
150156
}
151157

152158
const prereleaseFlag = isPrerelease ? '--prerelease' : ''
159+
const latestFlag = isPrerelease ? '' : ' --latest'
153160
const tmpFile = path.join(tmpdir(), `release-notes-${tagName}.md`)
154161
fs.writeFileSync(tmpFile, body)
155162

156163
try {
157164
execSync(
158-
`gh release create ${tagName} ${prereleaseFlag} --title "Release ${titleDate}" --notes-file ${tmpFile} --latest`,
165+
`gh release create ${tagName} ${prereleaseFlag} --title "Release ${titleDate}" --notes-file ${tmpFile}${latestFlag}`,
159166
{ stdio: 'inherit' },
160167
)
161168
console.info(`GitHub release ${tagName} created.`)

0 commit comments

Comments
 (0)