diff --git a/__tests__/Action.test.ts b/__tests__/Action.test.ts index 120646b..985ae1e 100644 --- a/__tests__/Action.test.ts +++ b/__tests__/Action.test.ts @@ -43,16 +43,12 @@ describe("Action", () => { it('creates release if no release exists to update', async () => { const action = createAction(true, true) const error = { - errors: [ - { - code: 'missing' - } - ] + status: 404 } getMock.mockRejectedValue(error) await action.perform() - + expect(createMock).toBeCalledWith(tag, body, commit, draft, name) expect(uploadMock).toBeCalledWith(artifacts, url) }) diff --git a/__tests__/ErrorMessage.test.ts b/__tests__/ErrorMessage.test.ts index 65596b4..ec8bb4f 100644 --- a/__tests__/ErrorMessage.test.ts +++ b/__tests__/ErrorMessage.test.ts @@ -14,15 +14,16 @@ describe('ErrorMessage', () => { code: 'already_exists', resource: 'release' } - ] + ], + status: 422 } - it('does not have error', ()=> { + it('does not have error', () => { const errorMessage = new ErrorMessage(error) expect(errorMessage.hasErrorWithCode('missing_field')).toBeFalsy() }) - it('has error', ()=> { + it('has error', () => { const errorMessage = new ErrorMessage(error) expect(errorMessage.hasErrorWithCode('missing')).toBeTruthy() }) @@ -41,22 +42,30 @@ describe('ErrorMessage', () => { code: 'already_exists', resource: 'release' } - ] + ], + status: 422 } const errorMessage = new ErrorMessage(error) - const expectedString = "something bad happened\nErrors:\n- release does not exist.\n- release already exists." + const expectedString = "Error 422: something bad happened\nErrors:\n- release does not exist.\n- release already exists." expect(errorMessage.toString()).toBe(expectedString) }) it('generates message without errors', () => { const error = { - message: 'something bad happened' + message: 'something bad happened', + status: 422 } const errorMessage = new ErrorMessage(error) - expect(errorMessage.toString()).toBe('something bad happened') + expect(errorMessage.toString()).toBe('Error 422: something bad happened') + }) + + it('provides error status', () => { + const error = { status: 404 } + const errorMessage = new ErrorMessage(error) + expect(errorMessage.status).toBe(404) }) }) \ No newline at end of file diff --git a/lib/Action.js b/lib/Action.js index ac7a4d0..0e375c4 100644 --- a/lib/Action.js +++ b/lib/Action.js @@ -27,16 +27,22 @@ class Action { } createOrUpdateRelease() { return __awaiter(this, void 0, void 0, function* () { - try { - return yield this.createRelease(); + if (this.inputs.allowUpdates) { + try { + const getResponse = yield this.releases.getByTag(this.inputs.tag); + return yield this.updateRelease(getResponse.data.id); + } + catch (error) { + if (this.noRelease(error)) { + return yield this.createRelease(); + } + else { + throw error; + } + } } - catch (error) { - if (this.releaseAlreadyExisted(error) && this.inputs.allowUpdates) { - return this.updateRelease(); - } - else { - throw error; - } + else { + return yield this.createRelease(); } }); } @@ -46,14 +52,12 @@ class Action { return response.data.upload_url; }); } - releaseAlreadyExisted(error) { + noRelease(error) { const errorMessage = new ErrorMessage_1.ErrorMessage(error); - return errorMessage.hasErrorWithCode('already_exists'); + return errorMessage.status == 404; } - updateRelease() { + updateRelease(id) { return __awaiter(this, void 0, void 0, function* () { - const getResponse = yield this.releases.getByTag(this.inputs.tag); - const id = getResponse.data.id; const response = yield this.releases.update(id, this.inputs.tag, this.inputs.body, this.inputs.commit, this.inputs.draft, this.inputs.name); return response.data.upload_url; });