Skip to content

Commit

Permalink
Create tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcanessa committed Sep 29, 2017
1 parent eb8cb23 commit 51abd84
Show file tree
Hide file tree
Showing 18 changed files with 951 additions and 106 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ rules:
array: false
prefer-spread: error
prefer-rest-params: error
prefer-promise-reject-errors: 0
valid-typeof: 0
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ before_install:
- npm install -g mocha

script:
- npm run test
- npm run coverage

after_success:
- codecov
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![npm version](https://badge.fury.io/js/github-release-notes.svg)](https://badge.fury.io/js/github-release-notes)
[![Build Status](https://travis-ci.org/github-tools/github-release-notes.svg?branch=master)](https://travis-ci.org/github-tools/github-release-notes)
[![Join the chat at https://gitter.im/github-release-notes/Lobby](https://badges.gitter.im/github-release-notes/Lobby.svg)](https://gitter.im/github-release-notes/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Codecov](https://codecov.io/gh/github-tools/github-release-notes/branch/second-refactor/graph/badge.svg)](https://codecov.io/gh/github-tools/github-release-notes/)

## Motivation

Expand Down
1 change: 0 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const eslint = require('gulp-eslint');
const ghPages = require('gulp-gh-pages');
const gulp = require('gulp');
const gulpIf = require('gulp-if');
const mocha = require('gulp-mocha');
const sass = require('gulp-sass');
const watch = require('gulp-watch');

Expand Down
6 changes: 1 addition & 5 deletions lib/_examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const chalk = require('chalk');
* Generate examples based on an Array
*
* @param {Array} examples
*/
*/// istanbul ignore next
function generateExamples(title, examples) {
process.stdout.write(`\n Examples for ${title}:`);
const spaceify = number => new Array(!number || number < 0 ? 0 : number + 1).join(' ');
Expand Down Expand Up @@ -80,10 +80,6 @@ module.exports = {
{
name: 'Create release notes for all the tags',
description: 'Get the commits or issues closed between the specified tag and the one before.',
code: 'gren release --tags=*'
},
{
description: '_`all` instead of `*` will be deprecated in 1.0.0_ ',
code: 'gren release --tags=all'
},
{
Expand Down
107 changes: 71 additions & 36 deletions lib/src/Gren.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const defaults = {
override: false,
ignoreLabels: false,
ignoreIssuesWith: false,
groupBy: false
groupBy: false,
milestoneMatch: 'Release {{tag_name}}'
};

/** Class creating release notes and changelog notes */
Expand Down Expand Up @@ -103,7 +104,7 @@ class Gren {
const filePath = process.cwd() + '/' + this.options.changelogFilename;

if (fs.existsSync(filePath) && !this.options.override) {
return Promise.reject(chalk.red('Looks like there is already a changelog, to override it use --override'));
return Promise.reject(chalk.black(chalk.bgYellow('Looks like there is already a changelog, to override it use --override')));
}

return Promise.resolve();
Expand Down Expand Up @@ -215,7 +216,7 @@ class Gren {

if (block.id) {
if (!this.options.override) {
console.warn(chalk.black(chalk.bgYellow('Skipping ' + block.release + ' (use --override to replace it)')));
console.warn(chalk.black(chalk.bgYellow(`Skipping ${block.release} (use --override to replace it)`)));

return Promise.resolve();
}
Expand All @@ -233,27 +234,29 @@ class Gren {
* @since 0.5.0
* @private
*
* @param {Array|string} selectedTags
* @param {Array|string} allTags
* @param {Object[]} tags
*
* @return {Boolean|Array}
*/
_getSelectedTags(optionTags = [], tags) {
if (optionTags.indexOf('all') >= 0 || optionTags.indexOf('*') >= 0) {
return tags;
_getSelectedTags(allTags) {
const { tags } = this.options;

if (tags.indexOf('all') >= 0) {
return allTags;
}

if (!optionTags.length) {
if (!allTags || !allTags.length || !tags.length) {
return false;
}

const selectedTags = [].concat(optionTags);
const selectedTags = [].concat(tags);

return tags.filter((tag, index) => {
const isSelectedTag = selectedTags.indexOf(tag.name) !== -1;
return allTags.filter(({ name }, index) => {
const isSelectedTag = selectedTags.includes(name);

if (isSelectedTag && selectedTags.length === 1 && tags[index + 1]) {
selectedTags.push(tags[index + 1].name);
if (isSelectedTag && selectedTags.length === 1 && allTags[index + 1]) {
selectedTags.push(allTags[index + 1].name);
}
return isSelectedTag;
}).slice(0, 2);
Expand All @@ -275,7 +278,12 @@ class Gren {
loaded();

const tags = response.data;
const filteredTags = (this._getSelectedTags(this.options.tags, tags) || [tags[0], tags[1]])

if (!tags.length) {
throw chalk.red('Looks like you have no tags! Tag a commit first and then run gren again');
}

const filteredTags = (this._getSelectedTags(tags) || [tags[0], tags[1]])
.filter(Boolean)
.map(tag => {
const tagRelease = releases ? releases.filter(release => release.tag_name === tag.name)[0] : false;
Expand Down Expand Up @@ -340,6 +348,7 @@ class Gren {
*
* @since 0.8.0
* @private
* @ignore
*
* @param {Array} releases The release Objects Array coming from GitHub
*
Expand Down Expand Up @@ -382,11 +391,13 @@ class Gren {
* @return {string}
*/
_templateLabels(issue) {
if (!issue.labels.length && this.options.template.noLabel) {
issue.labels.push({name: this.options.template.noLabel});
const labels = Array.from(issue.labels);

if (!labels.length && this.options.template.noLabel) {
labels.push({name: this.options.template.noLabel});
}

return issue.labels
return labels
.filter(label => this.options.ignoreLabels.indexOf(label.name) === -1)
.map(label => generate({
label: label.name
Expand Down Expand Up @@ -423,7 +434,15 @@ class Gren {
* @return {string}
*/
_templateIssueBody(body, rangeBody) {
return (body.length ? body.join('\n') : rangeBody || '*No changelog for this release.*') + '\n';
if (Array.isArray(body) && body.length) {
return body.join('\n') + '\n';
}

if (rangeBody) {
return `${rangeBody}\n`;
}

return '*No changelog for this release.*\n';
}

/**
Expand Down Expand Up @@ -456,13 +475,18 @@ class Gren {
* @since 0.1.0
* @private
*
* @param {string} message
* @param {Array} messages
*
* @return {string}
*/
_generateCommitsBody(messages) {
messages.length === 1 && messages.push(null);
return messages
_generateCommitsBody(messages = []) {
const bodyMessages = Array.from(messages);

if (bodyMessages.length === 1) {
bodyMessages.push(null);
}

return bodyMessages
.slice(0, -1)
.filter(message => {
const messageType = this.options.includeMessages;
Expand Down Expand Up @@ -498,8 +522,12 @@ class Gren {
*
* @return {String[]}
*/
_commitMessages(commits) {
return commits.map(commitObject => commitObject.commit.message);
_commitMessages(commits = []) {
if (!Array.isArray(commits)) {
return [];
}

return commits.map(({ commit }) => commit && commit.message).filter(Boolean);
}

/**
Expand Down Expand Up @@ -555,17 +583,24 @@ class Gren {
/**
* Compare the ignored labels with the passed ones
*
* @since 0.6.0
* @since 0.10.0
* @private
*
* @param {Array} ignoreLabels The labels to ignore
* @param {Array} labels The labels to check
* @example [{
* name: 'bug'
* }]
*
* @return {boolean} If the labels array contain any of the ignore ones
* @return {boolean} If the labels array contains any of the ignore ones
*/
_compareIssueLabels(ignoreLabels, labels) {
return ignoreLabels
.reduce((carry, ignoredLabel) => carry && labels.map(label => label.name).indexOf(ignoredLabel) === -1, true);
_lablesAreIgnored(labels) {
if (!labels || !Array.isArray(labels)) {
return false;
}

const { ignoreIssuesWith } = this.options;

return ignoreIssuesWith.some(label => labels.map(({ name }) => name).includes(label));
}

/**
Expand All @@ -579,7 +614,7 @@ class Gren {
* @return {Boolean}
*/
_filterIssue(issue) {
return !issue.pull_request && this._compareIssueLabels(this.options.ignoreIssuesWith, issue.labels) &&
return !issue.pull_request && !this._lablesAreIgnored(issue.labels) &&
!((this.options.onlyMilestones || this.options.dataSource === 'milestones') && !issue.milestone);
}

Expand Down Expand Up @@ -666,10 +701,10 @@ class Gren {
throw chalk.red('The option for groupBy is invalid, please check the documentation');
}

const allLabels = Object.keys(groupBy).reduce((carry, group) => carry.concat(groupBy[group]), []);
const allLabels = Object.values(groupBy).reduce((carry, group) => carry.concat(group), []);

const groups = Object.keys(groupBy).reduce((carry, group) => {
const groupIssues = issues.filter(issue => {
const groupIssues = Array.from(issues).filter(issue => {
if (!issue.labels.length && this.options.template.noLabel) {
issue.labels.push({name: this.options.template.noLabel});
}
Expand Down Expand Up @@ -755,7 +790,7 @@ class Gren {
* @return {Array}
*/
_sortReleasesByDate(releaseDates) {
return releaseDates.sort((release1, release2) => new Date(release2.date) - new Date(release1.date));
return Array.from(releaseDates).sort((release1, release2) => new Date(release2.date) - new Date(release1.date));
}

/**
Expand All @@ -773,7 +808,7 @@ class Gren {
const range = 2;
const sortedReleaseDates = this._sortReleasesByDate(releaseDates);

if (sortedReleaseDates.length === 1) {
if (sortedReleaseDates.length === 1 || this.options.tags === 'all') {
sortedReleaseDates.push({
id: 0,
date: new Date(0)
Expand Down Expand Up @@ -832,7 +867,7 @@ class Gren {
console.warn(chalk.yellow('WARNING: Looks like you don\'t have network connectivity!'));
}

resolve();
resolve(isOnline);
});
});
}
Expand Down
38 changes: 3 additions & 35 deletions lib/src/Program.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import fs from 'fs';
import GitHubInfo from './GitHubInfo';
import program from 'commander';
import { getConfigFromFile } from './_utils.js';

/** Class creating a Commander program, managing the options passed via bash and config file. */
class Program {
constructor(props) {
const { programOptions, defaults, cwd } = this._consumeOptions(props.options);
const { programOptions, defaults } = this._consumeOptions(props.options);

this.name = props.name;
this.description = props.description;
this.examples = props.examples;
this.defaults = defaults;
this.cwd = cwd;
this.program = this._programWithEvents(this._programWithOptions(program, programOptions), props.events)
.name(this.name)
.description(this.description)
.parse(props.argv);

this.options = Object.assign({}, this._getConfigFromFile(this.cwd), this._getOptionsFromProgram(this.defaults));
this.options = Object.assign({}, getConfigFromFile(props.cwd), this._getOptionsFromProgram(this.defaults));
}

/**
Expand Down Expand Up @@ -82,37 +81,6 @@ class Program {
}, {});
}

/**
* Get configuration from the one of the config files
*
* @since 0.6.0
* @private
*
* @param {string} path Path where to look for config files
* @return {Object} The configuration from the first found file or empty object
*/
_getConfigFromFile(path) {
const _requireConfig = filepath => {
if (!fs.existsSync(filepath)) {
return false;
}

if (filepath.match(/\./g).length === 1) {
return JSON.parse(fs.readFileSync(filepath, 'utf8'));
}

return require(filepath);
};

return [
'.grenrc.yml',
'.grenrc.json',
'.grenrc.yaml',
'.grenrc.js',
'.grenrc'
].reduce((carry, filename) => carry || _requireConfig(path + '/' + filename), false) || {};
}

/**
* Add all the given events to a program
*
Expand Down
Loading

0 comments on commit 51abd84

Please sign in to comment.