UNPKG

4.36 kBJavaScriptView Raw
1require('lazy-ass');
2var check = require('check-more-types');
3var utils = require('./utils');
4var R = require('ramda');
5var _ = require('lodash');
6var Promise = require('bluebird');
7var debug = require('debug')('between');
8
9var reposApi = utils.github.repos;
10var getCommits = Promise.promisify(reposApi.getCommits);
11
12function getCommitsFrom(user, repo, latest, stop, previousCommits) {
13 previousCommits = previousCommits || [];
14 debug('commits %s/%s latest %s stop %s, previous number %d',
15 user, repo, utils.shortenSha(latest), utils.shortenSha(stop),
16 previousCommits.length);
17
18 return getCommits({
19 user: user,
20 repo: repo,
21 sha: latest
22 }).then(function (commits) {
23 debug('got %d new commits from %s to %s',
24 commits.length,
25 utils.shortenSha(R.head(commits).sha),
26 utils.shortenSha(R.last(commits).sha)
27 );
28
29 var allCommits = previousCommits.length ? previousCommits.concat(commits.slice(1)) : commits;
30 var fromIndex = _.findIndex(allCommits, 'sha', stop);
31 if (fromIndex === -1) {
32 if (commits.length === 1) {
33 debug('fetched single commit %s, stopping', utils.shortenSha(commits[0].sha));
34 return allCommits;
35 }
36
37 var last = R.last(allCommits).sha;
38 console.log('could not find the stop commit, fetching more commits starting with %s', last);
39 // using delay to debug
40 return Promise.delay(1).then(function () {
41 return getCommitsFrom(user, repo, last, stop, allCommits);
42 });
43 } else {
44 return allCommits;
45 }
46 });
47}
48
49// returns list of commits between two given tags
50// latest commit is first in the list
51function getCommitsBetween(options) {
52 utils.verifyRepoOptions(options);
53 var schema = {
54 from: check.commitId,
55 to: check.commitId
56 };
57 la(check.schema(schema, options), 'invalid from and to commits', options);
58
59 return getCommitsFrom(options.user, options.repo, options.to, options.from)
60 .then(function (commits) {
61 la(check.array(commits), 'could not get list of commits', options);
62 debug('found %d commits finishing with the latest commit %s',
63 commits.length, utils.shortenSha(options.to));
64
65 return R.map(function (commit) {
66 return {
67 sha: commit.sha,
68 message: commit.commit.message
69 };
70 }, commits);
71 }).then(function (commits) {
72 var fromIndex = _.findIndex(commits, 'sha', options.from);
73 debug('from commit %s is at index %d', options.from, fromIndex);
74 // we are not really interested in FROM commit, so start
75 // slice at index 1
76 // we ARE interested in TO commit, so make sure to grab
77 // the item at the fromIndex position
78 return _.slice(commits, 0, fromIndex);
79 });
80}
81
82var Report = require('./report');
83var areValidOptions = check.schema.bind(null, {
84 user: check.unemptyString,
85 repo: check.unemptyString,
86 from: check.commitId,
87 to: check.commitId
88});
89
90function getCommentsBetweenCommits(options) {
91 la(areValidOptions(options), 'bad options', options);
92 var report = new Report(options);
93
94 return getCommitsBetween(options)
95 .tap(function (commits) {
96 report.ids = _.pluck(commits, 'sha');
97 report.comments = _.pluck(commits, 'message');
98 })
99 .then(R.always(report));
100}
101
102// resolves with Report object
103module.exports = getCommentsBetweenCommits;
104
105if (!module.parent) {
106
107 (function examples() {
108 /* eslint no-unused-vars:0 */
109 function smallNumberOfCommitsExample() {
110 var options = {
111 user: 'bahmutov',
112 repo: 'next-update',
113 from: '627250039b89fba678f57f428ee9151c370d4dad',
114 to: '3d2b1fa3523c0be35ecfb30d4c81407fd4ce30a6'
115 };
116 getCommentsBetweenCommits(options)
117 .tap(function (report) {
118 la(check.object(report), 'did not get a report', report);
119 report.print();
120 });
121 }
122
123 function largeNumberOfCommitsExample() {
124 var options = {
125 user: 'chalk',
126 repo: 'chalk',
127 from: 'b0a0e42bfe96f77e0ce273c87b910ccc9280bbeb', // older (0.3.0)
128 to: '994758f01293f1fdcf63282e9917cb9f2cfbdaac' // latest (tag 0.5.1)
129 };
130 getCommentsBetweenCommits(options)
131 .tap(function (report) {
132 la(check.object(report), 'did not get a report', report);
133 report.print();
134 });
135 }
136
137 largeNumberOfCommitsExample();
138 }());
139}