Skip to content

Commit 0e726fb

Browse files
pawkalexcanessa
authored andcommitted
PRs with issues data source (#200)
* new issues repo option * pull requests with issues as a new data source * linter fixes
1 parent cc0c851 commit 0e726fb

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

lib/_options.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ module.exports = {
8282
{
8383
short: '-D',
8484
name: 'data-source',
85-
valueType: '<issues|commits|milestones|prs>',
85+
valueType: '<issues|commits|milestones|prs|prs-with-issues>',
8686
description:
8787
'The informations you want to use to build release notes. [issues]',
88-
action: /^(issues|commits|milestones|prs)$/i,
88+
action: /^(issues|commits|milestones|prs|prs-with-issues)$/i,
8989
defaultValue: 'issues'
9090
},
9191
{

lib/src/Gren.js

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ class Gren {
6767
this._outputOptions(this.options);
6868
}
6969

70-
const githubApi = new Github({
70+
this.githubApi = new Github({
7171
token
7272
}, apiUrl);
7373

74-
this.repo = githubApi.getRepo(username, repo);
75-
this.issues = githubApi.getIssues(username, repo);
74+
this.repo = this.githubApi.getRepo(username, repo);
75+
this.issues = this.githubApi.getIssues(username, repo);
7676
}
7777

7878
/**
@@ -731,6 +731,13 @@ class Gren {
731731
return issues;
732732
}
733733

734+
async _getSingleIssue({ user, repo, number }) {
735+
const loaded = utils.task(this, `Getting single ${number} issue data`);
736+
const { data: issue } = await this.githubApi.getIssues(user, repo).getIssue(number);
737+
loaded(`Issue details found: ${issue.number} ${issue.title}`);
738+
return issue;
739+
}
740+
734741
/**
735742
* Group the issues based on their first label
736743
*
@@ -954,6 +961,55 @@ class Gren {
954961
return release;
955962
}
956963

964+
async _getPullRequestWithIssueBlocks(releaseRanges) {
965+
const loaded = utils.task(this, `Getting all merged pull requests`);
966+
const since = releaseRanges[releaseRanges.length - 1][1].date;
967+
let relevantIssues = [];
968+
969+
const re = new RegExp(`([\\w-]+)/([\\w-]+)/issues/([0-9]+)`, 'gi');
970+
971+
const prs = (await this._getMergedPullRequests(since))
972+
.filter(this._filterPullRequest.bind(this))
973+
.map(pr => {
974+
const matches = pr.body.match(re);
975+
const relatedIssues = matches && matches.map(issue => {
976+
const [user, repo, , number] = issue.split('/');
977+
return { user, repo, number };
978+
});
979+
return Object.assign({}, pr, { relatedIssues });
980+
});
981+
982+
const release = releaseRanges
983+
.map(range => {
984+
const list = prs.filter(this._filterBlockPullRequest.bind(this, range));
985+
list.forEach(({ relatedIssues }) => { relevantIssues = relevantIssues.concat(relatedIssues || []); });
986+
loaded(`Pull Requests found: ${list.length}`);
987+
return Object.assign({}, range, { list });
988+
});
989+
990+
const issuesDetails = (await Promise.all(relevantIssues.map(this._getSingleIssue.bind(this))))
991+
.reduce((acc, el) => { acc[el.number] = el; return acc; }, {});
992+
993+
return release
994+
.map(range => {
995+
const list = range.list.map(el => el.relatedIssues && el.relatedIssues.length ? el.relatedIssues.map(({ number }) => issuesDetails[number]) : [el]);
996+
return Object.assign({}, range, { list: [].concat(...list) });
997+
})
998+
.map(this._render.bind(this));
999+
}
1000+
1001+
_render(range) {
1002+
const body = (!range[0].body || this.options.override) && this._groupBy(range.list);
1003+
1004+
return {
1005+
id: range[0].id,
1006+
release: range[0].name,
1007+
name: this.options.prefix + range[0].name,
1008+
published_at: range[0].date,
1009+
body: this._templateBody(body, range[0].body)
1010+
};
1011+
}
1012+
9571013
/**
9581014
* Sort releases by dates
9591015
*
@@ -1017,7 +1073,8 @@ class Gren {
10171073
issues: this._getIssueBlocks.bind(this),
10181074
commits: this._getCommitBlocks.bind(this),
10191075
milestones: this._getIssueBlocks.bind(this),
1020-
prs: this._getPullRequestsBlocks.bind(this)
1076+
prs: this._getPullRequestsBlocks.bind(this),
1077+
'prs-with-issues': this._getPullRequestWithIssueBlocks.bind(this)
10211078
};
10221079
const releases = await this._getListReleases();
10231080
this.tasks['Getting releases'].text = 'Getting tags';

0 commit comments

Comments
 (0)