package main
import (
"bytes"
"fmt"
"net/http"
"text/template"
"github.com/sethvargo/go-githubactions"
)
const (
defaultColor = "483d8b/6495ed"
successColor = "006400/228b22"
failedColor = "8b0000/dc143c"
)
type templateData struct {
Title string
SubTitle string
Author string
IconUrl string
Project string
Commit string
Branch string
Status string
ActionID string
GitURL string
Color string
Add string
EventBody string
}
func main() {
data := templateData{}
data.Add = ""
author := githubactions.GetInput("author")
if author == "" {
githubactions.Infof("[INFO] Missing input 'author', using default")
author = "STACKIT git action"
}
data.Author = author
title := githubactions.GetInput("title")
if title == "" {
githubactions.Infof("[INFO] Missing input 'title', using default")
title = "STACKIT git action"
}
data.Title = title
project := githubactions.GetInput("project")
if project == "" {
githubactions.Fatalf("Missing input 'project'")
}
data.Project = project
commit := githubactions.GetInput("commit")
if commit == "" {
githubactions.Fatalf("Missing input 'commit'")
}
data.Commit = commit
branch := githubactions.GetInput("branch")
if branch == "" {
githubactions.Fatalf("Missing input 'branch'")
}
data.Branch = branch
status := githubactions.GetInput("status")
if status == "" {
githubactions.Fatalf("Missing input 'status'")
}
data.Status = status
actionid := githubactions.GetInput("actionid")
if actionid == "" {
githubactions.Fatalf("Missing input 'actionid'")
}
data.ActionID = actionid
webhook := githubactions.GetInput("webhook")
if webhook == "" {
githubactions.Fatalf("Missing input 'webshook'")
}
giturl := githubactions.GetInput("giturl")
if webhook == "" {
githubactions.Fatalf("Missing input 'giturl'")
}
data.GitURL = giturl
body := githubactions.GetInput("event_body")
data.EventBody = body
fmt.Println("URL:> ", webhook)
// var jsonStr = []byte(fmt.Sprintf(data, project, commit, branch, status, actionid))
jsonStr, err := card(data)
if err != nil {
githubactions.Fatalf(err.Error())
}
req, err := http.NewRequest("POST", webhook, bytes.NewBuffer(jsonStr))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
}
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 = "https://github.githubassets.com/images/modules/logos_page/Octocat.png"
}
if d.EventBody == "" {
d.EventBody = "Project: {{ .Project }}
Commit-id: {{ .Commit }}
Branch: {{ .Branch }}
Build Status: {{ .Status }}"
}
//data := `{
// "cards": [
// {
// "header": {
// "title": "{{ .Title }}",
// "subtitle": "{{ .SubTitle }}",
// "imageUrl": "{{ .IconUrl }}",
// "imageStyle": "IMAGE"
// },
// "sections": [
// {
// "widgets": [
// {
// "textParagraph": {
// "text": "Project: {{ .Project }}
Commit-id: {{ .Commit }}
Branch: {{ .Branch }}
Build Status: {{ .Status }}"
// },
// "buttons": [
// {
// "textButton": {
// "text": "Job Details",
// "onClick": {
// "openLink": {
// "url": "{{ .GitURL }}/{{ .Project }}/actions/runs/{{ .ActionID }}"
// }
// }
// }
// }
// ]
// }
// ]
// }
// ]
// }
// ]
//}`
if d.Status == "success" {
d.Add = `
{
"decoratedText": {
"startIcon": {
"materialIcon": {
"name": "check_circle"
}
},
"text": "SUCCESS"
}
},`
}
if d.Status == "failure" {
d.Add = `
{
"decoratedText": {
"startIcon": {
"materialIcon": {
"name": "stop_circle"
}
},
"text": "FAILURE"
}
},`
}
tpl := `
{
"cardsV2": [
{
"cardId": "notify-{{ .ActionID }}",
"card": {
"header": {
"title": "{{ .Title }}",
"subtitle": "{{ .SubTitle }}",
"imageUrl": "{{ .IconUrl }}",
"imageType": "SQUARE"
},
"sections": [
{
"header": "{{ .Title }}",
"collapsible": false,
"widgets": [
{{ .Add }}
{
"decoratedText": {
"startIcon": {
"knownIcon": "PERSON"
},
"text": "{{ .Author }}"
}
},
{
"textParagraph": {
"text": "{{ .EventBody }}",
"maxLines": 2
}
}
]
},
{
"widgets": [
{
"buttonList": {
"buttons": [
{
"text": "View Source Event",
"type": "FILLED",
"onClick": {
"openLink": {
"url": "{{ .GitURL }}/{{ .Project }}/actions/runs/{{ .ActionID }}"
}
}
}
]
}
}
]
}
]
}
}
]
}
`
tmpl, err := template.New("").Parse(tpl)
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
}