This repository has been archived by the owner on Feb 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ci_flowdock_message.go
108 lines (77 loc) · 2.31 KB
/
ci_flowdock_message.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
// Copyright © 2016-present Thomas Rabaix <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package flowdock
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"strings"
"github.com/mitchellh/cli"
)
type CiFlowdockMessageCommand struct {
Ui cli.Ui
Verbose bool
}
func (c *CiFlowdockMessageCommand) Run(args []string) int {
config := &FlowdockConfig{
Server: "https://api.flowdock.com",
}
message := &FlowdockMessage{}
cmdFlags := flag.NewFlagSet("ci:notification:flowdock", flag.ContinueOnError)
cmdFlags.Usage = func() {
c.Ui.Output(c.Help())
}
cmdFlags.BoolVar(&c.Verbose, "verbose", false, "")
cmdFlags.StringVar(&config.Token, "token", os.Getenv("FLOWDOCK_SOURCE_TOKEN"), "The room's token (default: env var FLOWDOCK_SOURCE_TOKEN)")
if err := cmdFlags.Parse(args); err != nil {
return 1
}
args = cmdFlags.Args()
if len(args) != 3 {
c.Ui.Error("Invalid number of arguments\n")
cmdFlags.Usage()
return 1
}
config.Organization = args[0]
config.Flow = args[1]
message.Event = "message"
message.Content = args[2]
message.Token = config.Token
message.Flow = config.Flow
buf := bytes.NewBuffer([]byte(""))
e := json.NewEncoder(buf)
e.Encode(message)
client := &http.Client{}
r, _ := http.NewRequest("POST", fmt.Sprintf("%s/flows/%s/%s/messages", config.Server, config.Organization, config.Flow), buf)
r.Header.Add("Content-Type", "application/json")
_, err := client.Do(r)
if err != nil {
c.Ui.Error(fmt.Sprintf("Err: %s", err.Error()))
return 1
}
return 0
}
func (c *CiFlowdockMessageCommand) Synopsis() string {
return "Send a message to one Flowdock's room."
}
func (c *CiFlowdockMessageCommand) Help() string {
helpText := fmt.Sprintf(`
Usage: gitlab-ci-helper flowdock:message [options] organisation flow message
Build a flowdock thread from the current build. Information are retrieved from
environment variables.
Arguments:
organisation The organisation name
flow The flow reference
message The message to send
Options:
-token The flow's token (default: env var FLOWDOCK_SOURCE_TOKEN)
-verbose Add verbose information to the output
%s
`, flowdockConfiguration)
return strings.TrimSpace(helpText)
}