Skip to content

Commit

Permalink
fix: reupload again if failed with "502 Bad Gateway" (3 times)
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Mar 13, 2016
1 parent d1801ae commit f131e33
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"babel-plugin-array-includes": "^2.0.3",
"babel-plugin-transform-es2015-parameters": "^6.7.0",
"electron-download": "^2.0.0",
"eslint": "^2.3.0",
"eslint": "^2.4.0",
"eslint-plugin-ava": "sindresorhus/eslint-plugin-ava",
"ghooks": "^1.0.3",
"json-parse-helpfulerror": "^1.0.3",
Expand Down
9 changes: 6 additions & 3 deletions src/gitHubPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class GitHubPublisher implements Publisher {

private async init(): Promise<Release> {
// 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
let releases = await gitHubRequest<Array<Release>>(`/repos/${this.owner}/${this.repo}/releases`, this.token)
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) {
Expand Down Expand Up @@ -75,6 +75,7 @@ export class GitHubPublisher implements Publisher {

const parsedUrl = parseUrl(release.upload_url.substring(0, release.upload_url.indexOf("{")) + "?name=" + fileName)
const fileStat = await stat(path)
let badGatewayCount = 0
uploadAttempt: for (let i = 0; i < 3; i++) {
const progressBar = (<ReadStream>process.stdin).isTTY ? new ProgressBar(`Uploading ${fileName} [:bar] :percent :etas`, {
total: fileStat.size,
Expand Down Expand Up @@ -107,8 +108,7 @@ export class GitHubPublisher implements Publisher {
}
catch (e) {
if (e instanceof HttpError) {
const httpError = <HttpError>e
if (httpError.response.statusCode === 422 && httpError.description != null && httpError.description.errors != null && httpError.description.errors[0].code === "already_exists") {
if (e.response.statusCode === 422 && e.description != null && e.description.errors != null && e.description.errors[0].code === "already_exists") {
// delete old artifact and re-upload
log("Artifact %s already exists, overwrite one", fileName)
const assets = await gitHubRequest<Array<Asset>>(`/repos/${this.owner}/${this.repo}/releases/${release.id}/assets`, this.token)
Expand All @@ -122,6 +122,9 @@ export class GitHubPublisher implements Publisher {
log("Artifact %s not found, trying to upload again", fileName)
continue
}
else if (e.response.statusCode === 502 && badGatewayCount++ < 3) {
continue
}
}

throw e
Expand Down
2 changes: 1 addition & 1 deletion test/src/ArtifactPublisherTest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from "ava-tf"
import test from "./helpers/avaEx"
import { GitHubPublisher } from "out/gitHubPublisher"
import { HttpError } from "out/gitHubRequest"
import { join } from "path"
Expand Down

0 comments on commit f131e33

Please sign in to comment.