/
failure.go
62 lines (56 loc) · 1.93 KB
/
failure.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
package command
import (
"fmt"
"path"
"github.com/lunarway/release-manager/internal/artifact"
intslack "github.com/lunarway/release-manager/internal/slack"
"github.com/nlopes/slack"
"github.com/spf13/cobra"
)
func failureCommand(options *Options) *cobra.Command {
var errorMessage string
command := &cobra.Command{
Use: "failure",
Short: "report failure in the pipeline",
RunE: func(c *cobra.Command, args []string) error {
client, err := intslack.NewClient(slack.New(options.SlackToken), options.UserMappings, options.EmailSuffix)
if err != nil {
fmt.Printf("Error, not able to create Slack client in failure command: %v", err)
return nil
}
err = client.UpdateMessage(path.Join(options.RootPath, options.MessageFileName), func(m intslack.Message) intslack.Message {
m.Color = intslack.MsgColorRed
m.Text += fmt.Sprintf(":no_entry: *%s*", errorMessage)
m.Title = ":jenkins: Jenkins :no_entry:"
return m
})
if err != nil {
fmt.Printf("Error, not able to update slack message with failure message: %v", err)
return nil
}
a, err := artifact.Get(path.Join(options.RootPath, options.FileName))
if err != nil {
fmt.Printf("Error, not able to retrieve artifact in failure command: %v", err)
return nil
}
err = client.NotifySlackBuildsChannel(intslack.BuildsOptions{
Service: a.Service,
ArtifactID: a.ID,
Branch: a.Application.Branch,
CommitSHA: a.Application.SHA,
CommitLink: a.Application.URL,
CommitAuthor: a.Application.AuthorName,
CommitMessage: a.Application.Message,
CIJobURL: a.CI.JobURL,
Color: intslack.MsgColorRed,
})
if err != nil {
fmt.Printf("Error, not able to notify #builds in failure command: %v", err)
return nil
}
return nil
},
}
command.Flags().StringVar(&errorMessage, "error-message", "Unexpected error in pipeline", "error message to send to slack")
return command
}