Skip to content

Commit 91909f0

Browse files
Bump dependencies + support husky v9
1 parent f17af8f commit 91909f0

File tree

9 files changed

+1733
-2141
lines changed

9 files changed

+1733
-2141
lines changed

.github/workflows/release.yml

+24-24
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@ name: release
33
# Allow to trigger this workflow manually and enter a release version to release
44
# See: https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/
55
on:
6-
workflow_dispatch:
7-
inputs:
8-
releaseVersion:
9-
description: "Release tag version"
10-
required: true
6+
workflow_dispatch:
7+
inputs:
8+
releaseVersion:
9+
description: "Release tag version"
10+
required: true
1111

1212
jobs:
13-
release:
14-
runs-on: ubuntu-latest
15-
steps:
16-
- name: Checkout branch
17-
uses: actions/checkout@v2
18-
with:
19-
ref: "master"
20-
fetch-depth: 0
21-
- uses: oleksiyrudenko/gha-git-credentials@v1
22-
with:
23-
token: "${{ secrets.GITHUB_TOKEN }}"
24-
- name: Use Node.js
25-
uses: actions/setup-node@v2-beta
26-
with:
27-
node-version: 14
28-
- name: Release version
29-
run: npx -p @release-it/[email protected] -p [email protected] release-it --verbose --increment ${{ github.event.inputs.releaseVersion }}
30-
env:
31-
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
13+
release:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout branch
17+
uses: actions/checkout@v2
18+
with:
19+
ref: "master"
20+
fetch-depth: 0
21+
- uses: oleksiyrudenko/gha-git-credentials@v1
22+
with:
23+
token: "${{ secrets.GITHUB_TOKEN }}"
24+
- name: Use Node.js
25+
uses: actions/setup-node@v2-beta
26+
with:
27+
node-version: 22
28+
- name: Release version
29+
run: npx -p @release-it/[email protected] -p [email protected] release-it --verbose --increment ${{ github.event.inputs.releaseVersion }}
30+
env:
31+
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"

.github/workflows/run-tests.yml

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
name: run-tests
22

3-
on: [push, pull_request]
3+
on: [push]
44

55
jobs:
66
tests:
7-
strategy:
8-
fail-fast: true
9-
matrix:
10-
node: [ '10', '12', '14', '15' ] # https://nodejs.org/en/about/releases/
11-
name: NPM test - (Node ${{ matrix.node }})
12-
runs-on: ubuntu-latest
13-
steps:
14-
- name: Checkout code
15-
uses: actions/checkout@v2
16-
- uses: actions/setup-node@v2-beta
17-
with:
18-
node-version: ${{ matrix.node }}
19-
- name: install dependencies
20-
run: npm ci
21-
- name: run tests
22-
run: npm test
7+
strategy:
8+
fail-fast: true
9+
matrix:
10+
node: ["18", "20", "22"] # https://nodejs.org/en/about/releases/
11+
name: NPM test - (Node ${{ matrix.node }})
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v2
16+
- uses: actions/setup-node@v2-beta
17+
with:
18+
node-version: ${{ matrix.node }}
19+
- name: Install dependencies
20+
run: npm ci
21+
- name: Run tests
22+
run: npm test

.prettierrc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"singleQuote": false,
3+
"tabWidth": 4,
4+
"printWidth": 120,
5+
"trailingComma": "all"
6+
}

README.md

