Skip to content

Commit

Permalink
fix: don't throw release must be a draft if onTag policy was guessed
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Jun 24, 2016
1 parent 7c84f5f commit 26c89f0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function createPublisher(packager: Packager, options: PublishOption
}
else {
log(`Creating Github Publisher — user: ${info.user}, project: ${info.project}, version: ${packager.metadata.version}`)
return new GitHubPublisher(info.user, info.project, packager.metadata.version, options, options.publish!)
return new GitHubPublisher(info.user, info.project, packager.metadata.version, options, isPublishOptionGuessed)
}
}

Expand Down
42 changes: 24 additions & 18 deletions src/gitHubPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ export interface Publisher {
upload(file: string, artifactName?: string): Promise<any>
}

export type PublishPolicy = "onTag" | "onTagOrDraft" | "always" | "never"

export interface PublishOptions {
publish?: "onTag" | "onTagOrDraft" | "always" | "never" | null
publish?: PublishPolicy | null
githubToken?: string | null

draft?: boolean
Expand All @@ -32,17 +34,19 @@ export class GitHubPublisher implements Publisher {
private _releasePromise: BluebirdPromise<Release>

private readonly token: string
private readonly policy: PublishPolicy

get releasePromise(): Promise<Release | null> {
return this._releasePromise
}

constructor(private owner: string, private repo: string, version: string, private options: PublishOptions, private policy: string = "always") {
constructor(private owner: string, private repo: string, version: string, private options: PublishOptions, private isPublishOptionGuessed: boolean = false) {
if (isEmptyOrSpaces(options.githubToken)) {
throw new Error("GitHub Personal Access Token is not specified")
}

this.token = options.githubToken!
this.policy = options.publish!

this.tag = "v" + version
this._releasePromise = <BluebirdPromise<Release>>this.init()
Expand All @@ -54,30 +58,31 @@ export class GitHubPublisher implements Publisher {
const releases = await gitHubRequest<Array<Release>>(`/repos/${this.owner}/${this.repo}/releases`, this.token)
for (let release of releases) {
if (release.tag_name === this.tag) {
if (!release.draft) {
if (this.policy === "onTag") {
throw new Error("Release must be a draft")
}
else {
const message = `Release ${this.tag} is not a draft, artifacts will be not published`
if (this.policy === "always") {
warn(message)
}
else {
log(message)
}
return null
}
if (release.draft) {
return release
}
return release!

if (!this.isPublishOptionGuessed && this.policy === "onTag") {
throw new Error(`Release with tag ${this.tag} must be a draft`)
}

const message = `Release with tag ${this.tag} is not a draft, artifacts will be not published`
if (this.isPublishOptionGuessed || this.policy === "onTagOrDraft") {
log(message)
}
else {
warn(message)
}
return null
}
}

if (createReleaseIfNotExists) {
log(`Release this.tag doesn't exists, creating one`)
log(`Release with tag ${this.tag} doesn't exists, creating one`)
return this.createRelease()
}
else {
log(`Cannot found release with tag ${this.tag}, artifacts will be not published`)
return null
}
}
Expand Down Expand Up @@ -162,6 +167,7 @@ export class GitHubPublisher implements Publisher {
}

// test only
//noinspection JSUnusedGlobalSymbols
async getRelease(): Promise<any> {
return gitHubRequest<Release>(`/repos/${this.owner}/${this.repo}/releases/${this._releasePromise.value().id}`, this.token)
}
Expand Down

0 comments on commit 26c89f0

Please sign in to comment.