This commit is contained in:
parent
a87686cb73
commit
f486b95d02
2 changed files with 205 additions and 39 deletions
5
.github/workflows/example.yml
vendored
5
.github/workflows/example.yml
vendored
|
|
@ -18,9 +18,12 @@ jobs:
|
||||||
if: ${{ always() }}
|
if: ${{ always() }}
|
||||||
uses: https://stackit-solutions.git.onstackit.cloud/actions/notify@v0.1.0
|
uses: https://stackit-solutions.git.onstackit.cloud/actions/notify@v0.1.0
|
||||||
with:
|
with:
|
||||||
|
author: ${{ forgejo.actor }}
|
||||||
|
giturl: ${{ forgejo.server_url }}
|
||||||
project: ${{ forgejo.repository }}
|
project: ${{ forgejo.repository }}
|
||||||
commit: ${{ steps.slug.outputs.sha8 }}
|
commit: ${{ steps.slug.outputs.sha8 }}
|
||||||
branch: ${{ forgejo.ref }}
|
branch: ${{ forgejo.ref }}
|
||||||
status: ${{ job.status }}
|
status: ${{ job.status }}
|
||||||
actionid: ${{ forgejo.repository }}/actions/runs/${{ forgejo.run_id }}
|
actionid: ${{ forgejo.run_id }}
|
||||||
webhook: "${{ secrets.WEBHOOK_URL }}"
|
webhook: "${{ secrets.WEBHOOK_URL }}"
|
||||||
|
event_body: ""
|
||||||
|
|
|
||||||
239
main.go
239
main.go
|
|
@ -4,77 +4,104 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
"github.com/sethvargo/go-githubactions"
|
"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() {
|
func main() {
|
||||||
|
data := templateData{}
|
||||||
|
data.Add = ""
|
||||||
|
|
||||||
|
author := githubactions.GetInput("author")
|
||||||
|
if author == "" {
|
||||||
|
githubactions.Infof("Missing input 'author'")
|
||||||
|
author = "STACKIT git action"
|
||||||
|
}
|
||||||
|
data.Author = author
|
||||||
|
|
||||||
|
title := githubactions.GetInput("title")
|
||||||
|
if title == "" {
|
||||||
|
githubactions.Infof("Missing input 'title'")
|
||||||
|
title = "STACKIT git action"
|
||||||
|
}
|
||||||
|
data.Title = title
|
||||||
|
|
||||||
project := githubactions.GetInput("project")
|
project := githubactions.GetInput("project")
|
||||||
if project == "" {
|
if project == "" {
|
||||||
githubactions.Fatalf("Missing input 'project'")
|
githubactions.Fatalf("Missing input 'project'")
|
||||||
}
|
}
|
||||||
|
data.Project = project
|
||||||
|
|
||||||
commit := githubactions.GetInput("commit")
|
commit := githubactions.GetInput("commit")
|
||||||
if commit == "" {
|
if commit == "" {
|
||||||
githubactions.Fatalf("Missing input 'commit'")
|
githubactions.Fatalf("Missing input 'commit'")
|
||||||
}
|
}
|
||||||
|
data.Commit = commit
|
||||||
|
|
||||||
branch := githubactions.GetInput("branch")
|
branch := githubactions.GetInput("branch")
|
||||||
if branch == "" {
|
if branch == "" {
|
||||||
githubactions.Fatalf("Missing input 'branch'")
|
githubactions.Fatalf("Missing input 'branch'")
|
||||||
}
|
}
|
||||||
|
data.Branch = branch
|
||||||
|
|
||||||
status := githubactions.GetInput("status")
|
status := githubactions.GetInput("status")
|
||||||
if status == "" {
|
if status == "" {
|
||||||
githubactions.Fatalf("Missing input 'status'")
|
githubactions.Fatalf("Missing input 'status'")
|
||||||
}
|
}
|
||||||
|
data.Status = status
|
||||||
|
|
||||||
actionid := githubactions.GetInput("actionid")
|
actionid := githubactions.GetInput("actionid")
|
||||||
if actionid == "" {
|
if actionid == "" {
|
||||||
githubactions.Fatalf("Missing input 'actionid'")
|
githubactions.Fatalf("Missing input 'actionid'")
|
||||||
}
|
}
|
||||||
|
data.ActionID = actionid
|
||||||
|
|
||||||
webhook := githubactions.GetInput("webhook")
|
webhook := githubactions.GetInput("webhook")
|
||||||
if webhook == "" {
|
if webhook == "" {
|
||||||
githubactions.Fatalf("Missing input 'webshook'")
|
githubactions.Fatalf("Missing input 'webshook'")
|
||||||
}
|
}
|
||||||
joburl := githubactions.GetInput("joburl")
|
|
||||||
|
giturl := githubactions.GetInput("giturl")
|
||||||
if webhook == "" {
|
if webhook == "" {
|
||||||
githubactions.Fatalf("Missing input 'joburl'")
|
githubactions.Fatalf("Missing input 'giturl'")
|
||||||
}
|
}
|
||||||
|
data.GitURL = giturl
|
||||||
|
|
||||||
|
body := githubactions.GetInput("event_body")
|
||||||
|
data.EventBody = body
|
||||||
|
|
||||||
fmt.Println("URL:> ", webhook)
|
fmt.Println("URL:> ", webhook)
|
||||||
data := `{
|
|
||||||
"cards": [
|
// var jsonStr = []byte(fmt.Sprintf(data, project, commit, branch, status, actionid))
|
||||||
{
|
|
||||||
"header": {
|
jsonStr, err := card(data)
|
||||||
"title": "GitHub Action",
|
if err != nil {
|
||||||
"subtitle": "Build Job",
|
githubactions.Fatalf(err.Error())
|
||||||
"imageUrl": "https://github.githubassets.com/images/modules/logos_page/Octocat.png",
|
}
|
||||||
"imageStyle": "IMAGE"
|
|
||||||
},
|
|
||||||
"sections": [
|
|
||||||
{
|
|
||||||
"widgets": [
|
|
||||||
{
|
|
||||||
"textParagraph": {
|
|
||||||
"text": "<b>Project:</b> %s<br><b>Commit-id:</b> <font color=\"#FF0000\">%s</font><br><b>Branch:</b> <font color=\"#00FF00f\">%s</font><br><b>Build Status:</b> <font color=\"#0000ff\">%s</font>"
|
|
||||||
},
|
|
||||||
"buttons": [
|
|
||||||
{
|
|
||||||
"textButton": {
|
|
||||||
"text": "Job Details",
|
|
||||||
"onClick": {
|
|
||||||
"openLink": {
|
|
||||||
"url": "https://github.com/%s"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}`
|
|
||||||
var jsonStr = []byte(fmt.Sprintf(data, project, commit, branch, status, actionid))
|
|
||||||
req, err := http.NewRequest("POST", webhook, bytes.NewBuffer(jsonStr))
|
req, err := http.NewRequest("POST", webhook, bytes.NewBuffer(jsonStr))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
@ -88,3 +115,139 @@ func main() {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
fmt.Println("response Status:", resp.Status)
|
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 = "<b>Project:</b> {{ .Project }}<br><b>Commit-id:</b> <font color=\"#FF0000\">{{ .Commit }}</font><br><b>Branch:</b> <font color=\"#00FF00f\">{{ .Branch }}</font><br><b>Build Status:</b> <font color=\"#0000ff\">{{ .Status }}</font>"
|
||||||
|
}
|
||||||
|
|
||||||
|
// data := `{
|
||||||
|
// "cards": [
|
||||||
|
// {
|
||||||
|
// "header": {
|
||||||
|
// "title": "{{ .Title }}",
|
||||||
|
// "subtitle": "{{ .SubTitle }}",
|
||||||
|
// "imageUrl": "{{ .IconUrl }}",
|
||||||
|
// "imageStyle": "IMAGE"
|
||||||
|
// },
|
||||||
|
// "sections": [
|
||||||
|
// {
|
||||||
|
// "widgets": [
|
||||||
|
// {
|
||||||
|
// "textParagraph": {
|
||||||
|
// "text": "<b>Project:</b> {{ .Project }}<br><b>Commit-id:</b> <font color=\"#FF0000\">{{ .Commit }}</font><br><b>Branch:</b> <font color=\"#00FF00f\">{{ .Branch }}</font><br><b>Build Status:</b> <font color=\"#0000ff\">{{ .Status }}</font>"
|
||||||
|
// },
|
||||||
|
// "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\": \"<b style='color: green;'>SUCCESS</b>\"}},"
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.Status == "failure" {
|
||||||
|
d.Add = "{\"decoratedText\": {\"startIcon\": {\"materialIcon\": {\"name\": \"stop_circle\"}},\"text\": \"<b style='color: red;'>FAILURE</b>\"}},"
|
||||||
|
}
|
||||||
|
|
||||||
|
tpl := `
|
||||||
|
{
|
||||||
|
"cardsV2": [
|
||||||
|
{
|
||||||
|
"cardId": "notify-${{ github.run_id }}",
|
||||||
|
"card": {
|
||||||
|
"header": {
|
||||||
|
"title": "{{ .Title }}",
|
||||||
|
"subtitle": "{{ .SubTitle }}",
|
||||||
|
"imageUrl": "{{ .IconUrl }}",
|
||||||
|
"imageStyle": "IMAGE"
|
||||||
|
"imageType": "SQUARE"
|
||||||
|
},
|
||||||
|
"sections": [
|
||||||
|
{
|
||||||
|
"header": "{{ .Title }}",
|
||||||
|
"collapsible": false,
|
||||||
|
"widgets": [
|
||||||
|
{{ .Add }}
|
||||||
|
{
|
||||||
|
"decoratedText": {
|
||||||
|
"startIcon": {
|
||||||
|
"knownIcon": "PERSON"
|
||||||
|
},
|
||||||
|
"text": "<b>{{ .Author }}</b>"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue