|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import {ChangelogSection} from '../changelog-notes'; |
| 16 | +import {ConventionalCommit} from '../commit'; |
| 17 | + |
| 18 | +const BREAKING_CHANGE_NOTE = 'BREAKING CHANGE'; |
| 19 | + |
| 20 | +const DEFAULT_CHANGELOG_SECTIONS = [ |
| 21 | + {type: 'feat', section: 'Features'}, |
| 22 | + {type: 'fix', section: 'Bug Fixes'}, |
| 23 | + {type: 'perf', section: 'Performance Improvements'}, |
| 24 | + {type: 'revert', section: 'Reverts'}, |
| 25 | + {type: 'chore', section: 'Miscellaneous Chores', hidden: true}, |
| 26 | + {type: 'docs', section: 'Documentation', hidden: true}, |
| 27 | + {type: 'style', section: 'Styles', hidden: true}, |
| 28 | + {type: 'refactor', section: 'Code Refactoring', hidden: true}, |
| 29 | + {type: 'test', section: 'Tests', hidden: true}, |
| 30 | + {type: 'build', section: 'Build System', hidden: true}, |
| 31 | + {type: 'ci', section: 'Continuous Integration', hidden: true}, |
| 32 | +]; |
| 33 | + |
| 34 | +/** |
| 35 | + * Given a set of conventional commits and the configured |
| 36 | + * changelog sections provided by the user, return the set |
| 37 | + * of commits that should be displayed: |
| 38 | + * |
| 39 | + * @param commits |
| 40 | + * @param changelogSections |
| 41 | + * @returns ConventionalCommit[] |
| 42 | + */ |
| 43 | +export function filterCommits( |
| 44 | + commits: ConventionalCommit[], |
| 45 | + changelogSections?: ChangelogSection[] |
| 46 | +): ConventionalCommit[] { |
| 47 | + changelogSections = changelogSections ?? DEFAULT_CHANGELOG_SECTIONS; |
| 48 | + const hiddenSections: Array<string> = []; |
| 49 | + const visibleSections: Array<string> = []; |
| 50 | + for (const section of changelogSections) { |
| 51 | + if (!section.hidden) visibleSections.push(section.type); |
| 52 | + else hiddenSections.push(section.type); |
| 53 | + } |
| 54 | + return commits.filter(commit => { |
| 55 | + const isBreaking = commit.notes.find(note => { |
| 56 | + return note.title === BREAKING_CHANGE_NOTE; |
| 57 | + }); |
| 58 | + return ( |
| 59 | + visibleSections.includes(commit.type) || |
| 60 | + (isBreaking && hiddenSections.includes(commit.type)) |
| 61 | + ); |
| 62 | + }); |
| 63 | +} |
0 commit comments