Update scripts

This commit is contained in:
Nick Cipollo
2019-12-12 18:46:17 -05:00
parent 7319d490c8
commit 455687cd65
6 changed files with 85 additions and 17 deletions

View File

@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}); });
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const ErrorMessage_1 = require("./ErrorMessage");
class Action { class Action {
constructor(inputs, releases, uploader) { constructor(inputs, releases, uploader) {
this.inputs = inputs; this.inputs = inputs;
@@ -17,12 +18,45 @@ class Action {
} }
perform() { perform() {
return __awaiter(this, void 0, void 0, function* () { 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; const artifacts = this.inputs.artifacts;
if (artifacts.length > 0) { 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; exports.Action = Action;

View File

@@ -4,18 +4,9 @@ const GithubError_1 = require("./GithubError");
class ErrorMessage { class ErrorMessage {
constructor(error) { constructor(error) {
this.error = error; this.error = error;
this.githubErrors = this.generateGithubErrors();
} }
toString() { generateGithubErrors() {
const message = this.error.message;
const errors = this.githubErrors();
if (errors.length > 0) {
return `${message}\nErrors:\n${this.errorBulletedList(errors)}`;
}
else {
return message;
}
}
githubErrors() {
const errors = this.error.errors; const errors = this.error.errors;
if (errors instanceof Array) { if (errors instanceof Array) {
return errors.map((err) => new GithubError_1.GithubError(err)); return errors.map((err) => new GithubError_1.GithubError(err));
@@ -24,6 +15,19 @@ class ErrorMessage {
return []; 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) { errorBulletedList(errors) {
return errors.map((err) => `- ${err}`).join("\n"); return errors.map((err) => `- ${err}`).join("\n");
} }

View File

@@ -4,6 +4,9 @@ class GithubError {
constructor(error) { constructor(error) {
this.error = error; this.error = error;
} }
get code() {
return this.error.code;
}
toString() { toString() {
const code = this.error.code; const code = this.error.code;
switch (code) { switch (code) {

View File

@@ -14,6 +14,10 @@ class CoreInputs {
this.artifactGlobber = artifactGlobber; this.artifactGlobber = artifactGlobber;
this.context = context; this.context = context;
} }
get allowUpdates() {
const allow = core.getInput('allowUpdates');
return allow == 'true';
}
get artifacts() { get artifacts() {
let artifacts = core.getInput('artifacts'); let artifacts = core.getInput('artifacts');
if (!artifacts) { if (!artifacts) {

View File

@@ -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) { uploadArtifact(assetUrl, contentLength, contentType, file, name) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
return this.git.repos.uploadReleaseAsset({ return this.git.repos.uploadReleaseAsset({

View File

@@ -7,8 +7,8 @@
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"clean": "rm -rf lib/*", "clean": "rm -rf lib/*",
"debug": "yarn install && yarn build", "debug": "yarn clean && yarn install && yarn build",
"release": "yarn install --production && yarn build", "release": "yarn clean && yarn install --production && yarn build",
"test": "jest" "test": "jest"
}, },
"repository": { "repository": {
@@ -25,14 +25,14 @@
"dependencies": { "dependencies": {
"@actions/core": "^1.0.0", "@actions/core": "^1.0.0",
"@actions/github": "^1.0.0", "@actions/github": "^1.0.0",
"@types/glob": "^7.1.1",
"@types/node": "^12.0.4",
"add": "^2.0.6", "add": "^2.0.6",
"glob": "^7.1.4", "glob": "^7.1.4",
"global": "^4.4.0" "global": "^4.4.0"
}, },
"devDependencies": { "devDependencies": {
"@types/glob": "^7.1.1",
"@types/jest": "^24.0.13", "@types/jest": "^24.0.13",
"@types/node": "^12.0.4",
"jest": "^24.8.0", "jest": "^24.8.0",
"jest-circus": "^24.7.1", "jest-circus": "^24.7.1",
"ts-jest": "^24.0.2", "ts-jest": "^24.0.2",