Skip to content

Commit

Permalink
refactor(linter)!: implementing conventionalcommits option (#19)
Browse files Browse the repository at this point in the history
* docs(readme)!: update readme usage version

* refactor(linter)!: implementing conventionalcommits option
  • Loading branch information
luabida authored Dec 14, 2022
1 parent 459d11a commit 98cbbf4
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-title-linter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ jobs:
- uses: actions/checkout@v3

- name: Semantic Release PR Title Check
uses: osl-incubator/semantic-release-pr-title-check@v1.2.0
uses: osl-incubator/semantic-release-pr-title-check@v1.3.0
with:
convention-name: conventionalcommits
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@ jobs:
- uses: actions/checkout@v3

- name: Semantic Release PR Title Check
uses: osl-incubator/[email protected]

uses: osl-incubator/[email protected]
with: #Optional
convention-name: conventionalcommits #Default: angular
```
## PR Title Format
```
<type>(<scope>): <short summary>
<tag>(<scope>): <short summary>
│ │ │
│ │ └─> Title message. Not capitalized.
│ │ No period at the end.
│ │
│ └─> The specific subject of the PR. Can be anything.
└─> build|chore|ci|docs|feat|fix|perf|refactor|test|BREAKING CHANGE
└─> build|chore|ci|docs|feat|fix|perf|refactor|test
```
## Tags
Expand Down
2 changes: 1 addition & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ author: 'Luã Bida Vacaro'
description: 'Checks for Angular commit message format in the Pull Request title'
inputs:
convention-name:
description: 'Commit Analyzer preset. Accepted: [angular, defaultcommits]'
description: 'Commit Analyzer preset. Accepted: [angular, conventionalcommits]'
required: false
default: 'angular'
branding:
Expand Down
41 changes: 21 additions & 20 deletions dist/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.linter = void 0;
const core = __importStar(require("@actions/core"));
const preset = core.getInput('convention-name');
const allowedTags = [
"build",
"ci",
Expand All @@ -38,35 +37,37 @@ const allowedTags = [
"test",
];
function linter(title) {
let [tag, subj, msg] = extractContext(title);
if (!allowedTags.includes(tag)) {
throw ("❌ Incorrect PR tag.");
let preset = core.getInput('convention-name');
try {
let splitTitle = extractContext(title, preset);
let tag = splitTitle[0];
if (!allowedTags.includes(tag)) {
throw (`- Incorrect PR tag: "${tag}" is not accepted by Semantic-Release`);
}
;
console.log("✅ PR title is correct!");
}
catch (err) {
console.log("❌ Incorrect PR title.");
throw (err);
}
;
console.log("✅ PR title is correct!");
}
exports.linter = linter;
;
function extractContext(title) {
let regEx = /(^[\w\s?]+)(\(.+\):\s)([^A-Z\W].*[^.]$)/g;
if (preset === 'conventionalcommits') {
regEx = /(^[\w\s?]+)(\(.+\)!:\s)([^A-Z\W].*[^.]$)/g;
}
;
let matches = title.match(regEx) || [];
function extractContext(title, preset) {
let regEx = /(^[\w\s?]+)(\(.+\)(!?):\s)([^A-Z\W].*[^.]$)/g;
let matches = title.matchAll(regEx);
try {
let tag = matches.map(e => e.replace(regEx, '$1'))[0];
let subj = matches.map(e => e.replace(regEx, '$2'))[0];
let msg = matches.map(e => e.replace(regEx, '$3'))[0];
if (!tag || !subj || !msg) {
throw ("❌ Incorrect Title.");
let results = Array.from(matches)[0].filter(Boolean).splice(1);
if (results.length === 4 && preset !== 'conventionalcommits') {
throw ("- To use '!' in the title, set preset as `convenvionalcommits`");
}
;
return [tag, subj, msg];
return results;
}
catch (err) {
console.log(err);
throw (err);
throw (`${err}\n- "${title}" format is incorrect. Please use Angular Commit Message Conventions`);
}
;
}
Expand Down
34 changes: 5 additions & 29 deletions dist/main.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,20 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = void 0;
const github_1 = require("@actions/github");
const linter = __importStar(require("./linter"));
const linter_1 = require("./linter");
function run() {
var _a;
try {
const pullRequestTitle = (_a = github_1.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.title;
let pullRequestTitle = (_a = github_1.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.title;
if (!pullRequestTitle) {
throw ("Title not found");
throw ("- Title not found");
}
;
linter.linter(pullRequestTitle);
(0, linter_1.linter)(pullRequestTitle);
}
catch (err) {
console.log('❌ PR Title check failed');
throw (err);
console.log(`❌ PR Title linter failed\n${err}`);
}
;
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "semantic-release-pr-title-check",
"version": "1.2.0",
"version": "1.3.0",
"description": "Checks for [Angular commit message format](https://github.com/angular/angular/blob/main/CONTRIBUTING.md#-commit-message-format) in the Pull Request Title.",
"main": "build/main.js",
"directories": {
Expand Down
45 changes: 24 additions & 21 deletions src/linter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as core from "@actions/core";


const preset: string = core.getInput('convention-name');
const allowedTags = [
"build",
"ci",
Expand All @@ -14,39 +12,44 @@ const allowedTags = [
"test",
];


export function linter(title: string) {
let [tag, subj, msg] = extractContext(title);

if (!allowedTags.includes(tag)) {
throw("❌ Incorrect PR tag.")
};
let preset: string = core.getInput('convention-name');

console.log("✅ PR title is correct!");
};
try {
let splitTitle = extractContext(title, preset);
let tag = splitTitle[0];

function extractContext(title: string): [string, string, string] {
if (!allowedTags.includes(tag)) {
throw(`- Incorrect PR tag: "${tag}" is not accepted by Semantic-Release`)
};

let regEx: RegExp = /(^[\w\s?]+)(\(.+\):\s)([^A-Z\W].*[^.]$)/g;
console.log("✅ PR title is correct!");

if (preset === 'conventionalcommits') {
regEx = /(^[\w\s?]+)(\(.+\)!:\s)([^A-Z\W].*[^.]$)/g;
} catch (err) {
console.log("❌ Incorrect PR title.")
throw(err)
};
};


function extractContext(title: string, preset: string): string[] {

let matches = title.match(regEx) || [];
let regEx: RegExp = /(^[\w\s?]+)(\(.+\)(!?):\s)([^A-Z\W].*[^.]$)/g;

let matches = title.matchAll(regEx);

try {
let tag = matches.map(e => e.replace(regEx, '$1'))[0];
let subj = matches.map(e => e.replace(regEx, '$2'))[0];
let msg = matches.map(e => e.replace(regEx, '$3'))[0];
let results = Array.from(matches)[0].filter(Boolean).splice(1);

if (!tag || !subj || !msg) {
throw("❌ Incorrect Title.");
if (results.length === 4 && preset !== 'conventionalcommits') {
throw("- To use '!' in the title, set preset as `convenvionalcommits`");
};

return [tag, subj, msg];
return results;

} catch (err) {
console.log(err);
throw(err);
throw(`${err}\n- "${title}" format is incorrect. Please use Angular Commit Message Conventions`)
};
};
11 changes: 5 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { context } from '@actions/github'
import * as linter from './linter'
import { linter } from './linter'


export function run() {
try {
const pullRequestTitle: string = context.payload.pull_request?.title;
let pullRequestTitle: string = context.payload.pull_request?.title;

if (!pullRequestTitle) {
throw("Title not found");
throw("- Title not found");
};

linter.linter(pullRequestTitle);
linter(pullRequestTitle);

} catch (err) {
console.log('❌ PR Title check failed');
throw(err);
console.log(`❌ PR Title linter failed\n${ err }`);
};
};

Expand Down

0 comments on commit 98cbbf4

Please sign in to comment.