blob: 53e3a635a1cce0fcebcb690a7a69bd4606663d98 [file] [log] [blame]
Chris Kay285c1302021-11-12 15:48:44 +00001/*
Chris Kay74acea22024-02-27 15:59:23 +00002 * Copyright (c) 2021-2024, Arm Limited. All rights reserved.
Chris Kay285c1302021-11-12 15:48:44 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7/* eslint-env es6 */
8
9"use strict";
10
Chris Kay74acea22024-02-27 15:59:23 +000011import fs from "fs";
12import rules from "@commitlint/rules";
13import yaml from "js-yaml";
Chris Kay285c1302021-11-12 15:48:44 +000014
Chris Kay025c87f2021-11-09 20:05:38 +000015/*
Chris Kay82117d72021-12-01 16:34:55 +000016 * The types and scopes accepted by both Commitlint and Commitizen are defined by the changelog
17 * configuration file - `changelog.yaml` - as they decide which section of the changelog commits
18 * with a given type and scope are placed in.
Chris Kay025c87f2021-11-09 20:05:38 +000019 */
Chris Kay82117d72021-12-01 16:34:55 +000020
21let changelog;
Chris Kay025c87f2021-11-09 20:05:38 +000022
Chris Kay82117d72021-12-01 16:34:55 +000023try {
24 const contents = fs.readFileSync("changelog.yaml", "utf8");
25
26 changelog = yaml.load(contents);
27} catch (err) {
28 console.log(err);
29
30 throw err;
31}
32
33function getTypes(sections) {
34 return sections.map(section => section.type)
35}
36
37function getScopes(subsections) {
38 return subsections.flatMap(subsection => {
Chris Kay74acea22024-02-27 15:59:23 +000039 const scope = subsection.scope ? [subsection.scope] : [];
Chris Kay82117d72021-12-01 16:34:55 +000040 const subscopes = getScopes(subsection.subsections || []);
Chris Kay025c87f2021-11-09 20:05:38 +000041
42 return scope.concat(subscopes);
43 })
44};
45
Chris Kay82117d72021-12-01 16:34:55 +000046const types = getTypes(changelog.sections).sort(); /* Sort alphabetically */
47const scopes = getScopes(changelog.subsections).sort(); /* Sort alphabetically */
Chris Kay025c87f2021-11-09 20:05:38 +000048
Chris Kay74acea22024-02-27 15:59:23 +000049export default {
Chris Kay285c1302021-11-12 15:48:44 +000050 extends: ["@commitlint/config-conventional"],
51 plugins: [
52 {
53 rules: {
Chris Kay74acea22024-02-27 15:59:23 +000054 "signed-off-by-exists": rules["trailer-exists"],
55 "change-id-exists": rules["trailer-exists"],
Chris Kay285c1302021-11-12 15:48:44 +000056 },
57 },
58 ],
59 rules: {
Chris Kay82117d72021-12-01 16:34:55 +000060 "header-max-length": [1, "always", 50], /* Warning */
61 "body-max-line-length": [1, "always", 72], /* Warning */
Chris Kay285c1302021-11-12 15:48:44 +000062
63 "change-id-exists": [1, "always", "Change-Id:"], /* Warning */
64 "signed-off-by-exists": [1, "always", "Signed-off-by:"], /* Warning */
Chris Kay025c87f2021-11-09 20:05:38 +000065
Chris Kay74acea22024-02-27 15:59:23 +000066 "type-case": [2, "always", "lower-case"], /* Error */
Chris Kay82117d72021-12-01 16:34:55 +000067 "type-enum": [2, "always", types], /* Error */
68
Yann Gautier54b08f92021-11-19 17:57:50 +010069 "scope-case": [2, "always", "lower-case"], /* Error */
Chris Kay025c87f2021-11-09 20:05:38 +000070 "scope-enum": [1, "always", scopes] /* Warning */
Chris Kay285c1302021-11-12 15:48:44 +000071 },
72};