From 0964d877eecd8020298f9c4f89a038c25b3ac12d Mon Sep 17 00:00:00 2001 From: Philipp Heuer Date: Sun, 28 Jul 2024 00:40:58 +0200 Subject: [PATCH] feat: add template func to remove issue mentions, add RenderCustom --- go.mod | 16 +++++++---- go.sum | 28 +++++++++---------- pkg/vcsapp/templateFunc.go | 15 ++++++++++ pkg/vcsapp/templateFunc_test.go | 24 ++++++++++++++++ pkg/vcsapp/{template.go => templateRender.go} | 7 ++++- pkg/vcsapp/templateRender_test.go | 16 +++++++++++ 6 files changed, 85 insertions(+), 21 deletions(-) create mode 100644 pkg/vcsapp/templateFunc.go create mode 100644 pkg/vcsapp/templateFunc_test.go rename pkg/vcsapp/{template.go => templateRender.go} (73%) create mode 100644 pkg/vcsapp/templateRender_test.go diff --git a/go.mod b/go.mod index 63f55f2..0643ea9 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,11 @@ go 1.21 require ( github.com/bradleyfalzon/ghinstallation/v2 v2.11.0 github.com/cidverse/go-ptr v0.0.0-20240331160646-489e694bebbf - github.com/cidverse/go-vcs v0.0.0-20240628180121-81fbf79b166a + github.com/cidverse/go-vcs v0.0.0-20240723214135-fa47fd4d8c9b github.com/go-git/go-git/v5 v5.12.0 github.com/google/go-github/v63 v63.0.0 github.com/rs/zerolog v1.33.0 + github.com/stretchr/testify v1.9.0 github.com/xanzy/go-gitlab v0.107.0 ) @@ -18,7 +19,8 @@ require ( github.com/Microsoft/go-winio v0.6.2 // indirect github.com/ProtonMail/go-crypto v1.0.0 // indirect github.com/cloudflare/circl v1.3.9 // indirect - github.com/cyphar/filepath-securejoin v0.2.5 // indirect + github.com/cyphar/filepath-securejoin v0.3.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/emirpasic/gods v1.18.1 // indirect github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect github.com/go-git/go-billy/v5 v5.5.0 // indirect @@ -33,13 +35,15 @@ require ( github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/pjbgf/sha1cd v0.3.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect - github.com/skeema/knownhosts v1.2.2 // indirect + github.com/skeema/knownhosts v1.3.0 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect - golang.org/x/crypto v0.24.0 // indirect - golang.org/x/net v0.26.0 // indirect + golang.org/x/crypto v0.25.0 // indirect + golang.org/x/net v0.27.0 // indirect golang.org/x/oauth2 v0.21.0 // indirect - golang.org/x/sys v0.21.0 // indirect + golang.org/x/sys v0.22.0 // indirect golang.org/x/time v0.5.0 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 5d5438d..ef49934 100644 --- a/go.sum +++ b/go.sum @@ -16,14 +16,14 @@ github.com/bradleyfalzon/ghinstallation/v2 v2.11.0/go.mod h1:0LWKQwOHewXO/1acI6T github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= github.com/cidverse/go-ptr v0.0.0-20240331160646-489e694bebbf h1:/DLETx+e0J8mWZyOOK8dAOMrdHUpJo8KQRuMt1IymtE= github.com/cidverse/go-ptr v0.0.0-20240331160646-489e694bebbf/go.mod h1:nvQPqid2KB17xXiiFp/Fhd+dO5lusCSuBQtOXfsd+mk= -github.com/cidverse/go-vcs v0.0.0-20240628180121-81fbf79b166a h1:+wM43AXrtOfs6VsYS2/1skVxgdJZMfZPv/gYREZa/48= -github.com/cidverse/go-vcs v0.0.0-20240628180121-81fbf79b166a/go.mod h1:Cvbn1Zx80b6rlt/t3CCEuQczd0t+jJjjR97MIG5SQr4= +github.com/cidverse/go-vcs v0.0.0-20240723214135-fa47fd4d8c9b h1:x6X1NQISWCIt2QREbQ6FnSxGs3bnT9d74AcMK/trq7w= +github.com/cidverse/go-vcs v0.0.0-20240723214135-fa47fd4d8c9b/go.mod h1:Cvbn1Zx80b6rlt/t3CCEuQczd0t+jJjjR97MIG5SQr4= github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= github.com/cloudflare/circl v1.3.9 h1:QFrlgFYf2Qpi8bSpVPK1HBvWpx16v/1TZivyo7pGuBE= github.com/cloudflare/circl v1.3.9/go.mod h1:PDRU+oXvdD7KCtgKxW95M5Z8BpSCJXQORiZFnBQS5QU= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= -github.com/cyphar/filepath-securejoin v0.2.5 h1:6iR5tXJ/e6tJZzzdMc1km3Sa7RRIVBKAK32O2s7AYfo= -github.com/cyphar/filepath-securejoin v0.2.5/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= +github.com/cyphar/filepath-securejoin v0.3.1 h1:1V7cHiaW+C+39wEfpH6XlLBQo3j/PciWFrgfCLS8XrE= +github.com/cyphar/filepath-securejoin v0.3.1/go.mod h1:F7i41x/9cBF7lzCrVsYs9fuzwRZm4NQsGTBdpp6mETc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -96,8 +96,8 @@ github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWR github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8= github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= -github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A= -github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= +github.com/skeema/knownhosts v1.3.0 h1:AM+y0rI04VksttfwjkSTNQorvGqmwATnvnAHpSgc0LY= +github.com/skeema/knownhosts v1.3.0/go.mod h1:sPINvnADmT/qYH1kfv+ePMmOBTH6Tbl7b5LvTDjFK7M= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= @@ -113,8 +113,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= -golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI= -golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM= +golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= +golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -124,8 +124,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= -golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= -golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= +golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -146,15 +146,15 @@ golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= -golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= -golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA= -golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0= +golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk= +golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/pkg/vcsapp/templateFunc.go b/pkg/vcsapp/templateFunc.go new file mode 100644 index 0000000..b418763 --- /dev/null +++ b/pkg/vcsapp/templateFunc.go @@ -0,0 +1,15 @@ +package vcsapp + +import ( + "regexp" +) + +var vcsPattern = regexp.MustCompile(`\s+\(#\d+\)$`) +var jiraPattern = regexp.MustCompile(`\s+\([A-Z]+-\d+\)$`) + +func RemoveIssueMentionFromMessage(text string) string { + text = vcsPattern.ReplaceAllString(text, "") + text = jiraPattern.ReplaceAllString(text, "") + + return text +} diff --git a/pkg/vcsapp/templateFunc_test.go b/pkg/vcsapp/templateFunc_test.go new file mode 100644 index 0000000..9e51fe7 --- /dev/null +++ b/pkg/vcsapp/templateFunc_test.go @@ -0,0 +1,24 @@ +package vcsapp + +import ( + "testing" +) + +func TestRemoveIssueMentionFromMessage(t *testing.T) { + tests := []struct { + input string + expected string + }{ + {"This is a test message (#123)", "This is a test message"}, + {"Another example (#4567)", "Another example"}, + {"No issue mentioned here", "No issue mentioned here"}, + {"Different Style (AB-1234)", "Different Style"}, + } + + for _, test := range tests { + result := RemoveIssueMentionFromMessage(test.input) + if result != test.expected { + t.Errorf("For input '%s', expected '%s', but got '%s'", test.input, test.expected, result) + } + } +} diff --git a/pkg/vcsapp/template.go b/pkg/vcsapp/templateRender.go similarity index 73% rename from pkg/vcsapp/template.go rename to pkg/vcsapp/templateRender.go index 829e2ff..bd4f8bc 100644 --- a/pkg/vcsapp/template.go +++ b/pkg/vcsapp/templateRender.go @@ -20,6 +20,7 @@ var templateFuncMap = template.FuncMap{ return false } }, + "removeIssueMentions": RemoveIssueMentionFromMessage, } func Render(tpl string, data interface{}) ([]byte, error) { @@ -27,7 +28,11 @@ func Render(tpl string, data interface{}) ([]byte, error) { } func RenderWithCustomDelimiter(tpl string, leftDelimiter string, rightDelimiter string, data interface{}) ([]byte, error) { - tmpl, err := template.New("template").Delims(leftDelimiter, rightDelimiter).Funcs(templateFuncMap).Parse(tpl) + return RenderCustom(tpl, leftDelimiter, rightDelimiter, data, nil) +} + +func RenderCustom(tpl string, leftDelimiter string, rightDelimiter string, data interface{}, customFuncs template.FuncMap) ([]byte, error) { + tmpl, err := template.New("template").Delims(leftDelimiter, rightDelimiter).Funcs(templateFuncMap).Funcs(customFuncs).Parse(tpl) if err != nil { return nil, fmt.Errorf("failed to parse template: %w", err) } diff --git a/pkg/vcsapp/templateRender_test.go b/pkg/vcsapp/templateRender_test.go new file mode 100644 index 0000000..e1deca2 --- /dev/null +++ b/pkg/vcsapp/templateRender_test.go @@ -0,0 +1,16 @@ +package vcsapp + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRender(t *testing.T) { + output, err := Render("Test Content: {{ .Content }}", map[string]interface{}{ + "Content": "Example", + }) + expected := "Test Content: Example" + assert.NoError(t, err) + assert.Equal(t, expected, string(output)) +}