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
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
githubactions.Fatalf(err.Error())
|
||||
githubactions.Fatalf("error %s", err.Error())
|
||||
}
|
||||
|
||||
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>"
|
||||
}
|
||||
|
||||
//data := `{
|
||||
// "cards": [
|
||||
// tpl := `
|
||||
//{
|
||||
// "cards": [
|
||||
// {
|
||||
// "header": {
|
||||
// "title": "{{ .Title }}",
|
||||
// "subtitle": "{{ .SubTitle }}",
|
||||
// "imageUrl": "{{ .IconUrl }}",
|
||||
// "imageStyle": "IMAGE"
|
||||
// },
|
||||
// "sections": [
|
||||
// {
|
||||
// "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": [
|
||||
// {
|
||||
// "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 }}"
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// ]
|
||||
// "textButton": {
|
||||
// "text": "Job Details",
|
||||
// "onClick": {
|
||||
// "openLink": {
|
||||
// "url": "{{ .GitURL }}/{{ .Project }}/actions/runs/{{ .ActionID }}"
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// ]
|
||||
// ]
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// ]
|
||||
// ]
|
||||
// }
|
||||
// ]
|
||||
//}`
|
||||
|
||||
if d.Status == "success" {
|
||||
|
|
@ -199,64 +200,64 @@ func card(d templateData) ([]byte, error) {
|
|||
}
|
||||
|
||||
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": "<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 }}"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
`
|
||||
{
|
||||
"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": "<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
|
||||
|
|
|
|||
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