git ssb

30+

cel / git-ssb-web



Tree: 47ebea298eda55724bb6e637ea900aada10c5eab

Files: 47ebea298eda55724bb6e637ea900aada10c5eab / lib / repos / pulls.js

16311 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(err)
94 if (!revs.head) return cb(null, pull.once('<section>' +
95 req._t('pullRequest.NoCommits') + '</section>'))
96 GitRepo.getMergeBase(baseRepo, revs.base, headRepo, revs.head,
97 function (err, mergeBase) {
98 if (err) return cb(err)
99 cb(null, cat([
100 pull.once('<section>'),
101 self.renderCommitLog(req, baseRepo, mergeBase, headRepo, revs.head),
102 pull.once('</section>')
103 ]))
104 }
105 )
106 })
107}
108
109P.renderPullReqFiles = function (req, pr, baseRepo, headRepo, cb) {
110 var self = this
111 self.web.pullReqs.getRevs(pr.id, function (err, revs) {
112 if (err) return cb(err)
113 if (!revs.head) return cb(null, pull.once('<section>' +
114 req._t('pullRequest.NoChanges') + '</section>'))
115 GitRepo.getMergeBase(baseRepo, revs.base, headRepo, revs.head,
116 function (err, mergeBase) {
117 if (err) return cb(err)
118 cb(null, cat([
119 pull.once('<section>'),
120 self.repo.renderDiffStat(req,
121 [baseRepo, headRepo], [mergeBase, revs.head]),
122 pull.once('</section>')
123 ]))
124 }
125 )
126 })
127}
128
129P.renderPullReqActivity = function (req, pr, repo, headRepo, authorLink, postId) {
130 var self = this
131 var msgTimeLink = u.link([pr.id],
132 new Date(pr.created_at).toLocaleString(req._locale))
133 var newestMsg = {key: pr.id, value: {timestamp: pr.created_at}}
134 return cat([
135 u.readOnce(function (cb) {
136 cb(null,
137 '<section class="collapse">' +
138 authorLink + ' &middot; ' + msgTimeLink +
139 markdown(pr.text, repo) + '</section>')
140 }),
141 // render posts, edits, and updates
142 pull(
143 many([
144 self.web.ssb.links({
145 dest: pr.id,
146 values: true
147 }),
148 pull(
149 self.web.ssb.links({
150 dest: headRepo.id,
151 rel: 'repo',
152 values: true,
153 reverse: true
154 }),
155 pull.take(function (link) {
156 return link.value.timestamp > pr.created_at
157 }),
158 pull.filter(function (link) {
159 return link.value.content.type == 'git-update'
160 && ('refs/heads/' + pr.headBranch) in link.value.content.refs
161 })
162 )
163 ]),
164 pull.unique('key'),
165 u.decryptMessages(self.web.ssb),
166 self.web.addAuthorName(),
167 pull.through(function (msg) {
168 if (msg.value
169 && msg.value.timestamp > newestMsg.value.timestamp
170 && msg.value.content.root === pr.id)
171 newestMsg = msg
172 }),
173 u.sortMsgs(),
174 pull.map(function (item) {
175 if (item.value.content.type == 'git-update')
176 return self.renderBranchUpdate(req, pr, item)
177 return self.repo.issues.renderIssueActivityMsg(req, repo, pr,
178 req._t('pull request'), postId, item)
179 })
180 ),
181 !self.web.isPublic && pr.open && pull.once(
182 '<section class="merge-instructions">' +
183 '<input type="checkbox" class="toggle" id="merge-instructions"/>' +
184 '<h4><label for="merge-instructions" class="toggle-link"><a>' +
185 req._t('mergeInstructions.MergeViaCmdLine') +
186 '</a></label></h4>' +
187 '<div class="contents">' +
188 '<p>' + req._t('mergeInstructions.CheckOut') + '</p>' +
189 '<pre>' +
190 'git fetch ssb://' + u.escape(pr.headRepo) + ' ' +
191 u.escape(pr.headBranch) + '\n' +
192 'git checkout -b ' + u.escape(pr.headBranch) + ' FETCH_HEAD' +
193 '</pre>' +
194 '<p>' + req._t('mergeInstructions.MergeAndPush') + '</p>' +
195 '<pre>' +
196 'git checkout ' + u.escape(pr.baseBranch) + '\n' +
197 'git merge ' + u.escape(pr.headBranch) + '\n' +
198 'git push ssb ' + u.escape(pr.baseBranch) +
199 '</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.renderRepoPage(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.renderRepoPage(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