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