Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enables custom separator for the commit message #15

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
# Enforce Pull Request Title Style Action

This action analyses the titles of Pull Requests to ensure they start with a Jira Issue Key. Issue Keys are a combination of a Project Key, a hyphen, and a number designating which issue it is. In general, Project Keys are two capital letters but Jira does allow for [custom Project Keys](https://confluence.atlassian.com/adminjiraserver/changing-the-project-key-format-938847081.html) and this issue attempts to abide by the custom format.
**IMPORTANT: Most of the content of this repository is coming from https://github.com/ryanvade/enforce-pr-title-style-action**

For example, if your project key were `AB` then the following would be allowed
This action analyses the titles of Pull Requests to ensure they start with a Jira Issue Key. Issue Keys are a combination of a Project Key, a hyphen, and a number designating which issue it is. In general, Project Keys are two capital letters but Jira does allow for [custom Project Keys](https://confluence.atlassian.com/adminjiraserver/changing-the-project-key-format-938847081.html) and this issue attempts to abide by the custom format.

The action also allows you to define a custom separator between your Jira Issue Key, and commit message.

For example, if your project key were `AB` then the following would be allowed (the default separator is an empty space):

```
AB-1 Initialize Project
```

If your custom separator is `:` then the following would be allowed:

```
AB-1: Initialize Project
```

However, the following examples would not be allowed

```
Expand All @@ -30,24 +40,39 @@ AB-1

By default, this action will allow any valid Issue Key so long as it *could* be valid. If you want to be specific to your project, use the `projectKey` input for the action.

By default, this action will expect and empty space between the Jira Issue Key, and the commit message. If you want to be specific with your separator, use the `commitMessageSeparator` input for the action.

## Inputs

### `projectKey`

A specific Project Key to always check for.

### `commitMessageSeparator`

A specific separator to split the ticket and the commit message

## Example Usage

```
- name: Enforce Jira Issue Key in Pull Request Title
uses: ryanvade/enforce-pr-title-style-action@v1
uses: devsbb/enforce-pr-title-style-action@v1.0.6
```

## Example Usage with a specific Project Key

```
- name: Enforce Jira Issue Key in Pull Request Title
uses: ryanvade/enforce-pr-title-style-action@v1
uses: devsbb/enforce-pr-title-style-action@v1.0.6
with:
projectKey: AB
```

## Example Usage with a specific commit message separator

```
- name: Enforce Jira Issue Key in Pull Request Title
uses: devsbb/[email protected]
with:
commitMessageSeparator: ":"
```
131 changes: 86 additions & 45 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,89 @@ import { readFileSync } from "fs";
import { getPullRequestTitle, getRegex } from "../src";

describe("index", () => {
describe("getPullRequestTitle", () => {
beforeEach(() => {
delete process.env["GITHUB_EVENT_PATH"];
});
it("can get the title from the context", () => {
process.env["GITHUB_EVENT_PATH"] = __dirname + "/valid-context.json";
github.context.payload = JSON.parse(
readFileSync(process.env.GITHUB_EVENT_PATH, { encoding: 'utf8' })
);
const title = getPullRequestTitle();
expect(title).toBe("Test Title");
});

it("raises an exception if the event is not for a pull_request", () => {
process.env["GITHUB_EVENT_PATH"] = __dirname + "/wrong-event-type-context.json";
github.context.payload = JSON.parse(
readFileSync(process.env.GITHUB_EVENT_PATH, { encoding: 'utf8' })
);
expect(getPullRequestTitle).toThrowError("This action should only be run with Pull Request Events");
})
});

describe("getRegex", () => {
const name = "projectKey";
it("gets the default when no project key is provided", () => {
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] = "";
const regex = getRegex();
let defaultRegex = /(?<=^|[a-z]\-|[\s\p{Punct}&&[^\-]])([A-Z][A-Z0-9_]*-\d+)(?![^\W_])(\s)+(.)+/;
expect(regex).toEqual(defaultRegex);
expect(regex.test("PR-4 this is valid")).toBe(true);
});

it("uses a project key if it exists", () => {
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] = "AB";
const regex = getRegex();
expect(regex).toEqual(new RegExp(`(^AB-){1}(\\d)+(\\s)+(.)+`));
expect(regex.test("AB-43 stuff and things")).toBe(true);
});

it("throws an exception if the provided project key is not valid", () => {
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] = "aB";
expect(getRegex).toThrowError("Project Key \"aB\" is invalid");
});
});
});
describe("getPullRequestTitle", () => {
beforeEach(() => {
delete process.env["GITHUB_EVENT_PATH"];
});

it("can get the title from the context", () => {
process.env["GITHUB_EVENT_PATH"] = __dirname + "/valid-context.json";
github.context.payload = JSON.parse(
readFileSync(process.env.GITHUB_EVENT_PATH, { encoding: "utf8" })
);
const title = getPullRequestTitle();
expect(title).toBe("Test Title");
});

it("raises an exception if the event is not for a pull_request", () => {
process.env["GITHUB_EVENT_PATH"] =
__dirname + "/wrong-event-type-context.json";
github.context.payload = JSON.parse(
readFileSync(process.env.GITHUB_EVENT_PATH, { encoding: "utf8" })
);
expect(getPullRequestTitle).toThrowError(
"This action should only be run with Pull Request Events"
);
});
});

describe("getRegex", () => {
const name = "projectKey";
const separator = "commitMessageSeparator";

it("accepts any project key when no project key is provided", () => {
process.env[`INPUT_${name.replace(/ /g, "_").toUpperCase()}`] = "";
process.env[`INPUT_${separator.replace(/ /g, "_").toUpperCase()}`] = "";
const regex = getRegex();
const defaultRegex =
/(?<=^|[a-z]\-|[\s\p{Punct}&&[^\-]])([A-Z][A-Z0-9_]*-\d+)(?![^\W_])(\s)+(.)+/;
expect(regex).toEqual(defaultRegex);
expect(regex.test("PR-4 this is valid")).toBe(true);
});

it("uses a project key if it exists with the default separator", () => {
process.env[`INPUT_${name.replace(/ /g, "_").toUpperCase()}`] = "AB";
process.env[`INPUT_${separator.replace(/ /g, "_").toUpperCase()}`] = "";
const regex = getRegex();
const defaultRegex = new RegExp(`(^AB-){1}(\\d)+(\\s)+(.)+`);
expect(regex).toEqual(defaultRegex);
expect(regex.test("AB-43 stuff and things")).toBe(true);
});

it("uses a project key if it exists with a custom separator", () => {
process.env[`INPUT_${name.replace(/ /g, "_").toUpperCase()}`] = "AB";
process.env[`INPUT_${separator.replace(/ /g, "_").toUpperCase()}`] = ":";
const regex = getRegex();
const defaultRegex = new RegExp(`(^AB-){1}(\\d)+(:)+(.)+`);
expect(regex).toEqual(defaultRegex);
expect(regex.test("AB-43: stuff and things")).toBe(true);
});

it("throws an exception if the provided project key is not valid", () => {
process.env[`INPUT_${name.replace(/ /g, "_").toUpperCase()}`] = "aB";
expect(getRegex).toThrowError('Project Key "aB" is invalid');
});

it("gets the default separator when no custom separator is provided", () => {
process.env[`INPUT_${name.replace(/ /g, "_").toUpperCase()}`] = "";
process.env[`INPUT_${separator.replace(/ /g, "_").toUpperCase()}`] = "";
const regex = getRegex();
const defaultRegex =
/(?<=^|[a-z]\-|[\s\p{Punct}&&[^\-]])([A-Z][A-Z0-9_]*-\d+)(?![^\W_])(\s)+(.)+/;
expect(regex).toEqual(defaultRegex);
expect(regex.test("PR-4 this is valid")).toBe(true);
});

it("uses a custom separator if it exists", () => {
process.env[`INPUT_${name.replace(/ /g, "_").toUpperCase()}`] = "";
process.env[`INPUT_${separator.replace(/ /g, "_").toUpperCase()}`] = ":";
const regex = getRegex();
const defaultRegex = new RegExp(
"(?<=^|[a-z]\\-|[sp{Punct}&&[^-]])([A-Z][A-Z0-9_]*-\\d+)(?![^\\W_])(:)+(.)+",
""
);
expect(regex).toEqual(defaultRegex);
expect(regex.test("AB-43: stuff and things")).toBe(true);
});
});
});
7 changes: 5 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
name: Enforce Pull Request Title includes Jira Issue Key
description: Check that a PR title starts with a Jira Issue Key
name: Enforce Pull Request Title includes Jira Issue Key and Custom Message Separator
description: Check that a PR title starts with a Jira Issue Key and Custom Message Separator
inputs:
projectKey:
description: 'A specific Jira Project Key that must be included.'
required: false
commitMessageSeparator:
description: 'A specific commit message separator must be included.'
required: false
runs:
using: node12
main: dist/index.js
Loading