Skip to content

Commit

Permalink
feat: support create comment on a pr
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Jan 6, 2023
1 parent 7a99b76 commit 2230f15
Show file tree
Hide file tree
Showing 19 changed files with 997 additions and 107 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ image:
build-workflow-executor-gogit:
cd cmd/argoworkflow && CGO_ENABLE=0 go build -ldflags "-w -s" -o ../../bin/workflow-executor-gogit
image-workflow-executor-gogit:
cd cmd/argoworkflow && docker build . -t ghcr.io/linuxsuren/workflow-executor-gogit
cd cmd/argoworkflow && docker build . -t ghcr.io/linuxsuren/workflow-executor-gogit:dev --build-arg GOPROXY=https://goproxy.io,direct
push-image-workflow-executor-gogit: image-workflow-executor-gogit
docker push ghcr.io/linuxsuren/workflow-executor-gogit:dev
test-workflow-executor-gogit:
cd cmd/argoworkflow && go test ./...
3 changes: 2 additions & 1 deletion cmd/argoworkflow/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
FROM golang:1.18 as builder
ARG GOPROXY=direct

WORKDIR /workspace
COPY . .

RUN go mod download
RUN CGO_ENABLE=0 go build -ldflags "-w -s" -o workflow-executor-gogit
RUN GOPROXY=${GOPROXY} CGO_ENABLE=0 go build -ldflags "-w -s" -o workflow-executor-gogit

FROM ubuntu:kinetic

Expand Down
16 changes: 13 additions & 3 deletions cmd/argoworkflow/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@ go 1.18

require (
github.com/argoproj/argo-workflows/v3 v3.4.4
github.com/linuxsuren/gogit v0.0.2-0.20221223013829-9a0df2fcbed4
github.com/linuxsuren/gogit v0.0.5-0.20230106063430-7d96e708cf7d
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.1
k8s.io/apimachinery v0.26.0
k8s.io/client-go v0.26.0
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.13 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
)

