forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.go
113 lines (94 loc) · 2.1 KB
/
slack.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
package web
import (
"strings"
"github.com/drone/drone/store"
"github.com/gin-gonic/gin"
)
const (
slashDeploy = "deploy"
slashRestart = "restart"
slashStatus = "status"
)
// Slack is handler function that handles Slack slash commands.
func Slack(c *gin.Context) {
command := c.Param("command")
text := c.PostForm("text")
args := strings.Split(text, " ")
if command == "" {
command = args[0]
args = args[1:]
}
switch command {
case slashStatus:
slackStatus(c, args)
case slashRestart:
slackRestart(c, args)
case slashDeploy:
slackDeploy(c, args)
default:
c.String(200, "sorry, I didn't understand [%s]", text)
}
}
func slackDeploy(c *gin.Context, args []string) {
if len(args) != 3 {
c.String(200, "Invalid command. Please provide [repo] [build number] [environment]")
return
}
var (
repo = args[0]
num = args[1]
env = args[2]
)
owner, name, _ := parseRepoBranch(repo)
c.String(200, "deploying build %s/%s#%s to %s", owner, name, num, env)
}
func slackRestart(c *gin.Context, args []string) {
var (
repo = args[0]
num = args[1]
)
owner, name, _ := parseRepoBranch(repo)
c.String(200, "restarting build %s/%s#%s", owner, name, num)
}
func slackStatus(c *gin.Context, args []string) {
var (
owner string
name string
branch string
)
if len(args) > 0 {
owner, name, branch = parseRepoBranch(args[0])
}
repo, err := store.GetRepoOwnerName(c, owner, name)
if err != nil {
c.String(200, "cannot find repository %s/%s", owner, name)
return
}
if branch == "" {
branch = repo.Branch
}
build, err := store.GetBuildLast(c, repo, branch)
if err != nil {
c.String(200, "cannot find status for %s/%s@%s", owner, name, branch)
return
}
c.String(200, "%s@%s build number %d finished with status %s",
repo.FullName,
build.Branch,
build.Number,
build.Status,
)
}
func parseRepoBranch(repo string) (owner, name, branch string) {
parts := strings.Split(repo, "@")
if len(parts) == 2 {
branch = parts[1]
repo = parts[0]
}
parts = strings.Split(repo, "/")
if len(parts) == 2 {
owner = parts[0]
name = parts[1]
}
return owner, name, branch
}