From 0303b4b9aafbfaab6019b31480407a993d319be7 Mon Sep 17 00:00:00 2001 From: "Marcel S. Henselin" Date: Thu, 16 Apr 2026 11:09:51 +0200 Subject: [PATCH] chore: use Dockerfile --- go.mod | 5 +- main.go | 179 ++++++++++++++++++++++++++------------------------- main_test.go | 138 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 232 insertions(+), 90 deletions(-) create mode 100644 main_test.go diff --git a/go.mod b/go.mod index a935e06..fc33f46 100644 --- a/go.mod +++ b/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 +) diff --git a/main.go b/main.go index de1485c..d9360bb 100644 --- a/main.go +++ b/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 = "Project: {{ .Project }}
Commit-id: {{ .Commit }}
Branch: {{ .Branch }}
Build Status: {{ .Status }}" } - //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": "Project: {{ .Project }}
Commit-id: {{ .Commit }}
Branch: {{ .Branch }}
Build Status: {{ .Status }}" + // }, + // "buttons": [ // { - // "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 }}" - // } - // } - // } - // } - // ] - // } - // ] + // "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": "{{ .Author }}" - } - }, - { - "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": "{{ .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 diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..c7bcd29 --- /dev/null +++ b/main_test.go @@ -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": "SUCCESS" +} +}, + { + "decoratedText": { + "startIcon": { + "knownIcon": "PERSON" + }, + "text": "gotest" + } + }, + { + "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) + } + }) + } +}