Skip to content

Commit

Permalink
Merge pull request #4 from willemduncan/otherSection
Browse files Browse the repository at this point in the history
Feat: Add option to also output uncategorized PRs
  • Loading branch information
w5l authored Jan 8, 2021
2 parents 56612c0 + 3fc8ded commit 37075cf
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 121 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ inputs:
title:
required: false
description: Optional title of the changelog
other_section_title:
required: false
description: Optional title for the section containing all non-prefixed pull requests
outputs:
version:
description: The current version
Expand Down
157 changes: 109 additions & 48 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ Currently mapped prefixes:

### Available options

| Name | Description |
| ---------------- | ------------------------------------------------------------------------------------------------------- |
| `title` | Optional title to use as a heading above the changelog. If not specified, the changelog has no heading. |
| `update_release` | When set to `true`, will update the current release with the generated release notes. Default `false`. |
| Name | Description |
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| `update_release` | When set to `true`, will update the current release with the generated release notes. Default `false`. |
| `title` | Title to use as a heading above the changelog. If not specified, the changelog has no heading. |
| `other_section_title` | Title to use as heading for section containing all pull requests not in other sections. If not specified, they are not displayed. |

### Generate a changelog message

Expand Down
8 changes: 8 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface Configuration {
/** Main title of the changelog. */
title: string | null,
/** Explicit section definitions. */
sections: { title: string, prefixes: string[] }[],
/** If not null, put any items that do not match an explicit section under this title. */
otherSectionTitle: string | null
}
61 changes: 61 additions & 0 deletions src/createChangelog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as core from '@actions/core';
import { Configuration } from './configuration';
import { Changelog, Issue } from './models';

/**
* Create a changelog model from a collection of issues.
* @param items The items to use as base collection.
*/
export function createChangelog(items: Issue[], config: Configuration): Changelog {
core.info(`Create changelog for ${items.length} items.`);

const result: Changelog = {
title: config.title,
sections: []
};

const addSection = (title: string, items: { title: string, item: Issue }[]) => {
core.info(`Create changelog section '${title}' with ${items.length} items.`);
if (items.length > 0) {
result.sections.push({ title, items });
}
};

config.sections.forEach(s =>
addSection(s.title, getItemsByPrefix(items, s.prefixes))
);

if (config.otherSectionTitle) {
// Take all items except those already in a section.
addSection(config.otherSectionTitle, items
.filter(i => !result.sections.some(s => s.items.some(si => si.item === i)))
.map(i => ({
title: i.title,
item: i
})));
}

return result;
}

/**
* Filter a collection of issues.
* @param items The items to filter.
* @param prefixes The prefixes to filter.
*/
function getItemsByPrefix(items: Issue[], prefixes: string[]) {
const regex = new RegExp(`^(${prefixes.join('|')})?:?\\s+`, 'i');
return items
// Match with allowed prefixes
.map(item => ({
data: item,
match: regex.exec(item.title)
}))
// Filter by positive matches.
.filter(item => item.match)
// Create title without filtered prefix.
.map(item => ({
title: item.data.title.substring(item.match![0].length),
item: item.data
}));
}
62 changes: 0 additions & 62 deletions src/generateChangelog.ts

This file was deleted.

Loading

0 comments on commit 37075cf

Please sign in to comment.