[Issue-133] Customizable Commit Messages (#134)

* Customizable Commit Messages

* Removes lodash

* Update package.json

* README

* Changes

* Check for null

* Adds a dash
This commit is contained in:
James Ives 2020-01-18 14:41:49 -05:00 committed by GitHub
parent 357db31c42
commit d17161afee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 95 additions and 85 deletions

4
.gitignore vendored
View File

@ -6,3 +6,7 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
node_modules
## Registry
package-lock.json
yarn-error.log

View File

@ -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** |

1
__tests__/env.js Normal file
View File

@ -0,0 +1 @@
process.env.UNIT_TEST = "true";

View File

@ -1 +0,0 @@
process.env.UNIT_TEST = 'true'

View File

@ -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', './')
describe("execute", () => {
it("should be called with the correct arguments", async () => {
await execute("echo Montezuma", "./");
expect(exec).toBeCalledWith(
"echo Montezuma", [], {
expect(exec).toBeCalledWith("echo Montezuma", [], {
cwd: "./",
listeners: {
stdout: expect.any(Function)
}
}
)
});
})
})
});
});

View File

@ -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", () => {

View File

@ -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();
});
});
});
})
})

View File

@ -8,5 +8,5 @@ module.exports = {
'^.+\\.ts$': 'ts-jest'
},
verbose: true,
setupFiles: ["<rootDir>/__tests__/env.ts"]
setupFiles: ["<rootDir>/__tests__/env.js"]
}

2
lib/__tests__/env.js Normal file
View File

@ -0,0 +1,2 @@
"use strict";
process.env.UNIT_TEST = "true";

View File

@ -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 ||

View File

@ -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... 🔧");

View File

@ -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 ||

View File

@ -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... 🔧");

View File

@ -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 <iam@jamesiv.es>",
"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",

View File

@ -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.

View File

@ -152,7 +152,11 @@ export async function deploy(): Promise<any> {
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(