git ssb

30+

cel / git-ssb-web



Tree: 14217c23e38f473d897977ae266e423d4f98ae19

Files: 14217c23e38f473d897977ae266e423d4f98ae19 / lib / repos / pulls.js

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

Built with git-ssb-web