git ssb

30+

cel / git-ssb-web



Tree: 229088888b146cdab7bf94e133046ee8ed2bea3b

Files: 229088888b146cdab7bf94e133046ee8ed2bea3b / lib / repos / pulls.js

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

Built with git-ssb-web