forked from tschaub/github-changelog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·179 lines (161 loc) · 4.63 KB
/
index.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
#!/usr/bin/env node
// core modules
var fs = require('fs');
var path = require('path');
// 3rd party modules
var Client = require('github');
var async = require('async');
var handlebars = require('handlebars');
var helpers = require('handlebars-helpers')({
handlebars: handlebars
});
var program = require('commander');
program
.option('-o, --owner <name>', 'Repository owner name. If not provided, ' +
'the "username" option will be used.')
.option('-r, --repo <repo>', 'Repository name (required).')
.option('-u, --username <name>', 'Your GitHub username (only required ' +
'for private repos).')
.option('-p, --password <pass>', 'Your GitHub password (only required ' +
'for private repos).')
.option('-t, --token <token>', 'A GitHub personal token (only required ' +
'for private repos).')
.option('-f, --file <filename>', 'Output file. If the file exists, ' +
'log will be prepended to it. Default is to write to stdout.')
.option('-s, --since <iso-date>', 'Last changelog date. If the "file" ' +
'option is used and "since" is not provided, the mtime of the output ' +
'file will be used.')
.option('-m, --merged', 'List merged pull requests only.')
.option('-e, --header <header>', 'Header text. Default is "Changes ' +
'since <since>".')
.option('-t, --template <path>', 'Handlebar template to format data.' +
'The default bundled template generates a list of issues in Markdown')
.parse(process.argv);
if (!program.repo) {
program.help();
process.exit(1);
}
if (!program.username && !program.owner) {
console.error('\nOne of "username" or "owner" options must be provided');
program.help();
process.exit(1);
}
if (!(program.since || program.file)) {
console.error('\nOne of "since" or "file" options must be provided');
program.help();
process.exit(1);
}
if (program.file && !fs.existsSync(program.file)) {
console.error('\nFile not found: %s', program.file);
program.help();
process.exit(1);
}
/* Handlebars config */
handlebars.registerHelper('toHeader', function(str, char) {
return str.replace(/./g, char);
});
var templatePath = program.template || path.join(__dirname, 'changelog.hbs');
var template = fs.readFileSync(templatePath, 'utf8');
var changelog = handlebars.compile(template, {noEscape: true});
var since = program.since || fs.statSync(program.file).mtime.toISOString();
var header = program.header || 'Changes since ' + since;
var owner = program.owner || program.username;
var github = new Client({version: '3.0.0'});
if (program.token) {
github.authenticate({
type: 'oauth',
token: program.token,
});
}
else if (program.username && program.password) {
github.authenticate({
type: 'basic',
username: program.username,
password: program.password
});
}
function fetchIssues(callback) {
var page = 1;
var limit = 100;
var issues = [];
function fetch() {
github.issues.repoIssues({
user: owner,
repo: program.repo,
state: 'closed',
sort: 'created',
since: since,
per_page: limit,
page: page
}, function(err, batch) {
if (err) {
return callback(err);
}
issues = issues.concat(batch);
if (batch.length === limit) {
++page;
fetch();
} else {
callback(null, issues);
}
});
}
fetch();
}
function filterIssues(issues, callback) {
if (!program.merged) {
process.nextTick(function() {
callback(null, issues);
});
} else {
async.filter(issues, function(issue, isMerged) {
github.pullRequests.getMerged({
user: owner,
repo: program.repo,
number: issue.number
}, function(err, result) {
isMerged(!err);
});
}, function(filtered) {
callback(null, filtered);
});
}
}
function formatChangelog(issues, callback) {
process.nextTick(function() {
callback(null, changelog({header: header, issues: issues}));
});
}
function writeChangelog(text, callback) {
if (program.file) {
var existing;
async.waterfall([
function(next) {
fs.readFile(program.file, next);
}, function(data, next) {
existing = data;
fs.writeFile(program.file, text, next);
}, function(next) {
fs.appendFile(program.file, existing, next);
}
], callback);
} else {
process.nextTick(function() {
console.log(text);
callback(null);
});
}
}
async.waterfall([
fetchIssues,
filterIssues,
formatChangelog,
writeChangelog
], function(err) {
if (err) {
console.error(err.message);
process.exit(1);
} else {
process.exit(0);
}
});