Skip to content

Commit

Permalink
fix: Don't throw an error if version doesn't start with `v´
Browse files Browse the repository at this point in the history
Closes #743
  • Loading branch information
develar committed Sep 18, 2016
1 parent 84225d7 commit ec8e69e
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 48 deletions.
50 changes: 39 additions & 11 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getRepositoryInfo } from "./repositoryInfo"
import { DIR_TARGET } from "./targets/targetFactory"
import { BintrayPublisher } from "./publish/BintrayPublisher"
import { BintrayOptions } from "./publish/bintray"
import { PublishConfiguration, GithubPublishConfiguration } from "./options/publishOptions"

//noinspection JSUnusedLocalSymbols
const __awaiter = require("./util/awaiter")
Expand Down Expand Up @@ -269,18 +270,23 @@ function publishManager(packager: Packager, publishTasks: Array<BluebirdPromise<
}

if (publishers == null && options.githubToken != null) {
publishers = ["github"]
publishers = {provider: "github"}
}
// if both tokens are set — still publish to github (because default publisher is github)
if (publishers == null && options.bintrayToken != null) {
publishers = ["bintray"]
publishers = {provider: "bintray"}
}
}

for (let publisherName of asArray(publishers)) {
for (let publishConfig of asArray(publishers)) {
if (typeof publishConfig === "string") {
publishConfig = {provider: publishConfig}
}

const publisherName = publishConfig.provider
let publisher = nameToPublisher.get(publisherName)
if (publisher == null) {
publisher = createPublisher(packager, options, publisherName, isPublishOptionGuessed)
publisher = createPublisher(packager, options, publishConfig, isPublishOptionGuessed)
nameToPublisher.set(publisherName, publisher)
}

Expand All @@ -292,9 +298,13 @@ function publishManager(packager: Packager, publishTasks: Array<BluebirdPromise<
})
}

export async function createPublisher(packager: Packager, options: PublishOptions, publisherName: string, isPublishOptionGuessed: boolean = false): Promise<Publisher | null> {
const info = await getRepositoryInfo(packager.metadata, packager.devMetadata)
if (info == null) {
export async function createPublisher(packager: Packager, options: PublishOptions, publishConfig: PublishConfiguration | GithubPublishConfiguration, isPublishOptionGuessed: boolean = false): Promise<Publisher | null> {
async function getInfo() {
const info = await getRepositoryInfo(packager.metadata, packager.devMetadata)
if (info != null) {
return info
}

if (isPublishOptionGuessed) {
return null
}
Expand All @@ -303,12 +313,30 @@ export async function createPublisher(packager: Packager, options: PublishOption
throw new Error(`Please specify 'repository' in the dev package.json ('${packager.devPackageFile}')`)
}

if (publisherName === "github") {
if (publishConfig.provider === "github") {
const config = <GithubPublishConfiguration>publishConfig
let user = config.owner
let repo = config.repo
if (!user || !repo) {
const info = await getInfo()
if (info == null) {
return null
}

user = info.user
repo = info.project
}

const version = packager.metadata.version!
log(`Creating Github Publisher — user: ${info.user}, project: ${info.project}, version: ${version}`)
return new GitHubPublisher(info.user, info.project, version, options, isPublishOptionGuessed)
log(`Creating Github Publisher — user: ${user}, project: ${repo}, version: ${version}`)
return new GitHubPublisher(user, repo, version, options, isPublishOptionGuessed, config)
}
if (publisherName === "bintray") {
if (publishConfig.provider === "bintray") {
const info = await getInfo()
if (info == null) {
return null
}

const version = packager.metadata.version!
//noinspection ReservedWordAsName
const bintrayInfo: BintrayOptions = {user: info.user, package: info.project, repo: "generic"}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export { DIR_TARGET, DEFAULT_TARGET } from "./targets/targetFactory"
export { BuildOptions, build, CliOptions, createTargets } from "./builder"
export { PublishOptions, Publisher } from "./publish/publisher"
export { AppMetadata, DevMetadata, Platform, Arch, archFromString, BuildMetadata, WinBuildOptions, LinuxBuildOptions, CompressionLevel } from "./metadata"
export { MacOptions, DmgOptions, MasBuildOptions } from "./macOptions"
export { MacOptions, DmgOptions, MasBuildOptions } from "./options/macOptions"
2 changes: 1 addition & 1 deletion src/macPackager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PlatformPackager, BuildInfo, Target } from "./platformPackager"
import { Platform, Arch } from "./metadata"
import { MasBuildOptions, MacOptions } from "./macOptions"
import { MasBuildOptions, MacOptions } from "./options/macOptions"
import * as path from "path"
import { Promise as BluebirdPromise } from "bluebird"
import { log, warn, task } from "./util/log"
Expand Down
5 changes: 3 additions & 2 deletions src/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AsarOptions } from "asar-electron-builder"
import { PlatformPackager } from "./platformPackager"
import { MacOptions, DmgOptions, MasBuildOptions } from "./macOptions"
import { MacOptions, DmgOptions, MasBuildOptions } from "./options/macOptions"
import { PublishConfiguration } from "./options/publishOptions"

export interface Metadata {
readonly repository?: string | RepositoryInfo | null
Expand Down Expand Up @@ -206,7 +207,7 @@ export interface BuildMetadata {

readonly dereference?: boolean

readonly publish?: string | Array<string> | null
readonly publish?: string | Array<string> | null | PublishConfiguration
}

export interface AfterPackContext {
Expand Down
2 changes: 1 addition & 1 deletion src/macOptions.ts → src/options/macOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PlatformSpecificBuildOptions } from "./metadata"
import { PlatformSpecificBuildOptions } from "../metadata"

/*
### `.build.mac`
Expand Down
13 changes: 13 additions & 0 deletions src/options/publishOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type PublishProvider = "github" | "bintray"

export interface PublishConfiguration {
provider: PublishProvider
}

export interface GithubPublishConfiguration extends PublishConfiguration {
repo: string
version: string
owner: string

vPrefixedTagName?: boolean
}
10 changes: 4 additions & 6 deletions src/publish/gitHubPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { githubRequest, HttpError, doApiRequest } from "./restApiRequest"
import { Promise as BluebirdPromise } from "bluebird"
import { PublishPolicy, PublishOptions, Publisher } from "./publisher"
import { uploadFile } from "./uploader"
import { GithubPublishConfiguration } from "../options/publishOptions"

//noinspection JSUnusedLocalSymbols
const __awaiter = require("../util/awaiter")
Expand Down Expand Up @@ -38,7 +39,7 @@ export class GitHubPublisher implements Publisher {
return this._releasePromise
}

constructor(private owner: string, private repo: string, private version: string, private options: PublishOptions, private isPublishOptionGuessed: boolean = false) {
constructor(private owner: string, private repo: string, private version: string, private options: PublishOptions, private isPublishOptionGuessed: boolean = false, config?: GithubPublishConfiguration | null) {
if (isEmptyOrSpaces(options.githubToken)) {
throw new Error("GitHub Personal Access Token is not specified")
}
Expand All @@ -50,7 +51,7 @@ export class GitHubPublisher implements Publisher {
throw new Error(`Version must not starts with "v": ${version}`)
}

this.tag = `v${version}`
this.tag = config != null && config.vPrefixedTagName ? `v${version}` : version
this._releasePromise = this.token === "__test__" ? BluebirdPromise.resolve(<any>null) : <BluebirdPromise<Release>>this.init()
}

Expand All @@ -59,7 +60,7 @@ export class GitHubPublisher implements Publisher {
// we don't use "Get a release by tag name" because "tag name" means existing git tag, but we draft release and don't create git tag
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.tag_name === this.tag || release.tag_name === this.version) {
if (release.draft) {
return release
}
Expand All @@ -77,9 +78,6 @@ export class GitHubPublisher implements Publisher {
}
return null
}
else if (release.tag_name === this.version) {
throw new Error(`Tag name must starts with "v": ${release.tag_name}`)
}
}

if (createReleaseIfNotExists) {
Expand Down
2 changes: 1 addition & 1 deletion src/targets/dmg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { deepAssign } from "../util/deepAssign"
import * as path from "path"
import { log } from "../util/log"
import { Target, PlatformPackager } from "../platformPackager"
import { MacOptions, DmgOptions } from "../macOptions"
import { MacOptions, DmgOptions } from "../options/macOptions"
import { Promise as BluebirdPromise } from "bluebird"
import { debug, use } from "../util/util"

Expand Down
27 changes: 2 additions & 25 deletions test/src/ArtifactPublisherTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,6 @@ testAndIgnoreApiRate("prerelease", async () => {
}
})

testAndIgnoreApiRate("incorrect tag name", async () => {
const publisher = new GitHubPublisher("actperepo", "ecb2", "5.0", {
githubToken: token,
draft: false,
prerelease: true,
publish: "onTagOrDraft",
})

// await publisher.deleteOldReleases()
try {
await publisher.releasePromise
//noinspection ExceptionCaughtLocallyJS
throw new Error("No expected error")
}
catch (e) {
if (e.message !== 'Tag name must starts with "v": 5.0') {
throw e
}
}
finally {
await publisher.deleteRelease()
}
})

testAndIgnoreApiRate("GitHub upload org", async () => {
//noinspection SpellCheckingInspection
const publisher = new GitHubPublisher("builder-gh-test", "darpa", versionNumber(), {
Expand All @@ -146,12 +122,13 @@ test("create publisher", async () => {
}
const publisher = await createPublisher(packager, {
githubToken: "__test__",
}, "github")
}, {provider: "github", vPrefixedTagName: false})

assertThat(publisher).hasProperties({
"owner": "develar",
"repo": "test",
"token": "__test__",
"version": "2.0.0",
"tag": "2.0.0",
})
})

0 comments on commit ec8e69e

Please sign in to comment.