diff --git a/lib/Action.js b/lib/Action.js index dc49bf2..ac7a4d0 100644 --- a/lib/Action.js +++ b/lib/Action.js @@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", { value: true }); +const ErrorMessage_1 = require("./ErrorMessage"); class Action { constructor(inputs, releases, uploader) { this.inputs = inputs; @@ -17,12 +18,45 @@ class Action { } perform() { return __awaiter(this, void 0, void 0, function* () { - const createResult = yield this.releases.create(this.inputs.tag, this.inputs.body, this.inputs.commit, this.inputs.draft, this.inputs.name); + const uploadUrl = yield this.createOrUpdateRelease(); const artifacts = this.inputs.artifacts; if (artifacts.length > 0) { - yield this.uploader.uploadArtifacts(artifacts, createResult.data.upload_url); + yield this.uploader.uploadArtifacts(artifacts, uploadUrl); } }); } + createOrUpdateRelease() { + return __awaiter(this, void 0, void 0, function* () { + try { + return yield this.createRelease(); + } + catch (error) { + if (this.releaseAlreadyExisted(error) && this.inputs.allowUpdates) { + return this.updateRelease(); + } + else { + throw error; + } + } + }); + } + 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); + return response.data.upload_url; + }); + } + releaseAlreadyExisted(error) { + const errorMessage = new ErrorMessage_1.ErrorMessage(error); + return errorMessage.hasErrorWithCode('already_exists'); + } + updateRelease() { + return __awaiter(this, void 0, void 0, function* () { + const getResponse = yield this.releases.getByTag(this.inputs.tag); + const id = getResponse.data.id; + const response = yield this.releases.update(id, this.inputs.tag, this.inputs.body, this.inputs.commit, this.inputs.draft, this.inputs.name); + return response.data.upload_url; + }); + } } exports.Action = Action; diff --git a/lib/ErrorMessage.js b/lib/ErrorMessage.js index 588c064..e0fd1a5 100644 --- a/lib/ErrorMessage.js +++ b/lib/ErrorMessage.js @@ -4,18 +4,9 @@ const GithubError_1 = require("./GithubError"); class ErrorMessage { constructor(error) { this.error = error; + this.githubErrors = this.generateGithubErrors(); } - toString() { - const message = this.error.message; - const errors = this.githubErrors(); - if (errors.length > 0) { - return `${message}\nErrors:\n${this.errorBulletedList(errors)}`; - } - else { - return message; - } - } - githubErrors() { + generateGithubErrors() { const errors = this.error.errors; if (errors instanceof Array) { return errors.map((err) => new GithubError_1.GithubError(err)); @@ -24,6 +15,19 @@ class ErrorMessage { return []; } } + hasErrorWithCode(code) { + return this.githubErrors.some((err) => err.code == code); + } + toString() { + const message = this.error.message; + const errors = this.githubErrors; + if (errors.length > 0) { + return `${message}\nErrors:\n${this.errorBulletedList(errors)}`; + } + else { + return message; + } + } errorBulletedList(errors) { return errors.map((err) => `- ${err}`).join("\n"); } diff --git a/lib/GithubError.js b/lib/GithubError.js index 36358eb..40ea7c4 100644 --- a/lib/GithubError.js +++ b/lib/GithubError.js @@ -4,6 +4,9 @@ class GithubError { constructor(error) { this.error = error; } + get code() { + return this.error.code; + } toString() { const code = this.error.code; switch (code) { diff --git a/lib/Inputs.js b/lib/Inputs.js index 1885922..d995e36 100644 --- a/lib/Inputs.js +++ b/lib/Inputs.js @@ -14,6 +14,10 @@ class CoreInputs { this.artifactGlobber = artifactGlobber; this.context = context; } + get allowUpdates() { + const allow = core.getInput('allowUpdates'); + return allow == 'true'; + } get artifacts() { let artifacts = core.getInput('artifacts'); if (!artifacts) { diff --git a/lib/Releases.js b/lib/Releases.js index d139dc3..d6499b4 100644 --- a/lib/Releases.js +++ b/lib/Releases.js @@ -27,6 +27,29 @@ class GithubReleases { }); }); } + getByTag(tag) { + return __awaiter(this, void 0, void 0, function* () { + return this.git.repos.getReleaseByTag({ + owner: this.context.repo.owner, + repo: this.context.repo.repo, + tag: tag + }); + }); + } + update(id, tag, body, commitHash, draft, name) { + 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, + repo: this.context.repo.repo, + target_commitish: commitHash, + tag_name: tag + }); + }); + } uploadArtifact(assetUrl, contentLength, contentType, file, name) { return __awaiter(this, void 0, void 0, function* () { return this.git.repos.uploadReleaseAsset({ diff --git a/package.json b/package.json index 0842375..fff34be 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,8 @@ "scripts": { "build": "tsc", "clean": "rm -rf lib/*", - "debug": "yarn install && yarn build", - "release": "yarn install --production && yarn build", + "debug": "yarn clean && yarn install && yarn build", + "release": "yarn clean && yarn install --production && yarn build", "test": "jest" }, "repository": { @@ -25,14 +25,14 @@ "dependencies": { "@actions/core": "^1.0.0", "@actions/github": "^1.0.0", + "@types/glob": "^7.1.1", + "@types/node": "^12.0.4", "add": "^2.0.6", "glob": "^7.1.4", "global": "^4.4.0" }, "devDependencies": { - "@types/glob": "^7.1.1", "@types/jest": "^24.0.13", - "@types/node": "^12.0.4", "jest": "^24.8.0", "jest-circus": "^24.7.1", "ts-jest": "^24.0.2",