Fixes #7 Check if release exists before creating
This commit is contained in:
@@ -40,6 +40,23 @@ describe("Action", () => {
|
||||
expect(uploadMock).not.toBeCalled()
|
||||
})
|
||||
|
||||
it('creates release if no release exists to update', async () => {
|
||||
const action = createAction(true, true)
|
||||
const error = {
|
||||
errors: [
|
||||
{
|
||||
code: 'missing'
|
||||
}
|
||||
]
|
||||
}
|
||||
getMock.mockRejectedValue(error)
|
||||
|
||||
await action.perform()
|
||||
|
||||
expect(createMock).toBeCalledWith(tag, body, commit, draft, name)
|
||||
expect(uploadMock).toBeCalledWith(artifacts, url)
|
||||
})
|
||||
|
||||
it('creates release then uploads artifact', async () => {
|
||||
const action = createAction(false, true)
|
||||
|
||||
@@ -65,7 +82,7 @@ describe("Action", () => {
|
||||
})
|
||||
|
||||
it('throws error when get fails', async () => {
|
||||
const action = createAction(true, false)
|
||||
const action = createAction(true, true)
|
||||
const error = {
|
||||
errors: [
|
||||
{
|
||||
@@ -73,7 +90,7 @@ describe("Action", () => {
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
createMock.mockRejectedValue(error)
|
||||
getMock.mockRejectedValue("error")
|
||||
expect.hasAssertions()
|
||||
@@ -82,34 +99,27 @@ describe("Action", () => {
|
||||
} catch (error) {
|
||||
expect(error).toEqual("error")
|
||||
}
|
||||
|
||||
expect(createMock).toBeCalledWith(tag, body, commit, draft, name)
|
||||
|
||||
expect(getMock).toBeCalledWith(tag)
|
||||
expect(updateMock).not.toBeCalled()
|
||||
expect(uploadMock).not.toBeCalled()
|
||||
|
||||
|
||||
})
|
||||
|
||||
it('throws error when update fails', async () => {
|
||||
const action = createAction(true, false)
|
||||
const error = {
|
||||
errors: [
|
||||
{
|
||||
code: 'already_exists'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
createMock.mockRejectedValue(error)
|
||||
const action = createAction(true, true)
|
||||
|
||||
updateMock.mockRejectedValue("error")
|
||||
|
||||
expect.hasAssertions()
|
||||
try {
|
||||
await action.perform()
|
||||
} catch (error) {
|
||||
expect(error).toEqual("error")
|
||||
}
|
||||
|
||||
expect(createMock).toBeCalledWith(tag, body, commit, draft, name)
|
||||
|
||||
expect(updateMock).toBeCalledWith(id, tag, body, commit, draft, name)
|
||||
expect(uploadMock).not.toBeCalled()
|
||||
|
||||
})
|
||||
|
||||
it('throws error when upload fails', async () => {
|
||||
@@ -129,40 +139,22 @@ describe("Action", () => {
|
||||
|
||||
it('updates release but does not upload if no artifact', async () => {
|
||||
const action = createAction(true, false)
|
||||
const error = {
|
||||
errors: [
|
||||
{
|
||||
code: 'already_exists'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
createMock.mockRejectedValue(error)
|
||||
|
||||
await action.perform()
|
||||
|
||||
expect(createMock).toBeCalledWith(tag, body, commit, draft, name)
|
||||
|
||||
expect(updateMock).toBeCalledWith(id, tag, body, commit, draft, name)
|
||||
expect(uploadMock).not.toBeCalled()
|
||||
|
||||
|
||||
})
|
||||
|
||||
it('updates release then uploads artifact', async () => {
|
||||
const action = createAction(true, true)
|
||||
const error = {
|
||||
errors: [
|
||||
{
|
||||
code: 'already_exists'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
createMock.mockRejectedValue(error)
|
||||
|
||||
await action.perform()
|
||||
|
||||
expect(createMock).toBeCalledWith(tag, body, commit, draft, name)
|
||||
|
||||
expect(updateMock).toBeCalledWith(id, tag, body, commit, draft, name)
|
||||
expect(uploadMock).toBeCalledWith(artifacts, url)
|
||||
|
||||
|
||||
})
|
||||
|
||||
function createAction(allowUpdates: boolean, hasArtifact: boolean): Action {
|
||||
|
||||
@@ -24,14 +24,19 @@ export class Action {
|
||||
}
|
||||
|
||||
private async createOrUpdateRelease(): Promise<string> {
|
||||
try {
|
||||
return await this.createRelease()
|
||||
} catch (error) {
|
||||
if (this.releaseAlreadyExisted(error) && this.inputs.allowUpdates) {
|
||||
return this.updateRelease()
|
||||
} else {
|
||||
throw error
|
||||
if(this.inputs.allowUpdates) {
|
||||
try {
|
||||
const getResponse = await this.releases.getByTag(this.inputs.tag)
|
||||
return this.updateRelease(getResponse.data.id)
|
||||
} catch (error) {
|
||||
if (this.noRelease(error)) {
|
||||
return await this.createRelease()
|
||||
} else {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return await this.createRelease()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,15 +52,12 @@ export class Action {
|
||||
return response.data.upload_url
|
||||
}
|
||||
|
||||
private releaseAlreadyExisted(error: any): boolean {
|
||||
private noRelease(error:any): boolean {
|
||||
const errorMessage = new ErrorMessage(error)
|
||||
return errorMessage.hasErrorWithCode('already_exists')
|
||||
return errorMessage.hasErrorWithCode('missing')
|
||||
}
|
||||
|
||||
private async updateRelease(): Promise<string> {
|
||||
const getResponse = await this.releases.getByTag(this.inputs.tag)
|
||||
const id = getResponse.data.id
|
||||
|
||||
private async updateRelease(id: number): Promise<string> {
|
||||
const response = await this.releases.update(
|
||||
id,
|
||||
this.inputs.tag,
|
||||
|
||||
Reference in New Issue
Block a user