blob: cfafbedc27cb044cf7cf2d8ebbec34f3ab4d6aad [file] [log] [blame]
Chris Kay285c1302021-11-12 15:48:44 +00001/*
2 * Copyright (c) 2021, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7/* eslint-env es6 */
8
9"use strict";
10
Chris Kay82117d72021-12-01 16:34:55 +000011const fs = require("fs");
12const yaml = require("js-yaml");
13
Chris Kay285c1302021-11-12 15:48:44 +000014const { "trailer-exists": trailerExists } = require("@commitlint/rules").default;
15
Chris Kay025c87f2021-11-09 20:05:38 +000016/*
Chris Kay82117d72021-12-01 16:34:55 +000017 * The types and scopes accepted by both Commitlint and Commitizen are defined by the changelog
18 * configuration file - `changelog.yaml` - as they decide which section of the changelog commits
19 * with a given type and scope are placed in.
Chris Kay025c87f2021-11-09 20:05:38 +000020 */
Chris Kay82117d72021-12-01 16:34:55 +000021
22let changelog;
Chris Kay025c87f2021-11-09 20:05:38 +000023
Chris Kay82117d72021-12-01 16:34:55 +000024try {
25 const contents = fs.readFileSync("changelog.yaml", "utf8");
26
27 changelog = yaml.load(contents);
28} catch (err) {
29 console.log(err);
30
31 throw err;
32}
33
34function getTypes(sections) {
35 return sections.map(section => section.type)
36}
37
38function getScopes(subsections) {
39 return subsections.flatMap(subsection => {
40 const scope = subsection.scope ? [ subsection.scope ] : [];
41 const subscopes = getScopes(subsection.subsections || []);
Chris Kay025c87f2021-11-09 20:05:38 +000042
43 return scope.concat(subscopes);
44 })
45};
46
Chris Kay82117d72021-12-01 16:34:55 +000047const types = getTypes(changelog.sections).sort(); /* Sort alphabetically */
48const scopes = getScopes(changelog.subsections).sort(); /* Sort alphabetically */
Chris Kay025c87f2021-11-09 20:05:38 +000049
Chris Kay285c1302021-11-12 15:48:44 +000050module.exports = {
51 extends: ["@commitlint/config-conventional"],
52 plugins: [
53 {
54 rules: {
55 "signed-off-by-exists": trailerExists,
56 "change-id-exists": trailerExists,
57 },
58 },
59 ],
60 rules: {
Chris Kay82117d72021-12-01 16:34:55 +000061 "header-max-length": [1, "always", 50], /* Warning */
62 "body-max-line-length": [1, "always", 72], /* Warning */
Chris Kay285c1302021-11-12 15:48:44 +000063
64 "change-id-exists": [1, "always", "Change-Id:"], /* Warning */
65 "signed-off-by-exists": [1, "always", "Signed-off-by:"], /* Warning */
Chris Kay025c87f2021-11-09 20:05:38 +000066
Chris Kay82117d72021-12-01 16:34:55 +000067 "type-case": [2, "always", "lower-case" ], /* Error */
68 "type-enum": [2, "always", types], /* Error */
69
Yann Gautier54b08f92021-11-19 17:57:50 +010070 "scope-case": [2, "always", "lower-case"], /* Error */
Chris Kay025c87f2021-11-09 20:05:38 +000071 "scope-enum": [1, "always", scopes] /* Warning */
Chris Kay285c1302021-11-12 15:48:44 +000072 },
73};