git ssb

30+

cel / git-ssb-web



Tree: 18def954d98179d5edece295ac81d9a404fd86ee

Files: 18def954d98179d5edece295ac81d9a404fd86ee / lib / repos / pulls.js

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

Built with git-ssb-web