git ssb

0+

cel / ssb-issues



Tree: 5f3fba84ff3409ced2dc5be22d400478119c0422

Files: 5f3fba84ff3409ced2dc5be22d400478119c0422 / lib / schemas.js

2407 bytesRaw
1var ssbRef = require('ssb-ref')
2var mlib = require('ssb-msgs')
3
4exports.new = function (project, title, text) {
5 if (!ssbRef.isLink(project))
6 throw new Error('invalid project id')
7 var msg = { type: 'issue', project: project }
8 if (text == null) {
9 text = title
10 title = null
11 }
12 if (title) {
13 if (typeof title === 'string')
14 msg.title = title
15 else
16 throw new Error('invalid issue title')
17 }
18 if (text) {
19 if (typeof text === 'string')
20 msg.text = text
21 else
22 throw new Error('invalid issue text')
23 }
24 return msg
25}
26
27exports.newLabel = function (project, name, issues) {
28 if (!ssbRef.isLink(project))
29 throw new Error('invalid project id')
30
31 var msg = { type: 'issue-label', project: project }
32 if (name) {
33 if (typeof name === 'string')
34 msg.name = name
35 else
36 throw new Error('invalid issue label name')
37 }
38 if (issues) {
39 if (Array.isArray(issues) && issues.every(ssbRef.isMsg))
40 msg.issues = issues
41 else
42 throw new Error('invalid issues list')
43 }
44 return msg
45}
46
47exports.edit = function (id, opts) {
48 if (!ssbRef.isMsg(id))
49 throw new Error('invalid issue id')
50 var msg = {
51 type: 'issue-edit',
52 issues: [{
53 link: id
54 }]
55 }
56 var labels = {}
57 if (opts.open != null)
58 msg.issues[0].open = opts.open
59 if (opts.title != null)
60 msg.issues[0].title = opts.title
61 if (opts.addLabels != null) {
62 if (Array.isArray(opts.addLabels) && opts.addLabels.every(ssbRef.isMsg)) {
63 msg.issues[0].labels = labels
64 labels.add = opts.addLabels
65 } else {
66 throw new Error('invalid addLabels')
67 }
68 }
69 if (opts.removeLabels != null) {
70 if (Array.isArray(opts.removeLabels)
71 && opts.removeLabels.every(ssbRef.isMsg)) {
72 msg.issues[0].labels = labels
73 labels.remove = opts.removeLabels
74 } else {
75 throw new Error('invalid removeLabels')
76 }
77 }
78 return msg
79}
80
81exports.close = function (id) {
82 return exports.edit(id, {open: false})
83}
84
85exports.reopen = function (id) {
86 return exports.edit(id, {open: true})
87}
88
89function editMsg(msg, id, open) {
90 if (!ssbRef.isMsg(id))
91 throw new Error('invalid issue id')
92 ;(msg.issues || (msg.issues = [])).push({
93 link: id,
94 open: open
95 })
96 return msg
97}
98
99exports.reopens = function (msg, id) {
100 return editMsg(msg, id, true)
101}
102
103exports.closes = function (msg, id) {
104 return editMsg(msg, id, false)
105}
106

Built with git-ssb-web