diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..74de91c --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,26 @@ +run: + deadline: 5m +linters: + enable-all: true + disable: + # Because globals might exist, but according to our codestyle they + # should be lowercased and considered as unexported. + - gochecknoglobals + # While it might be useful it'll create more problems that will solve. + - gocritic + # Complains about main() lengths, which isn't an issue. + - funlen +linters-settings: + lll: + line-length: 420 + gocyclo: + min-complexity: 40 + +issues: + exclude-rules: + # gosec says that there is a "Potential HTTP request made with + # variable URL". Well, he's right, we're composing webhook URL + # based on user's input. We do not need that linting here. + - linters: + - gosec + text: "G107: " \ No newline at end of file diff --git a/env/env.go b/env/env.go index f2dea86..8db9bfb 100644 --- a/env/env.go +++ b/env/env.go @@ -140,6 +140,7 @@ type environmentData struct { func ParseEnv() { Data = &environmentData{} + err := envconfig.Init(Data) if err != nil { fmt.Println(err) diff --git a/main.go b/main.go index 54941ab..ab91f54 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( func main() { env.ParseEnv() + if env.Data.Debug { log.Printf("Starting Discordrone with parameters: %+v\n", env.Data) } diff --git a/message/payload.go b/message/payload.go index 91cc9b2..5ac3999 100644 --- a/message/payload.go +++ b/message/payload.go @@ -3,11 +3,11 @@ package message type ( // Payload that will be sent to Discord. payload struct { + TTS bool `json:"tts"` Wait bool `json:"wait"` + AvatarURL string `json:"avatar_url"` Content string `json:"content"` Username string `json:"username"` - AvatarURL string `json:"avatar_url"` - TTS bool `json:"tts"` Embeds []EmbedObject `json:"embeds"` } diff --git a/message/process.go b/message/process.go index 38171e8..19615cc 100644 --- a/message/process.go +++ b/message/process.go @@ -44,7 +44,9 @@ func Process() error { if err != nil { return err } + p.Content = text + err1 := sendMessage(p) if err1 != nil { return err1 diff --git a/message/send_message.go b/message/send_message.go index a9a1813..f4722c2 100644 --- a/message/send_message.go +++ b/message/send_message.go @@ -15,10 +15,14 @@ import ( func sendMessage(message interface{}) error { webhookURL := fmt.Sprintf("https://discordapp.com/api/webhooks/%s/%s", env.Data.Plugin.Webhook.ID, env.Data.Plugin.Webhook.Token) b := new(bytes.Buffer) + if err := json.NewEncoder(b).Encode(message); err != nil { return err } - _, err := http.Post(webhookURL, "application/json; charset=utf-8", b) + + resp, err := http.Post(webhookURL, "application/json; charset=utf-8", b) + // We do not need response's body. + resp.Body.Close() if err != nil { return err diff --git a/message/template.go b/message/template.go index f94847d..d799bb1 100644 --- a/message/template.go +++ b/message/template.go @@ -24,7 +24,7 @@ func createEmbed() EmbedObject { // Compose description. var description string - // ToDo: promote/rollback? + // Add promote/rollback? switch env.Data.Drone.Build.Event { case "pull_request": var branch string @@ -33,6 +33,7 @@ func createEmbed() EmbedObject { } else { branch = env.Data.Drone.Commit.Branch } + description = env.Data.Drone.Commit.Author.Name + " updated pull request " + branch case "push": description = env.Data.Drone.Commit.Author.Name + " pushed to " + env.Data.Drone.Commit.Branch @@ -44,14 +45,15 @@ func createEmbed() EmbedObject { // Compose color. var color int64 + if env.Data.Plugin.Color != "" { env.Data.Plugin.Color = strings.Replace(env.Data.Plugin.Color, "#", "", -1) + s, err := strconv.ParseInt(env.Data.Plugin.Color, 16, 32) if err == nil { color = s } } else { - switch env.Data.Drone.Build.Status { case "success": // green