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
path: /go
commands:
- go mod tidy
- GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o bin/ cmd/drone-ntfy/main.go
- name: Build docker image

View file

@ -6,93 +6,93 @@ import (
"net/http"
"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/joho/godotenv"
)
type Config struct {
App config.App
Drone config.Drone
Ci config.Ci
type Data struct {
App model.App
Drone model.Drone
Ci model.Ci
}
func main() {
cfg := loadCfg()
data := loadData()
updateAppConfig(&cfg)
addTagsBasedOnResult(&cfg)
sendNotification(&cfg)
buildAppData(&data)
addResultToTags(&data)
sendNotification(&data)
}
func loadCfg() Config {
func loadData() Data {
envErr := godotenv.Load()
if envErr == nil {
fmt.Printf("loaded .env file.")
}
conf := Config{
App: config.App{},
Drone: config.Drone{},
Ci: config.Ci{},
data := Data{
App: model.App{},
Drone: model.Drone{},
Ci: model.Ci{},
}
loadConfig(&conf.App)
loadConfig(&conf.Drone)
loadConfig(&conf.Ci)
loadDataFromEnv(&data.App)
loadDataFromEnv(&data.Drone)
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) {
err := env.Parse(cfg)
func loadDataFromEnv(data any) {
err := env.Parse(data)
if err != nil {
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/") {
cfg.App.Tags = append(cfg.App.Tags, cfg.Drone.Tag)
cfg.App.Message = "Tag " + cfg.Drone.Tag + " created"
if strings.Contains(data.Ci.CommitRef, "refs/tags/") {
data.App.Tags = append(data.App.Tags, data.Drone.Tag)
data.App.Message = "Tag " + data.Drone.Tag + " created"
} else {
cfg.App.Tags = append(cfg.App.Tags, cfg.Drone.RepoName+"/"+cfg.Ci.CommitBranch)
cfg.App.Message = "[" + cfg.Ci.CommitSha[0:8] + "] " + cfg.Ci.CommitMessage
data.App.Tags = append(data.App.Tags, data.Drone.RepoName+"/"+data.Ci.CommitBranch)
data.App.Message = "[" + data.Ci.CommitSha[0:8] + "] " + data.Ci.CommitMessage
}
}
func addTagsBasedOnResult(cfg *Config) {
func addResultToTags(data *Data) {
if cfg.Drone.BuildStatus == "success" {
cfg.App.Tags = append(cfg.App.Tags, "white_check_mark")
} else if cfg.Drone.BuildStatus == "failure" {
cfg.App.Tags = append(cfg.App.Tags, "x")
if data.Drone.BuildStatus == "success" {
data.App.Tags = append(data.App.Tags, "white_check_mark")
} else if data.Drone.BuildStatus == "failure" {
data.App.Tags = append(data.App.Tags, "x")
} 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",
cfg.App.BaseUrl+"/"+cfg.App.Topic,
strings.NewReader(cfg.App.Message))
data.App.BaseUrl+"/"+data.App.Topic,
strings.NewReader(data.App.Message))
if cfg.App.Token != "" {
req.Header.Add("Authorization", "Bearer "+cfg.App.Token)
if data.App.Token != "" {
req.Header.Add("Authorization", "Bearer "+data.App.Token)
} 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("Priority", cfg.App.Priority)
req.Header.Set("Tags", strings.Join(cfg.App.Tags, ","))
req.Header.Set("Actions", getActions(cfg))
req.Header.Set("Title", data.App.Title)
req.Header.Set("Priority", data.App.Priority)
req.Header.Set("Tags", strings.Join(data.App.Tags, ","))
req.Header.Set("Actions", getActions(data))
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("error trying to notify the result. Error: %+v", err)
@ -102,10 +102,10 @@ func sendNotification(cfg *Config) {
}
}
func getActions(cfg *Config) string {
var buildLink = "view, Build, " + cfg.Drone.BuildLink
if strings.Contains(cfg.Ci.CommitRef, "refs/tags/") {
func getActions(data *Data) string {
var buildLink = "view, Build, " + data.Drone.BuildLink
if strings.Contains(data.Ci.CommitRef, "refs/tags/") {
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 {
BaseUrl string `env:"PLUGIN_URL" envDefault:"https://ntfy.sh"`