1 | require('lazy-ass');
|
2 | var check = require('check-more-types');
|
3 | var packageRepo = require('./package-repo');
|
4 |
|
5 | var log = console.log.bind(console);
|
6 | var debug = require('debug')('main');
|
7 | var _ = require('lodash');
|
8 | var utils = require('./utils');
|
9 |
|
10 | function findCommitIds(options, repoInfo) {
|
11 | la(check.object(repoInfo), 'missing repo info', repoInfo);
|
12 |
|
13 | var tagsToCommits = require('./get-commits-from-tags');
|
14 | return tagsToCommits({
|
15 | user: repoInfo.user,
|
16 | repo: repoInfo.repo,
|
17 | from: options.from,
|
18 | to: options.to
|
19 | }).tap(debug)
|
20 | .then(function mergeCommitInfo(tagsInfo) {
|
21 | return _.extend({}, repoInfo, options, tagsInfo);
|
22 | });
|
23 | }
|
24 |
|
25 | function findCommentsBetweenTags(options) {
|
26 | la(check.object(options), 'missing options', options);
|
27 | la(check.object(options.fromTag), 'missing fromTag', options);
|
28 | la(check.object(options.toTag), 'missing toTag', options);
|
29 |
|
30 | var findCommits = require('./get-comments-between-commits');
|
31 | return findCommits({
|
32 | user: options.user,
|
33 | repo: options.repo,
|
34 | from: options.fromTag.sha,
|
35 | to: options.toTag.sha
|
36 | }).then(function (report) {
|
37 | report.options.name = options.name;
|
38 | report.options.from = options.from;
|
39 | report.options.to = options.to;
|
40 | return report;
|
41 | });
|
42 | }
|
43 |
|
44 | function askGithubUsernameAndPassword() {
|
45 | var inquirer = require('inquirer');
|
46 |
|
47 | var username = {
|
48 | type: 'input',
|
49 | name: 'username',
|
50 | message: 'github username'
|
51 | };
|
52 | var password = {
|
53 | type: 'password',
|
54 | name: 'password',
|
55 | message: 'github password (not stored locally)'
|
56 | };
|
57 |
|
58 | return new Promise(function (resolve, reject) {
|
59 | inquirer.prompt([username, password], function (answers) {
|
60 | la(check.unemptyString(answers.username), 'missing username');
|
61 | la(check.unemptyString(answers.password), 'missing password');
|
62 | resolve({
|
63 | username: answers.username,
|
64 | password: answers.password
|
65 | });
|
66 | });
|
67 | });
|
68 | }
|
69 |
|
70 | function githubLogin() {
|
71 | return askGithubUsernameAndPassword()
|
72 | .then(function (info) {
|
73 | log('trying to login to github %s', info.username);
|
74 | la(check.unemptyString(info.password), 'empty password for', info.username);
|
75 |
|
76 | utils.github.authenticate({
|
77 | type: 'basic',
|
78 | username: info.username,
|
79 | password: info.password
|
80 | });
|
81 | });
|
82 | }
|
83 |
|
84 |
|
85 | function changedLogReport(options, reportOptions) {
|
86 |
|
87 | options = options || {};
|
88 | reportOptions = reportOptions || {};
|
89 |
|
90 | var allTags;
|
91 |
|
92 | return packageRepo(options.name)
|
93 | .tap(debug)
|
94 | .then(_.partial(findCommitIds, options))
|
95 | .tap(function (result) {
|
96 | la(check.array(result.allTags), 'expected all tags in', result);
|
97 | allTags = result.allTags;
|
98 | })
|
99 | .then(findCommentsBetweenTags)
|
100 | .then(function mergeCommitsAndTags(report) {
|
101 | report.allTags = allTags;
|
102 | return report;
|
103 | })
|
104 | .tap(debug)
|
105 | .tap(function (report) {
|
106 | la(check.object(report), 'missing report', report);
|
107 | la(check.fn(report.print), 'cannot print report', report);
|
108 | report.print(reportOptions);
|
109 | });
|
110 | }
|
111 |
|
112 | function changedLog(options, reportOptions) {
|
113 |
|
114 | options = options || {};
|
115 | reportOptions = reportOptions || {};
|
116 |
|
117 | if (options.auth) {
|
118 | log('Please login to github to increase the API rate limit');
|
119 | return githubLogin()
|
120 | .then(_.partial(changedLogReport, options, reportOptions));
|
121 | }
|
122 |
|
123 | return changedLogReport(options, reportOptions);
|
124 | }
|
125 |
|
126 | module.exports = changedLog;
|