diff --git a/dist/index.js b/dist/index.js index 8120150..f7b3ca5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -31663,19 +31663,19 @@ var __importStar = (this && this.__importStar) || function (mod) { }; Object.defineProperty(exports, "__esModule", ({ value: true })); const core = __importStar(__nccwpck_require__(2186)); -const maybe_create_temporary_branch_1 = __nccwpck_require__(5330); -const maybe_remove_temporary_tags_1 = __nccwpck_require__(624); const artifacts_1 = __nccwpck_require__(1870); const tags_1 = __nccwpck_require__(7816); const create_git_1 = __nccwpck_require__(6704); +const temporary_branch_1 = __nccwpck_require__(2786); async function main() { const git = (0, create_git_1.createGit)(); const tags = new tags_1.Tags(); const artifacts = new artifacts_1.Artifacts(git, tags); + const temporaryBranch = new temporary_branch_1.TemporaryBranch(git); Promise.resolve() - .then(maybe_create_temporary_branch_1.maybeCreateTemporaryBranch) + .then(() => temporaryBranch.create()) .then(() => artifacts.update()) - .then(maybe_remove_temporary_tags_1.maybeRemoveTemporaryBranch) + .then(() => temporaryBranch.delete()) .catch((error) => core.setFailed(`Failed to create and push artifacts: ${error}`)); } exports["default"] = main; @@ -31728,27 +31728,40 @@ class Artifacts { core.startGroup("📦 Creating artifacts"); try { await this.compile(); - await this.push(); + await this.deploy(); } catch (error) { - core.warning(`Failed creating artifacts: ${error}`); core.endGroup(); - throw error; + throw new Error(`Failed creating artifacts: ${error}`); } } async compile() { const result = await exec.exec(Artifacts.COMMAND); if (result !== 0) { - throw new Error("Failed to compile artifacts. Process exited with non-zero code."); + throw new Error("Failing to compile artifacts. Process exited with non-zero code."); } } - async push() { + async deploy() { + await this.add(); await this.tags.collect(); - await exec.exec(`git add -f ${Artifacts.TARGET_DIR}/*`); + await this.commit(); + await this.push(); + await this.tags.move(); + } + async add() { + const result = await exec.exec(`git add -f ${Artifacts.TARGET_DIR}/*`); + if (result !== 0) { + throw new Error("Failing to git-add the artifacts build. Process exited with non-zero code."); + } + } + async commit() { const commitResult = await this.git.commit("🚀 Build Artifacts"); - const pushingResult = await this.git.push(); core.info(`Committed changes: ${commitResult.summary.changes}`); - await this.tags.move(); + core.info(`Committed insertions: ${commitResult.summary.insertions}`); + core.info(`Committed deletions: ${commitResult.summary.deletions}`); + } + async push() { + const pushingResult = await this.git.push(); const messages = pushingResult?.remoteMessages.all.join("\n"); messages && core.info(`Pushed artifacts with messages: ${messages}`); } @@ -31759,44 +31772,6 @@ exports.Artifacts = Artifacts; /***/ }), /***/ 7816: -/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => { - -"use strict"; - -Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.Tags = void 0; -const create_git_1 = __nccwpck_require__(6704); -class Tags { - tags = []; - git = (0, create_git_1.createGit)(); - async collect() { - this.tags = (await this.git.tags(["--contains"])).all; - } - async move() { - await this.remove(); - await this.create(); - } - toString() { - if (!this.tags.length) { - return ""; - } - return [...this.tags].join(", "); - } - async remove() { - await this.git.tag(["-d", ...this.tags]); - await this.git.push(["--delete", "origin", ...this.tags]); - } - async create() { - await Promise.all(this.tags.map(async (tag) => this.git.addTag(tag))); - await this.git.pushTags(); - } -} -exports.Tags = Tags; - - -/***/ }), - -/***/ 5330: /***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { "use strict"; @@ -31825,31 +31800,52 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.maybeCreateTemporaryBranch = void 0; +exports.Tags = void 0; const create_git_1 = __nccwpck_require__(6704); const core = __importStar(__nccwpck_require__(2186)); -async function maybeCreateTemporaryBranch() { - const git = (0, create_git_1.createGit)(); - const _isDetached = await isDetached(); - if (!_isDetached) { - return; +class Tags { + tags = []; + git = (0, create_git_1.createGit)(); + async collect() { + this.tags = (await this.git.tags(["--contains"])).all; + core.info(`Collecting tags: ${this.toString()}`); + } + async move() { + core.info(`Moving tags: ${this.toString()}`); + await this.remove(); + await this.create(); + } + async remove() { + await this.git.tag(["-d", ...this.tags]); + const pushResult = await this.git.push([ + "--delete", + "origin", + ...this.tags, + ]); + this.pushInfo(pushResult, "Removed tags with messages"); + } + async create() { + await Promise.all(this.tags.map(async (tag) => this.git.addTag(tag))); + const pushResult = await this.git.pushTags(); + this.pushInfo(pushResult, "Pushed tags with messages"); + } + toString() { + if (!this.tags.length) { + return ""; + } + return [...this.tags].join(", "); + } + pushInfo(result, message) { + const messages = result?.remoteMessages.all.join("\n"); + messages && core.info(`${message}: ${messages}`); } - const currentHash = await git.revparse(["--short", "HEAD"]); - const temporaryBranchName = `ci-tag-${currentHash}`; - await git.checkoutLocalBranch(temporaryBranchName); - core.info(`Temporary branch ${temporaryBranchName} created successfully.`); - await git.push(["-u", "origin", temporaryBranchName]); -} -exports.maybeCreateTemporaryBranch = maybeCreateTemporaryBranch; -async function isDetached() { - const branchName = await (0, create_git_1.createGit)().revparse(["--abbrev-ref", "HEAD"]); - return branchName === "HEAD"; } +exports.Tags = Tags; /***/ }), -/***/ 624: +/***/ 2786: /***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { "use strict"; @@ -31878,25 +31874,61 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.maybeRemoveTemporaryBranch = void 0; -const create_git_1 = __nccwpck_require__(6704); +exports.TemporaryBranch = void 0; const core = __importStar(__nccwpck_require__(2186)); -async function maybeRemoveTemporaryBranch() { - const git = (0, create_git_1.createGit)(); - const _temporaryBranch = await temporaryBranch(); - if (!_temporaryBranch) { - return; +class TemporaryBranch { + git; + constructor(git) { + this.git = git; } - await git.checkout("--detach"); - await git.deleteLocalBranch(_temporaryBranch); - await git.push(["--delete", "origin", _temporaryBranch]); - core.info(`Temporary branch ${_temporaryBranch} removed successfully.`); -} -exports.maybeRemoveTemporaryBranch = maybeRemoveTemporaryBranch; -async function temporaryBranch() { - const branchName = await (0, create_git_1.createGit)().revparse(["--abbrev-ref", "HEAD"]); - return branchName.startsWith("ci-tag-") ? branchName : ""; -} + async create() { + const _isDetached = await this.isDetached(); + if (!_isDetached) { + return; + } + const currentHash = await this.git.revparse(["--short", "HEAD"]); + const temporaryBranchName = `ci-tag-${currentHash}`; + await this.git.checkoutLocalBranch(temporaryBranchName); + const pushResult = await this.git.push([ + "-u", + "origin", + temporaryBranchName, + ]); + this.pushInfo(pushResult, "Created temporary branch with messages"); + core.info(`Temporary branch ${temporaryBranchName} created successfully.`); + } + async delete() { + const _temporaryBranch = await this.branchName(); + if (!_temporaryBranch) { + return; + } + await this.git.checkout("--detach"); + const deleteResult = await this.git.deleteLocalBranch(_temporaryBranch); + core.info(deleteResult.success + ? `Temporary branch ${_temporaryBranch} deleted successfully.` + : `Failed to delete temporary branch ${_temporaryBranch}.`); + const pushResult = await this.git.push([ + "--delete", + "origin", + _temporaryBranch, + ]); + this.pushInfo(pushResult, "Removed temporary branch with messages"); + core.info(`Temporary branch ${_temporaryBranch} removed successfully.`); + } + async isDetached() { + const branchName = await this.git.revparse(["--abbrev-ref", "HEAD"]); + return branchName === "HEAD"; + } + async branchName() { + const branchName = await this.git.revparse(["--abbrev-ref", "HEAD"]); + return branchName.startsWith("ci-tag-") ? branchName : ""; + } + pushInfo(result, message) { + const messages = result?.remoteMessages.all.join("\n"); + messages && core.info(`${message}: ${messages}`); + } +} +exports.TemporaryBranch = TemporaryBranch; /***/ }), diff --git a/src/main.ts b/src/main.ts index 531a4e5..b0a3ef6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,19 +1,19 @@ import * as core from "@actions/core"; -import { maybeCreateTemporaryBranch } from "./tasks/maybe-create-temporary-branch"; -import { maybeRemoveTemporaryBranch } from "./tasks/maybe-remove-temporary-tags"; import { Artifacts } from "./model/artifacts"; import { Tags } from "./model/tags"; import { createGit } from "./create-git"; +import { TemporaryBranch } from "./model/temporary-branch"; async function main(): Promise { const git = createGit(); const tags = new Tags(); const artifacts = new Artifacts(git, tags); + const temporaryBranch = new TemporaryBranch(git); Promise.resolve() - .then(maybeCreateTemporaryBranch) + .then(() => temporaryBranch.create()) .then(() => artifacts.update()) - .then(maybeRemoveTemporaryBranch) + .then(() => temporaryBranch.delete()) .catch((error) => core.setFailed(`Failed to create and push artifacts: ${error}`), diff --git a/src/model/artifacts.ts b/src/model/artifacts.ts index 4ffb578..e2bae01 100644 --- a/src/model/artifacts.ts +++ b/src/model/artifacts.ts @@ -20,11 +20,10 @@ export class Artifacts { try { await this.compile(); - await this.push(); + await this.deploy(); } catch (error) { - core.warning(`Failed creating artifacts: ${error}`); core.endGroup(); - throw error; + throw new Error(`Failed creating artifacts: ${error}`); } } @@ -32,22 +31,38 @@ export class Artifacts { const result = await exec.exec(Artifacts.COMMAND); if (result !== 0) { throw new Error( - "Failed to compile artifacts. Process exited with non-zero code.", + "Failing to compile artifacts. Process exited with non-zero code.", ); } } - private async push() { + private async deploy(): Promise { + await this.add(); + await this.tags.collect(); + await this.commit(); + await this.push(); + await this.tags.move(); + } - await exec.exec(`git add -f ${Artifacts.TARGET_DIR}/*`); - const commitResult = await this.git.commit("🚀 Build Artifacts"); - const pushingResult = await this.git.push(); + private async add(): Promise { + const result = await exec.exec(`git add -f ${Artifacts.TARGET_DIR}/*`); + if (result !== 0) { + throw new Error( + "Failing to git-add the artifacts build. Process exited with non-zero code.", + ); + } + } + private async commit(): Promise { + const commitResult = await this.git.commit("🚀 Build Artifacts"); core.info(`Committed changes: ${commitResult.summary.changes}`); + core.info(`Committed insertions: ${commitResult.summary.insertions}`); + core.info(`Committed deletions: ${commitResult.summary.deletions}`); + } - await this.tags.move(); - + private async push(): Promise { + const pushingResult = await this.git.push(); const messages = pushingResult?.remoteMessages.all.join("\n"); messages && core.info(`Pushed artifacts with messages: ${messages}`); } diff --git a/src/model/tags.ts b/src/model/tags.ts index a1b6c99..78eecc3 100644 --- a/src/model/tags.ts +++ b/src/model/tags.ts @@ -1,5 +1,6 @@ import { createGit } from "../create-git"; -import { SimpleGit } from "simple-git"; +import { PushResult, SimpleGit } from "simple-git"; +import * as core from "@actions/core"; export class Tags { private tags: Array = []; @@ -7,14 +8,32 @@ export class Tags { public async collect(): Promise { this.tags = (await this.git.tags(["--contains"])).all; + core.info(`Collecting tags: ${this.toString()}`); } public async move(): Promise { + core.info(`Moving tags: ${this.toString()}`); await this.remove(); await this.create(); } - public toString(): string { + private async remove(): Promise { + await this.git.tag(["-d", ...this.tags]); + const pushResult = await this.git.push([ + "--delete", + "origin", + ...this.tags, + ]); + this.pushInfo(pushResult, "Removed tags with messages"); + } + + private async create(): Promise { + await Promise.all(this.tags.map(async (tag) => this.git.addTag(tag))); + const pushResult = await this.git.pushTags(); + this.pushInfo(pushResult, "Pushed tags with messages"); + } + + private toString(): string { if (!this.tags.length) { return ""; } @@ -22,13 +41,8 @@ export class Tags { return [...this.tags].join(", "); } - private async remove(): Promise { - await this.git.tag(["-d", ...this.tags]); - await this.git.push(["--delete", "origin", ...this.tags]); - } - - private async create(): Promise { - await Promise.all(this.tags.map(async (tag) => this.git.addTag(tag))); - await this.git.pushTags(); + private pushInfo(result: PushResult, message: string): void { + const messages = result?.remoteMessages.all.join("\n"); + messages && core.info(`${message}: ${messages}`); } } diff --git a/src/model/temporary-branch.ts b/src/model/temporary-branch.ts new file mode 100644 index 0000000..8b90df9 --- /dev/null +++ b/src/model/temporary-branch.ts @@ -0,0 +1,71 @@ +import * as core from "@actions/core"; +import { PushResult, SimpleGit } from "simple-git"; + +export class TemporaryBranch { + private git: SimpleGit; + + constructor(git: SimpleGit) { + this.git = git; + } + + async create(): Promise { + const _isDetached = await this.isDetached(); + + if (!_isDetached) { + return; + } + + const currentHash = await this.git.revparse(["--short", "HEAD"]); + const temporaryBranchName = `ci-tag-${currentHash}`; + await this.git.checkoutLocalBranch(temporaryBranchName); + + const pushResult = await this.git.push([ + "-u", + "origin", + temporaryBranchName, + ]); + + this.pushInfo(pushResult, "Created temporary branch with messages"); + core.info(`Temporary branch ${temporaryBranchName} created successfully.`); + } + + async delete(): Promise { + const _temporaryBranch = await this.branchName(); + + if (!_temporaryBranch) { + return; + } + + await this.git.checkout("--detach"); + + const deleteResult = await this.git.deleteLocalBranch(_temporaryBranch); + core.info( + deleteResult.success + ? `Temporary branch ${_temporaryBranch} deleted successfully.` + : `Failed to delete temporary branch ${_temporaryBranch}.`, + ); + + const pushResult = await this.git.push([ + "--delete", + "origin", + _temporaryBranch, + ]); + this.pushInfo(pushResult, "Removed temporary branch with messages"); + core.info(`Temporary branch ${_temporaryBranch} removed successfully.`); + } + + private async isDetached(): Promise { + const branchName = await this.git.revparse(["--abbrev-ref", "HEAD"]); + return branchName === "HEAD"; + } + + async branchName(): Promise { + const branchName = await this.git.revparse(["--abbrev-ref", "HEAD"]); + return branchName.startsWith("ci-tag-") ? branchName : ""; + } + + private pushInfo(result: PushResult, message: string): void { + const messages = result?.remoteMessages.all.join("\n"); + messages && core.info(`${message}: ${messages}`); + } +} diff --git a/src/tasks/maybe-create-temporary-branch.ts b/src/tasks/maybe-create-temporary-branch.ts deleted file mode 100644 index 4d9c0f5..0000000 --- a/src/tasks/maybe-create-temporary-branch.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { createGit } from "../create-git"; -import * as core from "@actions/core"; - -export async function maybeCreateTemporaryBranch(): Promise { - const git = createGit(); - const _isDetached = await isDetached(); - - if (!_isDetached) { - return; - } - - const currentHash = await git.revparse(["--short", "HEAD"]); - const temporaryBranchName = `ci-tag-${currentHash}`; - await git.checkoutLocalBranch(temporaryBranchName); - - core.info(`Temporary branch ${temporaryBranchName} created successfully.`); - - await git.push(["-u", "origin", temporaryBranchName]); -} - -async function isDetached(): Promise { - const branchName = await createGit().revparse(["--abbrev-ref", "HEAD"]); - return branchName === "HEAD"; -} diff --git a/src/tasks/maybe-remove-temporary-tags.ts b/src/tasks/maybe-remove-temporary-tags.ts deleted file mode 100644 index 4aa3325..0000000 --- a/src/tasks/maybe-remove-temporary-tags.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { createGit } from "../create-git"; -import * as core from "@actions/core"; - -export async function maybeRemoveTemporaryBranch(): Promise { - const git = createGit(); - const _temporaryBranch = await temporaryBranch(); - - if (!_temporaryBranch) { - return; - } - - await git.checkout("--detach"); - await git.deleteLocalBranch(_temporaryBranch); - await git.push(["--delete", "origin", _temporaryBranch]); - - core.info(`Temporary branch ${_temporaryBranch} removed successfully.`); -} - -async function temporaryBranch(): Promise { - const branchName = await createGit().revparse(["--abbrev-ref", "HEAD"]); - return branchName.startsWith("ci-tag-") ? branchName : ""; -}