Files: e3d280e0a83dd60d5e5880b996b30782d4a68f67 / modules / git.js
4285 bytesRaw
1 | var h = require('hyperscript') |
2 | var pull = require('pull-stream') |
3 | var paramap = require('pull-paramap') |
4 | var moment = require('moment') |
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 | |
13 | var self_id = require('../keys').id |
14 | |
15 | function shortRefName(ref) { |
16 | return ref.replace(/^refs\/(heads|tags)\//, '') |
17 | } |
18 | |
19 | function repoLink(id) { |
20 | var el = h('a', {href: '#'+id}, id.substr(0, 10) + '…') |
21 | getAvatar({links: sbot_links}, self_id, id, function (err, avatar) { |
22 | if(err) return console.error(err) |
23 | el.textContent = avatar.name |
24 | }) |
25 | return el |
26 | } |
27 | |
28 | function getIssueState(id, cb) { |
29 | pull( |
30 | sbot_links({dest: id, rel: 'issues', values: true}), |
31 | pull.map(function (msg) { |
32 | var issues = msg.value.content.issues |
33 | if (!Array.isArray(issues)) return |
34 | return issues.filter(function (issue) { |
35 | return issue.link === id |
36 | }).map(function (issue) { |
37 | return { |
38 | ts: msg.value.timestamp, |
39 | open: issue.open, |
40 | merged: issue.merged, |
41 | } |
42 | }) |
43 | }), |
44 | pull.flatten(), |
45 | pull.collect(function (err, updates) { |
46 | if (err) return cb(err) |
47 | var open = true, merged = false |
48 | updates.sort(function (a, b) { |
49 | return b.ts - a.ts |
50 | }).forEach(function (update) { |
51 | if (update.open != null) |
52 | open = update.open |
53 | if (update.merged != null) |
54 | merged = update.merged |
55 | }) |
56 | cb(null, open ? 'open' : merged ? 'merged' : 'closed') |
57 | }) |
58 | ) |
59 | } |
60 | |
61 | exports.message_content = function (msg, sbot) { |
62 | var c = msg.value.content |
63 | |
64 | if(c.type === 'git-repo') { |
65 | var nameEl, refsTable |
66 | var div = h('div', |
67 | h('p', 'git repo ', nameEl = h('ins')), |
68 | h('p', h('code', 'ssb://' + msg.key)), |
69 | refsTable = h('table') |
70 | ) |
71 | |
72 | // show repo name |
73 | getAvatar({links: sbot_links}, self_id, msg.key, function (err, avatar) { |
74 | if(err) return console.error(err) |
75 | nameEl.textContent = avatar.name |
76 | }) |
77 | |
78 | // compute refs |
79 | var refs = {} |
80 | var first = true |
81 | pull( |
82 | sbot_links({ |
83 | reverse: true, |
84 | source: msg.value.author, |
85 | dest: msg.key, |
86 | rel: 'repo', |
87 | values: true |
88 | }), |
89 | pull.drain(function (link) { |
90 | var refUpdates = link.value.content.refs |
91 | if (first) { |
92 | first = false |
93 | refsTable.appendChild(h('tr', |
94 | h('th', 'branch'), |
95 | h('th', 'commit'), |
96 | h('th', 'last update'))) |
97 | } |
98 | for (var ref in refUpdates) { |
99 | if (refs[ref]) continue |
100 | refs[ref] = true |
101 | var rev = refUpdates[ref] |
102 | var m = moment(link.value.timestamp) |
103 | refsTable.appendChild(h('tr', |
104 | h('td', shortRefName(ref)), |
105 | h('td', h('code', rev)), |
106 | h('td', h('a.timestamp', { |
107 | timestamp: m, |
108 | title: m.format('LLLL'), |
109 | href: '#'+link.key |
110 | }, m.fromNow())))) |
111 | } |
112 | }, function (err) { |
113 | if (err) console.error(err) |
114 | }) |
115 | ) |
116 | |
117 | return div |
118 | } |
119 | |
120 | if(c.type === 'git-update') { |
121 | return h('p', |
122 | 'pushed to ', |
123 | repoLink(c.repo), |
124 | c.refs ? h('ul', Object.keys(c.refs).map(function (ref) { |
125 | var rev = c.refs[ref] |
126 | return h('li', |
127 | shortRefName(ref) + ': ', |
128 | rev ? h('code', rev) : h('em', 'deleted')) |
129 | })) : null, |
130 | Array.isArray(c.issues) ? c.issues.map(function (issue) { |
131 | if (issue.merged === true) |
132 | return ['Merged ', message_link(issue.link), ' in ', |
133 | h('code', issue.object), ' ', h('q', issue.label)] |
134 | if (issue.open === false) |
135 | return ['Closed ', message_link(issue.link), ' in ', |
136 | h('code', issue.object), ' ', h('q', issue.label)] |
137 | }) : null |
138 | ) |
139 | } |
140 | } |
141 | |
142 | exports.message_meta = function (msg, sbot) { |
143 | var type = msg.value.content.type |
144 | if (type == 'issue' || type == 'pull-request') { |
145 | var el = h('em', '...') |
146 | getIssueState(msg.key, function (err, state) { |
147 | if (err) return console.error(err) |
148 | el.textContent = state |
149 | }) |
150 | return el |
151 | } |
152 | } |
153 |
Built with git-ssb-web