Naming refactor
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
parra 2023-03-15 20:38:50 +01:00
parent a628f978e6
commit 9f86b72ce9
3 changed files with 50 additions and 49 deletions

View file

@ -9,6 +9,7 @@ steps:
- name: deps - name: deps
path: /go path: /go
commands: commands:
- go mod tidy
- GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o bin/ cmd/drone-ntfy/main.go - GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o bin/ cmd/drone-ntfy/main.go
- name: Build docker image - name: Build docker image

View file

@ -6,93 +6,93 @@ import (
"net/http" "net/http"
"strings" "strings"
"git.parravidales.es/parra/drone-ntfy/cmd/drone-ntfy/config" "git.parravidales.es/parra/drone-ntfy/pkg/model"
"github.com/caarlos0/env/v7" "github.com/caarlos0/env/v7"
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
type Config struct { type Data struct {
App config.App App model.App
Drone config.Drone Drone model.Drone
Ci config.Ci Ci model.Ci
} }
func main() { func main() {
cfg := loadCfg() data := loadData()
updateAppConfig(&cfg) buildAppData(&data)
addTagsBasedOnResult(&cfg) addResultToTags(&data)
sendNotification(&cfg) sendNotification(&data)
} }
func loadCfg() Config { func loadData() Data {
envErr := godotenv.Load() envErr := godotenv.Load()
if envErr == nil { if envErr == nil {
fmt.Printf("loaded .env file.") fmt.Printf("loaded .env file.")
} }
conf := Config{ data := Data{
App: config.App{}, App: model.App{},
Drone: config.Drone{}, Drone: model.Drone{},
Ci: config.Ci{}, Ci: model.Ci{},
} }
loadConfig(&conf.App) loadDataFromEnv(&data.App)
loadConfig(&conf.Drone) loadDataFromEnv(&data.Drone)
loadConfig(&conf.Ci) loadDataFromEnv(&data.Ci)
conf.App.Tags = append(conf.App.DefaultTags, conf.App.Tags...) data.App.Tags = append(data.App.DefaultTags, data.App.Tags...)
return conf return data
} }
func loadConfig(cfg any) { func loadDataFromEnv(data any) {
err := env.Parse(cfg) err := env.Parse(data)
if err != nil { if err != nil {
log.Fatalf("unable to parse environment variables: %e", err) log.Fatalf("unable to parse environment variables: %e", err)
} }
} }
func updateAppConfig(cfg *Config) { func buildAppData(data *Data) {
cfg.App.Title = "Build #" + cfg.Drone.BuildNumber + " " + cfg.Drone.BuildStatus data.App.Title = "Build #" + data.Drone.BuildNumber + " " + data.Drone.BuildStatus
if strings.Contains(cfg.Ci.CommitRef, "refs/tags/") { if strings.Contains(data.Ci.CommitRef, "refs/tags/") {
cfg.App.Tags = append(cfg.App.Tags, cfg.Drone.Tag) data.App.Tags = append(data.App.Tags, data.Drone.Tag)
cfg.App.Message = "Tag " + cfg.Drone.Tag + " created" data.App.Message = "Tag " + data.Drone.Tag + " created"
} else { } else {
cfg.App.Tags = append(cfg.App.Tags, cfg.Drone.RepoName+"/"+cfg.Ci.CommitBranch) data.App.Tags = append(data.App.Tags, data.Drone.RepoName+"/"+data.Ci.CommitBranch)
cfg.App.Message = "[" + cfg.Ci.CommitSha[0:8] + "] " + cfg.Ci.CommitMessage data.App.Message = "[" + data.Ci.CommitSha[0:8] + "] " + data.Ci.CommitMessage
} }
} }
func addTagsBasedOnResult(cfg *Config) { func addResultToTags(data *Data) {
if cfg.Drone.BuildStatus == "success" { if data.Drone.BuildStatus == "success" {
cfg.App.Tags = append(cfg.App.Tags, "white_check_mark") data.App.Tags = append(data.App.Tags, "white_check_mark")
} else if cfg.Drone.BuildStatus == "failure" { } else if data.Drone.BuildStatus == "failure" {
cfg.App.Tags = append(cfg.App.Tags, "x") data.App.Tags = append(data.App.Tags, "x")
} else { } else {
cfg.App.Tags = append(cfg.App.Tags, "grey_question") data.App.Tags = append(data.App.Tags, "grey_question")
} }
} }
func sendNotification(cfg *Config) { func sendNotification(data *Data) {
req, _ := http.NewRequest("POST", req, _ := http.NewRequest("POST",
cfg.App.BaseUrl+"/"+cfg.App.Topic, data.App.BaseUrl+"/"+data.App.Topic,
strings.NewReader(cfg.App.Message)) strings.NewReader(data.App.Message))
if cfg.App.Token != "" { if data.App.Token != "" {
req.Header.Add("Authorization", "Bearer "+cfg.App.Token) req.Header.Add("Authorization", "Bearer "+data.App.Token)
} else { } else {
req.SetBasicAuth(cfg.App.Username, cfg.App.Password) req.SetBasicAuth(data.App.Username, data.App.Password)
} }
req.Header.Set("Title", cfg.App.Title) req.Header.Set("Title", data.App.Title)
req.Header.Set("Priority", cfg.App.Priority) req.Header.Set("Priority", data.App.Priority)
req.Header.Set("Tags", strings.Join(cfg.App.Tags, ",")) req.Header.Set("Tags", strings.Join(data.App.Tags, ","))
req.Header.Set("Actions", getActions(cfg)) req.Header.Set("Actions", getActions(data))
res, err := http.DefaultClient.Do(req) res, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
log.Fatalf("error trying to notify the result. Error: %+v", err) log.Fatalf("error trying to notify the result. Error: %+v", err)
@ -102,10 +102,10 @@ func sendNotification(cfg *Config) {
} }
} }
func getActions(cfg *Config) string { func getActions(data *Data) string {
var buildLink = "view, Build, " + cfg.Drone.BuildLink var buildLink = "view, Build, " + data.Drone.BuildLink
if strings.Contains(cfg.Ci.CommitRef, "refs/tags/") { if strings.Contains(data.Ci.CommitRef, "refs/tags/") {
return buildLink return buildLink
} }
return buildLink + "; view, Changes, " + cfg.Drone.CommitLink return buildLink + "; view, Changes, " + data.Drone.CommitLink
} }

View file

@ -1,4 +1,4 @@
package config package model
type App struct { type App struct {
BaseUrl string `env:"PLUGIN_URL" envDefault:"https://ntfy.sh"` BaseUrl string `env:"PLUGIN_URL" envDefault:"https://ntfy.sh"`