Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sd 805 improve diff comment #32

Merged
merged 5 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions internal/pkg/githubapi/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ const (
)

type DiffCommentData struct {
DiffOfChangedComponents []argocd.DiffResult
HasSyncableComponents bool
BranchName string
Header string
DiffOfChangedComponents []argocd.DiffResult
DisplaySyncBranchCheckBox bool
BranchName string
Header string
}

type promotionInstanceMetaData struct {
Expand Down Expand Up @@ -104,6 +104,30 @@ func (ghPrClientDetails *GhPrClientDetails) getBlameURLPrefix() string {
return fmt.Sprintf("%s/%s/%s/blame", githubHost, ghPrClientDetails.Owner, ghPrClientDetails.Repo)
}

// This function is used to check if we should display the sync branch checkbox in the PR comment.
Oded-B marked this conversation as resolved.
Show resolved Hide resolved
// This depends on the allowSyncfromBranchPathRegex configuration and whether the component is new or not.
func shouldSyncBranchCheckBoxBeDisplayed(componentPathList []string, allowSyncfromBranchPathRegex string, diffOfChangedComponents []argocd.DiffResult) bool {
var displaySyncBranchCheckBox bool

out:
Oded-B marked this conversation as resolved.
Show resolved Hide resolved
for _, componentPath := range componentPathList {
if isSyncFromBranchAllowedForThisPath(allowSyncfromBranchPathRegex, componentPath) {
Oded-B marked this conversation as resolved.
Show resolved Hide resolved
// Here we are checking if the syncable component is not a new app that was temporarily created.
// We don't support syncing new apps from branches
for _, diffOfChangedComponent := range diffOfChangedComponents {
if diffOfChangedComponent.ComponentPath == componentPath {
if !diffOfChangedComponent.AppWasTemporarilyCreated {
Oded-B marked this conversation as resolved.
Show resolved Hide resolved
displaySyncBranchCheckBox = true
break out
}
}
}
}
}

return displaySyncBranchCheckBox
}

func HandlePREvent(eventPayload *github.PullRequestEvent, ghPrClientDetails GhPrClientDetails, mainGithubClientPair GhClientPair, approverGithubClientPair GhClientPair, ctx context.Context) {
ghPrClientDetails.getPrMetadata(eventPayload.PullRequest.GetBody())
// wasCommitStatusSet and the placement of SetCommitStatus in the flow is used to ensure an API call is only made where it needed
Expand Down Expand Up @@ -192,12 +216,8 @@ func HandlePREvent(eventPayload *github.PullRequestEvent, ghPrClientDetails GhPr
BranchName: ghPrClientDetails.Ref,
}

for _, componentPath := range componentPathList {
if isSyncFromBranchAllowedForThisPath(config.Argocd.AllowSyncfromBranchPathRegex, componentPath) {
diffCommentData.HasSyncableComponents = true
break
}
}
diffCommentData.DisplaySyncBranchCheckBox = shouldSyncBranchCheckBoxBeDisplayed(componentPathList, config.Argocd.AllowSyncfromBranchPathRegex, diffOfChangedComponents)

comments, err := generateArgoCdDiffComments(diffCommentData, githubCommentMaxSize)
if err != nil {
prHandleError = err
Expand Down
54 changes: 54 additions & 0 deletions internal/pkg/githubapi/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/wayfair-incubator/telefonistka/internal/pkg/argocd"
)

func TestGenerateSafePromotionBranchName(t *testing.T) {
Expand Down Expand Up @@ -289,3 +290,56 @@ func TestGhPrClientDetailsGetBlameURLPrefix(t *testing.T) {
assert.Equal(t, tc.ExpectURL, blameURLPrefix)
}
}

func TestShouldSyncBranchCheckBoxBeDisplayed(t *testing.T) {
t.Parallel()
tests := map[string]struct {
componentPathList []string
allowSyncfromBranchPathRegex string
diffOfChangedComponents []argocd.DiffResult
expected bool
}{
"New App": {
componentPathList: []string{"workspace/app1"},
allowSyncfromBranchPathRegex: `^workspace/.*$`,
diffOfChangedComponents: []argocd.DiffResult{
{
AppWasTemporarilyCreated: true,
ComponentPath: "workspace/app1",
},
},
expected: false,
},
"Existing App": {
componentPathList: []string{"workspace/app1"},
allowSyncfromBranchPathRegex: `^workspace/.*$`,
diffOfChangedComponents: []argocd.DiffResult{
{
AppWasTemporarilyCreated: false,
ComponentPath: "workspace/app1",
},
},
expected: true,
},
"Mixed New and Existing Apps": {
componentPathList: []string{"workspace/app1", "workspace/app2"},
allowSyncfromBranchPathRegex: `^workspace/.*$`,
diffOfChangedComponents: []argocd.DiffResult{
{
AppWasTemporarilyCreated: false,
ComponentPath: "workspace/app1",
},
{
AppWasTemporarilyCreated: true,
ComponentPath: "workspace/app2",
},
},
expected: true,
},
}

for i, tc := range tests {
result := shouldSyncBranchCheckBoxBeDisplayed(tc.componentPathList, tc.allowSyncfromBranchPathRegex, tc.diffOfChangedComponents)
assert.Equal(t, tc.expected, result, i)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"AppWasTemporarilyCreated": false
}
],
"HasSyncableComponents": false,
"DisplaySyncBranchCheckBox": false,
"BranchName": "promotions/284-simulate-error-5c159151017f",
"Header": ""
}
13 changes: 7 additions & 6 deletions templates/argoCD-diff-pr-comment-concise.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Diff of ArgoCD applications(⚠️ concise view, full diff didn't fit GH comment


{{if $appDiffResult.DiffError }}
⚠️ ⚠️ **Error getting diff from ArgoCD** (`{{ $appDiffResult.ComponentPath }}`) ⚠️ ⚠️
> [!CAUTION]
> **Error getting diff from ArgoCD** (`{{ $appDiffResult.ComponentPath }}`)

```
{{ $appDiffResult.DiffError }}

Expand All @@ -27,17 +29,16 @@ Diff of ArgoCD applications(⚠️ concise view, full diff didn't fit GH comment
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
⚠️ ⚠️ ⚠️
> [!NOTE]
> 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.
{{- end}}

{{- end }}

{{- end }}

{{- if .HasSyncableComponents }}
{{- if .DisplaySyncBranchCheckBox }}

- [ ] <!-- telefonistka-argocd-branch-sync --> Set ArgoCD apps Target Revision to `{{ .BranchName }}`

Expand Down
17 changes: 10 additions & 7 deletions templates/argoCD-diff-pr-comment.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ Diff of ArgoCD applications:


{{if $appDiffResult.DiffError }}
⚠️ ⚠️ **Error getting diff from ArgoCD** (`{{ $appDiffResult.ComponentPath }}`) ⚠️ ⚠️
> [!CAUTION]
> **Error getting diff from ArgoCD** (`{{ $appDiffResult.ComponentPath }}`)

Please check the App Conditions of <img src="https://argo-cd.readthedocs.io/en/stable/assets/favicon.png" width="20"/> **[{{ $appDiffResult.ArgoCdAppName }}]({{ $appDiffResult.ArgoCdAppURL }})** for more details.
{{- if $appDiffResult.AppWasTemporarilyCreated }}
⚠️ For investigation we kept the temporary application, please make sure to clean it up later! ⚠️
> [!WARNING]
> For investigation we kept the temporary application, please make sure to clean it up later!

{{- end}}
```
{{ $appDiffResult.DiffError }}
Expand All @@ -37,17 +41,16 @@ Please check the App Conditions of <img src="https://argo-cd.readthedocs.io/en/s
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
⚠️ ⚠️ ⚠️
> [!NOTE]
> 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.
{{- end}}

{{- end }}

{{- end }}

{{- if .HasSyncableComponents }}
{{- if .DisplaySyncBranchCheckBox }}

- [ ] <!-- telefonistka-argocd-branch-sync --> Set ArgoCD apps Target Revision to `{{ .BranchName }}`

Expand Down
Loading