forked from Trac-Systems/intercom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.js
More file actions
140 lines (124 loc) · 4.53 KB
/
protocol.js
File metadata and controls
140 lines (124 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { Protocol } from 'trac-peer'
const ALLOWED_OPS = new Set([
'release_new',
'release_check',
'release_approve',
'release_unblock',
'release_deploy',
'release_cancel',
'release_list',
'release_state'
])
const MUTATING_OPS = new Set([
'release_new',
'release_check',
'release_approve',
'release_unblock',
'release_deploy',
'release_cancel'
])
class ReleaseGatePeerProtocol extends Protocol {
constructor (peer, base, options = {}) {
super(peer, base, options)
}
mapTxCommand (command) {
const json = this.safeJsonParse(command)
if (!json || typeof json !== 'object') return null
if (!ALLOWED_OPS.has(json.op)) return null
return { type: 'releaseOp', value: json }
}
async tx (subject, sim = false, surrogate = null) {
const result = await super.tx(subject, sim, surrogate)
if (sim) return result
const json = this.safeJsonParse(subject?.command || '')
const op = json?.op
if (!MUTATING_OPS.has(op)) return result
const event = await this.get('release_gate_last_event')
const releaseId = event?.releaseId || json?.releaseId || null
if (!releaseId) return result
await this._broadcastReleaseUpdate(releaseId, {
op,
status: event?.status || null,
by: event?.by || null,
at: event?.at || null
})
return result
}
async _broadcastReleaseUpdate (releaseId, payload) {
if (!this.peer.sidechannel) return
const channel = `release-${releaseId}`
const joined = await this.peer.sidechannel.addChannel(channel)
if (!joined) return
const lines = [
`Release update: ${payload.op}`,
`Release: ${releaseId}`
]
if (payload.status) lines.push(`Status: ${payload.status}`)
if (payload.by) lines.push(`By: ${payload.by}`)
if (payload.at) lines.push(`At: ${payload.at}`)
this.peer.sidechannel.broadcast(channel, lines.join('\n'))
}
async printOptions () {
console.log(' ')
console.log('- Release Gate Commands:')
console.log('/tx --command \'{"op":"release_new","service":"api-gateway","version":"v1.4.2","approvers":["qa","sec"],"minApprovals":2,"checks":["unit-tests","integration-tests","smoke-prod"]}\'')
console.log('/tx --command \'{"op":"release_check","releaseId":"<id>","checkId":"c1","status":"passed","note":"green on CI"}\'')
console.log('/tx --command \'{"op":"release_approve","releaseId":"<id>"}\'')
console.log('/tx --command \'{"op":"release_unblock","releaseId":"<id>","reason":"Fix merged"}\'')
console.log('/tx --command \'{"op":"release_deploy","releaseId":"<id>"}\'')
console.log('/tx --command \'{"op":"release_state","releaseId":"<id>"}\'')
console.log('/tx --command \'{"op":"release_list"}\'')
console.log('/tx --command \'{"op":"release_list","filter":"ready"}\'')
console.log('/release_join --releaseId "<id>"')
console.log('/release_ping --releaseId "<id>" --message "optional text"')
}
async customCommand (input) {
await super.tokenizeInput(input)
if (this.input.startsWith('/release_help')) {
console.log('Use /tx with release_* ops. Example: /tx --command \'{"op":"release_list"}\'')
return
}
if (this.input.startsWith('/release_join')) {
if (!this.peer.sidechannel) {
console.log('Sidechannel not initialized.')
return
}
const args = this.parseArgs(input)
const releaseId = args.releaseId || args.id
if (!releaseId) {
console.log('Usage: /release_join --releaseId "<id>"')
return
}
const channel = `release-${releaseId}`
const ok = await this.peer.sidechannel.addChannel(channel)
if (!ok) {
console.log(`Could not join sidechannel ${channel}`)
return
}
console.log(`Joined sidechannel ${channel}`)
return
}
if (this.input.startsWith('/release_ping')) {
if (!this.peer.sidechannel) {
console.log('Sidechannel not initialized.')
return
}
const args = this.parseArgs(input)
const releaseId = args.releaseId || args.id
if (!releaseId) {
console.log('Usage: /release_ping --releaseId "<id>" [--message "<text>"]')
return
}
const channel = `release-${releaseId}`
const msg = args.message || args.msg || 'ping'
const ok = await this.peer.sidechannel.addChannel(channel)
if (!ok) {
console.log(`Could not join sidechannel ${channel}`)
return
}
this.peer.sidechannel.broadcast(channel, `release ping: ${msg}`)
console.log(`Pinged ${channel}`)
}
}
}
export default ReleaseGatePeerProtocol