Skip to content

Commit

Permalink
Add prerelease
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Cipollo committed Dec 13, 2019
1 parent 5ce7223 commit cb3417d
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}`.

Expand Down
18 changes: 10 additions & 8 deletions __tests__/Action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -36,7 +37,7 @@ 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()
})

Expand All @@ -49,7 +50,7 @@ describe("Action", () => {

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

Expand All @@ -58,7 +59,7 @@ describe("Action", () => {

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

Expand All @@ -73,7 +74,7 @@ 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()
})

Expand Down Expand Up @@ -114,7 +115,7 @@ describe("Action", () => {
expect(error).toEqual("error")
}

expect(updateMock).toBeCalledWith(id, tag, body, commit, draft, name)
expect(updateMock).toBeCalledWith(id, tag, body, commit, draft, name, prerelease)
expect(uploadMock).not.toBeCalled()
})

Expand All @@ -129,7 +130,7 @@ 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)
})

Expand All @@ -138,7 +139,7 @@ describe("Action", () => {

await action.perform()

expect(updateMock).toBeCalledWith(id, tag, body, commit, draft, name)
expect(updateMock).toBeCalledWith(id, tag, body, commit, draft, name, prerelease)
expect(uploadMock).not.toBeCalled()

})
Expand All @@ -148,7 +149,7 @@ describe("Action", () => {

await action.perform()

expect(updateMock).toBeCalledWith(id, tag, body, commit, draft, name)
expect(updateMock).toBeCalledWith(id, tag, body, commit, draft, name, prerelease)
expect(uploadMock).toBeCalledWith(artifacts, url)

})
Expand Down Expand Up @@ -194,6 +195,7 @@ describe("Action", () => {
commit: commit,
draft: draft,
name: name,
prerelease: prerelease,
tag: tag,
token: token,
readArtifact: () => artifactData
Expand Down
11 changes: 11 additions & 0 deletions __tests__/Inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''
Expand Down
4 changes: 2 additions & 2 deletions lib/Action.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Action {
}
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;
});
}
Expand All @@ -58,7 +58,7 @@ class Action {
}
updateRelease(id) {
return __awaiter(this, void 0, void 0, function* () {
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;
});
}
Expand Down
4 changes: 4 additions & 0 deletions lib/Inputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions lib/Releases.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,14 +37,15 @@ 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,
body: body,
name: name,
draft: draft,
owner: this.context.repo.owner,
prerelease: prerelease,
repo: this.context.repo.repo,
target_commitish: commitHash,
tag_name: tag
Expand Down
10 changes: 6 additions & 4 deletions src/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Action {
}

private async createOrUpdateRelease(): Promise<string> {
if(this.inputs.allowUpdates) {
if (this.inputs.allowUpdates) {
try {
const getResponse = await this.releases.getByTag(this.inputs.tag)
return await this.updateRelease(getResponse.data.id)
Expand All @@ -46,13 +46,14 @@ 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 noRelease(error:any): boolean {
private noRelease(error: any): boolean {
const errorMessage = new ErrorMessage(error)
return errorMessage.status == 404
}
Expand All @@ -64,7 +65,8 @@ 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
Expand Down
6 changes: 6 additions & 0 deletions src/Inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 10 additions & 4 deletions src/Releases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>>
Expand All @@ -19,7 +20,8 @@ export interface Releases {
body?: string,
commitHash?: string,
draft?: boolean,
name?: string
name?: string,
prerelease?: boolean
): Promise<Response<ReposCreateReleaseResponse>>

uploadArtifact(
Expand All @@ -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
Expand All @@ -72,14 +76,16 @@ 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,
body: body,
name: name,
draft: draft,
owner: this.context.repo.owner,
prerelease: prerelease,
repo: this.context.repo.repo,
target_commitish: commitHash,
tag_name: tag
Expand Down

0 comments on commit cb3417d

Please sign in to comment.