-
Notifications
You must be signed in to change notification settings - Fork 6
/
api.js
228 lines (192 loc) · 5.64 KB
/
api.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
'use strict';
var Client = require('github');
module.exports = function(credentials, owner, repo) {
var github = new Client({version: '3.0.0'});
if (credentials.token) {
github.authenticate({
type: 'oauth',
token: credentials.token
});
} else {
throw new Exception('No credentials given for github');
}
function isDate(value) {
var date = new Date(value);
return !isNaN(date.valueOf());
}
function transformCommitIdToDate(commitId) {
var params = {
user: owner,
repo: repo,
sha: commitId
};
return Bacon
.fromNodeCallback(github.gitdata.getCommit, params)
.flatMap(function(commit) {
return {
date: commit.author.date,
commit: commit
};
});
}
function getPullRequestClosedSinceFilter(dateString) {
return function(pullRequest) {
return new Date(pullRequest.closed_at) > new Date(dateString);
};
}
function getPullRequestClosedUntilFilter(dateString) {
return function(pullRequest) {
return new Date(pullRequest.closed_at) <= new Date(dateString);
};
}
function paginator(pageStreamCallback, stopCondition) {
var paginationNeeded = true;
return Bacon.repeat(function(index) {
if (!paginationNeeded) {
return false;
}
return pageStreamCallback(index + 1)
.doAction(function(item) {
if (stopCondition(item)) {
paginationNeeded = false;
}
});
});
}
/**
* @param {Object} params of type:
* {
* since: '2015-09-07T10:16:41Z',
* until: '2015-09-10T12:50:09Z'
* }
*/
function streamAllPullRequestsBetween(params) {
function pageOfPullRequests(page) {
var requestParams = {
user: owner,
repo: program.repo,
base: 'master',
state: 'closed',
sort: 'updated',
direction: 'desc',
per_page: 50,
page: page
};
return Bacon
.fromNodeCallback(github.pullRequests.getAll, requestParams)
.flatMap(Bacon.fromArray);
}
function stopWhenSinceIsReached(pullRequest) {
return new Date(pullRequest.updated_at) < new Date(params.since.date);
}
var allPullRequests = paginator(pageOfPullRequests, stopWhenSinceIsReached)
.filter(getPullRequestClosedSinceFilter(params.since.date));
if (params.until) {
allPullRequests = allPullRequests.filter(getPullRequestClosedUntilFilter(params.until.date));
}
return allPullRequests;
}
function retrieveCommits(params) {
function pageOfCommits(page) {
var requestParams = {
user: owner,
repo: repo,
per_page: 50,
page: page
};
if (params.until && params.until.commit) {
requestParams.sha = params.until.commit.sha;
}
if (params.since && params.since.date) {
requestParams.since = params.since.date;
}
return Bacon
.fromNodeCallback(github.repos.getCommits, requestParams)
.flatMap(Bacon.fromArray);
}
var stopWhenSinceIsReached;
if (params.since.commit) {
stopWhenSinceIsReached = function(commit) {
return commit.sha === params.since.commit.sha;
};
} else {
stopWhenSinceIsReached = function(commit) {
return new Date(commit.commit.committer.date) < new Date(params.since.date);
};
}
return paginator(pageOfCommits, stopWhenSinceIsReached);
}
function createGist(changelog) {
var params = {
description: 'Release note',
public: false,
files: {
'release note.md': {
content: changelog.text
}
}
};
changelog.gist = Bacon
.fromNodeCallback(github.gists.create, params)
.map('.html_url');
return Bacon.combineTemplate(changelog);
}
/**
* Get a stream providing a single string date,
* the incoming parameter being either a date string or a commit sha.
*/
function streamDateFromDateStringOrCommitId(dateStringOrCommitId) {
return Bacon
.once(dateStringOrCommitId)
.flatMap(function(value) {
if (isDate(value)) {
return {
date: value
};
}
if (value) {
return transformCommitIdToDate(value);
}
return undefined;
});
}
function pullRequestIsMerged(pullRequest) {
return pullRequest.merged_at !== null;
}
function commitIsPullRequestMergeCommit(commit) {
return commit.parents.length > 1;
}
function getPullRequestIdFromCommit(commit) {
var matches = /Merge pull request #(\d+) from/.exec(commit.commit.message);
return matches ? matches[1] : null;
}
function retrievePullRequestById(pullRequestId) {
var requestParams = {
user: owner,
repo: repo,
number: pullRequestId
};
return Bacon
.fromNodeCallback(github.pullRequests.get, requestParams);
}
function commitIsMergedPullRequest(commit) {
return commit.parents.length > 1;
}
function searchPullRequestByCommit(commit) {
return Bacon
.fromNodeCallback(github.search.issues, {q: commit.sha})
.flatMap(function(search) {
return search.items && search.items.length ? search.items[0] : undefined;
});
}
return {
streamDateFromDateStringOrCommitId: streamDateFromDateStringOrCommitId,
createGist: createGist,
retrieveCommits: retrieveCommits,
getPullRequestIdFromCommit: getPullRequestIdFromCommit,
retrievePullRequestById: retrievePullRequestById,
commitIsPullRequestMergeCommit: commitIsPullRequestMergeCommit,
searchPullRequestByCommit: searchPullRequestByCommit,
commitIsMergedPullRequest: commitIsMergedPullRequest
};
};