index.jsView |
---|
19 | 19 | name: 'pull-requests', |
20 | 20 | version: '1.0.0', |
21 | 21 | manifest: { |
22 | 22 | get: 'async', |
| 23 | + getRevs: 'async', |
23 | 24 | list: 'source' |
24 | 25 | }, |
25 | 26 | schemas: require('./lib/schemas'), |
26 | 27 | init: function (sbot) { |
39 | 40 | cb(null, issue) |
40 | 41 | }) |
41 | 42 | }) |
42 | 43 | |
| 44 | + function getBranchLastUpdate(repoId, branch, lte, cb) { |
| 45 | + |
| 46 | + getMsg(repoId, function (err, msg) { |
| 47 | + if (err) return cb(err) |
| 48 | + var repoAuthor = msg.author |
| 49 | + pull( |
| 50 | + sbot.links({ |
| 51 | + dest: repoId, |
| 52 | + source: repoAuthor, |
| 53 | + rel: 'repo', |
| 54 | + values: true, |
| 55 | + reverse: true |
| 56 | + }), |
| 57 | + pull.filter(function (link) { |
| 58 | + return link.value.content.type == 'git-update' |
| 59 | + }), |
| 60 | + pull.map(function (link) { |
| 61 | + return { |
| 62 | + timestamp: link.value.timestamp, |
| 63 | + rev: (link.value.content.refs || {})['refs/heads/' + branch] |
| 64 | + } |
| 65 | + }), |
| 66 | + pull.filter(function (update) { |
| 67 | + return update.rev |
| 68 | + && (lte ? update.timestamp <= lte : true) |
| 69 | + }), |
| 70 | + pull.take(1), |
| 71 | + pull.collect(function (err, links) { |
| 72 | + cb(err, links && links[0]) |
| 73 | + }) |
| 74 | + ) |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + function getRevs(prId, cb) { |
| 79 | + getPullReq(prId, function (err, pr) { |
| 80 | + if (err) return cb(err) |
| 81 | + |
| 82 | + getBranchLastUpdate(pr.headRepo, pr.headBranch, pr.updated_at, |
| 83 | + function (err, headUpdate) { |
| 84 | + if (err) return cb(err) |
| 85 | + |
| 86 | + getBranchLastUpdate(pr.baseRepo, pr.baseBranch, |
| 87 | + headUpdate.timestamp, function (err, baseUpdate) { |
| 88 | + if (err) return cb(err) |
| 89 | + cb(null, {base: baseUpdate.rev, head: headUpdate.rev}) |
| 90 | + }) |
| 91 | + }) |
| 92 | + }) |
| 93 | + } |
| 94 | + |
43 | 95 | function listPullReqs(opts) { |
44 | 96 | opts = opts || {} |
45 | 97 | opts.type = 'pull-request' |
46 | 98 | return pull( |
59 | 111 | } |
60 | 112 | |
61 | 113 | return { |
62 | 114 | get: getPullReq, |
| 115 | + getRevs: getRevs, |
63 | 116 | list: listPullReqs |
64 | 117 | } |
65 | 118 | } |
66 | 119 | } |