require (
code.gitea.io/sdk/gitea v0.14.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/bluekeyes/go-gitdiff v0.4.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
Expand Down Expand Up @@ -41,7 +53,6 @@ require (
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.1 // indirect
golang.org/x/net v0.3.1-0.20221206200815-1e63c2f08a10 // indirect
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
golang.org/x/sys v0.3.0 // indirect
Expand All @@ -56,7 +67,6 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.26.0 // indirect
k8s.io/apimachinery v0.26.0 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d // indirect
Expand Down
30 changes: 19 additions & 11 deletions cmd/argoworkflow/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
code.gitea.io/sdk/gitea v0.14.0 h1:m4J352I3p9+bmJUfS+g0odeQzBY/5OXP91Gv6D4fnJ0=
code.gitea.io/sdk/gitea v0.14.0/go.mod h1:89WiyOX1KEcvjP66sRHdu0RafojGo60bT9UqW17VbWs=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZCSYp4Z0m2dk6cEM60=
github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/argoproj/argo-workflows/v3 v3.4.4 h1:6ODs/SZNvbkUICG9pRHQPyHLV5L0ZSVhj7AwgUZA+jE=
github.com/argoproj/argo-workflows/v3 v3.4.4/go.mod h1:wYGIbAMl6BYUz5YeKbcwbEtjrvLyTsmbs9vWp6n7FGU=
Expand Down Expand Up @@ -65,12 +71,18 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw=
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
Expand All @@ -84,16 +96,13 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/linuxsuren/gogit v0.0.1 h1:5s4CP8VJ/Y4pbgJQyiGq6aeEhU2WgPd1a0vnIc5h47w=
github.com/linuxsuren/gogit v0.0.1/go.mod h1:TnBIafwTIHU8QxJSytFXzAOxGhbOsEgepkUo7bNkFUQ=
github.com/linuxsuren/gogit v0.0.2-0.20221222123617-1d1a87f89251 h1:KHs4dukN7Z3NCS3g2L87GqyonbDUnEQ8WVk6cozAxqg=
github.com/linuxsuren/gogit v0.0.2-0.20221222123617-1d1a87f89251/go.mod h1:jgD+pRy5I2/8CFc6MwsyI+1wQha7odLsB77i+x/IzGc=
github.com/linuxsuren/gogit v0.0.2-0.20221223013829-9a0df2fcbed4 h1:ye1D9dGJ+5ySV1ll5pZ5BkeDk6jikfVMi82cMmzTREE=
github.com/linuxsuren/gogit v0.0.2-0.20221223013829-9a0df2fcbed4/go.mod h1:jgD+pRy5I2/8CFc6MwsyI+1wQha7odLsB77i+x/IzGc=
github.com/linuxsuren/gogit v0.0.5-0.20230106063430-7d96e708cf7d h1:TDKAC8+UNSQt3YtQXkFdWxVQQUgcFHGEoWbOxmvDn5s=
github.com/linuxsuren/gogit v0.0.5-0.20230106063430-7d96e708cf7d/go.mod h1:Qo/oN8JkdigUQqxwU7yPzQdbvmp8dyKmhQHle1YzWoc=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
Expand All @@ -110,11 +119,7 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo/v2 v2.4.0 h1:+Ig9nvqgS5OBSACXNk15PLdp0U9XPYROt9CFzVdFGIs=
github.com/onsi/gomega v1.23.0 h1:/oxKu9c2HVap+F3PfKort2Hw5DEU+HGlW8n+tguWsys=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -150,6 +155,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
Expand Down Expand Up @@ -241,8 +248,8 @@ google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/h2non/gock.v1 v1.0.16 h1:F11k+OafeuFENsjei5t2vMTSTs9L62AdyTe4E1cgdG8=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
Expand All @@ -253,6 +260,7 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
82 changes: 66 additions & 16 deletions cmd/argoworkflow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"time"

wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
wfclientset "github.com/argoproj/argo-workflows/v3/pkg/client/clientset/versioned"
"github.com/argoproj/argo-workflows/v3/pkg/plugins/executor"
"github.com/linuxsuren/gogit/argoworkflow/template"
"github.com/linuxsuren/gogit/pkg"
"github.com/spf13/cobra"
"io"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"net/http"
"os"
"strconv"
"strings"
"time"
)

func main() {
Expand All @@ -39,6 +41,8 @@ func main() {
"The root URL of Argo Workflows UI")
flags.IntVarP(&opt.Port, "port", "", 3001,
"The port of the HTTP server")
flags.BoolVarP(&opt.CreateComment, "create-comment", "", false, "Indicate if want to create a status comment")
flags.StringVarP(&opt.CommentTemplate, "comment-template", "", "", "The template of the comment")
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
Expand All @@ -63,6 +67,9 @@ type option struct {
Token string
Port int

CreateComment bool
CommentTemplate string

Owner string
Repo string
PR string
Expand All @@ -80,8 +87,15 @@ type pluginOption struct {
Option *option `json:"gogit-executor-plugin"`
}

func (e *DefaultPluginExecutor) Execute(args executor.ExecuteTemplateArgs, name string, status wfv1.WorkflowStatus) (
func (e *DefaultPluginExecutor) Execute(args executor.ExecuteTemplateArgs, wf *wfv1.Workflow) (
resp executor.ExecuteTemplateResponse, err error) {
ctx := context.Background()
var name string
if wf.Spec.WorkflowTemplateRef != nil {
name = wf.Spec.WorkflowTemplateRef.Name
}
status := wf.Status

p := args.Template.Plugin.Value

opt := &pluginOption{Option: e.option}
Expand Down Expand Up @@ -132,18 +146,58 @@ func (e *DefaultPluginExecutor) Execute(args executor.ExecuteTemplateArgs, name

fmt.Println("send status", repo)
var nodeResult *wfv1.NodeResult
if err = pkg.Reconcile(context.Background(), repo); err == nil {
if err = pkg.CreateStatus(ctx, repo); err == nil {
nodeResult = &wfv1.NodeResult{
Phase: wfv1.NodeSucceeded,
Message: "success",
}
fmt.Println("send success")
fmt.Println("send status success")
} else {
fmt.Println("failed to send status", err)
}

if err == nil && opt.Option.CreateComment {
fmt.Println("start to create comment")
tplText := EmptyThen(opt.Option.CommentTemplate, template.CommentTemplate)

// find useless nodes
var toRemoves []string
for key, val := range wf.Status.Nodes {
if strings.HasSuffix(val.Name, ".onExit") || strings.Contains(val.Name, ".hooks.") {
toRemoves = append(toRemoves, key)
}
}
wf.Status.Phase = wfv1.WorkflowPhase(wf.Status.Nodes[wf.Name].Phase)
// remove useless nodes
delete(wf.Status.Nodes, wf.Name)
for _, key := range toRemoves {
delete(wf.Status.Nodes, key)
}

// put the workflow link into annotations
if wf.Annotations == nil {
wf.Annotations = map[string]string{}
}
wf.Annotations["workflow.link"] = targetAddress

var message string
message, err = template.RenderTemplate(tplText, wf)
if err == nil {
err = pkg.CreateComment(ctx, repo, message)
} else {
err = fmt.Errorf("failed to render comment template: %v", err)
}

if err != nil {
fmt.Println("failed to create comment", err)
}
}

if err != nil {
nodeResult = &wfv1.NodeResult{
Phase: wfv1.NodeFailed,
Message: err.Error(),
}
fmt.Println("failed to send", err)
}

resp = executor.ExecuteTemplateResponse{
Expand All @@ -156,7 +210,7 @@ func (e *DefaultPluginExecutor) Execute(args executor.ExecuteTemplateArgs, name

type PluginExecutor interface {
// Execute commands based on the args provided from the workflow
Execute(args executor.ExecuteTemplateArgs, name string, status wfv1.WorkflowStatus) (executor.ExecuteTemplateResponse, error)
Execute(args executor.ExecuteTemplateArgs, wf *wfv1.Workflow) (executor.ExecuteTemplateResponse, error)
}

var (
Expand Down Expand Up @@ -200,11 +254,7 @@ func plugin(p PluginExecutor, client *wfclientset.Clientset) func(w http.Respons
return
}

var name string
if workflow.Spec.WorkflowTemplateRef != nil {
name = workflow.Spec.WorkflowTemplateRef.Name
}
_, _ = p.Execute(args, name, workflow.Status)
_, _ = p.Execute(args, workflow)
}(client, args)

jsonResp, err := json.Marshal(executor.ExecuteTemplateReply{
Expand Down
12 changes: 12 additions & 0 deletions cmd/argoworkflow/template/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package template

// CommentTemplate is the default comment template
const CommentTemplate = `
{{.Spec.WorkflowTemplateRef.Name}} is {{.Status.Phase}}. It takes {{duration .Status.FinishedAt .Status.StartedAt}}. Please check log output from [here]({{get .Annotations "workflow.link"}}).
| Stage | Status | Duration |
|---|---|---|
{{- range $_, $status := .Status.Nodes}}
| {{$status.DisplayName}} | {{$status.Phase}} | {{duration $status.FinishedAt $status.StartedAt}} |
{{- end}}
`
70 changes: 70 additions & 0 deletions cmd/argoworkflow/template/comment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package template_test

import (
"testing"
"time"

"github.com/linuxsuren/gogit/argoworkflow/template"
"github.com/stretchr/testify/assert"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestCommentTemlate(t *testing.T) {
startTime := v1.Now()
endTime := v1.Time{Time: startTime.Add(time.Second * 5)}

node1 := map[string]interface{}{"Phase": "Success", "DisplayName": "node-1", "StartedAt": startTime, "FinishedAt": endTime, "StartedTime": "2023-01-06T07:49:07Z", "EndedTime": "2023-01-06T07:54:26Z"}
node2 := map[string]interface{}{"Phase": "Failed", "DisplayName": "node-2", "StartedAt": startTime, "FinishedAt": endTime, "StartedTime": "2023-01-06T07:49:07Z", "EndedTime": "2023-01-06T07:54:26Z"}
// TODO cannot handle the situation in template
// node3 := map[string]interface{}{"DisplayName": "node-2.OnExit"}
// node4 := map[string]interface{}{"DisplayName": "plugin-8bs4n.hooks.all"}

nodes := map[string]interface{}{}
nodes["node1"] = node1
nodes["node2"] = node2
// nodes["node3"] = node3
// nodes["node4"] = node4

status := map[string]interface{}{}
status["Nodes"] = nodes
status["Phase"] = "Failed"
status["StartedAt"] = startTime
status["FinishedAt"] = endTime

object := map[string]interface{}{}
object["Status"] = status
object["Annotations"] = map[string]string{
"workflow.link": "/~https://github.com/linxusuren/gogit",
}
object["Spec"] = map[string]interface{}{
"WorkflowTemplateRef": map[string]string{
"Name": "Sample",
},
}

result, err := template.RenderTemplate(template.CommentTemplate, object)
assert.Nil(t, err)
assert.Equal(t, `
Sample is Failed. It takes 5s. Please check log output from [here](/~https://github.com/linxusuren/gogit).
| Stage | Status | Duration |
|---|---|---|
| node-1 | Success | 5s |
| node-2 | Failed | 5s |
`, result)

result, err = template.RenderTemplate(`
| Stage | Status | Duration |
|---|---|---|
{{ range $node, $status := .Status.Nodes -}}
| {{$status.DisplayName}} | {{$status.Phase}} | {{durationStr $status.EndedTime $status.StartedTime}} |
{{end -}}
`, object)
assert.Nil(t, err)
assert.Equal(t, `
| Stage | Status | Duration |
|---|---|---|
| node-1 | Success | 5m19s |
| node-2 | Failed | 5m19s |
`, result)
}
Loading

0 comments on commit 2230f15

Please sign in to comment.