5 Commits

Author SHA1 Message Date
Nick Cipollo
e75a11bff3 Create 1.4.0 release
Some checks failed
PR Checks / check_pr (push) Has been cancelled
2019-12-13 16:07:38 -05:00
Nick Cipollo
cb3417d207 Add prerelease 2019-12-13 16:05:44 -05:00
Nick Cipollo
5ce722314c Fix tests 2019-12-13 15:52:52 -05:00
Nick Cipollo
734853d8c8 Clean up logging and fix error handling 2019-12-13 15:40:34 -05:00
Nick Cipollo
b25a3c63b3 Fixes #7 Check if release exists before creating 2019-12-13 14:27:57 -05:00
14 changed files with 152 additions and 95 deletions

View File

@@ -12,6 +12,7 @@ This action will create a github release and optionally upload an artifact to it
- **commit**: An optional commit reference. This will be used to create the tag if it does not exist.
- **draft**: Optionally marks this release as a draft release. Set to `true` to enable.
- **name**: An optional name for the release. If this is omitted the tag will be used.
- **prerelease**: Optionally marks this release as prerelease. Set to true to enable.
- **tag**: An optional tag for the release. If this is omitted the git ref will be used (if it is a tag).
- **token**: (**Required**) The Github token. Typically this will be `${{ secrets.GITHUB_TOKEN }}`.

View File

@@ -19,6 +19,7 @@ const commit = 'commit'
const draft = true
const id = 100
const name = 'name'
const prerelease = true
const tag = 'tag'
const token = 'token'
const url = 'http://api.example.com'
@@ -36,16 +37,29 @@ describe("Action", () => {
await action.perform()
expect(createMock).toBeCalledWith(tag, body, commit, draft, name)
expect(createMock).toBeCalledWith(tag, body, commit, draft, name, prerelease)
expect(uploadMock).not.toBeCalled()
})
it('creates release if no release exists to update', async () => {
const action = createAction(true, true)
const error = {
status: 404
}
getMock.mockRejectedValue(error)
await action.perform()
expect(createMock).toBeCalledWith(tag, body, commit, draft, name, prerelease)
expect(uploadMock).toBeCalledWith(artifacts, url)
})
it('creates release then uploads artifact', async () => {
const action = createAction(false, true)
await action.perform()
expect(createMock).toBeCalledWith(tag, body, commit, draft, name)
expect(createMock).toBeCalledWith(tag, body, commit, draft, name, prerelease)
expect(uploadMock).toBeCalledWith(artifacts, url)
})
@@ -60,12 +74,12 @@ describe("Action", () => {
expect(error).toEqual("error")
}
expect(createMock).toBeCalledWith(tag, body, commit, draft, name)
expect(createMock).toBeCalledWith(tag, body, commit, draft, name, prerelease)
expect(uploadMock).not.toBeCalled()
})
it('throws error when get fails', async () => {
const action = createAction(true, false)
const action = createAction(true, true)
const error = {
errors: [
{
@@ -73,7 +87,7 @@ describe("Action", () => {
}
]
}
createMock.mockRejectedValue(error)
getMock.mockRejectedValue("error")
expect.hasAssertions()
@@ -82,34 +96,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, prerelease)
expect(uploadMock).not.toBeCalled()
})
it('throws error when upload fails', async () => {
@@ -123,46 +130,28 @@ describe("Action", () => {
expect(error).toEqual("error")
}
expect(createMock).toBeCalledWith(tag, body, commit, draft, name)
expect(createMock).toBeCalledWith(tag, body, commit, draft, name, prerelease)
expect(uploadMock).toBeCalledWith(artifacts, url)
})
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, prerelease)
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, prerelease)
expect(uploadMock).toBeCalledWith(artifacts, url)
})
function createAction(allowUpdates: boolean, hasArtifact: boolean): Action {
@@ -206,6 +195,7 @@ describe("Action", () => {
commit: commit,
draft: draft,
name: name,
prerelease: prerelease,
tag: tag,
token: token,
readArtifact: () => artifactData

View File

@@ -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)
})
})

View File

@@ -133,6 +133,17 @@ describe('Inputs', () => {
})
})
describe('prerelase', () => {
it('returns false', () => {
expect(inputs.prerelease).toBe(false)
})
it('returns true', () => {
mockGetInput.mockReturnValue('true')
expect(inputs.prerelease).toBe(true)
})
})
describe('tag', () => {
it('returns input tag', () => {
mockGetInput.mockReturnValue('tag')

View File

@@ -29,6 +29,9 @@ inputs:
name:
description: 'An optional name for the release. If this is omitted the tag will be used.'
default: ''
prerelease:
description: "Optionally marks this release as prerelease. Set to true to enable."
default: ''
tag:
description: 'An optional tag for the release. If this is omitted the git ref will be used (if it is a tag).'
default: ''

View File

@@ -27,34 +27,38 @@ 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();
}
});
}
createRelease() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.releases.create(this.inputs.tag, this.inputs.body, this.inputs.commit, this.inputs.draft, this.inputs.name);
const response = yield this.releases.create(this.inputs.tag, this.inputs.body, this.inputs.commit, this.inputs.draft, this.inputs.name, this.inputs.prerelease);
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);
const response = yield this.releases.update(id, this.inputs.tag, this.inputs.body, this.inputs.commit, this.inputs.draft, this.inputs.name, this.inputs.prerelease);
return response.data.upload_url;
});
}

View File

