blob: 51493eaf064f4776e603fb1974c572e1070d74b6 [file] [log] [blame]
Chris Kayfa3a1382021-11-12 15:48:44 +00001/*
Yann Gautier022f8fe2025-06-11 18:20:56 +02002 * Copyright (c) 2021-2025, Arm Limited and Contributors. All rights reserved.
Chris Kayfa3a1382021-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 Kayc7080f62024-02-27 15:59:23 +000011import fs from "fs";
12import rules from "@commitlint/rules";
13import yaml from "js-yaml";
Chris Kayfa3a1382021-11-12 15:48:44 +000014
Chris Kayc4e8eda2021-11-09 20:05:38 +000015/*
Chris Kayf64c5582021-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 Kayc4e8eda2021-11-09 20:05:38 +000019 */
Chris Kayc4e8eda2021-11-09 20:05:38 +000020
Chris Kayf64c5582021-12-01 16:34:55 +000021let changelog;
22
23try {
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 Kayc7080f62024-02-27 15:59:23 +000039 const scope = subsection.scope ? [subsection.scope] : [];
Chris Kayf64c5582021-12-01 16:34:55 +000040 const subscopes = getScopes(subsection.subsections || []);
Chris Kayc4e8eda2021-11-09 20:05:38 +000041
42 return scope.concat(subscopes);
43 })
44};
45
Chris Kayf64c5582021-12-01 16:34:55 +000046const types = getTypes(changelog.sections).sort(); /* Sort alphabetically */
47const scopes = getScopes(changelog.subsections).sort(); /* Sort alphabetically */
Chris Kayc4e8eda2021-11-09 20:05:38 +000048
Chris Kayc7080f62024-02-27 15:59:23 +000049export default {
Chris Kayfa3a1382021-11-12 15:48:44 +000050 extends: ["@commitlint/config-conventional"],
51 plugins: [
52 {
53 rules: {
Chris Kayc7080f62024-02-27 15:59:23 +000054 "signed-off-by-exists": rules["trailer-exists"],
55 "change-id-exists": rules["trailer-exists"],
Chris Kayfa3a1382021-11-12 15:48:44 +000056 },
57 },
58 ],
59 rules: {
Chris Kayf64c5582021-12-01 16:34:55 +000060 "header-max-length": [1, "always", 50], /* Warning */
61 "body-max-line-length": [1, "always", 72], /* Warning */
Chris Kayfa3a1382021-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 Kayc4e8eda2021-11-09 20:05:38 +000065
Chris Kayc7080f62024-02-27 15:59:23 +000066 "type-case": [2, "always", "lower-case"], /* Error */
Chris Kayf64c5582021-12-01 16:34:55 +000067 "type-enum": [2, "always", types], /* Error */
68
Yann Gautier804e52e2021-11-19 17:57:50 +010069 "scope-case": [2, "always", "lower-case"], /* Error */
Yann Gautier022f8fe2025-06-11 18:20:56 +020070 "scope-enum": [2, "always", scopes] /* Error */
Chris Kayfa3a1382021-11-12 15:48:44 +000071 },
72};