-
Notifications
You must be signed in to change notification settings - Fork 325
/
Gren.js
951 lines (823 loc) · 26.5 KB
/
Gren.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
import chalk from 'chalk';
import Github from 'github-api';
import utils from './_utils.js';
import { generate } from './_template.js';
import connectivity from 'connectivity';
import templateConfig from './templates.json';
import ObjectAssign from 'object-assign-deep';
import fs from 'fs';
const defaults = {
tags: [],
prefix: '',
template: templateConfig,
prerelease: false,
generate: false,
quiet: false,
override: false,
debug: false,
ignoreLabels: false,
ignoreIssuesWith: false,
ignoreCommitsWith: false,
groupBy: false,
milestoneMatch: 'Release {{tag_name}}'
};
const MAX_TAGS_LIMIT = 99;
const TAGS_LIMIT = 30;
/** Class creating release notes and changelog notes */
class Gren {
constructor(props = {}) {
this.options = ObjectAssign({}, defaults, props);
this.tasks = [];
const {
username,
repo,
token,
apiUrl,
tags,
ignoreLabels,
ignoreIssuesWith,
ignoreCommitsWith,
ignoreTagsWith
} = this.options;
this.options.tags = utils.convertStringToArray(tags);
this.options.ignoreLabels = utils.convertStringToArray(ignoreLabels);
this.options.ignoreIssuesWith = utils.convertStringToArray(ignoreIssuesWith);
this.options.ignoreCommitsWith = utils.convertStringToArray(ignoreCommitsWith);
this.options.ignoreTagsWith = utils.convertStringToArray(ignoreTagsWith);
this.options.limit = this.options.tags.indexOf('all') >= 0 ? MAX_TAGS_LIMIT : TAGS_LIMIT;
if (!token) {
throw chalk.red('You must provide the TOKEN');
}
if (this.options.debug) {
this._outputOptions(this.options);
}
const githubApi = new Github({
token
}, apiUrl);
this.repo = githubApi.getRepo(username, repo);
this.issues = githubApi.getIssues(username, repo);
}
/**
* Generate release notes and draft a new release
*
* @since 0.10.0
* @public
*
* @return {Promise}
*/
async release() {
utils.printTask(this.options.quiet, 'Generate release notes');
await this._hasNetwork();
const blocks = await this._getReleaseBlocks();
return blocks.reduce((carry, block) => carry.then(this._prepareRelease.bind(this, block)), Promise.resolve());
}
/**
* Generate changelog file based on the release notes or generate new one
*
* @since 0.10.0
* @public
*
* @return {Promise}
*/
async changelog() {
utils.printTask(this.options.quiet, 'Generate changelog file');
await this._hasNetwork();
this._checkChangelogFile();
const releases = this.options.generate ? await this._getReleaseBlocks() : await this._getListReleases();
if (releases.length === 0) {
throw chalk.red('There are no releases, use --generate to create release notes, or run the release command.');
}
return this._createChangelog(this._templateReleases(releases));
}
/**
* Check if the changelog file exists
*
* @since 0.8.0
* @private
*
* @return {string}
*/
_checkChangelogFile() {
const filePath = process.cwd() + '/' + this.options.changelogFilename;
if (fs.existsSync(filePath) && !this.options.override) {
throw chalk.black(chalk.bgYellow('Looks like there is already a changelog, to override it use --override'));
}
return filePath;
}
/**
* Create the changelog file
*
* @since 0.8.0
* @private
*
* @param {string} body The body of the file
*/
_createChangelog(body) {
const loaded = utils.task(this, `Creating ${this.options.changelogFilename}`);
const filePath = process.cwd() + '/' + this.options.changelogFilename;
fs.writeFileSync(filePath, this.options.template.changelogTitle + body);
loaded(chalk.green(`\nChangelog created in ${filePath}`));
}
/**
* Edit a release from a given tag (in the options)
*
* @since 0.5.0
* @private
*
* @param {number} releaseId The id of the release to edit
* @param {Object} releaseOptions The options to build the release:
* @example
* {
* "tag_name": "v1.0.0",
* "target_commitish": "master",
* "name": "v1.0.0",
* "body": "Description of the release",
* "draft": false,
* "prerelease": false
* }
*
* @return {Promise}
*/
async _editRelease(releaseId, releaseOptions) {
const loaded = utils.task(this, 'Updating latest release');
const { data: release } = await this.repo.updateRelease(releaseId, releaseOptions);
loaded(chalk.green(`${release.name} has been successfully updated!`) + chalk.blue(`\nSee the results here: ${release.html_url}`));
return release;
}
/**
* Create a release from a given tag (in the options)
*
* @since 0.1.0
* @private
*
* @param {Object} releaseOptions The options to build the release:
* @example {
* "tag_name": "1.0.0",
* "target_commitish": "master",
* "name": "v1.0.0",
* "body": "Description of the release",
* "draft": false,
* "prerelease": false
* }
*
* @return {Promise}
*/
async _createRelease(releaseOptions) {
const loaded = utils.task(this, 'Preparing the release');
const { data: release } = await this.repo.createRelease(releaseOptions);
loaded(chalk.green(`\n${release.name} has been successfully created!`) + chalk.blue(`\nSee the results here: ${release.html_url}`));
return release;
}
/**
* Creates the options to make the release
*
* @since 0.2.0
* @private
*
* @param {Object[]} tags The collection of tags
*
* @return {Promise}
*/
_prepareRelease(block) {
const releaseOptions = {
tag_name: block.release,
name: block.name,
body: block.body,
draft: this.options.draft,
prerelease: this.options.prerelease
};
if (block.id) {
if (!this.options.override) {
console.warn(chalk.black(chalk.bgYellow(`Skipping ${block.release} (use --override to replace it)`)));
return Promise.resolve();
}
return this._editRelease(block.id, releaseOptions);
}
return this._createRelease(releaseOptions);
}
/**
* Get the tags information from the given ones, and adds
* the next one in case only one is given
*
* @since 0.5.0
* @private
*
* @param {Array|string} allTags
* @param {Object[]} tags
*
* @return {Boolean|Array}
*/
_getSelectedTags(allTags) {
const { tags } = this.options;
if (tags.indexOf('all') >= 0) {
return allTags;
}
if (!allTags || !allTags.length || !tags.length) {
return false;
}
const selectedTags = [].concat(tags);
return allTags.filter(({ name }, index) => {
const isSelectedTag = selectedTags.includes(name);
if (isSelectedTag && selectedTags.length === 1 && allTags[index + 1]) {
selectedTags.push(allTags[index + 1].name);
}
return isSelectedTag;
}).slice(0, 2);
}
/**
* Temporary function for this.repo.listReleases to accept options
*
* @see https://github.com/github-tools/github/pull/485
* @param {Object} options
*
* @return {Promise}
*/
_listTags(options) {
return this.repo._request('GET', `/repos/${this.repo.__fullname}/tags`, options);
}
/**
* Get all the tags of the repo
*
* @since 0.1.0
* @private
*
* @param {Array} releases
* @param {number} page
*
* @return {Promise}
*/
async _getLastTags(releases, page = 1, limit = this.options.limit) {
const { headers: { link }, data: tags } = await this._listTags({
per_page: limit,
page
});
if (!tags.length) {
throw chalk.red('\nLooks 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)
.filter(({ name }) => this.options.ignoreTagsWith.every(ignoreTag => !name.match(ignoreTag)))
.map(tag => {
const tagRelease = releases ? releases.filter(release => release.tag_name === tag.name)[0] : false;
const releaseId = tagRelease ? tagRelease.id : null;
return {
tag: tag,
releaseId: releaseId
};
});
const totalPages = this._getLastPage(link);
if (this.options.tags.indexOf('all') >= 0 && totalPages && +page < totalPages) {
return this._getLastTags(releases, page + 1).then(moreTags => moreTags.concat(filteredTags));
}
return filteredTags;
}
/**
* Get the dates of the last two tags
*
* @since 0.1.0
* @private
*
* @param {Object[]} tags List of all the tags in the repo
*
* @return {Promise[]} The promises which returns the dates
*/
_getTagDates(tags) {
return tags.map(async tag => {
const { data: { committer } } = await this.repo.getCommit(tag.tag.commit.sha);
return {
id: tag.releaseId,
name: tag.tag.name,
date: committer.date
};
});
}
/**
* Temporary function for this.repo.listReleases to accept options
*
* @see https://github.com/github-tools/github/pull/485
* @param {Object} options
*
* @return {Promise}
*/
_listReleases(options) {
return this.repo._request('GET', `/repos/${this.repo.__fullname}/releases`, options);
}
/**
* Get the last page from a Hypermedia link
*
* @since 0.11.1
* @private
*
* @param {string} link
*
* @return {boolean|number}
*/
_getLastPage(link) {
const linkMatch = Boolean(link) && link.match(/page=(\d+)>; rel="last"/);
return linkMatch && +linkMatch[1];
}
/**
* Get all releases
*
* @since 0.5.0
* @private
*
* @return {Promise} The promise which resolves an array of releases
*/
async _getListReleases(page = 1, limit = this.options.limit) {
const loaded = utils.task(this, 'Getting the list of releases');
const { headers: { link }, data: releases } = await this._listReleases({
per_page: limit,
page
});
const totalPages = this._getLastPage(link);
if (this.options.tags.indexOf('all') >= 0 && totalPages && +page < totalPages) {
return this._getListReleases(page + 1).then(moreReleases => moreReleases.concat(releases));
}
loaded(`Releases found: ${releases.length}`);
return releases;
}
/**
* Generate the releases bodies from a release Objects Array
*
* @since 0.8.0
* @private
* @ignore
*
* @param {Array} releases The release Objects Array coming from GitHub
*
* @return {string}
*/
_templateReleases(releases) {
const { template } = this.options;
return releases.map(release => generate({
release: release.name,
date: utils.formatDate(new Date(release.published_at)),
body: release.body
}, template.release)).join(template.releaseSeparator);
}
/**
* Return the templated commit message
*
* @since 0.1.0
* @private
*
* @param {Object} commit
*
* @return {string}
*/
_templateCommits({ sha, commit: { author: { name }, message, url } }) {
return generate({
sha,
message: message.split('\n')[0],
url,
author: name
}, this.options.template.commit);
}
/**
* Generate the MD template from all the labels of a specific issue
*
* @since 0.5.0
* @private
*
* @param {Object} issue
*
* @return {string}
*/
_templateLabels(issue) {
const labels = Array.from(issue.labels);
if (!labels.length && this.options.template.noLabel) {
labels.push({name: this.options.template.noLabel});
}
return labels
.filter(label => this.options.ignoreLabels.indexOf(label.name) === -1)
.map(label => generate({
label: label.name
}, this.options.template.label)).join('');
}
/**
* Generate the MD template for each issue
*
* @since 0.5.0
* @private
*
* @param {Object} issue
*
* @return {string}
*/
_templateIssue(issue) {
return generate({
labels: this._templateLabels(issue),
name: issue.title,
text: '#' + issue.number,
url: issue.html_url
}, this.options.template.issue);
}
/**
* Generate the Changelog issues body template
*
* @since 0.5.0
* @private
*
* @param {Object[]} blocks
*
* @return {string}
*/
_templateIssueBody(body, rangeBody) {
if (Array.isArray(body) && body.length) {
return body.join('\n') + '\n';
}
if (rangeBody) {
return `${rangeBody}\n`;
}
return '*No changelog for this release.*\n';
}
/**
* Generates the template for the groups
*
* @since 0.8.0
* @private
*
* @param {Object} groups The groups to template e.g.
* {
* 'bugs': [{...}, {...}, {...}]
* }
*
* @return {string}
*/
_templateGroups(groups) {
return Object.entries(groups).map(([key, value]) => {
const heading = generate({
heading: key
}, this.options.template.group);
const body = value.join('\n');
return heading + '\n' + body;
});
}
/**
* Filter a commit based on the includeMessages option and commit message
*
* @since 0.10.0
* @private
*
* @param {Object} commit
*
* @return {Boolean}
*/
_filterCommit({ commit: { message } }) {
const messageType = this.options.includeMessages;
const filterMap = {
merges: message => message.match(/^merge/i),
commits: message => !message.match(/^merge/i),
all: () => true
};
const shouldIgnoreMessage = this.options.ignoreCommitsWith.every(commitMessage => {
const regex = new RegExp(commitMessage, 'i');
return !message.split('\n')[0].match(regex);
});
if (filterMap[messageType]) {
return filterMap[messageType](message) && shouldIgnoreMessage;
}
return filterMap.commits(message) && shouldIgnoreMessage;
}
/**
* Return a commit messages generated body
*
* @since 0.1.0
* @private
*
* @param {Array} commits
*
* @return {string}
*/
_generateCommitsBody(commits = []) {
const bodyMessages = Array.from(commits);
if (bodyMessages.length === 1) {
bodyMessages.push(null);
}
return bodyMessages
.slice(0, -1)
.filter(this._filterCommit.bind(this))
.map(this._templateCommits.bind(this))
.join('\n');
}
/**
* Gets all the commits between two dates
*
* @since 0.1.0
* @private
*
* @param {string} since The since date in ISO
* @param {string} until The until date in ISO
*
* @return {Promise} The promise which resolves the [Array] commit messages
*/
async _getCommitsBetweenTwo(since, until) {
const options = {
since: since,
until: until,
per_page: 100
};
const { data } = await this.repo.listCommits(options);
return data;
}
/**
* Get the blocks of commits based on release dates
*
* @since 0.5.0
* @private
*
* @param {Array} releaseRanges The array of date ranges
*
* @return {Promise[]}
*/
async _getCommitBlocks(releaseRanges) {
const taskName = 'Creating the body blocks from commits';
const loaded = utils.task(this, taskName);
const ranges = await Promise.all(
releaseRanges
.map(async range => {
const [{ date: since }, { date: until }] = range;
this.tasks[taskName].text = `Get commits between ${utils.formatDate(new Date(since))} and ${utils.formatDate(new Date(until))}`;
const commits = await this._getCommitsBetweenTwo(range[1].date, range[0].date);
return {
id: range[0].id,
name: this.options.prefix + range[0].name,
release: range[0].name,
published_at: range[0].date,
body: this._generateCommitsBody(commits) + '\n'
};
})
);
loaded(`Commit ranges loaded: ${ranges.length}`);
return ranges;
}
/**
* Compare the ignored labels with the passed ones
*
* @since 0.10.0
* @private
*
* @param {Array} labels The labels to check
* @example [{
* name: 'bug'
* }]
*
* @return {boolean} If the labels array contains any of the ignore ones
*/
_lablesAreIgnored(labels) {
if (!labels || !Array.isArray(labels)) {
return false;
}
const { ignoreIssuesWith } = this.options;
return ignoreIssuesWith.some(label => labels.map(({ name }) => name).includes(label));
}
/**
* Get all the closed issues from the current repo
*
* @since 0.5.0
* @private
*
* @param {Array} releaseRanges The array of date ranges
*
* @return {Promise} The promise which resolves the list of the issues
*/
async _getClosedIssues(releaseRanges) {
const type = {
issues: 'Issues',
milestones: 'Issues',
prs: 'Pull Requests'
}[this.options.dataSource];
const loaded = utils.task(this, `Getting all closed ${type}`);
const { data: issues } = await this.issues.listIssues({
state: 'closed',
since: releaseRanges[releaseRanges.length - 1][1].date
});
loaded(`${type} found: ${issues.length}`);
return issues;
}
/**
* Group the issues based on their first label
*
* @since 0.8.0
* @private
*
* @param {Array} issues
*
* @return {string}
*/
_groupByLabel(issues) {
const groups = [];
Object.values(ObjectAssign({}, issues)).forEach(issue => {
if (!issue.labels.length) {
if (!this.options.template.noLabel) {
return;
}
issue.labels.push({name: this.options.template.noLabel});
}
const labelName = issue.labels[0].name;
if (!groups[labelName]) {
groups[labelName] = [];
}
groups[labelName].push(this._templateIssue(issue));
});
return this._templateGroups(utils.sortObject(groups));
}
/**
* Create groups of issues based on labels
*
* @since 0.8.0
* @private
*
* @param {Array} issues The array of all the issues.
*
* @return {Array}
*/
_groupBy(passedIssues) {
const { groupBy } = this.options;
const issues = Object.values(ObjectAssign({}, passedIssues));
if (!groupBy || groupBy === 'false') {
return issues.map(this._templateIssue.bind(this));
}
if (groupBy === 'label') {
return this._groupByLabel(issues);
}
if (typeof groupBy !== 'object' || Array.isArray(groupBy)) {
throw chalk.red('The option for groupBy is invalid, please check the documentation');
}
const allLabels = Object.values(groupBy).reduce((carry, group) => carry.concat(group), []);
const groups = Object.keys(groupBy).reduce((carry, group, i, arr) => {
const groupIssues = issues.filter(issue => {
if (!issue.labels.length && this.options.template.noLabel) {
issue.labels.push({name: this.options.template.noLabel});
}
return issue.labels.some(label => {
const isOtherLabel = groupBy[group].indexOf('...') !== -1 && allLabels.indexOf(label.name) === -1;
return groupBy[group].indexOf(label.name) !== -1 || isOtherLabel;
}) && !arr.filter(title => carry[title]).some(title => carry[title].indexOf(this._templateIssue(issue)) !== -1);
}).map(this._templateIssue.bind(this));
if (groupIssues.length) {
carry[group] = groupIssues;
}
return carry;
}, {});
return this._templateGroups(groups);
}
/**
* Filter the issue based on gren options and labels
*
* @since 0.9.0
* @private
*
* @param {Object} issue
*
* @return {Boolean}
*/
_filterIssue(issue) {
const { dataSource } = this.options;
return (issue.pull_request ? dataSource === 'prs' : dataSource === 'issues' | dataSource === 'milestones') && !this._lablesAreIgnored(issue.labels) &&
!((this.options.onlyMilestones || dataSource === 'milestones') && !issue.milestone);
}
/**
* Filter the issue based on the date range, or if is in the release
* milestone.
*
* @since 0.9.0
* @private
*
* @param {Array} range The release ranges
* @param {Object} issue GitHub issue
*
* @return {Boolean}
*/
_filterBlockIssue(range, issue) {
if (this.options.dataSource === 'milestones') {
return this.options.milestoneMatch.replace('{{tag_name}}', range[0].name) === issue.milestone.title;
}
return utils.isInRange(
Date.parse(issue.closed_at),
Date.parse(range[1].date),
Date.parse(range[0].date)
);
}
/**
* Get the blocks of issues based on release dates
*
* @since 0.5.0
* @private
*
* @param {Array} releaseRanges The array of date ranges
*
* @return {Promise[]}
*/
async _getIssueBlocks(releaseRanges) {
const issues = await this._getClosedIssues(releaseRanges);
const release = releaseRanges
.map(range => {
const filteredIssues = Array.from(issues)
.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)
};
});
return release;
}
/**
* Sort releases by dates
*
* @since 0.5.0
* @private
*
* @param {Array} releaseDates
*
* @return {Array}
*/
_sortReleasesByDate(releaseDates) {
return Array.from(releaseDates).sort((release1, release2) => new Date(release2.date) - new Date(release1.date));
}
/**
* Create the ranges of release dates
*
* @since 0.5.0
* @private
*
* @param {Array} releaseDates The release dates
*
* @return {Array}
*/
_createReleaseRanges(releaseDates) {
const ranges = [];
const range = 2;
const sortedReleaseDates = this._sortReleasesByDate(releaseDates);
if (sortedReleaseDates.length === 1 || this.options.tags.indexOf('all') >= 0) {
sortedReleaseDates.push({
id: 0,
date: new Date(0)
});
}
for (let i = 0; i < sortedReleaseDates.length - 1; i++) {
ranges.push(sortedReleaseDates.slice(i, i + range));
}
return ranges;
}
/**
* Generate release blocks based on issues or commit messages
* depending on the option.
*
* @return {Promise} Resolving the release blocks
*/
async _getReleaseBlocks() {
const loaded = utils.task(this, 'Getting releases');
const dataSource = {
issues: this._getIssueBlocks.bind(this),
commits: this._getCommitBlocks.bind(this),
milestones: this._getIssueBlocks.bind(this),
prs: this._getIssueBlocks.bind(this)
};
const releases = await this._getListReleases();
this.tasks['Getting releases'].text = 'Getting tags';
const tags = await this._getLastTags(releases.length ? releases : false);
const releaseDates = await Promise.all(this._getTagDates(tags));
loaded(`Tags found: ${tags.map(({ tag: { name } }) => name).join(', ')}`);
return dataSource[this.options.dataSource](
this._createReleaseRanges(releaseDates)
);
}
/**
* Check if there is connectivity
*
* @since 0.5.0
* @private
*
* @return {Promise}
*/
_hasNetwork() {
return new Promise(resolve => {
connectivity(isOnline => {
if (!isOnline) {
console.warn(chalk.yellow('WARNING: Looks like you don\'t have network connectivity!'));
}
resolve(isOnline);
});
});
}
/**
* Output the options in the terminal in a formatted way
*
* @param {Object} options
*/
_outputOptions(options) {
const camelcaseToSpaces = value => value.replace(/([A-Z])/g, ' $1').toLowerCase().replace(/\w/, a => a.toUpperCase());
const outputs = Object.entries(options)
.filter(option => option !== 'debug')
.map(([key, value]) => `${chalk.yellow(camelcaseToSpaces(key))}: ${value.toString() || 'empty'}`);
process.stdout.write('\n' + chalk.blue('Options: \n') + outputs.join('\n') + '\n');
}
}
export default Gren;