This commit is contained in:
parent
f2566ae21d
commit
0303b4b9aa
3 changed files with 232 additions and 90 deletions
5
go.mod
5
go.mod
|
|
@ -2,4 +2,7 @@ module google-chat-action
|
||||||
|
|
||||||
go 1.26.2
|
go 1.26.2
|
||||||
|
|
||||||
require github.com/sethvargo/go-githubactions v1.3.2
|
require (
|
||||||
|
github.com/google/go-cmp v0.7.0
|
||||||
|
github.com/sethvargo/go-githubactions v1.3.2
|
||||||
|
)
|
||||||
|
|
|
||||||
179
main.go
179
main.go
|
|
@ -99,7 +99,7 @@ func main() {
|
||||||
|
|
||||||
jsonStr, err := card(data)
|
jsonStr, err := card(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
githubactions.Fatalf(err.Error())
|
githubactions.Fatalf("error %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", webhook, bytes.NewBuffer(jsonStr))
|
req, err := http.NewRequest("POST", webhook, bytes.NewBuffer(jsonStr))
|
||||||
|
|
@ -134,40 +134,41 @@ func card(d templateData) ([]byte, error) {
|
||||||
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>"
|
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 := `{
|
// tpl := `
|
||||||
// "cards": [
|
//{
|
||||||
|
// "cards": [
|
||||||
|
// {
|
||||||
|
// "header": {
|
||||||
|
// "title": "{{ .Title }}",
|
||||||
|
// "subtitle": "{{ .SubTitle }}",
|
||||||
|
// "imageUrl": "{{ .IconUrl }}",
|
||||||
|
// "imageStyle": "IMAGE"
|
||||||
|
// },
|
||||||
|
// "sections": [
|
||||||
// {
|
// {
|
||||||
// "header": {
|
// "widgets": [
|
||||||
// "title": "{{ .Title }}",
|
// {
|
||||||
// "subtitle": "{{ .SubTitle }}",
|
// "textParagraph": {
|
||||||
// "imageUrl": "{{ .IconUrl }}",
|
// "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>"
|
||||||
// "imageStyle": "IMAGE"
|
// },
|
||||||
// },
|
// "buttons": [
|
||||||
// "sections": [
|
|
||||||
// {
|
// {
|
||||||
// "widgets": [
|
// "textButton": {
|
||||||
// {
|
// "text": "Job Details",
|
||||||
// "textParagraph": {
|
// "onClick": {
|
||||||
// "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>"
|
// "openLink": {
|
||||||
// },
|
// "url": "{{ .GitURL }}/{{ .Project }}/actions/runs/{{ .ActionID }}"
|
||||||
// "buttons": [
|
// }
|
||||||
// {
|
// }
|
||||||
// "textButton": {
|
// }
|
||||||
// "text": "Job Details",
|
|
||||||
// "onClick": {
|
|
||||||
// "openLink": {
|
|
||||||
// "url": "{{ .GitURL }}/{{ .Project }}/actions/runs/{{ .ActionID }}"
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// ]
|
|
||||||
// }
|
|
||||||
// ]
|
|
||||||
// }
|
// }
|
||||||
// ]
|
// ]
|
||||||
|
// }
|
||||||
|
// ]
|
||||||
// }
|
// }
|
||||||
// ]
|
// ]
|
||||||
|
// }
|
||||||
|
// ]
|
||||||
//}`
|
//}`
|
||||||
|
|
||||||
if d.Status == "success" {
|
if d.Status == "success" {
|
||||||
|
|
@ -199,64 +200,64 @@ func card(d templateData) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
tpl := `
|
tpl := `
|
||||||
{
|
{
|
||||||
"cardsV2": [
|
"cardsV2": [
|
||||||
{
|
{
|
||||||
"cardId": "notify-{{ .ActionID }}",
|
"cardId": "notify-{{ .ActionID }}",
|
||||||
"card": {
|
"card": {
|
||||||
"header": {
|
"header": {
|
||||||
"title": "{{ .Title }}",
|
"title": "{{ .Title }}",
|
||||||
"subtitle": "{{ .SubTitle }}",
|
"subtitle": "{{ .SubTitle }}",
|
||||||
"imageUrl": "{{ .IconUrl }}",
|
"imageUrl": "{{ .IconUrl }}",
|
||||||
"imageType": "SQUARE"
|
"imageType": "SQUARE"
|
||||||
},
|
},
|
||||||
"sections": [
|
"sections": [
|
||||||
{
|
{
|
||||||
"header": "{{ .Title }}",
|
"header": "{{ .Title }}",
|
||||||
"collapsible": false,
|
"collapsible": false,
|
||||||
"widgets": [
|
"widgets": [
|
||||||
{{ .Add }}
|
{{ .Add }}
|
||||||
{
|
{
|
||||||
"decoratedText": {
|
"decoratedText": {
|
||||||
"startIcon": {
|
"startIcon": {
|
||||||
"knownIcon": "PERSON"
|
"knownIcon": "PERSON"
|
||||||
},
|
},
|
||||||
"text": "<b>{{ .Author }}</b>"
|
"text": "<b>{{ .Author }}</b>"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"textParagraph": {
|
"textParagraph": {
|
||||||
"text": "{{ .EventBody }}",
|
"text": "{{ .EventBody }}",
|
||||||
"maxLines": 2
|
"maxLines": 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"widgets": [
|
"widgets": [
|
||||||
{
|
{
|
||||||
"buttonList": {
|
"buttonList": {
|
||||||
"buttons": [
|
"buttons": [
|
||||||
{
|
{
|
||||||
"text": "View Source Event",
|
"text": "View Source Event",
|
||||||
"type": "FILLED",
|
"type": "FILLED",
|
||||||
"onClick": {
|
"onClick": {
|
||||||
"openLink": {
|
"openLink": {
|
||||||
"url": "{{ .GitURL }}/{{ .Project }}/actions/runs/{{ .ActionID }}"
|
"url": "{{ .GitURL }}/{{ .Project }}/actions/runs/{{ .ActionID }}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
tmpl, err := template.New("").Parse(tpl)
|
tmpl, err := template.New("").Parse(tpl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
138
main_test.go
Normal file
138
main_test.go
Normal file
|
|
@ -0,0 +1,138 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_card(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
d templateData
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want []byte
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "test one",
|
||||||
|
args: args{
|
||||||
|
d: templateData{
|
||||||
|
Title: "test",
|
||||||
|
SubTitle: "first test",
|
||||||
|
Author: "gotest",
|
||||||
|
IconUrl: "https://github.githubassets.com/images/modules/logos_page/Octocat.png",
|
||||||
|
Project: "proj",
|
||||||
|
Commit: "12345",
|
||||||
|
Branch: "main",
|
||||||
|
Status: "success",
|
||||||
|
ActionID: "123",
|
||||||
|
GitURL: "https://test.url",
|
||||||
|
Color: "c0c0c0",
|
||||||
|
EventBody: "no body",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: []byte(`
|
||||||
|
{
|
||||||
|
"cardsV2": [
|
||||||
|
{
|
||||||
|
"cardId": "notify-123",
|
||||||
|
"card": {
|
||||||
|
"header": {
|
||||||
|
"title": "test",
|
||||||
|
"subtitle": "first test",
|
||||||
|
"imageUrl": "https://github.githubassets.com/images/modules/logos_page/Octocat.png",
|
||||||
|
"imageType": "SQUARE"
|
||||||
|
},
|
||||||
|
"sections": [
|
||||||
|
{
|
||||||
|
"header": "test",
|
||||||
|
"collapsible": false,
|
||||||
|
"widgets": [
|
||||||
|
|
||||||
|
{
|
||||||
|
"decoratedText": {
|
||||||
|
"startIcon": {
|
||||||
|
"materialIcon": {
|
||||||
|
"name": "check_circle"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"text": "<b style=\"color: green;\">SUCCESS</b>"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"decoratedText": {
|
||||||
|
"startIcon": {
|
||||||
|
"knownIcon": "PERSON"
|
||||||
|
},
|
||||||
|
"text": "<b>gotest</b>"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"textParagraph": {
|
||||||
|
"text": "no body",
|
||||||
|
"maxLines": 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"widgets": [
|
||||||
|
{
|
||||||
|
"buttonList": {
|
||||||
|
"buttons": [
|
||||||
|
{
|
||||||
|
"text": "View Source Event",
|
||||||
|
"type": "FILLED",
|
||||||
|
"onClick": {
|
||||||
|
"openLink": {
|
||||||
|
"url": "https://test.url/proj/actions/runs/123"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
`),
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := card(tt.args.d)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("card() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var gotJson any
|
||||||
|
err = json.Unmarshal(got, &gotJson)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var wantJson any
|
||||||
|
err = json.Unmarshal(tt.want, &wantJson)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
//if !reflect.DeepEqual(wantJson, gotJson) {
|
||||||
|
// t.Errorf("card() got = %v, want %v", gotJson, wantJson)
|
||||||
|
//}
|
||||||
|
if diff := cmp.Diff(wantJson, gotJson); diff != "" {
|
||||||
|
t.Errorf("MakeGatewayInfo() mismatch (-want +got):\n%s", diff)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue