Files: 69b2bd9359ee60e0852ecfeb89a4216ce7945ae0 / index.js
3422 bytesRaw
1 | var pull = require('pull-stream') |
2 | var paramap = require('pull-paramap') |
3 | var asyncMemo = require('asyncmemo') |
4 | var Issues = require('ssb-issues') |
5 | |
6 | function readNext(fn) { |
7 | var next |
8 | return function (end, cb) { |
9 | if (next) return next(end, cb) |
10 | fn(function (err, _next) { |
11 | if (err) return cb(err) |
12 | next = _next |
13 | next(null, cb) |
14 | }) |
15 | } |
16 | } |
17 | |
18 | module.exports = { |
19 | name: 'pull-requests', |
20 | version: '1.0.0', |
21 | manifest: { |
22 | get: 'async', |
23 | getRevs: 'async', |
24 | list: 'source' |
25 | }, |
26 | schemas: require('./lib/schemas'), |
27 | init: function (sbot) { |
28 | |
29 | var issues = Issues.init(sbot) |
30 | var getMsg = asyncMemo(sbot.get) |
31 | |
32 | var getPullReq = asyncMemo(function (id, cb) { |
33 | issues.get(id, function (err, issue) { |
34 | if (err) return cb(err) |
35 | var c = issue.msg.value.content |
36 | issue.baseRepo = c.repo |
37 | issue.baseBranch = c.branch |
38 | issue.headRepo = c.head_repo |
39 | issue.headBranch = c.head_branch |
40 | cb(null, issue) |
41 | }) |
42 | }) |
43 | |
44 | function getBranchLastUpdate(repoId, branch, lte, cb) { |
45 | // TODO: detect and skip updates from subsequent PR with same branch name |
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 | // get the latest rev of the head branch before it was deleted or the |
82 | // PR closed |
83 | var lastTime = pr.open ? null : pr.updated_at |
84 | getBranchLastUpdate(pr.headRepo, pr.headBranch, lastTime, |
85 | function (err, headUpdate) { |
86 | if (err) return cb(err) |
87 | // get the rev of base when head was last updated |
88 | getBranchLastUpdate(pr.baseRepo, pr.baseBranch, |
89 | headUpdate.timestamp, function (err, baseUpdate) { |
90 | if (err) return cb(err) |
91 | cb(null, {base: baseUpdate.rev, head: headUpdate.rev}) |
92 | }) |
93 | }) |
94 | }) |
95 | } |
96 | |
97 | function listPullReqs(opts) { |
98 | opts = opts || {} |
99 | opts.type = 'pull-request' |
100 | return pull( |
101 | sbot.messagesByType(opts), |
102 | pull.filter(function (msg) { |
103 | return (!opts.repo || opts.repo == msg.value.content.repo) |
104 | && (!opts.author || opts.author == msg.value.author) |
105 | }), |
106 | paramap(function (msg, cb) { |
107 | getPullReq(msg.key, cb) |
108 | }, 8), |
109 | pull.filter(opts.open != null && function (pr) { |
110 | return pr.open == opts.open |
111 | }) |
112 | ) |
113 | } |
114 | |
115 | return { |
116 | get: getPullReq, |
117 | getRevs: getRevs, |
118 | list: listPullReqs |
119 | } |
120 | } |
121 | } |
122 |
Built with git-ssb-web