notify/main.go
Marcel S. Henselin 45a4a7e647
Some checks failed
CI / lint (push) Failing after 3m58s
CI / release (push) Successful in 6m30s
fix: fix linting
2026-04-17 17:27:40 +02:00

192 lines
4 KiB
Go

package main
import (
"bytes"
_ "embed"
"fmt"
"net/http"
"os"
"text/template"
"github.com/google/uuid"
"github.com/sethvargo/go-githubactions"
)
const (
defaultColor = "483d8b/6495ed"
successColor = "006400/228b22"
failedColor = "8b0000/dc143c"
)
//go:embed message.tpl
var message string
type templateData struct {
CardID string
IconSlug string
Title string
SubTitle string
Author string
IconUrl string
Status string
ActionID string
GitURL string
Project string
Color string
Add string
EventBody string
}
func main() {
data := templateData{}
data.CardID = uuid.NewString()
data.Add = ""
getData(&data)
webhook := githubactions.GetInput("webhook")
if webhook == "" {
githubactions.Fatalf("Missing input 'webhook'")
os.Exit(1)
}
webhook = fmt.Sprintf("%s&threadKey=notify%s&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD", webhook, data.ActionID)
giturl := githubactions.GetInput("giturl")
if giturl == "" {
githubactions.Fatalf("Missing input 'giturl'")
os.Exit(1)
}
data.GitURL = giturl
body := githubactions.GetInput("event_body")
data.EventBody = body
githubactions.Infof("using URL: %s", webhook)
jsonStr, err := card(data)
if err != nil {
githubactions.Fatalf("err %s", err.Error())
os.Exit(1)
}
req, err := http.NewRequest("POST", webhook, bytes.NewBuffer(jsonStr))
if err != nil {
githubactions.Fatalf("error %s", err.Error())
os.Exit(1)
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
githubactions.Fatalf("error %s", err.Error())
os.Exit(1)
}
defer resp.Body.Close() //nolint:gocritic
fmt.Println("response Status:", resp.Status)
if resp.StatusCode != http.StatusOK {
githubactions.Infof("json: %s", jsonStr)
githubactions.Fatalf("response: %+v\n", resp)
os.Exit(1)
}
}
func getData(data *templateData) {
prj, ok := os.LookupEnv("FORGEJO_REPOSITORY")
if ok {
data.Project = prj
}
author := githubactions.GetInput("author")
if author == "" {
githubactions.Infof("[INFO] Missing input 'author', using default")
author = "STACKIT git action"
}
data.Author = author
icon := githubactions.GetInput("iconslug")
if icon == "" {
githubactions.Infof("[INFO] Missing input 'iconslug', using default")
icon = "git"
}
data.IconSlug = icon
title := githubactions.GetInput("title")
if title == "" {
githubactions.Infof("[INFO] Missing input 'title', using default")
title = "STACKIT git action"
}
data.Title = title
status := githubactions.GetInput("status")
if status == "" {
githubactions.Infof("[INFO] Missing input 'status', trying default")
var ok bool
if status, ok = os.LookupEnv("JOB_STATUS"); ok {
githubactions.Infof("[INFO] found job status")
}
}
data.Status = status
actionid := githubactions.GetInput("actionid")
if actionid == "" {
githubactions.Fatalf("Missing input 'actionid'")
os.Exit(1)
}
data.ActionID = actionid
}
func card(d templateData) ([]byte, error) {
switch d.Status {
case "success":
d.Color = successColor
case "failure":
d.Color = failedColor
default:
d.Color = defaultColor
}
if d.IconUrl == "" {
d.IconUrl = fmt.Sprintf(
"https://cdn.simpleicons.org/%s/%s",
d.IconSlug,
d.Color,
)
}
if d.EventBody == "" {
bdy := ""
if d.Project != "" {
bdy += fmt.Sprintf("<b>Project:</b> %s<br>", d.Project)
}
sha, ok := os.LookupEnv("FORGEJO_SHA")
if ok {
bdy += fmt.Sprintf("<b>Commit-id:</b> <font color='#FF0000'> %.*s</font><br>", 8, sha)
}
ref, ok := os.LookupEnv("FORGEJO_REF")
if ok {
bdy += fmt.Sprintf("<b>Branch:</b> <font color='#00FF00f'>%s</font><br>", ref)
}
if d.Status != "" {
bdy += fmt.Sprintf("<b>Build Status:</b> <font color='#0000ff'>%s</font>", d.Status)
}
d.EventBody = bdy
}
tmpl, err := template.New("message").Parse(message)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, d)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}