From 2bd4696ea4c716023f0d48a233982c96baeac4a0 Mon Sep 17 00:00:00 2001 From: Oded Ben Ozer Date: Tue, 6 Aug 2024 10:30:52 +0200 Subject: [PATCH] Use "concise" diff comment in cases "regular" diff comment is > 65536 --- internal/pkg/githubapi/github.go | 14 +++++- .../argoCD-diff-pr-comment-concise.gotmpl | 47 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 templates/argoCD-diff-pr-comment-concise.gotmpl diff --git a/internal/pkg/githubapi/github.go b/internal/pkg/githubapi/github.go index c83eb96..76832c5 100644 --- a/internal/pkg/githubapi/github.go +++ b/internal/pkg/githubapi/github.go @@ -28,6 +28,8 @@ import ( "golang.org/x/exp/maps" ) +const githubCommentMaxSize = 65536 + type promotionInstanceMetaData struct { SourcePath string `json:"sourcePath"` TargetPaths []string `json:"targetPaths"` @@ -186,12 +188,20 @@ func HandlePREvent(eventPayload *github.PullRequestEvent, ghPrClientDetails GhPr err, templateOutput := executeTemplate(ghPrClientDetails.PrLogger, "argoCdDiff", "argoCD-diff-pr-comment.gotmpl", diffCommentData) if err != nil { prHandleError = err - log.Errorf("Failed to generate ArgoCD diff comment template: err=%s\n", err) + ghPrClientDetails.PrLogger.Errorf("Failed to generate ArgoCD diff comment template: err=%s\n", err) + } else if len(templateOutput) > githubCommentMaxSize { + ghPrClientDetails.PrLogger.Warnf("Diff comment is too large (%d bytes), using concise template", len(templateOutput)) + err, templateOutput = executeTemplate(ghPrClientDetails.PrLogger, "argoCdDiffConcise", "argoCD-diff-pr-comment-concise.gotmpl", diffCommentData) + if err != nil { + prHandleError = err + ghPrClientDetails.PrLogger.Errorf("Failed to generate ArgoCD diff comment template: err=%s\n", err) + } } + err = commentPR(ghPrClientDetails, templateOutput) if err != nil { prHandleError = err - log.Errorf("Failed to comment ArgoCD diff: err=%s\n", err) + ghPrClientDetails.PrLogger.Errorf("Failed to comment ArgoCD diff: err=%s\n", err) } } else { ghPrClientDetails.PrLogger.Debugf("Diff not find affected ArogCD apps") diff --git a/templates/argoCD-diff-pr-comment-concise.gotmpl b/templates/argoCD-diff-pr-comment-concise.gotmpl new file mode 100644 index 0000000..4a9fc14 --- /dev/null +++ b/templates/argoCD-diff-pr-comment-concise.gotmpl @@ -0,0 +1,47 @@ +{{define "argoCdDiffConcise"}} +Diff of ArgoCD applications(⚠️ concise view, full diff didn't fit GH comment): +{{ range $appDiffResult := .DiffOfChangedComponents }} + + +{{if $appDiffResult.DiffError }} +⚠️ ⚠️ **Error getting diff from ArgoCD** (`{{ $appDiffResult.ComponentPath }}`) ⚠️ ⚠️ +``` +{{ $appDiffResult.DiffError }} + +``` + +{{- else }} + **[{{ $appDiffResult.ArgoCdAppName }}]({{ $appDiffResult.ArgoCdAppURL }})** @ `{{ $appDiffResult.ComponentPath }}` +{{if $appDiffResult.HasDiff }} + +
ArgoCD list of changed objects(Click to expand): + +{{ range $objectDiff := $appDiffResult.DiffElements }} +{{- if $objectDiff.Diff}} +`{{ $objectDiff.ObjectNamespace }}/{{ $objectDiff.ObjectKind}}/{{ $objectDiff.ObjectName }}` +{{- end}} +{{- end }} + +
+{{- else }} +No diff 🤷 +{{- end}} +{{if $appDiffResult.AppWasTemporarilyCreated }} +⚠️ ⚠️ ⚠️ +This PR appears to create this new application, Telefonistka has **temporarly** created an ArgoCD app object for it just to render its manifests. +It will not be present in ArgoCD UI for more than a few seconds and it can not be synced from the PR branch +⚠️ ⚠️ ⚠️ +{{- end}} + +{{- end }} + +{{- end }} + +{{- if .HasSyncableComponens }} + +- [ ] Set ArgoCD apps Target Revision to `{{ .BranchName }}` + +{{ end}} + + +{{- end }}