Skip to content

Commit

Permalink
fix: remove the duplicated comments (#26)
Browse files Browse the repository at this point in the history
Co-authored-by: rick <[email protected]>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored Mar 14, 2023
1 parent 62caf5f commit f8ded59
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
35 changes: 20 additions & 15 deletions cmd/argoworkflow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import (
"github.com/linuxsuren/gogit/pkg"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"

"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

func main() {
Expand All @@ -42,6 +45,7 @@ func main() {
"The root URL of Argo Workflows UI")
flags.IntVarP(&opt.Port, "port", "", 3001,
"The port of the HTTP server")
flags.StringVarP(&opt.KubeConfig, "kubeconfig", "", "", "The kubeconfig file path")
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")
flags.StringVarP(&opt.CommentIdentity, "comment-identity", "", pkg.CommentEndMarker, "The identity for matching exiting comment")
Expand All @@ -52,7 +56,7 @@ func main() {

func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
var config *rest.Config
if config, err = rest.InClusterConfig(); err != nil {
if config, err = clientcmd.BuildConfigFromFlags("", o.KubeConfig); err != nil {
return
}
client := wfclientset.NewForConfigOrDie(config)
Expand All @@ -63,11 +67,12 @@ func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
}

type option struct {
Provider string
Server string
Username string
Token string
Port int
Provider string
Server string
Username string
Token string
Port int
KubeConfig string

CreateComment bool
CommentTemplate string
Expand Down Expand Up @@ -141,16 +146,16 @@ func (e *DefaultPluginExecutor) Execute(args executor.ExecuteTemplateArgs, wf *w
}

fmt.Println("send status", repo)
var nodeResult *wfv1.NodeResult
if err = pkg.CreateStatus(ctx, repo); err == nil {
nodeResult = &wfv1.NodeResult{
Phase: wfv1.NodeSucceeded,
Message: "success",
}
fmt.Println("send status success")
} else {
fmt.Println("failed to send status", err)
wait.PollImmediate(time.Second*2, time.Second*30, func() (bool, error) {
err := pkg.CreateStatus(ctx, repo)
return err == nil, nil
})

nodeResult := &wfv1.NodeResult{
Phase: wfv1.NodeSucceeded,
Message: "success",
}
fmt.Println("send status success")

if err == nil && opt.Option.CreateComment {
fmt.Println("start to create comment")
Expand Down
14 changes: 9 additions & 5 deletions pkg/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,27 @@ func (s *StatusMaker) CreateComment(ctx context.Context, message, endMarker stri
}
}

commentID := -1
var commentIDs []int //:= -1
for _, comment := range comments {
if strings.HasSuffix(comment.Body, endMarker) {
commentID = comment.ID
break
commentIDs = append(commentIDs, comment.ID)
}
}

commentInput := &scm.CommentInput{
Body: fmt.Sprintf("%s\n\n%s", message, endMarker),
}

if commentID == -1 {
if len(commentIDs) == 0 {
// not found existing comment, create a new one
_, _, err = scmClient.PullRequests.CreateComment(ctx, s.repo, s.pr, commentInput)
} else {
_, _, err = scmClient.PullRequests.EditComment(ctx, s.repo, s.pr, commentID, commentInput)
_, _, err = scmClient.PullRequests.EditComment(ctx, s.repo, s.pr, commentIDs[0], commentInput)

// remove the duplicated comments
for i := 1; i < len(commentIDs); i++ {
_, _ = scmClient.PullRequests.DeleteComment(ctx, s.repo, s.pr, commentIDs[i])
}
}
return
}
Expand Down

0 comments on commit f8ded59

Please sign in to comment.