git ssb

0+

ev / microbay



forked from Dominic / patchbay

Tree: faff49e67cc84e5fa54a14136d98a6126f176499

Files: faff49e67cc84e5fa54a14136d98a6126f176499 / modules / git.js

6994 bytesRaw
1var h = require('hyperscript')
2var pull = require('pull-stream')
3var paramap = require('pull-paramap')
4var human = require('human-time')
5
6var plugs = require('../plugs')
7var message_link = plugs.first(exports.message_link = [])
8var sbot_links = plugs.first(exports.sbot_links = [])
9var sbot_links2 = plugs.first(exports.sbot_links2 = [])
10var sbot_get = plugs.first(exports.sbot_get = [])
11var getAvatar = require('ssb-avatar')
12var avatar_name = plugs.first(exports.avatar_name = [])
13var markdown = plugs.first(exports.markdown = [])
14
15var self_id = require('../keys').id
16
17function shortRefName(ref) {
18 return ref.replace(/^refs\/(heads|tags)\//, '')
19}
20
21function repoLink(id) {
22 var el = h('a', {href: '#'+id}, id.substr(0, 10) + '…')
23 getAvatar({links: sbot_links}, self_id, id, function (err, avatar) {
24 if(err) return console.error(err)
25 el.textContent = avatar.name
26 })
27 return el
28}
29
30function getIssueState(id, cb) {
31 pull(
32 sbot_links({dest: id, rel: 'issues', values: true, reverse: true}),
33 pull.map(function (msg) {
34 var issues = msg.value.content.issues
35 if (!Array.isArray(issues)) return
36 return issues.filter(function (issue) {
37 return issue.link === id
38 })
39 }),
40 pull.flatten(),
41 pull.map(function (issue) {
42 return issue.merged ? 'merged' : issue.open ? 'open' : 'closed'
43 }),
44 pull.take(1),
45 pull.collect(function (err, updates) {
46 cb(err, updates && updates[0] || 'open')
47 })
48 )
49}
50
51//todo:
52function messageTimestampLink(msg) {
53 return h('a.timestamp', {
54 timestamp: msg.value.timestamp,
55 title: new Date(msg.value.timestamp),
56 href: '#'+msg.key
57 }, human(msg.value.timestamp))
58}
59
60function tableRows(headerRow) {
61 var thead = h('thead'), tbody = h('tbody')
62 var first = true
63 var t = [thead, tbody]
64 t.append = function (row) {
65 if (first) {
66 first = false
67 thead.appendChild(headerRow)
68 }
69 tbody.appendChild(row)
70 }
71 return t
72}
73
74function repoName(id, link) {
75 var el = link
76 ? h('a', {href: '#'+id}, id.substr(0, 8) + '…')
77 : h('ins', id.substr(0, 8) + '…')
78 getAvatar({links: sbot_links}, self_id, id, function (err, avatar) {
79 if(err) return console.error(err)
80 el.textContent = avatar.name
81 })
82 return el
83}
84
85exports.message_content = function (msg, sbot) {
86 var c = msg.value.content
87
88 if(c.type === 'git-repo') {
89 var nameEl
90 var branchesT, tagsT, openIssuesT, closedIssuesT, openPRsT, closedPRsT
91 var div = h('div',
92 h('p', 'git repo ', repoName(msg.key)),
93 c.upstream ? h('p', 'fork of ', repoName(c.upstream, true)) : '',
94 h('p', h('code', 'ssb://' + msg.key)),
95 h('div.git-table-wrapper', {style: {'max-height': '12em'}},
96 h('table',
97 branchesT = tableRows(h('tr',
98 h('th', 'branch'),
99 h('th', 'commit'),
100 h('th', 'last update'))),
101 tagsT = tableRows(h('tr',
102 h('th', 'tag'),
103 h('th', 'commit'),
104 h('th', 'last update'))))),
105 h('div.git-table-wrapper', {style: {'max-height': '16em'}},
106 h('table',
107 openIssuesT = tableRows(h('tr',
108 h('th', 'open issues'))),
109 closedIssuesT = tableRows(h('tr',
110 h('th', 'closed issues'))))),
111 h('div.git-table-wrapper', {style: {'max-height': '16em'}},
112 h('table',
113 openPRsT = tableRows(h('tr',
114 h('th', 'open pull requests'))),
115 closedPRsT = tableRows(h('tr',
116 h('th', 'closed pull requests'))))))
117
118 // compute refs
119 var refs = {}
120 pull(
121 sbot_links({
122 reverse: true,
123 source: msg.value.author,
124 dest: msg.key,
125 rel: 'repo',
126 values: true
127 }),
128 pull.drain(function (link) {
129 var refUpdates = link.value.content.refs
130 for (var ref in refUpdates) {
131 if (refs[ref]) continue
132 refs[ref] = true
133 var rev = refUpdates[ref]
134 if (!rev) continue
135 var parts = /^refs\/(heads|tags)\/(.*)$/.exec(ref) || []
136 var t
137 if (parts[1] === 'heads') t = branchesT
138 else if (parts[1] === 'tags') t = tagsT
139 if (t) t.append(h('tr',
140 h('td', parts[2]),
141 h('td', h('code', rev)),
142 h('td', messageTimestampLink(link))))
143 }
144 }, function (err) {
145 if (err) console.error(err)
146 })
147 )
148
149 // list issues and pull requests
150 pull(
151 sbot_links({
152 reverse: true,
153 dest: msg.key,
154 rel: 'project',
155 values: true
156 }),
157 paramap(function (link, cb) {
158 getIssueState(link.key, function (err, state) {
159 if(err) return cb(err)
160 link.state = state
161 cb(null, link)
162 })
163 }),
164 pull.drain(function (link) {
165 var c = link.value.content
166 var title = c.title || (c.text ? c.text.length > 70
167 ? c.text.substr(0, 70) + '…'
168 : c.text : link.key)
169 var author = link.value.author
170 var t = c.type === 'pull-request'
171 ? link.state === 'open' ? openPRsT : closedPRsT
172 : link.state === 'open' ? openIssuesT : closedIssuesT
173 t.append(h('tr',
174 h('td',
175 h('a', {href: '#'+link.key}, title), h('br'),
176 h('small',
177 'opened ', messageTimestampLink(link),
178 ' by ', h('a', {href: '#'+author}, avatar_name(author))))))
179 }, function (err) {
180 if (err) console.error(err)
181 })
182 )
183
184 return div
185 }
186
187 if(c.type === 'git-update') {
188 return h('p',
189 'pushed to ',
190 repoLink(c.repo),
191 c.refs ? h('ul', Object.keys(c.refs).map(function (ref) {
192 var rev = c.refs[ref]
193 return h('li',
194 shortRefName(ref) + ': ',
195 rev ? h('code', rev) : h('em', 'deleted'))
196 })) : null,
197 Array.isArray(c.issues) ? c.issues.map(function (issue) {
198 if (issue.merged === true)
199 return ['Merged ', message_link(issue.link), ' in ',
200 h('code', issue.object), ' ', h('q', issue.label)]
201 if (issue.open === false)
202 return ['Closed ', message_link(issue.link), ' in ',
203 h('code', issue.object), ' ', h('q', issue.label)]
204 }) : null
205 )
206 }
207
208 if (c.type === 'issue') {
209 return h('div',
210 h('p', 'opened issue on ', repoLink(c.project)),
211 c.title ? h('h4', c.title) : '',
212 markdown(c)
213 )
214 }
215
216 if (c.type === 'pull-request') {
217 return h('div',
218 h('p', 'opened pull-request ',
219 'to ', repoLink(c.repo), ':', c.branch, ' ',
220 'from ', repoLink(c.head_repo), ':', c.head_branch),
221 c.title ? h('h4', c.title) : '',
222 markdown(c)
223 )
224 }
225}
226
227exports.message_meta = function (msg, sbot) {
228 var type = msg.value.content.type
229 if (type == 'issue' || type == 'pull-request') {
230 var el = h('em', '...')
231 getIssueState(msg.key, function (err, state) {
232 if (err) return console.error(err)
233 el.textContent = state
234 })
235 return el
236 }
237}
238
239
240

Built with git-ssb-web