-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinteractor_jenkins.go
93 lines (79 loc) · 3.39 KB
/
interactor_jenkins.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
package main
import (
"fmt"
"strings"
"net/http"
"github.com/slack-go/slack"
)
type InteractorJenkins struct {
InteractorContext
}
func NewInteractorJenkins(i InteractorContext) (o InteractorJenkins) {
o = InteractorJenkins{i}
o.kind = "jenkins"
return
}
func (i InteractorJenkins) Request(pj DeployProject, phase string, branch string, assigner string, channel string) ([]slack.Block, error) {
var txt *slack.TextBlockObject
if phase == "production" && branch != pj.DefaultBranch() {
txt = slack.NewTextBlockObject(
"mrkdwn",
fmt.Sprintf(
":star::star::star::star::star::star::star::star::star::star::star::star::star::star::star::star::star::star::star::star::star:\n本番環境に master ブランチ以外をデプロイしようとしています\n:star::star::star::star::star::star::star::star::star::star::star::star::star::star::star::star::star::star::star::star::star:\n*%s*\n*%s*\n*%s* ブランチ\nをデプロイしますか?",
pj.GitHubRepository(),
phase,
branch,
),
false,
false,
)
} else {
txt = slack.NewTextBlockObject("mrkdwn", fmt.Sprintf("*%s*\n*%s*\n*%s* ブランチ\nをデプロイしますか?", pj.GitHubRepository(), phase, branch), false, false)
}
btnTxt := slack.NewTextBlockObject("plain_text", "Deploy", false, false)
btn := slack.NewButtonBlockElement("", fmt.Sprintf("%s|%s_%s_%s", i.actionHeader("approve"), pj.ID, phase, branch), btnTxt)
section := slack.NewSectionBlock(txt, nil, slack.NewAccessory(btn))
return []slack.Block{section, CloseButton()}, nil
}
func (i InteractorJenkins) BranchList(pj DeployProject, phase string) ([]slack.Block, error) {
return i.branchList(pj, phase)
}
func (i InteractorJenkins) BranchListFromRaw(params string) ([]slack.Block, error) {
p := strings.Split(params, "_")
pj := i.projectList.Find(p[0])
return i.branchList(pj, p[1])
}
func (i InteractorJenkins) SelectBranch(params string, branch string, userID string, channel string) ([]slack.Block, error) {
p := strings.Split(params, "_")
pj := i.projectList.Find(p[0])
return i.Request(pj, p[1], branch, userID, channel)
}
func (i InteractorJenkins) Approve(params string, userID string, channel string) ([]slack.Block, error) {
p := strings.Split(params, "_")
return i.approve(p[0], p[1], p[2], userID)
}
func (i InteractorJenkins) approve(target string, phase string, branch string, userID string) (blocks []slack.Block, err error) {
pj := i.projectList.Find(target)
jobName := pj.JenkinsJob()
url := fmt.Sprintf("https://bot:%s@%s/job/%s/buildWithParameters?token=%s&cause=slack-bot&ENV=%s&BRANCH=%s", i.config.JenkinsBotToken, i.config.JenkinsHost, jobName, i.config.JenkinsJobToken, phase, branch)
resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
res := fmt.Sprintf("Execute https://%s/job/%s/ \n selected branch: %s", i.config.JenkinsHost, jobName, branch)
if err != nil {
res = err.Error()
}
if resp.StatusCode != 201 {
res = jobName + " Request failed. responsed " + fmt.Sprint(resp.StatusCode)
}
blockObject := slack.NewTextBlockObject("mrkdwn", res, false, false)
blocks = append(blocks, slack.NewSectionBlock(blockObject, nil, nil))
userObject := slack.NewTextBlockObject("mrkdwn", "by <@"+userID+">", false, false)
blocks = append(blocks, slack.NewSectionBlock(userObject, nil, nil))
return
}
func (i InteractorJenkins) Reject(params string, userID string) (blocks []slack.Block, err error) {
return
}