mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-30 21:04:59 +00:00
fix(cli): discover draft releases before publishing
This commit is contained in:
@@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
- **cli**: Fix release publication failing to find staged standalone downloads.
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
## [0.6.6] - 2026-11-13
|
## [0.6.6] - 2026-11-13
|
||||||
|
|||||||
@@ -16,13 +16,26 @@ describe('CLI release publication', () => {
|
|||||||
let manifest: string
|
let manifest: string
|
||||||
let assets: { name: string; digest: string }[]
|
let assets: { name: string; digest: string }[]
|
||||||
let draft: boolean | undefined
|
let draft: boolean | undefined
|
||||||
|
let releasePages: unknown[][] | undefined
|
||||||
let source = 'release-commit'
|
let source = 'release-commit'
|
||||||
const calls: string[][] = []
|
const calls: string[][] = []
|
||||||
|
const releaseEntry = (tag: string, isDraft: boolean) => ({
|
||||||
|
// GitHub's REST API uses snake_case field names.
|
||||||
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||||
|
tag_name: tag,
|
||||||
|
draft: isDraft,
|
||||||
|
assets,
|
||||||
|
})
|
||||||
const run = vi.fn((command: string, args: string[]): string => {
|
const run = vi.fn((command: string, args: string[]): string => {
|
||||||
calls.push([command, ...args])
|
calls.push([command, ...args])
|
||||||
if (command === 'git') return args[1] === 'HEAD' ? source : 'release-commit'
|
if (command === 'git') return args[1] === 'HEAD' ? source : 'release-commit'
|
||||||
if (args[0] === 'api') {
|
if (args[0] === 'api') {
|
||||||
if (draft === undefined) throw new Error('gh: Not Found (HTTP 404)')
|
if (args.includes('--paginate'))
|
||||||
|
return JSON.stringify(
|
||||||
|
releasePages ?? [draft === undefined ? [] : [releaseEntry(`v${version}`, draft)]],
|
||||||
|
)
|
||||||
|
// GitHub's tag endpoint only returns published releases, even with write access.
|
||||||
|
if (draft !== false) throw new Error('gh: Not Found (HTTP 404)')
|
||||||
return JSON.stringify({ draft, assets })
|
return JSON.stringify({ draft, assets })
|
||||||
}
|
}
|
||||||
if (args[1] === 'create') draft = true
|
if (args[1] === 'create') draft = true
|
||||||
@@ -51,6 +64,7 @@ describe('CLI release publication', () => {
|
|||||||
)
|
)
|
||||||
await writeFile(join(directory, 'notes.md'), 'Test release notes')
|
await writeFile(join(directory, 'notes.md'), 'Test release notes')
|
||||||
draft = undefined
|
draft = undefined
|
||||||
|
releasePages = undefined
|
||||||
source = 'release-commit'
|
source = 'release-commit'
|
||||||
calls.length = 0
|
calls.length = 0
|
||||||
run.mockClear()
|
run.mockClear()
|
||||||
@@ -88,6 +102,45 @@ describe('CLI release publication', () => {
|
|||||||
.filter((call) => call[0] === 'gh')
|
.filter((call) => call[0] === 'gh')
|
||||||
.every((call) => call[1] === 'api' || call[2] === 'download'),
|
.every((call) => call[1] === 'api' || call[2] === 'download'),
|
||||||
).toBe(true)
|
).toBe(true)
|
||||||
|
expect(calls.some((call) => call.includes('--paginate'))).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('finds an existing draft on a later release-list page when the tag endpoint returns 404', async () => {
|
||||||
|
draft = true
|
||||||
|
releasePages = [[releaseEntry('v0.0.1', true)], [releaseEntry(`v${version}`, true)]]
|
||||||
|
await publishCliRelease('publish', context(), run)
|
||||||
|
expect(draft).toBe(false)
|
||||||
|
expect(calls.some((call) => call.includes('--paginate') && call.includes('--slurp'))).toBe(true)
|
||||||
|
expect(calls.some((call) => call[2] === 'create' || call[2] === 'upload')).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not create a release when the draft-list request fails', async () => {
|
||||||
|
run
|
||||||
|
.mockImplementationOnce(() => 'release-commit')
|
||||||
|
.mockImplementationOnce(() => 'release-commit')
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
throw new Error('gh: Not Found (HTTP 404)')
|
||||||
|
})
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
throw new Error('gh: Forbidden (HTTP 403)')
|
||||||
|
})
|
||||||
|
await expect(publishCliRelease('stage', context(), run)).rejects.toThrow('HTTP 403')
|
||||||
|
expect(
|
||||||
|
run.mock.calls.filter(([command, args]) => command === 'gh' && args[0] !== 'api'),
|
||||||
|
).toHaveLength(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects malformed or ambiguous release lists before changing GitHub', async () => {
|
||||||
|
releasePages = [[{ name: 'missing tag' }]]
|
||||||
|
await expect(publishCliRelease('stage', context(), run)).rejects.toThrow(
|
||||||
|
'Invalid GitHub release list entry',
|
||||||
|
)
|
||||||
|
const release = releaseEntry(`v${version}`, true)
|
||||||
|
releasePages = [[release], [release]]
|
||||||
|
await expect(publishCliRelease('stage', context(), run)).rejects.toThrow(
|
||||||
|
'Multiple GitHub releases',
|
||||||
|
)
|
||||||
|
expect(calls.every((call) => call[0] === 'git' || call[1] === 'api')).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('rejects a source/tag mismatch before making any GitHub changes', async () => {
|
it('rejects a source/tag mismatch before making any GitHub changes', async () => {
|
||||||
|
|||||||
@@ -65,6 +65,37 @@ function parseRelease(value: unknown): Release {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function findRelease(repository: string, tag: string, run: Runner): Release | undefined {
|
||||||
|
try {
|
||||||
|
return parseRelease(JSON.parse(run('gh', ['api', `repos/${repository}/releases/tags/${tag}`])))
|
||||||
|
} catch (error) {
|
||||||
|
if (!(error instanceof Error) || !error.message.includes('HTTP 404')) throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
// The tag endpoint only returns published releases. Authenticated release lists include drafts.
|
||||||
|
const pages: unknown = JSON.parse(
|
||||||
|
run('gh', ['api', '--paginate', '--slurp', `repos/${repository}/releases?per_page=100`]),
|
||||||
|
)
|
||||||
|
assert.ok(Array.isArray(pages), 'Invalid GitHub release list')
|
||||||
|
let release: Release | undefined
|
||||||
|
for (const page of pages) {
|
||||||
|
assert.ok(Array.isArray(page), 'Invalid GitHub release list page')
|
||||||
|
for (const entry of page as unknown[]) {
|
||||||
|
assert.ok(
|
||||||
|
entry &&
|
||||||
|
typeof entry === 'object' &&
|
||||||
|
'tag_name' in entry &&
|
||||||
|
typeof entry.tag_name === 'string',
|
||||||
|
'Invalid GitHub release list entry',
|
||||||
|
)
|
||||||
|
if (entry.tag_name !== tag) continue
|
||||||
|
assert.ok(!release, 'Multiple GitHub releases found for the release tag')
|
||||||
|
release = parseRelease(entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return release
|
||||||
|
}
|
||||||
|
|
||||||
export async function publishCliRelease(
|
export async function publishCliRelease(
|
||||||
mode: 'stage' | 'publish',
|
mode: 'stage' | 'publish',
|
||||||
context: { tag: string; repository: string; outdir: string; notes: string },
|
context: { tag: string; repository: string; outdir: string; notes: string },
|
||||||
@@ -85,13 +116,7 @@ export async function publishCliRelease(
|
|||||||
`Local checksum mismatch: ${name}`,
|
`Local checksum mismatch: ${name}`,
|
||||||
)
|
)
|
||||||
|
|
||||||
const endpoint = `repos/${context.repository}/releases/tags/${context.tag}`
|
let release = findRelease(context.repository, context.tag, run)
|
||||||
let release: Release | undefined
|
|
||||||
try {
|
|
||||||
release = parseRelease(JSON.parse(run('gh', ['api', endpoint])))
|
|
||||||
} catch (error) {
|
|
||||||
if (!(error instanceof Error) || !error.message.includes('HTTP 404')) throw error
|
|
||||||
}
|
|
||||||
if (!release) {
|
if (!release) {
|
||||||
assert.equal(mode, 'stage', 'Release must be staged before publishing')
|
assert.equal(mode, 'stage', 'Release must be staged before publishing')
|
||||||
run('gh', [
|
run('gh', [
|
||||||
|
|||||||
Reference in New Issue
Block a user