git ssb

30+

cel / git-ssb-web



Tree: fcfe11a278429f351696774edb26b410c3d07c49

Files: fcfe11a278429f351696774edb26b410c3d07c49 / lib / repos / pulls.js

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

Built with git-ssb-web