git ssb

30+

cel / git-ssb-web



Tree: a282c43eb17a12a9a7d49e9c95560668ec1f08b7

Files: a282c43eb17a12a9a7d49e9c95560668ec1f08b7 / lib / repos / pulls.js

15869 bytesRaw
1var pull = require('pull-stream')
2var paramap = require('pull-paramap')
3var cat = require('pull-cat')
4var many = require('pull-many')
5var multicb = require('multicb')
6var GitRepo = require('pull-git-repo')
7var u = require('../../lib/util')
8var markdown = require('../../lib/markdown')
9var forms = require('../../lib/forms')
10var ssbRef = require('ssb-ref')
11
12module.exports = function (repoRoutes, web) {
13 return new RepoPullReqRoutes(repoRoutes, web)
14}
15
16function RepoPullReqRoutes(repoRoutes, web) {
17 this.repo = repoRoutes
18 this.web = web
19}
20
21var P = RepoPullReqRoutes.prototype
22
23/* Pull Request */
24
25P.serveRepoPullReq = function (req, repo, pr, path, postId) {
26 var self = this
27 var headRepo, authorLink
28 var page = path[0] || 'activity'
29 var title = u.escape(pr.title) + ' · %{author}/%{repo}'
30 return self.repo.renderRepoPage(req, repo, 'pulls', null, title, cat([
31 pull.once('<div class="pull-request">' +
32 '<h3>' + u.link([pr.id], pr.title) + '</h3>' +
33 '<code>' + pr.id + '</code>'),
34 u.readOnce(function (cb) {
35 var done = multicb({ pluck: 1, spread: true })
36 var gotHeadRepo = done()
37 self.web.about.getName(pr.author, done())
38 var sameRepo = (pr.headRepo == pr.baseRepo)
39 self.web.getRepo(pr.headRepo, function (err, headRepo) {
40 if (err) return cb(err)
41 self.web.getRepoName(headRepo.feed, headRepo.id, done())
42 self.web.about.getName(headRepo.feed, done())
43 gotHeadRepo(null, GitRepo(headRepo))
44 })
45
46 done(function (err, _headRepo, issueAuthorName,
47 headRepoName, headRepoAuthorName) {
48 if (err) return cb(err)
49 headRepo = _headRepo
50 authorLink = u.link([pr.author], issueAuthorName)
51 var repoLink = u.link([pr.headRepo], headRepoName)
52 var headRepoAuthorLink = u.link([headRepo.feed], headRepoAuthorName)
53 var headRepoLink = u.link([headRepo.id], headRepoName)
54 var headBranchLink = u.link([headRepo.id, 'tree', pr.headBranch])
55 var baseBranchLink = u.link([repo.id, 'tree', pr.baseBranch])
56 cb(null, '<section class="collapse">' +
57 '<strong class="issue-status ' +
58 (pr.open ? 'open' : 'closed') + '">' +
59 req._t(pr.open ? 'issue.state.Open' : 'issue.state.Closed') +
60 '</strong> ' +
61 req._t('pullRequest.WantToMerge', {
62 name: authorLink,
63 base: '<code>' + baseBranchLink + '</code>',
64 head: (sameRepo ?
65 '<code>' + headBranchLink + '</code>' :
66 '<code class="bgslash">' +
67 headRepoAuthorLink + ' / ' +
68 headRepoLink + ' / ' +
69 headBranchLink + '</code>')
70 }) + '</section>')
71 })
72 }),
73 pull.once(
74 u.nav([
75 [[pr.id], req._t('Discussion'), 'activity'],
76 [[pr.id, 'commits'], req._t('Commits'), 'commits'],
77 [[pr.id, 'files'], req._t('Files'), 'files']
78 ], page)),
79 u.readNext(function (cb) {
80 if (page == 'commits')
81 self.renderPullReqCommits(req, pr, repo, headRepo, cb)
82 else if (page == 'files')
83 self.renderPullReqFiles(req, pr, repo, headRepo, cb)
84 else cb(null,
85 self.renderPullReqActivity(req, pr, repo, headRepo, authorLink, postId))
86 })
87 ]))
88}
89
90P.renderPullReqCommits = function (req, pr, baseRepo, headRepo, cb) {
91 var self = this
92 self.web.pullReqs.getRevs(pr.id, function (err, revs) {
93 if (err) return cb(null, self.web.renderError(err))
94 cb(null, cat([
95 pull.once('<section>'),
96 self.renderCommitLog(req, baseRepo, revs.base, headRepo, revs.head),
97 pull.once('</section>')
98 ]))
99 })
100}
101
102P.renderPullReqFiles = function (req, pr, baseRepo, headRepo, cb) {
103 var self = this
104 self.web.pullReqs.getRevs(pr.id, function (err, revs) {
105 if (err) return cb(null, self.web.renderError(err))
106 cb(null, cat([
107 pull.once('<section>'),
108 self.repo.renderDiffStat(req,
109 [baseRepo, headRepo], [revs.base, revs.head]),
110 pull.once('</section>')
111 ]))
112 })
113}
114
115P.renderPullReqActivity = function (req, pr, repo, headRepo, authorLink, postId) {
116 var self = this
117 var msgTimeLink = u.link([pr.id],
118 new Date(pr.created_at).toLocaleString(req._locale))
119 var newestMsg = {key: pr.id, value: {timestamp: pr.created_at}}
120 var isAuthor = (self.web.myId == pr.author) || (self.web.myId == repo.feed)
121 return cat([
122 u.readOnce(function (cb) {
123 cb(null,
124 '<section class="collapse">' +
125 authorLink + ' &middot; ' + msgTimeLink +
126 markdown(pr.text, repo) + '</section>')
127 }),
128 // render posts, edits, and updates
129 pull(
130 many([
131 self.web.ssb.links({
132 dest: pr.id,
133 values: true
134 }),
135 u.readNext(function (cb) {
136 cb(null, pull(
137 self.web.ssb.links({
138 dest: headRepo.id,
139 source: headRepo.feed,
140 rel: 'repo',
141 values: true,
142 reverse: true
143 }),
144 pull.take(function (link) {
145 return link.value.timestamp > pr.created_at
146 }),
147 pull.filter(function (link) {
148 return link.value.content.type == 'git-update'
149 && ('refs/heads/' + pr.headBranch) in link.value.content.refs
150 })
151 ))
152 })
153 ]),
154 self.web.addAuthorName(),
155 pull.unique('key'),
156 pull.through(function (msg) {
157 if (msg.value.timestamp > newestMsg.value.timestamp)
158 newestMsg = msg
159 }),
160 u.sortMsgs(),
161 pull.map(function (item) {
162 if (item.value.content.type == 'git-update')
163 return self.renderBranchUpdate(req, pr, item)
164 return self.repo.issues.renderIssueActivityMsg(req, repo, pr,
165 req._t('pull request'), postId, item)
166 })
167 ),
168 !self.web.isPublic && isAuthor && pr.open && pull.once(
169 '<section class="merge-instructions">' +
170 '<input type="checkbox" class="toggle" id="merge-instructions"/>' +
171 '<h4><label for="merge-instructions" class="toggle-link"><a>' +
172 req._t('mergeInstructions.MergeViaCmdLine') +
173 '</a></label></h4>' +
174 '<div class="contents">' +
175 '<p>' + req._t('mergeInstructions.CheckOut') + '</p>' +
176 '<pre>' +
177 'git fetch ssb://' + u.escape(pr.headRepo) + ' ' +
178 u.escape(pr.headBranch) + '\n' +
179 'git checkout -b ' + u.escape(pr.headBranch) + ' FETCH_HEAD' +
180 '</pre>' +
181 '<p>' + req._t('mergeInstructions.MergeAndPush') + '</p>' +
182 '<pre>' +
183 'git checkout ' + u.escape(pr.baseBranch) + '\n' +
184 'git merge ' + u.escape(pr.headBranch) + '\n' +
185 'git push ssb ' + u.escape(pr.baseBranch) +
186 '</pre>' +
187 '</div></section>'),
188 !self.web.isPublic && u.readOnce(function (cb) {
189 cb(null, forms.issueComment(req, pr, repo, newestMsg.key,
190 isAuthor, req._t('pull request')))
191 })
192 ])
193}
194
195P.renderBranchUpdate = function (req, pr, msg) {
196 var authorLink = u.link([msg.value.author], msg.authorName)
197 var msgLink = u.link([msg.key],
198 new Date(msg.value.timestamp).toLocaleString(req._locale))
199 var rev = msg.value.content.refs['refs/heads/' + pr.headBranch]
200 if (!rev)
201 return '<section class="collapse">' +
202 req._t('NameDeletedBranch', {
203 name: authorLink,
204 branch: '<code>' + pr.headBranch + '</code>'
205 }) + ' &middot; ' + msgLink +
206 '</section>'
207
208 var revLink = u.link([pr.headRepo, 'commit', rev], rev.substr(0, 8))
209 return '<section class="collapse">' +
210 req._t('NameUpdatedBranch', {
211 name: authorLink,
212 rev: '<code>' + revLink + '</code>'
213 }) + ' &middot; ' + msgLink +
214 '</section>'
215}
216
217/* Compare changes */
218
219P.branchMenu = function (repo, name, currentName) {
220 return cat([
221 pull.once('<select name="' + name + '">'),
222 pull(
223 repo.refs(),
224 pull.map(function (ref) {
225 var m = ref.name.match(/^refs\/([^\/]*)\/(.*)$/) || [,, ref.name]
226 return m[1] == 'heads' && m[2]
227 }),
228 pull.filter(Boolean),
229 u.pullSort(),
230 pull.map(this.repo.formatRevOptions(currentName))
231 ),
232 pull.once('</select>')
233 ])
234}
235
236P.serveRepoCompare = function (req, repo) {
237 var self = this
238 var query = req._u.query
239 var base
240 var count = 0
241 var title = req._t('CompareChanges') + ' · %{author}/%{repo}'
242
243 return self.repo.renderRepoPage(req, repo, 'pulls', null, title, cat([
244 pull.once('<h3>' + req._t('CompareChanges') + '</h3>' +
245 '<form action="' + u.encodeLink(repo.id) + '/comparing" method="get">' +
246 '<section>'),
247 pull.once(req._t('BaseBranch') + ': '),
248 u.readNext(function (cb) {
249 if (query.base) gotBase(null, query.base)
250 else repo.getSymRef('HEAD', true, gotBase)
251 function gotBase(err, ref) {
252 if (err) return cb(err)
253 cb(null, self.branchMenu(repo, 'base', base = ref || 'HEAD'))
254 }
255 }),
256 pull.once('<br/>' + req._t('ComparisonRepoBranch') + ':'),
257 pull(
258 self.repo.getForks(repo, true),
259 pull.asyncMap(function (msg, cb) {
260 self.web.getRepo(msg.key, function (err, repo) {
261 if (err) return cb(err)
262 cb(null, {
263 msg: msg,
264 repo: repo
265 })
266 })
267 }),
268 pull.map(renderFork),
269 pull.flatten()
270 ),
271 pull.once('<div class="bgslash">' +
272 '<input type="radio" name="head" value="other" id="other-radio"> ' +
273 '<label for="other-radio">other</label>: ' +
274 '<input name="other_repo" placeholder="repo id"> / ' +
275 '<input name="other_branch" placeholder="branch"></div>'),
276 pull.once('</section>'),
277 u.readOnce(function (cb) {
278 cb(null,
279 '<button type="submit" class="btn">' +
280 req._t('Compare') + '</button>')
281 }),
282 pull.once('</form>')
283 ]))
284
285 function renderFork(fork) {
286 return pull(
287 fork.repo.refs({inherited: false}),
288 pull.map(function (ref) {
289 var m = /^refs\/([^\/]*)\/(.*)$/.exec(ref.name) || [,ref.name]
290 return {
291 type: m[1],
292 name: m[2],
293 value: ref.value
294 }
295 }),
296 pull.filter(function (ref) {
297 return ref.type == 'heads'
298 && !(ref.name == base && fork.msg.key == repo.id)
299 }),
300 pull.map(function (ref) {
301 var branchLink = u.link([fork.msg.key, 'tree', ref.name], ref.name)
302 var authorLink = u.link([fork.msg.value.author], fork.msg.authorName)
303 var repoLink = u.link([fork.msg.key], fork.msg.repoName)
304 var value = fork.msg.key + ':' + ref.name
305 return '<div class="bgslash">' +
306 '<input type="radio" name="head"' +
307 ' value="' + u.escape(value) + '"' +
308 (query.head == value ? ' checked="checked"' : '') + '> ' +
309 authorLink + ' / ' + repoLink + ' / ' + branchLink + '</div>'
310 })
311 )
312 }
313}
314
315P.serveRepoComparing = function (req, repo) {
316 var self = this
317 var query = req._u.query
318 var baseBranch = query.base
319 var headRepoId, headBranch
320
321 if (query.head === 'other') {
322 headRepoId = String(query.other_repo).replace(/^ssb:\/*/, '')
323 headBranch = String(query.other_branch)
324 } else if (query.head) {
325 var s = String(query.head).split(':')
326 headRepoId = s[0]
327 headBranch = s[1]
328 }
329
330 if (!baseBranch)
331 return self.web.serveRedirect(req, u.encodeLink([repo.id, 'compare']))
332 if (!ssbRef.isMsgId(headRepoId))
333 return self.web.serveError(req, new Error('bad repo id'), 400)
334
335 var baseLink = u.link([repo.id, 'tree', baseBranch])
336 var headBranchLink = u.link([headRepoId, 'tree', headBranch])
337 var backHref = u.encodeLink([repo.id, 'compare']) + req._u.search
338 var title = req._t(query.expand ? 'OpenPullRequest': 'ComparingChanges')
339 var pageTitle = title + ' · %{author}/%{repo}'
340
341 return self.repo.renderRepoPage(req, repo, 'pulls', null, pageTitle, cat([
342 pull.once('<h3>' + title + '</h3>'),
343 u.readNext(function (cb) {
344 self.web.getRepo(headRepoId, function (err, headRepo) {
345 if (err) return cb(err)
346 self.web.getRepoFullName(headRepo.feed, headRepo.id,
347 function (err, repoName, authorName) {
348 if (err) return cb(err)
349 cb(null, renderRepoInfo(GitRepo(headRepo), repoName, authorName))
350 }
351 )
352 })
353 })
354 ]))
355
356 function renderRepoInfo(headRepo, headRepoName, headRepoAuthorName) {
357 var authorLink = u.link([headRepo.feed], headRepoAuthorName)
358 var repoLink = u.link([headRepoId], headRepoName)
359 return cat([
360 pull.once('<section>' +
361 req._t('Base') + ': ' + baseLink + '<br/>' +
362 req._t('Head') + ': ' +
363 '<span class="bgslash">' + authorLink + ' / ' + repoLink +
364 ' / ' + headBranchLink + '</span>' +
365 '</section>' +
366 (query.expand ? '<section><form method="post" action="">' +
367 u.hiddenInputs({
368 action: 'new-pull',
369 branch: baseBranch,
370 head_repo: headRepoId,
371 head_branch: headBranch
372 }) +
373 forms.post(req, repo, null, 8) +
374 '<button type="submit" class="btn open">' +
375 req._t('Create') + '</button>' +
376 '</form></section>'
377 : self.web.isPublic ? ''
378 : '<section><form method="get" action="">' +
379 u.hiddenInputs({
380 base: baseBranch,
381 head: query.head,
382 other_repo: query.other_repo,
383 other_branch: query.other_branch,
384 }) +
385 '<button class="btn open" type="submit" name="expand" value="1">' +
386 '<i>⎇</i> ' + req._t('CreatePullRequest') + '</button> ' +
387 '<a href="' + backHref + '">' + req._t('Back') + '</a>' +
388 '</form></section>') +
389 '<div id="commits"></div>' +
390 '<div class="tab-links">' +
391 '<a href="#" id="files-link">' + req._t('FilesChanged') + '</a> ' +
392 '<a href="#commits" id="commits-link">' +
393 req._t('Commits') + '</a>' +
394 '</div>'),
395 u.readNext(function (cb) {
396 GitRepo.getMergeBase(repo, baseBranch, headRepo, headBranch,
397 function (err, concestor) {
398 if (err) return cb(err)
399 cb(null, cat([
400 pull.once('<section id="files-tab">'),
401 self.repo.renderDiffStat(req, [repo, headRepo],
402 [concestor, headBranch]),
403 pull.once('</section>' +
404 '<section id="commits-tab">'),
405 self.renderCommitLog(req, repo, concestor, headRepo, headBranch),
406 pull.once('</section>')
407 ]))
408 }
409 )
410 })
411 ])
412 }
413}
414
415P.renderCommitLog = function (req, baseRepo, baseBranch, headRepo, headBranch) {
416 return cat([
417 pull.once('<table class="compare-commits">'),
418 u.readNext(function (cb) {
419 baseRepo.resolveRef(baseBranch, function (err, baseBranchRev) {
420 if (err) return cb(err)
421 var currentDay
422 return cb(null, pull(
423 headRepo.readLog(headBranch),
424 pull.take(function (rev) { return rev != baseBranchRev }),
425 u.pullReverse(),
426 paramap(headRepo.getCommitParsed.bind(headRepo), 8),
427 pull.map(function (commit) {
428 var commitPath = [headRepo.id, 'commit', commit.id]
429 var commitIdShort = '<tt>' + commit.id.substr(0, 8) + '</tt>'
430 var day = Math.floor(commit.author.date / 86400000)
431 var dateRow = day == currentDay ? '' :
432 '<tr><th colspan=3 class="date-info">' +
433 commit.author.date.toLocaleDateString(req._locale) +
434 '</th><tr>'
435 currentDay = day
436 return dateRow + '<tr>' +
437 '<td>' + u.escape(commit.author.name) + '</td>' +
438 '<td>' + u.link(commitPath, commit.title) + '</td>' +
439 '<td>' + u.link(commitPath, commitIdShort, true) + '</td>' +
440 '</tr>'
441 })
442 ))
443 })
444 }),
445 pull.once('</table>')
446 ])
447}
448

Built with git-ssb-web