var ssbRef = require('ssb-ref') var mlib = require('ssb-msgs') exports.new = function (project, title, text) { if (!ssbRef.isLink(project)) throw new Error('invalid project id') var msg = { type: 'issue', project: project } if (text == null) { text = title title = null } if (title) { if (typeof title === 'string') msg.title = title else throw new Error('invalid issue title') } if (text) { if (typeof text === 'string') msg.text = text else throw new Error('invalid issue text') } return msg } exports.newLabel = function (project, name, issues) { if (!ssbRef.isLink(project)) throw new Error('invalid project id') var msg = { type: 'issue-label', project: project } if (name) { if (typeof name === 'string') msg.name = name else throw new Error('invalid issue label name') } if (issues) { if (Array.isArray(issues) && issues.every(ssbRef.isMsg)) msg.issues = issues else throw new Error('invalid issues list') } return msg } exports.edit = function (id, opts) { if (!ssbRef.isMsg(id)) throw new Error('invalid issue id') var msg = { type: 'issue-edit', issues: [{ link: id }] } var labels = {} if (opts.open != null) msg.issues[0].open = opts.open if (opts.title != null) msg.issues[0].title = opts.title if (opts.addLabels != null) { if (Array.isArray(opts.addLabels) && opts.addLabels.every(ssbRef.isMsg)) { msg.issues[0].labels = labels labels.add = opts.addLabels } else { throw new Error('invalid addLabels') } } if (opts.removeLabels != null) { if (Array.isArray(opts.removeLabels) && opts.removeLabels.every(ssbRef.isMsg)) { msg.issues[0].labels = labels labels.remove = opts.removeLabels } else { throw new Error('invalid removeLabels') } } return msg } exports.close = function (id) { return exports.edit(id, {open: false}) } exports.reopen = function (id) { return exports.edit(id, {open: true}) } function editMsg(msg, id, open) { if (!ssbRef.isMsg(id)) throw new Error('invalid issue id') ;(msg.issues || (msg.issues = [])).push({ link: id, open: open }) return msg } exports.reopens = function (msg, id) { return editMsg(msg, id, true) } exports.closes = function (msg, id) { return editMsg(msg, id, false) }