summaryrefslogtreecommitdiffstats
path: root/tools/conventional-changelog-tf-a/index.js
blob: 2a9d5b4d8a5bf88cfc1dd7abdd5a9c21f8aa982d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright (c) 2021, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

/* eslint-env es6 */

"use strict";

const Handlebars = require("handlebars");
const Q = require("q");
const _ = require("lodash");

const ccConventionalChangelog = require("conventional-changelog-conventionalcommits/conventional-changelog");
const ccParserOpts = require("conventional-changelog-conventionalcommits/parser-opts");
const ccRecommendedBumpOpts = require("conventional-changelog-conventionalcommits/conventional-recommended-bump");
const ccWriterOpts = require("conventional-changelog-conventionalcommits/writer-opts");

const execa = require("execa");

const readFileSync = require("fs").readFileSync;
const resolve = require("path").resolve;

/*
 * Register a Handlebars helper that lets us generate Markdown lists that can support multi-line
 * strings. This is driven by inconsistent formatting of breaking changes, which may be multiple
 * lines long and can terminate the list early unintentionally.
 */
Handlebars.registerHelper("tf-a-mdlist", function (indent, options) {
    const spaces = new Array(indent + 1).join(" ");
    const first = spaces + "- ";
    const nth = spaces + "  ";

    return first + options.fn(this).replace(/\n(?!\s*\n)/gm, `\n${nth}`).trim() + "\n";
});

/*
 * Register a Handlebars helper that concatenates multiple variables. We use this to generate the
 * title for the section partials.
 */
Handlebars.registerHelper("tf-a-concat", function () {
    let argv = Array.prototype.slice.call(arguments, 0);

    argv.pop();

    return argv.join("");
});

function writerOpts(config) {
    /*
     * Flatten the configuration's sections list. This helps us iterate over all of the sections
     * when we don't care about the hierarchy.
     */

    const flattenSections = function (sections) {
        return sections.flatMap(section => {
            const subsections = flattenSections(section.sections || []);

            return [section].concat(subsections);
        })
    };

    const flattenedSections = flattenSections(config.sections);

    /*
     * Register a helper to return a restructured version of the note groups that includes notes
     * categorized by their section.
     */
    Handlebars.registerHelper("tf-a-notes", function (noteGroups, options) {
        const generateTemplateData = function (sections, notes) {
            return (sections || []).flatMap(section => {
                const templateData = {
                    title: section.title,
                    sections: generateTemplateData(section.sections, notes),
                    notes: notes.filter(note => section.scopes?.includes(note.commit.scope)),
                };

                /*
                 * Don't return a section if it contains no notes and no sub-sections.
                 */
                if ((templateData.sections.length == 0) && (templateData.notes.length == 0)) {
                    return [];
                }

                return [templateData];
            });
        };

        return noteGroups.map(noteGroup => {
            return {
                title: noteGroup.title,
                sections: generateTemplateData(config.sections, noteGroup.notes),
                notes: noteGroup.notes.filter(note =>
                    !flattenedSections.some(section => section.scopes?.includes(note.commit.scope))),
            };
        });
    });

    /*
     * Register a helper to return a restructured version of the commit groups that includes commits
     * categorized by their section.
     */
    Handlebars.registerHelper("tf-a-commits", function (commitGroups, options) {
        const generateTemplateData = function (sections, commits) {
            return (sections || []).flatMap(section => {
                const templateData = {
                    title: section.title,
                    sections: generateTemplateData(section.sections, commits),
                    commits: commits.filter(commit => section.scopes?.includes(commit.scope)),
                };

                /*
                 * Don't return a section if it contains no notes and no sub-sections.
                 */
                if ((templateData.sections.length == 0) && (templateData.commits.length == 0)) {
                    return [];
                }

                return [templateData];
            });
        };

        return commitGroups.map(commitGroup => {
            return {
                title: commitGroup.title,
                sections: generateTemplateData(config.sections, commitGroup.commits),
                commits: commitGroup.commits.filter(commit =>
                    !flattenedSections.some(section => section.scopes?.includes(commit.scope))),
            };
        });
    });

    const writerOpts = ccWriterOpts(config)
        .then(writerOpts => {
            const ccWriterOptsTransform = writerOpts.transform;

            /*
             * These configuration properties can't be injected directly into the template because
             * they themselves are templates. Instead, we register them as partials, which allows
             * them to be evaluated as part of the templates they're used in.
             */
            Handlebars.registerPartial("commitUrl", config.commitUrlFormat);
            Handlebars.registerPartial("compareUrl", config.compareUrlFormat);
            Handlebars.registerPartial("issueUrl", config.issueUrlFormat);

            /*
             * Register the partials that allow us to recursively create changelog sections.
             */

            const notePartial = readFileSync(resolve(__dirname, "./templates/note.hbs"), "utf-8");
            const noteSectionPartial = readFileSync(resolve(__dirname, "./templates/note-section.hbs"), "utf-8");
            const commitSectionPartial = readFileSync(resolve(__dirname, "./templates/commit-section.hbs"), "utf-8");

            Handlebars.registerPartial("tf-a-note", notePartial);
            Handlebars.registerPartial("tf-a-note-section", noteSectionPartial);
            Handlebars.registerPartial("tf-a-commit-section", commitSectionPartial);

            /*
             * Override the base templates so that we can generate a changelog that looks at least
             * similar to the pre-Conventional Commits TF-A changelog.
             */
            writerOpts.mainTemplate = readFileSync(resolve(__dirname, "./templates/template.hbs"), "utf-8");
            writerOpts.headerPartial = readFileSync(resolve(__dirname, "./templates/header.hbs"), "utf-8");
            writerOpts.commitPartial = readFileSync(resolve(__dirname, "./templates/commit.hbs"), "utf-8");
            writerOpts.footerPartial = readFileSync(resolve(__dirname, "./templates/footer.hbs"), "utf-8");

            writerOpts.transform = function (commit, context) {
                /*
                 * Fix up commit trailers, which for some reason are not correctly recognized and
                 * end up showing up in the breaking changes.
                 */

                commit.notes.forEach(note => {
                    const trailers = execa.sync("git", ["interpret-trailers", "--parse"], {
                        input: note.text
                    }).stdout;

                    note.text = note.text.replace(trailers, "").trim();
                });

                return ccWriterOptsTransform(commit, context);
            };

            return writerOpts;
        });

    return writerOpts;
}

module.exports = function (parameter) {
    const config = parameter || {};

    return Q.all([
        ccConventionalChangelog(config),
        ccParserOpts(config),
        ccRecommendedBumpOpts(config),
        writerOpts(config)
    ]).spread((
        conventionalChangelog,
        parserOpts,
        recommendedBumpOpts,
        writerOpts
    ) => {
        if (_.isFunction(parameter)) {
            return parameter(null, {
                gitRawCommitsOpts: { noMerges: null },
                conventionalChangelog,
                parserOpts,
                recommendedBumpOpts,
                writerOpts
            });
        } else {
            return {
                conventionalChangelog,
                parserOpts,
                recommendedBumpOpts,
                writerOpts
            };
        }
    });
};