From d17161afee419d87cb926e5dcfde6dec071fd356 Mon Sep 17 00:00:00 2001 From: James Ives Date: Sat, 18 Jan 2020 14:41:49 -0500 Subject: [PATCH] [Issue-133] Customizable Commit Messages (#134) * Customizable Commit Messages * Removes lodash * Update package.json * README * Changes * Check for null * Adds a dash --- .gitignore | 6 +++++- README.md | 3 ++- __tests__/env.js | 1 + __tests__/env.ts | 1 - __tests__/execute.test.ts | 34 +++++++++++++++------------------- __tests__/git.test.ts | 5 ++--- __tests__/util.test.ts | 25 ++++++++++++------------- jest.config.js | 2 +- lib/__tests__/env.js | 2 ++ lib/constants.js | 23 ++++++++++++----------- lib/git.js | 4 +++- lib/src/constants.js | 23 ++++++++++++----------- lib/src/git.js | 15 ++++++++------- package.json | 5 ++--- src/constants.ts | 25 +++++++++++++------------ src/git.ts | 6 +++++- 16 files changed, 95 insertions(+), 85 deletions(-) create mode 100644 __tests__/env.js delete mode 100644 __tests__/env.ts create mode 100644 lib/__tests__/env.js diff --git a/.gitignore b/.gitignore index b78b4fb6..915123c1 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,8 @@ npm-debug.log* yarn-debug.log* yarn-error.log* -node_modules \ No newline at end of file +node_modules + +## Registry +package-lock.json +yarn-error.log \ No newline at end of file diff --git a/README.md b/README.md index 3d2ef11f..7a6ff6d7 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,8 @@ Below you'll find a description of what each option does. | `BRANCH` | This is the branch you wish to deploy to, for example `gh-pages` or `docs`. | `with` | **Yes** | | `FOLDER` | The folder in your repository that you want to deploy. If your build script compiles into a directory named `build` you'd put it here. **Folder paths cannot have a leading `/` or `./`**. If you wish to deploy the root directory you can place a `.` here. | `with` | **Yes** | | `TARGET_FOLDER` | If you'd like to push the contents of the deployment folder into a specific directory on the deployment branch you can specify it here. | `with` | **No** | -| `BASE_BRANCH` | The base branch of your repository which you'd like to checkout prior to deploying. This defaults to the current commit [SHA](http://en.wikipedia.org/wiki/SHA-1) that triggered the build followed by `master` if it doesn't exist. This is useful for making deployments from another branch, and also may be neccersary when using a scheduled job. | `with` | **No** | +| `BASE_BRANCH` | The base branch of your repository which you'd like to checkout prior to deploying. This defaults to the current commit [SHA](http://en.wikipedia.org/wiki/SHA-1) that triggered the build followed by `master` if it doesn't exist. This is useful for making deployments from another branch, and also may be necessary when using a scheduled job. | `with` | **No** | +| `COMMIT_MESSAGE` | If you need to customize the commit message for an integration you can do so. | `with` | **No** | | `CLEAN` | If your project generates hashed files on build you can use this option to automatically delete them from the deployment branch with each deploy. This option can be toggled on by setting it to `true`. | `with` | **No** | | `CLEAN_EXCLUDE` | If you need to use `CLEAN` but you'd like to preserve certain files or folders you can use this option. This should be formatted as an array but stored as a string. For example: `'["filename.js", "folder"]'` | `with` | **No** | diff --git a/__tests__/env.js b/__tests__/env.js new file mode 100644 index 00000000..06ab3fd5 --- /dev/null +++ b/__tests__/env.js @@ -0,0 +1 @@ +process.env.UNIT_TEST = "true"; diff --git a/__tests__/env.ts b/__tests__/env.ts deleted file mode 100644 index 3a0445b1..00000000 --- a/__tests__/env.ts +++ /dev/null @@ -1 +0,0 @@ -process.env.UNIT_TEST = 'true' \ No newline at end of file diff --git a/__tests__/execute.test.ts b/__tests__/execute.test.ts index 892e222f..778f3cd9 100644 --- a/__tests__/execute.test.ts +++ b/__tests__/execute.test.ts @@ -1,23 +1,19 @@ -import {execute} from '../src/execute'; -import {exec} from '@actions/exec'; +import { execute } from "../src/execute"; +import { exec } from "@actions/exec"; -jest.mock('@actions/exec', () => ({ +jest.mock("@actions/exec", () => ({ exec: jest.fn() -})) +})); -describe('execute', () => { - describe('execute', () => { - it('should be called with the correct arguments', async() => { - await execute('echo Montezuma', './') - - expect(exec).toBeCalledWith( - "echo Montezuma", [], { - cwd: "./", - listeners: { - stdout: expect.any(Function) - } - } - ) +describe("execute", () => { + it("should be called with the correct arguments", async () => { + await execute("echo Montezuma", "./"); + + expect(exec).toBeCalledWith("echo Montezuma", [], { + cwd: "./", + listeners: { + stdout: expect.any(Function) + } }); - }) -}) \ No newline at end of file + }); +}); diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts index 53d66be0..aeb199f0 100644 --- a/__tests__/git.test.ts +++ b/__tests__/git.test.ts @@ -2,12 +2,11 @@ process.env["INPUT_FOLDER"] = "build"; process.env["GITHUB_SHA"] = "123"; -import _ from "lodash"; import { action } from "../src/constants"; import { deploy, generateBranch, init, switchToBaseBranch } from "../src/git"; import { execute } from "../src/execute"; -const originalAction = _.cloneDeep(action); +const originalAction = JSON.stringify(action); jest.mock("../src/execute", () => ({ execute: jest.fn() @@ -15,7 +14,7 @@ jest.mock("../src/execute", () => ({ describe("git", () => { afterEach(() => { - _.assignIn(action, originalAction); + Object.assign(action, JSON.parse(originalAction)); }); describe("init", () => { diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts index cf304bd5..316b0032 100644 --- a/__tests__/util.test.ts +++ b/__tests__/util.test.ts @@ -1,21 +1,20 @@ -import {isNullOrUndefined} from '../src/util'; +import { isNullOrUndefined } from "../src/util"; - -describe('util', () => { - describe('isNullOrUndefined', () => { - it('should return true if the value is null', async() => { +describe("util", () => { + describe("isNullOrUndefined", () => { + it("should return true if the value is null", async () => { const value = null; - expect(isNullOrUndefined(value)).toBeTruthy() + expect(isNullOrUndefined(value)).toBeTruthy(); }); - it('should return true if the value is undefined', async() => { + it("should return true if the value is undefined", async () => { const value = undefined; - expect(isNullOrUndefined(value)).toBeTruthy() + expect(isNullOrUndefined(value)).toBeTruthy(); }); - it('should return false if the value is defined', async() => { - const value = 'montezuma'; - expect(isNullOrUndefined(value)).toBeFalsy() + it("should return false if the value is defined", async () => { + const value = "montezuma"; + expect(isNullOrUndefined(value)).toBeFalsy(); }); - }) -}) \ No newline at end of file + }); +}); diff --git a/jest.config.js b/jest.config.js index 5001d643..6114ff66 100644 --- a/jest.config.js +++ b/jest.config.js @@ -8,5 +8,5 @@ module.exports = { '^.+\\.ts$': 'ts-jest' }, verbose: true, - setupFiles: ["/__tests__/env.ts"] + setupFiles: ["/__tests__/env.js"] } \ No newline at end of file diff --git a/lib/__tests__/env.js b/lib/__tests__/env.js new file mode 100644 index 00000000..6d151e8c --- /dev/null +++ b/lib/__tests__/env.js @@ -0,0 +1,2 @@ +"use strict"; +process.env.UNIT_TEST = "true"; diff --git a/lib/constants.js b/lib/constants.js index 56ba5f95..654000f2 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -16,27 +16,28 @@ exports.root = "."; exports.isTest = process.env.UNIT_TEST; // Required action data. exports.action = { + accessToken: core.getInput("ACCESS_TOKEN"), + baseBranch: core.getInput("BASE_BRANCH"), build: exports.folder, + branch: core.getInput("BRANCH"), + commitMessage: core.getInput("COMMIT_MESSAGE"), + clean: core.getInput("CLEAN"), + cleanExclude: core.getInput("CLEAN_EXCLUDE"), + defaultBranch: process.env.GITHUB_SHA ? process.env.GITHUB_SHA : "master", + email: pusher && pusher.email + ? pusher.email + : `${process.env.GITHUB_ACTOR || + "github-pages-deploy-action"}@users.noreply.github.com`, gitHubRepository: repository && repository.full_name ? repository.full_name : process.env.GITHUB_REPOSITORY, gitHubToken: core.getInput("GITHUB_TOKEN"), - accessToken: core.getInput("ACCESS_TOKEN"), - branch: core.getInput("BRANCH"), - targetFolder: core.getInput("TARGET_FOLDER"), - baseBranch: core.getInput("BASE_BRANCH"), - defaultBranch: process.env.GITHUB_SHA ? process.env.GITHUB_SHA : "master", name: pusher && pusher.name ? pusher.name : process.env.GITHUB_ACTOR ? process.env.GITHUB_ACTOR : "GitHub Pages Deploy Action", - email: pusher && pusher.email - ? pusher.email - : `${process.env.GITHUB_ACTOR || - "github-pages-deploy-action"}@users.noreply.github.com`, - clean: core.getInput("CLEAN"), - cleanExclude: core.getInput("CLEAN_EXCLUDE") + targetFolder: core.getInput("TARGET_FOLDER") }; // Repository path used for commits/pushes. exports.repositoryPath = `https://${exports.action.accessToken || diff --git a/lib/git.js b/lib/git.js index bcd3475e..be915266 100644 --- a/lib/git.js +++ b/lib/git.js @@ -130,7 +130,9 @@ function deploy() { // Commits to GitHub. yield execute_1.execute(`git add --all .`, temporaryDeploymentDirectory); yield execute_1.execute(`git checkout -b ${temporaryDeploymentBranch}`, temporaryDeploymentDirectory); - yield execute_1.execute(`git commit -m "Deploying to ${constants_1.action.branch} from ${constants_1.action.baseBranch} ${process.env.GITHUB_SHA}" --quiet`, temporaryDeploymentDirectory); + yield execute_1.execute(`git commit -m "${!util_1.isNullOrUndefined(constants_1.action.commitMessage) + ? constants_1.action.commitMessage + : `Deploying to ${constants_1.action.branch} from ${constants_1.action.baseBranch}`} - ${process.env.GITHUB_SHA} 🚀" --quiet`, temporaryDeploymentDirectory); yield execute_1.execute(`git push --force ${constants_1.repositoryPath} ${temporaryDeploymentBranch}:${constants_1.action.branch}`, temporaryDeploymentDirectory); // Cleans up temporary files/folders and restores the git state. console.log("Running post deployment cleanup jobs... 🔧"); diff --git a/lib/src/constants.js b/lib/src/constants.js index 56ba5f95..654000f2 100644 --- a/lib/src/constants.js +++ b/lib/src/constants.js @@ -16,27 +16,28 @@ exports.root = "."; exports.isTest = process.env.UNIT_TEST; // Required action data. exports.action = { + accessToken: core.getInput("ACCESS_TOKEN"), + baseBranch: core.getInput("BASE_BRANCH"), build: exports.folder, + branch: core.getInput("BRANCH"), + commitMessage: core.getInput("COMMIT_MESSAGE"), + clean: core.getInput("CLEAN"), + cleanExclude: core.getInput("CLEAN_EXCLUDE"), + defaultBranch: process.env.GITHUB_SHA ? process.env.GITHUB_SHA : "master", + email: pusher && pusher.email + ? pusher.email + : `${process.env.GITHUB_ACTOR || + "github-pages-deploy-action"}@users.noreply.github.com`, gitHubRepository: repository && repository.full_name ? repository.full_name : process.env.GITHUB_REPOSITORY, gitHubToken: core.getInput("GITHUB_TOKEN"), - accessToken: core.getInput("ACCESS_TOKEN"), - branch: core.getInput("BRANCH"), - targetFolder: core.getInput("TARGET_FOLDER"), - baseBranch: core.getInput("BASE_BRANCH"), - defaultBranch: process.env.GITHUB_SHA ? process.env.GITHUB_SHA : "master", name: pusher && pusher.name ? pusher.name : process.env.GITHUB_ACTOR ? process.env.GITHUB_ACTOR : "GitHub Pages Deploy Action", - email: pusher && pusher.email - ? pusher.email - : `${process.env.GITHUB_ACTOR || - "github-pages-deploy-action"}@users.noreply.github.com`, - clean: core.getInput("CLEAN"), - cleanExclude: core.getInput("CLEAN_EXCLUDE") + targetFolder: core.getInput("TARGET_FOLDER") }; // Repository path used for commits/pushes. exports.repositoryPath = `https://${exports.action.accessToken || diff --git a/lib/src/git.js b/lib/src/git.js index a9e8e152..c948bbd5 100644 --- a/lib/src/git.js +++ b/lib/src/git.js @@ -31,12 +31,13 @@ function init() { return core.setFailed("You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy."); } if (constants_1.action.build.startsWith("/") || constants_1.action.build.startsWith("./")) { - console.log("2"); return core.setFailed(`The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly.`); } yield execute_1.execute(`git init`, constants_1.workspace); yield execute_1.execute(`git config user.name ${constants_1.action.name}`, constants_1.workspace); yield execute_1.execute(`git config user.email ${constants_1.action.email}`, constants_1.workspace); + yield execute_1.execute(`git remote rm origin`, constants_1.workspace); + yield execute_1.execute(`git remote add origin ${constants_1.repositoryPath}`, constants_1.workspace); yield execute_1.execute(`git fetch`, constants_1.workspace); } catch (error) { @@ -53,9 +54,7 @@ exports.init = init; */ function switchToBaseBranch() { return __awaiter(this, void 0, void 0, function* () { - yield execute_1.execute(constants_1.action.baseBranch - ? `git switch ${constants_1.action.baseBranch}` - : `git checkout --progress --force ${constants_1.action.defaultBranch}`, constants_1.workspace); + yield execute_1.execute(`git checkout --progress --force ${constants_1.action.baseBranch ? constants_1.action.baseBranch : constants_1.action.defaultBranch}`, constants_1.workspace); return Promise.resolve("Switched to the base branch..."); }); } @@ -68,7 +67,7 @@ function generateBranch() { try { console.log(`Creating ${constants_1.action.branch} branch... 🔧`); yield switchToBaseBranch(); - yield execute_1.execute(`git switch --orphan ${constants_1.action.branch}`, constants_1.workspace); + yield execute_1.execute(`git checkout --orphan ${constants_1.action.branch}`, constants_1.workspace); yield execute_1.execute(`git reset --hard`, constants_1.workspace); yield execute_1.execute(`git commit --allow-empty -m "Initial ${constants_1.action.branch} commit."`, constants_1.workspace); yield execute_1.execute(`git push ${constants_1.repositoryPath} ${constants_1.action.branch}`, constants_1.workspace); @@ -130,8 +129,10 @@ function deploy() { } // Commits to GitHub. yield execute_1.execute(`git add --all .`, temporaryDeploymentDirectory); - yield execute_1.execute(`git switch -c ${temporaryDeploymentBranch}`, temporaryDeploymentDirectory); - yield execute_1.execute(`git commit -m "Deploying to ${constants_1.action.branch} from ${constants_1.action.baseBranch} ${process.env.GITHUB_SHA}" --quiet`, temporaryDeploymentDirectory); + yield execute_1.execute(`git checkout -b ${temporaryDeploymentBranch}`, temporaryDeploymentDirectory); + yield execute_1.execute(`git commit -m "${constants_1.action.commitMessage + ? constants_1.action.commitMessage + : `Deploying to ${constants_1.action.branch} from ${constants_1.action.baseBranch}`} ${process.env.GITHUB_SHA} 🚀" --quiet`, temporaryDeploymentDirectory); yield execute_1.execute(`git push --force ${constants_1.repositoryPath} ${temporaryDeploymentBranch}:${constants_1.action.branch}`, temporaryDeploymentDirectory); // Cleans up temporary files/folders and restores the git state. console.log("Running post deployment cleanup jobs... 🔧"); diff --git a/package.json b/package.json index 61c61936..fd01f9e5 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "build": "tsc", "test": "jest", "lint": "tslint -p tsconfig.json --project '.' || (echo Project needs formatting)", - "format": "prettier --write './src/*.ts'" + "format": "prettier --write './**/*.ts'" }, "repository": { "type": "git", @@ -29,7 +29,7 @@ "deploy", "deployment" ], - "author": "James Ives", + "author": "James Ives ", "license": "MIT", "dependencies": { "@actions/core": "^1.2.0", @@ -41,7 +41,6 @@ "@types/node": "^13.1.2", "jest": "^24.8.0", "jest-circus": "^24.7.1", - "lodash": "^4.17.15", "prettier": "^1.19.1", "ts-jest": "^24.2.0", "tslint": "^5.20.0", diff --git a/src/constants.ts b/src/constants.ts index 06da2086..693cda8c 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -10,30 +10,31 @@ export const isTest = process.env.UNIT_TEST; // Required action data. export const action = { + accessToken: core.getInput("ACCESS_TOKEN"), + baseBranch: core.getInput("BASE_BRANCH"), build: folder, + branch: core.getInput("BRANCH"), + commitMessage: core.getInput("COMMIT_MESSAGE"), + clean: core.getInput("CLEAN"), + cleanExclude: core.getInput("CLEAN_EXCLUDE"), + defaultBranch: process.env.GITHUB_SHA ? process.env.GITHUB_SHA : "master", + email: + pusher && pusher.email + ? pusher.email + : `${process.env.GITHUB_ACTOR || + "github-pages-deploy-action"}@users.noreply.github.com`, gitHubRepository: repository && repository.full_name ? repository.full_name : process.env.GITHUB_REPOSITORY, gitHubToken: core.getInput("GITHUB_TOKEN"), - accessToken: core.getInput("ACCESS_TOKEN"), - branch: core.getInput("BRANCH"), - targetFolder: core.getInput("TARGET_FOLDER"), - baseBranch: core.getInput("BASE_BRANCH"), - defaultBranch: process.env.GITHUB_SHA ? process.env.GITHUB_SHA : "master", name: pusher && pusher.name ? pusher.name : process.env.GITHUB_ACTOR ? process.env.GITHUB_ACTOR : "GitHub Pages Deploy Action", - email: - pusher && pusher.email - ? pusher.email - : `${process.env.GITHUB_ACTOR || - "github-pages-deploy-action"}@users.noreply.github.com`, - clean: core.getInput("CLEAN"), - cleanExclude: core.getInput("CLEAN_EXCLUDE") + targetFolder: core.getInput("TARGET_FOLDER") }; // Repository path used for commits/pushes. diff --git a/src/git.ts b/src/git.ts index 08a96012..444ab46a 100644 --- a/src/git.ts +++ b/src/git.ts @@ -152,7 +152,11 @@ export async function deploy(): Promise { temporaryDeploymentDirectory ); await execute( - `git commit -m "Deploying to ${action.branch} from ${action.baseBranch} ${process.env.GITHUB_SHA}" --quiet`, + `git commit -m "${ + !isNullOrUndefined(action.commitMessage) + ? action.commitMessage + : `Deploying to ${action.branch} from ${action.baseBranch}` + } - ${process.env.GITHUB_SHA} 🚀" --quiet`, temporaryDeploymentDirectory ); await execute(