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