generated from mattermost/mattermost-plugin-starter-template
/
command_hooks.go
555 lines (442 loc) · 15.7 KB
/
command_hooks.go
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package main
import (
"bytes"
"encoding/csv"
"fmt"
"net/url"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
"github.com/icelander/teamcity-sdk-go/teamcity"
"github.com/icelander/teamcity-sdk-go/types"
"github.com/olekukonko/tablewriter"
)
const (
configTeamCityVersion = "2018.1"
// Magic Date: Mon Jan 2 15:04:05 MST 2006
fmtDateTime = "Jan 2, 2006 3:04 PM MST"
commandTriggerHooks = "teamcity"
commandTriggerEnable = "enable"
commandTriggerDisable = "disable"
commandTriggerInstall = "install"
commandTriggerList = "list"
commandTriggerListProjects = "projects"
commandTriggerListBuilds = "builds"
commandTriggerBuild = "build"
commandTriggerBuildStart = "start"
commandTriggerBuildCancel = "cancel"
commandTriggerStats = "stats"
errorNotInstalled = "To use the TeamCity Plugin first install it with `/teamcity install <teamcity url> <token>`"
errorDisabled = "TeamCity Plugin disabled. First enable it with `/teamcity enable`"
errorWhatList = "Try `/teamcity list builds` or `/teamcity list projects`"
errorNoBuildID = "Please provide a build ID, `/teamcity build start <build_id>`"
errorNoBuildCommand = "Please provide a build command, e.g. `/teamcity build start <build_id>`"
msgInstalled = "TeamCity Plugin Installed!"
msgEnabled = "TeamCity Plugin Enabled"
msgDisabled = "TeamCity Plugin Disabled"
iconGood = ":white_check_mark:"
iconBad = ":x:"
commandDialogHelp = "Use one of the following slash commands to interact with TeamCity from within Mattermost\n" +
"- `/teamcity install <teamcity url> <token>` - Set up the TeamCity plugin\n" +
"- `/teamcity list projects` - List projects with description and project id\n" +
"- `/teamcity list builds` - List builds with description, project, and build id\n" +
"- `/teamcity build status <build_id>` - Get the status of a specific build\n" +
"- `/teamcity build start <project>` - Trigger a build on a specific project\n" +
"- `/teamcity build cancel <build_id>` - Cancel a build\n" +
"- `/teamcity stats` - Basic build statistics (Project Level and Build Configuration level)"
)
func (p *Plugin) registerCommands() error {
if err := p.API.RegisterCommand(&model.Command{
Trigger: commandTriggerHooks,
DisplayName: "TeamCity",
Description: "Integration with JetBeans TeamCity",
AutoComplete: true,
AutoCompleteHint: "[command]",
AutoCompleteDesc: "Available commands: install, list, build, stats",
}); err != nil {
return errors.Wrapf(err, "failed to register %s command", commandTriggerHooks)
}
if err := p.API.RegisterCommand(&model.Command{
Trigger: "/" + commandTriggerHooks,
DisplayName: "TeamCity",
Description: "Integration with JetBeans TeamCity",
AutoComplete: true,
AutoCompleteHint: "[command]",
AutoCompleteDesc: "Available commands: install, list, build, stats",
}); err != nil {
return errors.Wrapf(err, "failed to register %s command", commandTriggerHooks)
}
return nil
}
// ExecuteCommand executes the slash commands
func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
cArgs, err := p.extractCommandArgs(args.Command)
if err != nil {
return p.postEphemeral("Could not extract command arguments"), nil
}
if cArgs[0] == "/"+commandTriggerHooks {
return p.executeCommandHooks(args), nil
}
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
Text: fmt.Sprintf("Unknown command: " + args.Command),
}, nil
}
func (p *Plugin) extractCommandArgs(cArgs string) ([]string, error) {
r := csv.NewReader(strings.NewReader(cArgs))
r.Comma = ' ' // space
fields, err := r.Read()
return fields, err
}
func (p *Plugin) invalidCommand(args *model.CommandArgs) *model.CommandResponse {
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
Text: commandDialogHelp,
}
}
func (p *Plugin) executeCommandHooks(args *model.CommandArgs) *model.CommandResponse {
configuration := p.getConfiguration()
cArgs, err := p.extractCommandArgs(args.Command)
if err != nil {
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
Text: commandDialogHelp,
}
}
if !strings.HasPrefix(args.Command, "/"+commandTriggerHooks) {
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
Text: commandDialogHelp,
}
}
if !configuration.Installed() && cArgs[1] != commandTriggerInstall {
return p.postEphemeral(errorNotInstalled)
}
switch cArgs[1] {
case commandTriggerDisable:
return p.executeCommandTriggerDisable(args)
case commandTriggerEnable:
return p.executeCommandTriggerEnable(args)
case commandTriggerInstall:
return p.executeCommandTriggerInstall(args)
case commandTriggerList:
if configuration.disabled {
return p.postEphemeral(errorDisabled)
}
if len(cArgs) == 2 {
return p.postEphemeral(errorWhatList)
}
switch cArgs[2] {
case commandTriggerListBuilds:
return p.executeCommandTriggerListBuilds(args)
case commandTriggerListProjects:
return p.executeCommandTriggerListProjects(args)
default:
return p.invalidCommand(args)
}
case commandTriggerBuild:
if configuration.disabled {
return p.postEphemeral(errorDisabled)
}
if len(cArgs) == 3 {
return p.postEphemeral(errorNoBuildCommand)
}
switch cArgs[2] {
case commandTriggerBuildStart:
return p.executeCommandTriggerBuildStart(cArgs[3])
case commandTriggerBuildCancel:
return p.executeCommandTriggerBuildCancel(args)
default:
return p.invalidCommand(args)
}
case commandTriggerStats:
if configuration.disabled {
return p.postEphemeral(errorDisabled)
}
return p.executeCommandTriggerStats(args)
default:
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
Text: commandDialogHelp,
}
}
}
func (p *Plugin) postEphemeral(text string) *model.CommandResponse {
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
Text: text,
}
}
func (p *Plugin) executeCommandTriggerInstall(args *model.CommandArgs) *model.CommandResponse {
configuration := p.getConfiguration()
cArgs, err := p.extractCommandArgs(args.Command)
if err != nil {
p.API.LogError(
"Could not extract arguments",
"error", err.Error(),
)
return p.postEphemeral("Could not read arguments")
}
// Install command is like this:
// - [0] : /teamcity
// - [1] : install
// - [2] : url
// - [3] : token
// TODO: Write test for not enough arguments
if len(cArgs) != 4 {
return p.postEphemeral(errorNotInstalled)
}
// Validate URL
// TODO: Write invalid URL test
u, err := url.ParseRequestURI(cArgs[2])
if err != nil {
// p.API.LogError("Invalid TeamCity URL: " + u.String())
return p.postEphemeral("Invalid URL: `" + err.Error() + "`\n")
}
// Attempt a test command
// TODO: Write "could not connect" test
client := teamcity.New(u.String(), cArgs[3])
server, err := client.Server()
if err != nil {
return p.postEphemeral("Could not connect to server.\nError: `" + err.Error() + "`")
}
configuration.TeamCityURL = u.String()
configuration.TeamCityToken = cArgs[3]
p.setConfiguration(configuration)
return p.postEphemeral("TeamCity Installed! Here are the server details:\n" +
"**Server:** " + u.String() + "\n" +
"**Server Version:** " + server.Version + "\n" +
"**Build Number:** " + server.BuildNumber)
}
func (p *Plugin) executeCommandTriggerEnable(args *model.CommandArgs) *model.CommandResponse {
configuration := p.getConfiguration()
if configuration.disabled {
configuration.disabled = false
p.setConfiguration(configuration)
}
return p.postEphemeral(msgEnabled)
}
func (p *Plugin) executeCommandTriggerDisable(args *model.CommandArgs) *model.CommandResponse {
configuration := p.getConfiguration()
if !configuration.disabled {
configuration.disabled = true
p.setConfiguration(configuration)
}
return p.postEphemeral(msgDisabled)
}
func (p *Plugin) executeCommandTriggerListProjects(args *model.CommandArgs) *model.CommandResponse {
configuration := p.getConfiguration()
client := teamcity.New(configuration.TeamCityURL,
configuration.TeamCityToken)
projects, err := client.GetShortProjects()
if err != nil {
return p.postEphemeral(fmt.Sprintf("Error listing projects: %s", err.Error()))
}
if len(projects) == 0 {
return p.postEphemeral("No projects found")
}
message := "**TeamCity Projects:**\n\n"
for _, project := range projects {
if project.ID == "_Root" {
continue
}
message += fmt.Sprintf(" - [%s (ID: %s)](%s)", project.Name, project.ID, project.WebURL)
}
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL,
Text: message,
}
}
func (p *Plugin) executeCommandTriggerListBuilds(args *model.CommandArgs) *model.CommandResponse {
configuration := p.getConfiguration()
client := teamcity.New(configuration.TeamCityURL,
configuration.TeamCityToken)
builds, err := client.GetBuilds()
if err != nil {
return p.postEphemeral("Error listing builds: `" + err.Error() + "`")
}
if len(builds) == 0 {
return p.postEphemeral("No builds found")
}
message := "**TeamCity Builds:**\n\n"
buf := new(bytes.Buffer)
buildTable := tablewriter.NewWriter(buf)
buildTable.SetAutoWrapText(false)
buildTable.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
buildTable.SetCenterSeparator("|")
buildTable.SetHeader([]string{"Build", "Project", "Build Start", "Build Finish"})
buildTable.SetAutoFormatHeaders(false)
buildTable.SetHeaderAlignment(tablewriter.ALIGN_CENTER)
maxBuilds := configuration.GetMaxBuilds()
if len(builds)-1 < maxBuilds {
maxBuilds = len(builds) - 1
}
for i := 0; i < maxBuilds; i++ {
build := builds[i]
nameLink := fmt.Sprintf("[%s #%s](%s)", build.BuildTypeID, build.Number, build.WebURL)
buildStartDate := build.StartDate.Time().Format(fmtDateTime)
buildFinishDate := build.FinishDate.Time().Format(fmtDateTime)
buildTable.Append([]string{
nameLink,
build.BuildType.ProjectName,
buildStartDate,
buildFinishDate,
})
}
buildTable.Render()
message += buf.String()
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL,
Text: message,
}
}
func (p *Plugin) executeCommandTriggerBuildStart(buildTypeID string) *model.CommandResponse {
configuration := p.getConfiguration()
client := teamcity.New(configuration.TeamCityURL,
configuration.TeamCityToken)
var emptyMap = make(map[string]string)
// Check BuildTypeID is correct
var emptyBuildType *types.BuildType
buildType, err := client.GetBuildType(buildTypeID)
if emptyBuildType == buildType {
return p.postEphemeral("Invalid Build ID: `" + buildTypeID + "`")
}
build, err := client.QueueBuild(buildTypeID, "", emptyMap)
if err != nil {
return p.postEphemeral("Error starting build: `" + err.Error() + "`")
}
message := "**TEAMCITY BUILD STARTED**\n\n" +
" - Build Type: [%s](%s)\n" +
" - [Build ID: %d](%s)\n" +
" - State: %s\n\n" +
" Stop this build with this slash command: `/teamcity build cancel %d`"
respText := fmt.Sprintf(message, build.BuildType.Name, build.BuildType.WebURL, build.ID, build.WebURL, build.State, build.ID)
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL,
Text: respText,
}
}
func (p *Plugin) executeCommandTriggerBuildCancel(args *model.CommandArgs) *model.CommandResponse {
configuration := p.getConfiguration()
client := teamcity.New(configuration.TeamCityURL,
configuration.TeamCityToken)
cArgs, err := p.extractCommandArgs(args.Command)
if err != nil {
return p.postEphemeral(fmt.Sprintf("Error parsing arguments: `%s`", err.Error()))
}
// Cancel command is like this:
// - [0] : /teamcity
// - [1] : build
// - [2] : cancel
// - [3] : buildID
// - [4] : Comments
// Verify the buildID
buildID, err := strconv.ParseInt(cArgs[3], 10, 64)
if buildID == 0 {
return p.postEphemeral(fmt.Sprintf("Invalid Build ID: %s", cArgs[3]))
}
if err != nil {
return p.postEphemeral(fmt.Sprintf("Error Cancelling Build: %s", err.Error()))
}
buildComment := ""
if len(cArgs) >= 5 {
var commentSlice []string = cArgs[4:]
buildComment = strings.Join(commentSlice, " ")
}
build, err := client.CancelBuild(buildID, buildComment)
if err != nil {
return p.postEphemeral(fmt.Sprintf("Error Cancelling Build: %s", err.Error()))
}
message := "**TEAMCITY BUILD CANCELLED**\n"
message += "----\n"
message += " - Build : [" + build.BuildTypeID + " #" + build.Number + "](" + build.WebURL + "))\n" +
"\t - Project: " + build.BuildType.ProjectName + "\n"
if build.Status == "SUCCESS" {
message += "\t - Status: " + build.StatusText + "\n"
} else {
message += "\t - Status: **" + build.StatusText + "**\n"
}
buildStartDate := build.StartDate.Time().Format(fmtDateTime)
message += "\t - Build Start: " + buildStartDate + "\n"
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL,
Text: message,
}
}
func (p *Plugin) executeCommandTriggerStats(args *model.CommandArgs) *model.CommandResponse {
configuration := p.getConfiguration()
client := teamcity.New(configuration.TeamCityURL,
configuration.TeamCityToken)
agents, err := client.GetAgentStats()
if err != nil {
return p.postEphemeral(fmt.Sprintf("Error getting agent stats: %s", err.Error()))
}
builds, err := client.GetBuildQueue()
if err != nil {
return p.postEphemeral(fmt.Sprintf("Error getting build queue: %s", err.Error()))
}
message := "**Agent Stats**\n\n"
buf := new(bytes.Buffer)
agentTable := tablewriter.NewWriter(buf)
agentTable.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
agentTable.SetCenterSeparator("|")
agentTable.SetHeader([]string{"Name", "Enabled", "Authorized", "Up to Date", "Connected", "Working"})
agentTable.SetAutoFormatHeaders(false)
agentTable.SetAutoWrapText(false)
agentTable.SetColumnAlignment([]int{
tablewriter.ALIGN_LEFT,
tablewriter.ALIGN_CENTER,
tablewriter.ALIGN_CENTER,
tablewriter.ALIGN_CENTER,
tablewriter.ALIGN_CENTER,
tablewriter.ALIGN_CENTER,
})
for _, agent := range agents {
working := (agent.ActiveBuild.BuildTypeID != "")
nameLink := fmt.Sprintf("[%s](%s)", agent.Name, agent.WebURL)
agentTable.Append([]string{nameLink,
p.redOrGreen(agent.Enabled),
p.redOrGreen(agent.Authorized),
p.redOrGreen(agent.UpToDate),
p.redOrGreen(agent.Connected),
p.redOrGreen(working),
})
}
agentTable.Render()
message += buf.String()
if len(builds) != 0 {
message += fmt.Sprintf("\n---\n**Build Queue** - Total Builds: %d\n\n", len(builds))
buf := new(bytes.Buffer)
buildTable := tablewriter.NewWriter(buf)
buildTable.SetAutoWrapText(false)
buildTable.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
buildTable.SetCenterSeparator("|")
buildTable.SetHeader([]string{"Project", "Build Name", "Date Queued", "Queue Position"})
buildTable.SetAutoFormatHeaders(false)
buildTable.SetHeaderAlignment(tablewriter.ALIGN_CENTER)
for _, build := range builds {
nameLink := fmt.Sprintf("[%s](%s)", build.BuildType.Name, build.WebURL)
queuedTime := build.QueuedDate.Time().Format(fmtDateTime)
buildTable.Append([]string{
build.BuildType.ProjectName,
nameLink,
queuedTime,
fmt.Sprintf("%d", build.QueuePosition),
})
}
buildTable.Render()
message += buf.String()
}
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL,
Text: message,
}
}
func (p *Plugin) redOrGreen(t bool) string {
if t {
return iconGood
}
return iconBad
}