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

Improve error handling for non existent releases #178

Merged
merged 16 commits into from
Nov 21, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 47 additions & 14 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26319,13 +26319,41 @@ const addArtifact = async (inputs, releaseId) => {
return artifact
}

const createDraftRelease = async (inputs, newVersion) => {
try {
const run = runSpawn()
const releaseCommitHash = await run('git', ['rev-parse', 'HEAD'])

logInfo(`Creating draft release from commit: ${releaseCommitHash}`)

const { data: draftRelease } = await callApi(
{
method: 'POST',
endpoint: 'release',
body: {
version: newVersion,
target: releaseCommitHash,
},
},
inputs
)

logInfo(`Draft release created successfully`)

return draftRelease
} catch (err) {
throw new Error(`Unable to create draft release: ${err.message}`)
}
}

module.exports = async function ({ context, inputs, packageVersion }) {
logInfo('** Starting Opening Release PR **')
const run = runSpawn()

if (!packageVersion) {
throw new Error('packageVersion is missing!')
}

const newVersion = `${inputs['version-prefix']}${packageVersion}`

const branchName = `release/${newVersion}`
Expand All @@ -26341,19 +26369,7 @@ module.exports = async function ({ context, inputs, packageVersion }) {

await run('git', ['push', 'origin', branchName])

const releaseCommitHash = await run('git', ['rev-parse', 'HEAD'])

const { data: draftRelease } = await callApi(
{
method: 'POST',
endpoint: 'release',
body: {
version: newVersion,
target: releaseCommitHash,
},
},
inputs
)
const draftRelease = await createDraftRelease(inputs, newVersion)

logInfo(`New version ${newVersion}`)

Expand Down Expand Up @@ -26448,6 +26464,24 @@ module.exports = async function ({ github, context, inputs }) {

const { opticUrl, npmTag, version, id } = releaseMeta

try {
const { data: draftRelease } = await github.rest.repos.getRelease({
owner,
repo,
release_id: id,
})

if (!draftRelease) {
core.setFailed(`Couldn't find draft release to publish. Aborting.`)
return
}
} catch (err) {
core.setFailed(
`Couldn't find draft release to publish. Aborting. Error: ${err.message}`
)
return
}

const run = runSpawn()
const branchName = `release/${version}`

Expand Down Expand Up @@ -26513,7 +26547,6 @@ module.exports = async function ({ github, context, inputs }) {
core.setFailed(`Unable to update the semver tags ${err.message}`)
}

// TODO: What if PR was closed, reopened and then merged. The draft release would have been deleted!
try {
const { data: release } = await callApi(
{
Expand Down
42 changes: 29 additions & 13 deletions src/openPr.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,41 @@ const addArtifact = async (inputs, releaseId) => {
return artifact
}

const createDraftRelease = async (inputs, newVersion) => {
try {
const run = runSpawn()
const releaseCommitHash = await run('git', ['rev-parse', 'HEAD'])

logInfo(`Creating draft release from commit: ${releaseCommitHash}`)

const { data: draftRelease } = await callApi(
{
method: 'POST',
endpoint: 'release',
body: {
version: newVersion,
target: releaseCommitHash,
},
},
inputs
)

logInfo(`Draft release created successfully`)

return draftRelease
} catch (err) {
throw new Error(`Unable to create draft release: ${err.message}`)
}
}

module.exports = async function ({ context, inputs, packageVersion }) {
logInfo('** Starting Opening Release PR **')
const run = runSpawn()

if (!packageVersion) {
throw new Error('packageVersion is missing!')
}

const newVersion = `${inputs['version-prefix']}${packageVersion}`

const branchName = `release/${newVersion}`
Expand All @@ -75,19 +103,7 @@ module.exports = async function ({ context, inputs, packageVersion }) {

await run('git', ['push', 'origin', branchName])

const releaseCommitHash = await run('git', ['rev-parse', 'HEAD'])

const { data: draftRelease } = await callApi(
{
method: 'POST',
endpoint: 'release',
body: {
version: newVersion,
target: releaseCommitHash,
},
},
inputs
)
const draftRelease = await createDraftRelease(inputs, newVersion)

logInfo(`New version ${newVersion}`)

Expand Down
19 changes: 18 additions & 1 deletion src/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ module.exports = async function ({ github, context, inputs }) {

const { opticUrl, npmTag, version, id } = releaseMeta

try {
const { data: draftRelease } = await github.rest.repos.getRelease({
owner,
repo,
release_id: id,
})

if (!draftRelease) {
core.setFailed(`Couldn't find draft release to publish. Aborting.`)
return
}
} catch (err) {
core.setFailed(
`Couldn't find draft release to publish. Aborting. Error: ${err.message}`
)
return
}

const run = runSpawn()
const branchName = `release/${version}`

Expand Down Expand Up @@ -107,7 +125,6 @@ module.exports = async function ({ github, context, inputs }) {
core.setFailed(`Unable to update the semver tags ${err.message}`)
}

// TODO: What if PR was closed, reopened and then merged. The draft release would have been deleted!
try {
const { data: release } = await callApi(
{
Expand Down
15 changes: 15 additions & 0 deletions test/bump.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,18 @@ tap.test(
sinon.assert.calledOnce(stubs.attachArtifactStub)
}
)

tap.test('should not open Pr if create release draft fails', async t => {
const { openPr, stubs } = setup()
stubs.callApiStub = stubs.callApiStub.throws({ message: 'error message' })

try {
await openPr({
...DEFAULT_ACTION_DATA,
})
t.fail('should have thrown an error')
} catch (error) {
t.ok(error)
t.match(error.message, 'Unable to create draft release: error message')
}
})
42 changes: 42 additions & 0 deletions test/release.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ let deleteReleaseStub = sinon.stub().resolves()

let pullsGetStub = sinon.stub()
let createCommentStub = sinon.stub()
let getReleaseStub = sinon.stub().returns({ data: { draft: true } })

const DEFAULT_ACTION_DATA = {
github: {
rest: {
repos: {
deleteRelease: deleteReleaseStub,
getRelease: getReleaseStub,
},
issues: { createComment: createCommentStub },
pulls: { get: pullsGetStub },
Expand Down Expand Up @@ -570,3 +572,43 @@ tap.test(
sinon.assert.notCalled(stubs.notifyIssuesStub)
}
)

tap.test('Should fail when release is not found', async () => {
guilhermelimak marked this conversation as resolved.
Show resolved Hide resolved
const { release, stubs } = setup()

await release({
...DEFAULT_ACTION_DATA,
github: {
...DEFAULT_ACTION_DATA.github,
rest: {
...DEFAULT_ACTION_DATA.github.rest,
repos: {
...DEFAULT_ACTION_DATA.github.rest,
getRelease: sinon.stub().returns(undefined),
},
},
},
})

sinon.assert.called(stubs.coreStub.setFailed)
})

tap.test('Should not fail when release is not a draft', async () => {
const { release, stubs } = setup()

await release({
...DEFAULT_ACTION_DATA,
github: {
...DEFAULT_ACTION_DATA.github,
rest: {
...DEFAULT_ACTION_DATA.github.rest,
repos: {
...DEFAULT_ACTION_DATA.github.rest,
getRelease: sinon.stub().returns({ data: { draft: false } }),
},
},
},
})

sinon.assert.notCalled(stubs.coreStub.setFailed)
})