Migrate project to Go #1

Merged
parra merged 5 commits from migrate-to-go into main 2023-02-23 21:04:26 +01:00
3 changed files with 125 additions and 37 deletions
Showing only changes of commit dc2ab0dcd0 - Show all commits

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
.vscode/
bin/
.env

View file

@ -0,0 +1,32 @@
package config
type App struct {
BaseUrl string `env:"PLUGIN_URL" envDefault:"https://ntfy.sh"`
Topic string `env:"PLUGIN_TOPIC" envDefault:"undefined"`
Username string `env:"PLUGIN_USERNAME"`
Password string `env:"PLUGIN_PASSWORD"`
Token string `env:"PLUGIN_TOKEN"`
Title string `env:"PLUGIN_TITLE" envDefault:"Drone notification"`
Priority string `env:"PLUGIN_PRIORITY" envDefault:"default"`
Tags []string `env:"PLUGIN_TAGS" envSeparator:","`
DefaultTags []string `env:"PLUGIN_DEFAULT_TAGS" envDefault:"drone"`
Message string `env:"PLUGIN_MESSAGE"`
}
type Drone struct {
BuildNumber string `env:"DRONE_BUILD_NUMBER"`
BuildStatus string `env:"DRONE_BUILD_STATUS"`
StageName string `env:"DRONE_STAGE_NUMBER"`
StageStatus string `env:"DRONE_STAGE_STATUS"`
Tag string `env:"DRONE_TAG"`
BuildLink string `env:"DRONE_BUILD_LINK"`
CommitLink string `env:"DRONE_COMMIT_LINK"`
}
type Ci struct {
RepoName string `env:"CI_REPO_NAME"`
CommitMessage string `env:"CI_COMMIT_MESSAGE"`
CommitSha string `env:"CI_COMMIT_SHA"`
CommitBranch string `env:"CI_COMMIT_BRANCH"`
CommitRef string `env:"CI_COMMIT_REF"`
}

View file

@ -1,50 +1,105 @@
package main
import (
"fmt"
"log"
"os"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/caarlos0/env/v7"
"git.parravidales.es/parra/drone-ntfy/cmd/drone-ntfy/config"
"github.com/caarlos0/env/v7"
"github.com/joho/godotenv"
)
type Config struct {
BaseUrl string `env:"PLUGIN_BASE_URL" envDefault:"https://ntfy.sh"`
Topic string `env:"PLUGIN_TOPIC,required"`
Title string `env:"PLUGIN_TITLE" envDefault:"Drone notification"`
Priority string `env:"PLUGIN_PRIORITY" envDefault:"default"`
Tags []string `env:"PLUGIN_TAGS" envSeparator:","`
App config.App
Drone config.Drone
Ci config.Ci
}
type DroneConfig struct {
BuildNumber string `envPrefix:"DRONE_"`
StageName string `envPrefix:"DRONE_"`
StageStatus string `envPrefix:"DRONE_"`
}
type CiConfig struct {
RepoName string `envPrefix:"CI_"`
CommitMessage string `envPrefix:"CI_"`
}
const DEFAULT_PRIORITY = "default"
const BASE_URL = "https://ntfy.sh"
const TOPIC = ""
const TITLE = ""
const PRIORITY = ""
const TAGS = ""
const MESSAGE = "Build $DRONE_BUILD_NUMBER of $CI_REPO_NAME at stage $DRONE_STAGE_NAME $DRONE_STAGE_STATUS.\n\nCommit: $CI_COMMIT_MESSAGE"
func main() {
fmt.Printf("Topic: %s\n", os.Environ())
cfg := Config{}
err := env.Parse(&cfg)
if err != nil {
log.Fatalf("unable to parse environment variables: %e", err)
}
fmt.Printf("Topic: %s\n", os.Environ())
cfg := loadCfg()
fmt.Printf("Config: %v\n", cfg)
updateAppConfig(&cfg)
addTagsBasedOnResult(&cfg)
fmt.Printf("App Config: %+v\n", cfg)
sendNotification(&cfg)
}
func loadCfg() Config {
envErr := godotenv.Load(".env")
if envErr != nil {
fmt.Printf("unable to load .env file: %e\n", envErr)
}
conf := Config{
App: config.App{},
Drone: config.Drone{},
Ci: config.Ci{},
}
loadConfig(&conf.App)
loadConfig(&conf.Drone)
loadConfig(&conf.Ci)
conf.App.Tags = append(conf.App.DefaultTags, conf.App.Tags...)
return conf
}
func loadConfig(cfg any) {
err := env.Parse(cfg)
if err != nil {
log.Fatalf("unable to parse environment variables: %e", err)
}
}
func updateAppConfig(cfg *Config) {
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"
} else {
cfg.App.Tags = append(cfg.App.Tags, "branch "+cfg.Ci.CommitBranch)
cfg.App.Message = "[" + cfg.Ci.CommitSha[0:8] + "] " + cfg.Ci.CommitMessage
}
}
func addTagsBasedOnResult(cfg *Config) {
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")
} else {
cfg.App.Tags = append(cfg.App.Tags, "grey_question")
}
}
func sendNotification(cfg *Config) {
req, _ := http.NewRequest("POST",
cfg.App.BaseUrl + "/" + cfg.App.Topic,
strings.NewReader(cfg.App.Message))
if cfg.App.Token != "" {
req.Header.Add("Authorization", "Bearer "+cfg.App.Token)
} else {
req.SetBasicAuth(cfg.App.Username, cfg.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", "view, Build, "+cfg.Drone.BuildLink+"; view, Changes, "+cfg.Drone.CommitLink)
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("error trying to notify the result. Error: %+v", err)
}
if res.StatusCode != 200 {
log.Fatalf("error from server. HTTP status: %d. Error: %e", res.StatusCode, err)
}
}