+11-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![tests](https://github.com/jessedobbelaere/jira-smart-commit/workflows/run-tests/badge.svg)
44
[![GitHub release](https://img.shields.io/github/release/jessedobbelaere/jira-smart-commit.svg)](https://github.com/jessedobbelaere/jira-smart-commit/releases/latest)
5-
[![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=jessedobbelaere/fork-cms-webpack-boilerplate)](https://dependabot.com)
5+
[![Dependabot](https://img.shields.io/badge/dependabot-active-brightgreen?style=flat-square&logo=dependabot)](https://dependabot.com)
66
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
77
[![Downloads](https://img.shields.io/npm/dt/jira-smart-commit.svg)](https://www.npmjs.com/package/jira-smart-commit)
88
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat)](http://makeapullrequest.com)
@@ -17,6 +17,7 @@ A Node.js git hook script to prefix commits automatically with the JIRA ticket,
1717

1818
```bash
1919
npm install --save-dev husky
20+
npx husky init
2021
```
2122

2223
2. Install this package in your project:
@@ -27,27 +28,24 @@ npm install --save-dev jira-smart-commit
2728

2829
3. Configure scripts in `package.json`. The script expects his first argument to be the JIRA tag of the project.
2930

30-
```json
31-
"husky": {
32-
"hooks": {
33-
"commit-msg": "jira-smart-commit YOUR_JIRA_ISSUE_KEY",
34-
"pre-commit": "lint-staged"
35-
}
36-
},
31+
```bash
32+
# .husky/commit-msg
33+
npx jira-smart-commit YOUR_JIRA_ISSUE_KEY
3734
```
3835

3936
Alternatively: use a regex to detect the Jira `ISSUE_KEY` in your branch.
4037

41-
- `TAG_MATCHER` - regular expression
42-
- `TAG_MATCH_INDEX` - match index
38+
- `TAG_MATCHER` - regular expression
39+
- `TAG_MATCH_INDEX` - match index
4340

4441
Example: if your branch names looke like `feature/JRA-1234/some-description` template
4542

46-
```
47-
"commit-msg": "TAG_MATCHER=\"^[^/]+/(JRA-[0-9]+)\" TAG_MATCH_INDEX=1 jira-smart-commit"
43+
```bash
44+
# .husky/commit-msg
45+
TAG_MATCHER=\"^[^/]+/(JRA-[0-9]+)\" TAG_MATCH_INDEX=1 npx jira-smart-commit
4846
```
4947

50-
4. Do your git commits like usual. If the branch was prefixed with a JIRA tag, your commit message will get prefixed with
48+
4. Commit with git like usual. If the branch was prefixed with a JIRA tag, your commit message will get a Jira prefix added with
5149
the same tag.
5250

5351
```

index.js

+15-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22

3-
const fs = require("fs");
3+
import fs from "fs";
4+
import childProcess from "child_process";
45

56
/**
67
* If commit message title:
@@ -11,11 +12,12 @@ const fs = require("fs");
1112
* then prepend the JIRA issue tag to the commit message.
1213
* E.g My awesome commit -> TAG-123 My awesome commit
1314
*/
14-
15-
if (!process.argv[2] && !process.env.TAG_MATCHER ) {
16-
console.error("Please run this script with the JIRA ticket prefix as CLI argument " +
17-
"(e.g. node smart-commit-msg.js SPAN) or pass regular expression as envirement variable " +
18-
"(e.g. TAG_MATCHER=\\\"SPAN-[0-9]+\\\" node smart-commit-msg.js)");
15+
if (!process.argv[2] && !process.env.TAG_MATCHER) {
16+
console.error(
17+
"Please run this script with the JIRA ticket prefix as CLI argument " +
18+
"(e.g. node smart-commit-msg.js SPAN) or pass regular expression as envirement variable " +
19+
'(e.g. TAG_MATCHER=\\"SPAN-[0-9]+\\" node smart-commit-msg.js)',
20+
);
1921
process.exit(1);
2022
}
2123

@@ -29,22 +31,11 @@ const isInvalidMessage = (commitMessage) => {
2931
return !startsWithMergeBranch(commitMessage) && !startsWithMergePR(commitMessage);
3032
};
3133

32-
/**
33-
* @returns {string}
34-
*/
35-
const getBranchName = () => {
36-
const branchName = fetchBranchNameFromGit();
37-
if (["master", "main", "develop"].includes(branchName)) {
38-
console.warn(`WARNING: Committing directly to ${branchName} is probably not a good idea. Consider making a PR.`);
39-
}
40-
return branchName;
41-
};
42-
4334
/**
4435
* @returns {String}
4536
*/
4637
const fetchBranchNameFromGit = () => {
47-
return require("child_process").execSync("git rev-parse --abbrev-ref HEAD", { encoding: "utf-8" }).split("\n")[0]
38+
return childProcess.execSync("git rev-parse --abbrev-ref HEAD", { encoding: "utf-8" }).split("\n")[0];
4839
};
4940

5041
/**
@@ -54,17 +45,19 @@ const fetchBranchNameFromGit = () => {
5445
const getIssueTagFromBranchName = (branchName) => {
5546
const matched = branchName.match(tagMatcher);
5647
const index = process.env.TAG_MATCH_INDEX ? Number(process.env.TAG_MATCH_INDEX) : 0;
57-
if (process.env.DEBUG === 'true') {
58-
console.log({branchName, tagMatcher, index, matched});
48+
if (process.env.DEBUG === "true") {
49+
console.log({ branchName, tagMatcher, index, matched });
5950
}
6051
return matched && matched[index];
6152
};
6253

6354
const jiraTag = process.argv[2];
64-
const tagMatcher = process.env.TAG_MATCHER ? new RegExp(process.env.TAG_MATCHER, "i") : new RegExp(`^${jiraTag}-\\d+`, "i");
55+
const tagMatcher = process.env.TAG_MATCHER
56+
? new RegExp(process.env.TAG_MATCHER, "i")
57+
: new RegExp(`^${jiraTag}-\\d+`, "i");
6558
const commitMsgFile = process.env.GIT_PARAMS || process.env.HUSKY_GIT_PARAMS;
6659
const commitMsg = fs.readFileSync(commitMsgFile, { encoding: "utf-8" });
67-
const branchName = getBranchName();
60+
const branchName = fetchBranchNameFromGit();
6861
const issueTag = getIssueTagFromBranchName(branchName);
6962
const rawCommitMsgTitle = commitMsg.split("\n")[0];
7063

index.spec.js

-60
This file was deleted.

index.test.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { expect, describe, it, beforeEach, afterEach, vi } from "vitest";
2+
import fs from "fs";
3+
import childProcess from "child_process";
4+
5+
vi.mock("fs");
6+
vi.mock("child_process");
7+
8+
describe("index.js", () => {
9+
beforeEach(() => {
10+
vi.clearAllMocks();
11+
vi.resetModules();
12+
13+
// Reset process env and args
14+
process.env.HUSKY_GIT_PARAMS = "COMMIT_EDITMSG";
15+
delete process.argv[2];
16+
});
17+
18+
afterEach(() => {
19+
vi.restoreAllMocks();
20+
vi.resetAllMocks();
21+
vi.resetModules();
22+
});
23+
24+
it.each([
25+
["SPAN", "SPAN-1-proof-of-concept", "Initial commit✨", "SPAN-1 Initial commit✨"],
26+
["PROJECT", "PROJECT-3-githook-test", "Add support for githooks", "PROJECT-3 Add support for githooks"],
27+
["TAG", "TAG-5032-add-readme", "Add readme to project", "TAG-5032 Add readme to project"],
28+
])(
29+
"should add a prefix to my unprefixed commit message",
30+
async (jiraPrefix, branchName, commitMessage, expected) => {
31+
await expect(executeScript(jiraPrefix, branchName, commitMessage)).resolves.to.equal(expected);
32+
},
33+
);
34+
35+
it("should add a prefix to my unprefixed commit message by detecting the tag using regex", async () => {
36+
process.env.TAG_MATCHER = "^[^/]+/(SPAN-[0-9]+)";
37+
process.env.TAG_MATCH_INDEX = "1";
38+
await expect(executeScript(undefined, "FEAT/SPAN-1234/init", "Initial commit")).resolves.to.equal(
39+
"SPAN-1234 Initial commit",
40+
);
41+
delete process.env.TAG_MATCHER;
42+
delete process.env.TAG_MATCH_INDEX;
43+
});
44+
45+
it.each([
46+
["SPAN", "SPAN-1-proof-of-concept", "SPAN-1 Initial commit✨", "SPAN-1 Initial commit✨"],
47+
[
48+
"PROJECT",
49+
"PROJECT-3-githook-test",
50+
"PROJECT-3 Add support for githooks",
51+
"PROJECT-3 Add support for githooks",
52+
],
53+
["TAG", "TAG-5032-add-readme", "TAG-5032 Add readme to project", "TAG-5032 Add readme to project"],
54+
])(
55+
"should not add a prefix again to my already prefixed commit message",
56+
async (jiraPrefix, branchName, commitMessage, expected) => {
57+
await expect(executeScript(jiraPrefix, branchName, commitMessage)).resolves.to.equal(expected);
58+
},
59+
);
60+
61+
it("should not add a prefix in merge pull requests", async () => {
62+
const commitMergeMessage =
63+
"Merge branch 'develop' of github.com:jessedobbelaere/jira-smart-commit into bugfixes";
64+
await expect(executeScript("SPAN", "SPAN-1-proof-of-concept", commitMergeMessage)).resolves.to.equal(
65+
commitMergeMessage,
66+
);
67+
});
68+
69+
it("should not add a prefix if a branch was not prefixed", async () => {
70+
await expect(executeScript("TAG", "conquer-the-world-PoC", "Initial commit")).resolves.to.equal(
71+
"Initial commit",
72+
);
73+
});
74+
75+
const executeScript = async (jiraPrefix, branchName, commitMessage) => {
76+
// Mock execution environment
77+
process.env.HUSKY_GIT_PARAMS = "COMMIT_EDITMSG";
78+
process.argv[2] = jiraPrefix; // Set CLI argument for JIRA prefix
79+
vi.mocked(childProcess.execSync).mockReturnValue(branchName);
80+
vi.mocked(fs.readFileSync).mockReturnValueOnce(commitMessage);
81+
const writeFileMock = vi.mocked(fs.writeFileSync);
82+
83+
// Run script
84+
const modulePath = require.resolve("./index.js");
85+
await import(modulePath);
86+
87+
return writeFileMock.mock.calls[0][1];
88+
};
89+
});

0 commit comments

Comments
 (0)