Skip to content

Commit

Permalink
feat: Add Ability to Create Pre-Releases and Releases
Browse files Browse the repository at this point in the history
Closes #446
  • Loading branch information
develar committed Jun 11, 2016
1 parent f1eaab8 commit e5b0c04
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .idea/runConfigurations/ArtifactPublisherTest.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Platform, Arch, archFromString } from "./metadata"
//noinspection JSUnusedLocalSymbols
const __awaiter = require("./awaiter")

export async function createPublisher(packager: Packager, options: BuildOptions, repoSlug: InfoRetriever, isPublishOptionGuessed: boolean = false): Promise<Publisher | null> {
export async function createPublisher(packager: Packager, options: PublishOptions, repoSlug: InfoRetriever, isPublishOptionGuessed: boolean = false): Promise<Publisher | null> {
const info = await repoSlug.getInfo(packager)
if (info == null) {
if (isPublishOptionGuessed) {
Expand All @@ -22,7 +22,7 @@ export async function createPublisher(packager: Packager, options: BuildOptions,
}
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.githubToken!, options.publish!)
return new GitHubPublisher(info.user, info.project, packager.metadata.version, options, options.publish!)
}
}

Expand Down Expand Up @@ -187,6 +187,13 @@ export async function build(rawOptions?: CliOptions): Promise<void> {
options.githubToken = process.env.GH_TOKEN
}

if (options.draft === undefined && !isEmptyOrSpaces(process.env.EP_DRAFT)) {
options.draft = process.env.EP_DRAFT.toLowerCase() === "true"
}
if (options.prerelease === undefined && !isEmptyOrSpaces(process.env.EP_PRELEASE)) {
options.prerelease = process.env.EP_PRELEASE.toLowerCase() === "true"
}

let isPublishOptionGuessed = false
if (options.publish === undefined) {
if (process.env.npm_lifecycle_event === "release") {
Expand Down
10 changes: 10 additions & 0 deletions src/cliOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ export function createYargs(): any {
describe: `Publish artifacts (to GitHub Releases), see ${underline("https://goo.gl/WMlr4n")}`,
choices: ["onTag", "onTagOrDraft", "always", "never"],
})
.option("draft", {
describe: "Create a draft (unpublished) release",
type: "boolean",
default: undefined,
})
.option("prerelease", {
describe: "Identify the release as a prerelease",
type: "boolean",
default: undefined,
})
.option("platform", {
describe: "The target platform (preferred to use --osx, --win or --linux)",
choices: ["osx", "win", "linux", "darwin", "win32", "all"],
Expand Down
26 changes: 19 additions & 7 deletions src/gitHubPublisher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Release, Asset } from "gh-release"
import { log, warn } from "./util"
import { log, warn, isEmptyOrSpaces } from "./util"
import { basename } from "path"
import { parse as parseUrl } from "url"
import * as mime from "mime"
Expand All @@ -21,21 +21,28 @@ export interface Publisher {
export interface PublishOptions {
publish?: "onTag" | "onTagOrDraft" | "always" | "never" | null
githubToken?: string | null

draft?: boolean
prerelease?: boolean
}

export class GitHubPublisher implements Publisher {
private tag: string
private _releasePromise: BluebirdPromise<Release>

private readonly token: string

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

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

this.token = options.githubToken!

this.tag = "v" + version
this._releasePromise = <BluebirdPromise<Release>>this.init()
}
Expand Down Expand Up @@ -144,20 +151,25 @@ export class GitHubPublisher implements Publisher {
return gitHubRequest<Release>(`/repos/${this.owner}/${this.repo}/releases`, this.token, {
tag_name: this.tag,
name: this.tag,
draft: true,
draft: this.options.draft == null || this.options.draft,
prerelease: this.options.prerelease != null && this.options.prerelease,
})
}

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

//noinspection JSUnusedGlobalSymbols
async deleteRelease(): Promise<void> {
async deleteRelease(): Promise<any> {
if (!this._releasePromise.isFulfilled()) {
return BluebirdPromise.resolve()
}

for (let i = 0; i < 3; i++) {
try {
return await
gitHubRequest<void>(`/repos/${this.owner}/${this.repo}/releases/${this._releasePromise.value().id}`, this.token, null, "DELETE")
return await gitHubRequest(`/repos/${this.owner}/${this.repo}/releases/${this._releasePromise.value().id}`, this.token, null, "DELETE")
}
catch (e) {
if (e instanceof HttpError && (e.response.statusCode === 405 || e.response.statusCode === 502)) {
Expand Down
32 changes: 29 additions & 3 deletions test/src/ArtifactPublisherTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import test from "./helpers/avaEx"
import { GitHubPublisher } from "out/gitHubPublisher"
import { HttpError } from "out/gitHubRequest"
import { join } from "path"
import * as assertThat from "should/as-function"

//noinspection JSUnusedLocalSymbols
const __awaiter = require("out/awaiter")
Expand Down Expand Up @@ -48,9 +49,30 @@ function testAndIgnoreApiRate(name: string, testFunction: () => Promise<any>) {
}

testAndIgnoreApiRate("GitHub upload", async () => {
const publisher = new GitHubPublisher("actperepo", "ecb2", versionNumber(), token)
const publisher = new GitHubPublisher("actperepo", "ecb2", versionNumber(), {
githubToken: token
})
try {
await publisher.upload(iconPath)
}
finally {
await publisher.deleteRelease()
}
})

testAndIgnoreApiRate("prerelease", async () => {
const publisher = new GitHubPublisher("actperepo", "ecb2", versionNumber(), {
githubToken: token,
draft: false,
prerelease: true,
})
try {
await publisher.upload(iconPath)
const r = await publisher.getRelease()
assertThat(r).has.properties({
prerelease: true,
draft: true,
})
}
finally {
await publisher.deleteRelease()
Expand All @@ -59,7 +81,9 @@ testAndIgnoreApiRate("GitHub upload", async () => {

testAndIgnoreApiRate("GitHub upload org", async () => {
//noinspection SpellCheckingInspection
const publisher = new GitHubPublisher("builder-gh-test", "darpa", versionNumber(), token)
const publisher = new GitHubPublisher("builder-gh-test", "darpa", versionNumber(), {
githubToken: token
})
try {
await publisher.upload(iconPath)
}
Expand All @@ -69,7 +93,9 @@ testAndIgnoreApiRate("GitHub upload org", async () => {
})

testAndIgnoreApiRate("GitHub overwrite on upload", async () => {
const publisher = new GitHubPublisher("actperepo", "ecb2", versionNumber(), token)
const publisher = new GitHubPublisher("actperepo", "ecb2", versionNumber(), {
githubToken: token
})
try {
await publisher.upload(iconPath)
await publisher.upload(iconPath)
Expand Down
8 changes: 5 additions & 3 deletions test/src/BuildTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { move, outputJson } from "fs-extra-p"
import { Promise as BluebirdPromise } from "bluebird"
import * as path from "path"
import { assertThat } from "./helpers/fileAssert"
import { archFromString, BuildOptions, Platform, Arch, PackagerOptions, DIR_TARGET, createTargets } from "out"
import { archFromString, BuildOptions, Platform, Arch, PackagerOptions, DIR_TARGET, createTargets, PublishOptions } from "out"
import { normalizeOptions } from "out/builder"
import { createYargs } from "out/cliOptions"

Expand All @@ -14,8 +14,10 @@ const __awaiter = require("out/awaiter")
test("cli", () => {
const yargs = createYargs()

const base = {
publish: <string>undefined,
const base: PublishOptions = {
publish: undefined,
draft: undefined,
prerelease: undefined,
}

function expected(opt: PackagerOptions): any {
Expand Down

0 comments on commit e5b0c04

Please sign in to comment.