-
Notifications
You must be signed in to change notification settings - Fork 43
/
job_status.go
101 lines (84 loc) · 2.07 KB
/
job_status.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
package jenkins
import (
"context"
"fmt"
"text/template"
"github.com/bndr/gojenkins"
"github.com/innogames/slack-bot/v2/bot"
"github.com/innogames/slack-bot/v2/bot/config"
"github.com/innogames/slack-bot/v2/bot/matcher"
"github.com/innogames/slack-bot/v2/bot/msg"
)
const (
actionEnable = "enable"
)
type statusCommand struct {
jenkinsCommand
jobs config.JenkinsJobs
}
// newStatusCommand is able to enable/disable (whitelisted) Jenkins jobs
func newStatusCommand(base jenkinsCommand, jobs config.JenkinsJobs) bot.Command {
return &statusCommand{base, jobs}
}
func (c *statusCommand) GetMatcher() matcher.Matcher {
return matcher.NewRegexpMatcher(`(?P<action>enable|disable) job (?P<job>[\w\-_\\/]+)`, c.run)
}
func (c *statusCommand) run(match matcher.Result, message msg.Message) {
action := match.GetString("action")
jobName := match.GetString("job")
ctx := context.TODO()
if _, ok := c.jobs[jobName]; !ok {
text := fmt.Sprintf(
"Sorry, job *%s* is not whitelisted",
jobName,
)
c.SendMessage(message, text)
return
}
job, err := c.jenkins.GetJob(ctx, jobName)
if err != nil {
c.ReplyError(message, err)
return
}
var text string
if action == actionEnable {
_, err = job.Enable(ctx)
text = fmt.Sprintf("Job *%s* is enabled now", jobName)
} else {
_, err = job.Disable(ctx)
text = fmt.Sprintf("Job *%s* is disabled now", jobName)
}
if err != nil {
c.ReplyError(message, err)
return
}
c.SendMessage(message, text)
}
func (c *statusCommand) GetTemplateFunction() template.FuncMap {
return template.FuncMap{
"jenkinsJob": func(jobName string) *gojenkins.Job {
job, _ := c.jenkins.GetJob(context.TODO(), jobName)
return job
},
}
}
func (c *statusCommand) GetHelp() []bot.Help {
return []bot.Help{
{
Command: "enable job <job>",
Description: "enabled a jenkins job",
Examples: []string{
"enable job MyJobName",
},
Category: category,
},
{
Command: "disable job <job>",
Description: "disable a jenkins job",
Examples: []string{
"disable job MyJobName",
},
Category: category,
},
}
}