Skip to content

Commit 3d30460

Browse files
authored
Filter milestones (#75)
* Create milestones data-source option * Add documentation for the milestones option
1 parent 1eb28d5 commit 3d30460

File tree

3 files changed

+62
-17
lines changed

3 files changed

+62
-17
lines changed

docs/examples.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ Adding the flag `--data-source=commits` will change the source of the release no
3535
gren --data-source=commits
3636
```
3737

38+
### Milestones
39+
40+
You can base your release notes on milestones.
41+
The way the script will _link_ the releases to your milestones is by the tag name, using the option `milestone-match`.
42+
_e.g._ `Release {{tag_name}}` will match the milestone titles that have the relative tag name, in this case something like `Release 0.4.1`.
43+
44+
```
45+
gren --data-source=milestones --milestone-match="v{{tag_name}}"
46+
```
47+
3848
### Release specific tags
3949

4050
The flag `--tags` accepts one or two tags.

docs/options.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@ To pass it to the `GithubReleaseNotes` class, in the [configuration file](#confi
1616

1717
| Command | Options | Description | Default |
1818
| ------- | ------- | ----------- | ------- |
19-
| `api-url` | **Optional** | Override the GitHub API URL, allows **gren** to connect to a private [GHE](https://enterprise.github.com/) installation. _e.g. `https://my-enterprise-domain.com/api/v3`_ | `null` |
2019
| `username` | **Required** | The username of the repo _e.g. `github-tools`_ | `null` |
2120
| `repo` | **Required** | The repository name _e.g. `github-release-notes`_ | `null` |
21+
| `api-url` | **Optional** | Override the GitHub API URL, allows **gren** to connect to a private [GHE](https://enterprise.github.com/) installation. _e.g. `https://my-enterprise-domain.com/api/v3`_ | `null` |
2222
| `action`| `release` `changelog` | The **gren** action to run. _(see details below for changelog generator)_ | `release` |
2323
| `tags` | `0.1.0` `0.2.0,0.1.0` `all` | A specific tag or the range of tags to build the release notes from. You can also specify `all` to write all releases. _(To override existing releases use the --override flag)_ | `false` |
2424
| `ignore-labels` | `wont_fix` `wont_fix,duplicate` | Ignore the specified labels. | `false` |
2525
| `ignore-issues-with` | `wont_fix` `wont_fix,duplicate` | Ignore issues that contains one of the specified labels. | `false` |
26-
| `data-source` | `issues` `commits` | The informations you want to use to build release notes. | `issues` |
26+
| `data-source` | `issues` `commits` `milestones` | The informations you want to use to build release notes. For more informations about the `milestones` option, [have a look here]({{ "example#milestones" | relative_url }}) | `issues` |
27+
| `milestone-match` | **String** | The title that the script needs to match to link the release to the milestone. [See further details]({{ "example#milestones" | relative_url }}) | `null` |
2728
| `prefix` | **String** `e.g. v` | Add a prefix to the tag version. | `null` |
2829
| `override` | **Flag** | Override the release notes if existing. | `false` |
2930
| `include-messages` | `merge` `commits` `all` | Filter the messages added to the release notes. _Only used when `data-source` used is `commits` | `commits` |
3031
| `group-by` | `label` `{...}` | Group the issues using the labels as group headings. You can set custom headings for groups of labels. [See example]({{ "example#group-by" | relative_url }}) | `false` |
32+
| `only-milestones` | **Flag** | Add to the release bodies only the issues that have a milestone | `false` |
3133

3234
### Release options
3335

src/gren.js

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ var configFile = utils.getConfigFromFile(process.cwd());
1414

1515
var defaults = {
1616
tags: false,
17-
timeWrap: 'latest', // || history
1817
changelogFilename: 'CHANGELOG.md',
1918
dataSource: 'issues', // || commits
19+
onlyMilestones: false,
20+
milestoneMatch: 'Release {{tag_name}}',
2021
draft: false,
2122
force: false,
2223
prefix: '',
@@ -508,6 +509,22 @@ function compareIssueLabels(ignoreLabels, labels) {
508509
}, true);
509510
}
510511

512+
/**
513+
* Filter the issue based on gren options and labels
514+
*
515+
* @since 0.9.0
516+
* @private
517+
*
518+
* @param {GithubReleaseNotes} gren
519+
* @param {Object} issue
520+
*
521+
* @return {Boolean}
522+
*/
523+
function filterIssue(gren, issue) {
524+
return !issue.pull_request && compareIssueLabels(gren.options.ignoreIssuesWith, issue.labels) &&
525+
!((gren.options.onlyMilestones || gren.options.dataSource === 'milestones') && !issue.milestone);
526+
}
527+
511528
/**
512529
* Get all the closed issues from the current repo
513530
*
@@ -529,9 +546,7 @@ function getClosedIssues(gren, releaseRanges) {
529546
.then(function(response) {
530547
loaded();
531548

532-
var filteredIssues = response.data.filter(function(issue) {
533-
return !issue.pull_request && compareIssueLabels(gren.options.ignoreIssuesWith, issue.labels);
534-
});
549+
var filteredIssues = response.data.filter(filterIssue.bind(null, gren));
535550

536551
process.stdout.write(filteredIssues.length + ' issues found\n');
537552

@@ -623,6 +638,31 @@ function groupBy(gren, issues) {
623638
return templateGroups(gren, groups);
624639
}
625640

641+
/**
642+
* Filter the issue based on the date range, or if is in the release
643+
* milestone.
644+
*
645+
* @since 0.9.0
646+
* @private
647+
*
648+
* @param {GithubReleaseNotes} gren
649+
* @param {Array} range The release ranges
650+
* @param {Object} issue GitHub issue
651+
*
652+
* @return {Boolean}
653+
*/
654+
function filterBlockIssue(gren, range, issue) {
655+
if (gren.options.dataSource === 'milestones') {
656+
return gren.options.milestoneMatch.replace('{{tag_name}}', range[0].name) === issue.milestone.title;
657+
}
658+
659+
return utils.isInRange(
660+
Date.parse(issue.closed_at),
661+
Date.parse(range[1].date),
662+
Date.parse(range[0].date)
663+
);
664+
}
665+
626666
/**
627667
* Get the blocks of issues based on release dates
628668
*
@@ -641,14 +681,7 @@ function getIssueBlocks(gren, releaseRanges) {
641681
.then(function(issues) {
642682
return releaseRanges
643683
.map(function(range) {
644-
var filteredIssues = issues.filter(function(issue) {
645-
return utils.isInRange(
646-
Date.parse(issue.closed_at),
647-
Date.parse(range[1].date),
648-
Date.parse(range[0].date)
649-
);
650-
});
651-
684+
var filteredIssues = issues.filter(filterBlockIssue.bind(null, gren, range));
652685
var body = (!range[0].body || gren.options.override) && groupBy(gren, filteredIssues);
653686

654687
return {
@@ -694,7 +727,7 @@ function createReleaseRanges(gren, releaseDates) {
694727
var range = 2;
695728
var sortedReleaseDates = sortReleasesByDate(releaseDates);
696729

697-
if (sortedReleaseDates.length === 1 || gren.options.timeWrap === 'history') {
730+
if (sortedReleaseDates.length === 1) {
698731
sortedReleaseDates.push({
699732
id: 0,
700733
date: new Date(0)
@@ -720,7 +753,8 @@ function getReleaseBlocks(gren) {
720753
var loaded;
721754
var dataSource = {
722755
issues: getIssueBlocks,
723-
commits: getCommitBlocks
756+
commits: getCommitBlocks,
757+
milestones: getIssueBlocks
724758
};
725759

726760
return getListReleases(gren)
@@ -829,7 +863,6 @@ function GithubReleaseNotes(options) {
829863
this.options.ignoreIssuesWith = utils.convertStringToArray(this.options.ignoreIssuesWith);
830864
this.repo = null;
831865
this.issues = null;
832-
this.isEditingLatestRelease = false;
833866
}
834867

835868
/**

0 commit comments

Comments
 (0)