@@ -15,17 +15,21 @@ class ErrorMessage {
return [];
}
}
get status() {
return this.error.status;
}
hasErrorWithCode(code) {
return this.githubErrors.some((err) => err.code == code);
}
toString() {
const message = this.error.message;
const errors = this.githubErrors;
const status = this.status;
if (errors.length > 0) {
return `${message}\nErrors:\n${this.errorBulletedList(errors)}`;
return `Error ${status}: ${message}\nErrors:\n${this.errorBulletedList(errors)}`;
}
else {
return message;
return `Error ${status}: ${message}`;
}
}
errorBulletedList(errors) {

View File

@@ -58,6 +58,10 @@ class CoreInputs {
}
return this.tag;
}
get prerelease() {
const draft = core.getInput('prerelease');
return draft == 'true';
}
get tag() {
const tag = core.getInput('tag');
if (tag) {

View File

@@ -14,13 +14,14 @@ class GithubReleases {
this.context = context;
this.git = git;
}
create(tag, body, commitHash, draft, name) {
create(tag, body, commitHash, draft, name, prerelease) {
return __awaiter(this, void 0, void 0, function* () {
return this.git.repos.createRelease({
body: body,
name: name,
draft: draft,
owner: this.context.repo.owner,
prerelease: prerelease,
repo: this.context.repo.repo,
target_commitish: commitHash,
tag_name: tag
@@ -36,7 +37,7 @@ class GithubReleases {
});
});
}
update(id, tag, body, commitHash, draft, name) {
update(id, tag, body, commitHash, draft, name, prerelease) {
return __awaiter(this, void 0, void 0, function* () {
return this.git.repos.updateRelease({
release_id: id,
@@ -44,6 +45,7 @@ class GithubReleases {
name: name,
draft: draft,
owner: this.context.repo.owner,
prerelease: prerelease,
repo: this.context.repo.repo,
target_commitish: commitHash,
tag_name: tag

10
node_modules/.yarn-integrity generated vendored
View File

@@ -643,5 +643,13 @@
"yargs@^13.3.0": "https://registry.yarnpkg.com/yargs/-/yargs-13.3.0.tgz#4c657a55e07e5f2cf947f8a366567c04a0dedc83"
},
"files": [],
"artifacts": {}
"artifacts": {
"fsevents@1.2.9": [
"lib",
"lib/binding",
"lib/binding/Release",
"lib/binding/Release/node-v72-darwin-x64",
"lib/binding/Release/node-v72-darwin-x64/fse.node"
]
}
}

View File

@@ -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 await this.updateRelease(getResponse.data.id)
} catch (error) {
if (this.noRelease(error)) {
return await this.createRelease()
} else {
throw error
}
}
} else {
return await this.createRelease()
}
}
@@ -41,28 +46,27 @@ export class Action {
this.inputs.body,
this.inputs.commit,
this.inputs.draft,
this.inputs.name
this.inputs.name,
this.inputs.prerelease
)
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.status == 404
}
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,
this.inputs.body,
this.inputs.commit,
this.inputs.draft,
this.inputs.name
this.inputs.name,
this.inputs.prerelease
)
return response.data.upload_url

View File

@@ -18,6 +18,10 @@ export class ErrorMessage {
}
}
get status():number {
return this.error.status
}
hasErrorWithCode(code: String): boolean {
return this.githubErrors.some((err) => err.code == code)
}
@@ -25,10 +29,11 @@ export class ErrorMessage {
toString(): string {
const message = this.error.message
const errors = this.githubErrors
const status = this.status
if (errors.length > 0) {
return `${message}\nErrors:\n${this.errorBulletedList(errors)}`
return `Error ${status}: ${message}\nErrors:\n${this.errorBulletedList(errors)}`
} else {
return message
return `Error ${status}: ${message}`
}
}

View File

@@ -11,6 +11,7 @@ export interface Inputs {
readonly commit: string
readonly draft: boolean
readonly name: string
readonly prerelease: boolean
readonly tag: string
readonly token: string
}
@@ -77,6 +78,11 @@ export class CoreInputs implements Inputs {
return this.tag
}
get prerelease(): boolean {
const draft = core.getInput('prerelease')
return draft == 'true'
}
get tag(): string {
const tag = core.getInput('tag')
if (tag) {

View File

@@ -8,7 +8,8 @@ export interface Releases {
body?: string,
commitHash?: string,
draft?: boolean,
name?: string
name?: string,
prerelease?: boolean
): Promise<Response<ReposCreateReleaseResponse>>
getByTag(tag: string): Promise<Response<ReposGetReleaseByTagResponse>>
@@ -19,7 +20,8 @@ export interface Releases {
body?: string,
commitHash?: string,
draft?: boolean,
name?: string
name?: string,
prerelease?: boolean
): Promise<Response<ReposCreateReleaseResponse>>
uploadArtifact(
@@ -45,13 +47,15 @@ export class GithubReleases implements Releases {
body?: string,
commitHash?: string,
draft?: boolean,
name?: string
name?: string,
prerelease?: boolean
): Promise<Response<ReposCreateReleaseResponse>> {
return this.git.repos.createRelease({
body: body,
name: name,
draft: draft,
owner: this.context.repo.owner,
prerelease: prerelease,
repo: this.context.repo.repo,
target_commitish: commitHash,
tag_name: tag
@@ -72,7 +76,8 @@ export class GithubReleases implements Releases {
body?: string,
commitHash?: string,
draft?: boolean,
name?: string
name?: string,
prerelease?: boolean
): Promise<Response<ReposCreateReleaseResponse>> {
return this.git.repos.updateRelease({
release_id: id,
@@ -80,6 +85,7 @@ export class GithubReleases implements Releases {
name: name,
draft: draft,
owner: this.context.repo.owner,
prerelease: prerelease,
repo: this.context.repo.repo,
target_commitish: commitHash,
tag_name: tag