Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change gren to take only pull requests that have been merged #154

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 96 additions & 6 deletions lib/src/Gren.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,32 @@ class Gren {
return this.repo._request('GET', `/repos/${this.repo.__fullname}/releases`, options);
}

/**
* Get the merged pull requests from the repo
*
* @private
*
* @param {number} page
* @param {number} limit
*
* @return {Promise[]} The promises which returns pull requests
*/
async _getMergedPullRequests(page = 1, limit = 100) {
const results = await this.repo.listPullRequests({
state: 'closed',
per_page: limit,
page
});
const { headers: { link }, data: prs } = results;
const totalPages = this._getLastPage(link);
const filterPrs = prs.filter(pr => pr.merged_at);
if (totalPages && +page < totalPages) {
return this._getMergedPullRequests(page + 1).then(prsResults => prsResults.concat(filterPrs));
}

return filterPrs;
}

/**
* Get the last page from a Hypermedia link
*
Expand Down Expand Up @@ -488,7 +514,7 @@ class Gren {
*
* @return {string}
*/
_templateIssueBody(body, rangeBody) {
_templateBody(body, rangeBody) {
if (Array.isArray(body) && body.length) {
return body.join('\n') + '\n';
}
Expand Down Expand Up @@ -673,8 +699,7 @@ class Gren {
async _getClosedIssues(releaseRanges) {
const type = {
issues: 'Issues',
milestones: 'Issues',
prs: 'Pull Requests'
milestones: 'Issues'
}[this.options.dataSource];
const loaded = utils.task(this, `Getting all closed ${type}`);
const { data: issues } = await this.issues.listIssues({
Expand Down Expand Up @@ -788,6 +813,18 @@ class Gren {
!((this.options.onlyMilestones || dataSource === 'milestones') && !issue.milestone);
}

/**
* Filter the pull request based on gren options and labels
* @private
*
* @param {Object} pullRequest
*
* @return {Boolean}
*/
_filterPullRequest(pullRequest) {
return !this._lablesAreIgnored(pullRequest.labels) && !(this.options.onlyMilestones && !pullRequest.milestone);
}

/**
* Filter the issue based on the date range, or if is in the release
* milestone.
Expand All @@ -812,6 +849,29 @@ class Gren {
);
}

/**
* Filter the pull requests in case the release is milestone,
* or otherwise by dates range.
*
* @private
*
* @param {Array} range The release ranges
* @param {Object} pullRequest GitHub pull request
*
* @return {Boolean}
*/
_filterBlockPullRequest(range, pullRequest) {
if (this.options.dataSource === 'milestones') {
return this.options.milestoneMatch.replace('{{tag_name}}', range[0].name) === pullRequest.milestone.title;
}

return utils.isInRange(
Date.parse(pullRequest.merged_at),
Date.parse(range[1].date),
Date.parse(range[0].date)
);
}

/**
* Get the blocks of issues based on release dates
*
Expand All @@ -830,18 +890,48 @@ class Gren {
.filter(this._filterIssue.bind(this))
.filter(this._filterBlockIssue.bind(this, range));
const body = (!range[0].body || this.options.override) && this._groupBy(filteredIssues);

return {
id: range[0].id,
release: range[0].name,
name: this.options.prefix + range[0].name,
published_at: range[0].date,
body: this._templateIssueBody(body, range[0].body)
body: this._templateBody(body, range[0].body)
};
});

return release;
}
/**
* Get the blocks of pull requests based on the release dates
*
* @private
*
* @param {Array} releaseRanges The array of date ranges
*
* @return {Promise[]}
*/
async _getPullRequestsBlocks(releaseRanges) {
const loaded = utils.task(this, `Getting all merged pull requests`);
const prs = await this._getMergedPullRequests();
let totalPrs = 0;
const release = releaseRanges
.map(range => {
const filteredPullRequests = Array.from(prs)
.filter(this._filterPullRequest.bind(this))
.filter(this._filterBlockPullRequest.bind(this, range));
totalPrs += filteredPullRequests.length;
const body = (!range[0].body || this.options.override) && this._groupBy(filteredPullRequests);
return {
id: range[0].id,
release: range[0].name,
name: this.options.prefix + range[0].name,
published_at: range[0].date,
body: this._templateBody(body, range[0].body)
};
});
loaded(`Pull Requests found: ${totalPrs}`);
return release;
}

/**
* Sort releases by dates
Expand Down Expand Up @@ -898,7 +988,7 @@ class Gren {
issues: this._getIssueBlocks.bind(this),
commits: this._getCommitBlocks.bind(this),
milestones: this._getIssueBlocks.bind(this),
prs: this._getIssueBlocks.bind(this)
prs: this._getPullRequestsBlocks.bind(this)
};
const releases = await this._getListReleases();
this.tasks['Getting releases'].text = 'Getting tags';
Expand Down
10 changes: 5 additions & 5 deletions test/Gren.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ describe('Gren', () => {
});
});

describe('_templateIssueBody', () => {
describe('_templateBody', () => {
it('Should always return a string', () => {
const body = [
'First',
Expand All @@ -248,10 +248,10 @@ describe('Gren', () => {
];
const rangeBody = 'This is one body';

assert.isString(gren._templateIssueBody(body), 'Passing only the body');
assert.isString(gren._templateIssueBody(false, rangeBody), 'Passing only the rangeBody');
assert.isString(gren._templateIssueBody(), 'No parameters');
assert.isString(gren._templateIssueBody('This is not an Array!'), 'No parameters');
assert.isString(gren._templateBody(body), 'Passing only the body');
assert.isString(gren._templateBody(false, rangeBody), 'Passing only the rangeBody');
assert.isString(gren._templateBody(), 'No parameters');
assert.isString(gren._templateBody('This is not an Array!'), 'No parameters');
});
});

Expand Down