git ssb

30+

cel / git-ssb-web



Tree: 88de0177402d7aacfb3a27acbb67242d60c3cc39

Files: 88de0177402d7aacfb3a27acbb67242d60c3cc39 / lib / repos / pulls.js

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

Built with git-ssb-web