var pull = require('pull-stream') var paramap = require('pull-paramap') var asyncMemo = require('asyncmemo') var Issues = require('ssb-issues') var ssbGit = require('ssb-git-repo') function readNext(fn) { var next return function (end, cb) { if (next) return next(end, cb) fn(function (err, _next) { if (err) return cb(err) next = _next next(null, cb) }) } } module.exports = { name: 'pull-requests', version: '1.0.0', manifest: { get: 'async', list: 'source' }, schemas: require('./lib/schemas'), init: function (sbot) { var issues = Issues.init(sbot) var getMsg = asyncMemo(sbot.get) var getRepo = asyncMemo(function (id, cb) { getMsg(id, function (err, msg) { if (err) return cb(err) ssbGit.getRepo(sbot, {key: id, value: msg}, {live: true}, cb) }) }) var getPullReq = asyncMemo(function (id, cb) { issues.get(id, function (err, issue) { if (err) return cb(err) var c = issue.msg.value.content issue.baseRepo = c.repo issue.baseBranch = c.branch issue.headRepo = c.head_repo issue.headBranch = c.head_branch cb(null, issue) }) }) function listPullReqs(opts) { opts = opts || {} opts.type = 'pull-request' return pull( sbot.messagesByType(opts), pull.filter(function (msg) { return (!opts.repo || opts.repo == msg.value.content.repo) && (!opts.author || opts.author == msg.value.author) }), paramap(function (msg, cb) { getPullReq(msg.key, cb) }, 8), pull.filter(opts.open != null && function (pr) { return pr.open == opts.open }) ) } return { get: getPullReq, list: listPullReqs } } }