Skip to content

Commit

Permalink
Only check for checkbox event on **updates** on comment the bot created.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oded-B committed Jul 1, 2024
1 parent e4cbcad commit 0c0d06f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
2 changes: 2 additions & 0 deletions internal/pkg/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ func SetArgoCDAppRevision(ctx context.Context, componentPath string, revision st
})
if err != nil {
return fmt.Errorf("Error setting app %s revision to %s failed: %v", foundApp.Name, revision, err)
} else {
log.Infof("ArgoCD App %s revision set to %s", foundApp.Name, revision)
}

return nil
Expand Down
56 changes: 29 additions & 27 deletions internal/pkg/githubapi/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ func handleEvent(eventPayloadInterface interface{}, mainGhClientCache *lru.Cache
"repo": *eventPayload.Repo.Owner.Login + "/" + *eventPayload.Repo.Name,
"prNumber": *eventPayload.Issue.Number,
})
// Ignore comment even sent by the bot (this is about who trigger the event not who wrote the comment)
if *eventPayload.Sender.Login != botIdentity {
ghPrClientDetails := GhPrClientDetails{
Ctx: ctx,
Expand All @@ -333,7 +334,7 @@ func handleEvent(eventPayloadInterface interface{}, mainGhClientCache *lru.Cache
PrAuthor: *eventPayload.Issue.User.Login,
PrLogger: prLogger,
}
_ = handleCommentPrEvent(ghPrClientDetails, eventPayload)
_ = handleCommentPrEvent(ghPrClientDetails, eventPayload, botIdentity)
} else {
log.Debug("Ignoring self comment")
}
Expand Down Expand Up @@ -374,7 +375,7 @@ func isSyncFromBranchAllowedForThisPath(allowedPathRegex string, path string) bo
return allowedPathsRegex.MatchString(path)
}

func handleCommentPrEvent(ghPrClientDetails GhPrClientDetails, ce *github.IssueCommentEvent) error {
func handleCommentPrEvent(ghPrClientDetails GhPrClientDetails, ce *github.IssueCommentEvent, botIdentity string) error {
defaultBranch, _ := ghPrClientDetails.GetDefaultBranch()
config, err := GetInRepoConfig(ghPrClientDetails, defaultBranch)
if err != nil {
Expand All @@ -384,39 +385,40 @@ func handleCommentPrEvent(ghPrClientDetails GhPrClientDetails, ce *github.IssueC
_, _ = ghPrClientDetails.GetRef()
_, _ = ghPrClientDetails.GetSHA()

checkboxPattern := `(?m)^\s*-\s*\[(.)\]\s*<!-- telefonistka-argocd-branch-sync -->.*$`
ghPrClientDetails.PrLogger.Debugf("=== old body: %s\n", *ce.Changes.Body.From)
ghPrClientDetails.PrLogger.Debugf("=== new body: %s\n", *ce.Comment.Body)
checkboxWaschecked, checkboxIsChecked := analyzeCommentUpdateCheckBox(*ce.Comment.Body, *ce.Changes.Body.From, checkboxPattern)
if !checkboxWaschecked && checkboxIsChecked {
ghPrClientDetails.PrLogger.Infof("Sync Checkbox was checked")
if config.AllowSyncArgoCDAppfromBranchPathRegex != "" {
ghPrClientDetails.getPrMetadata(ce.Issue.GetBody()) // TODO is issue is not a PR but an actual issue, this will fail?

// Promotion PR have the list of paths to promote in the PR metadata
// For non promotion PR, we will generate the list of changed components based the PR changed files and the telefonistka configuration(sourcePath)
var componentPathList []string
if len(ghPrClientDetails.PrMetadata.PromotedPaths) > 0 {
componentPathList = ghPrClientDetails.PrMetadata.PromotedPaths
} else {
componentPathList, err = generateListOfChangedComponentPaths(ghPrClientDetails, config)
if err != nil {
ghPrClientDetails.PrLogger.Errorf("Failed to get list of changed components: err=%s\n", err)
// This part should only happen on edits of bot comments - this part is about catching checkbox changes
if *ce.Action == "edited" && *ce.Comment.User.Login == botIdentity {
checkboxPattern := `(?m)^\s*-\s*\[(.)\]\s*<!-- telefonistka-argocd-branch-sync -->.*$`
checkboxWaschecked, checkboxIsChecked := analyzeCommentUpdateCheckBox(*ce.Comment.Body, *ce.Changes.Body.From, checkboxPattern)
if !checkboxWaschecked && checkboxIsChecked {
ghPrClientDetails.PrLogger.Infof("Sync Checkbox was checked")
if config.AllowSyncArgoCDAppfromBranchPathRegex != "" {
ghPrClientDetails.getPrMetadata(ce.Issue.GetBody()) // TODO is issue is not a PR but an actual issue, this will fail?

// Promotion PR have the list of paths to promote in the PR metadata
// For non promotion PR, we will generate the list of changed components based the PR changed files and the telefonistka configuration(sourcePath)
var componentPathList []string
if len(ghPrClientDetails.PrMetadata.PromotedPaths) > 0 {
componentPathList = ghPrClientDetails.PrMetadata.PromotedPaths
} else {
componentPathList, err = generateListOfChangedComponentPaths(ghPrClientDetails, config)
if err != nil {
ghPrClientDetails.PrLogger.Errorf("Failed to get list of changed components: err=%s\n", err)
}
}
}

for _, componentPath := range componentPathList {
if isSyncFromBranchAllowedForThisPath(config.AllowSyncArgoCDAppfromBranchPathRegex, componentPath) {
err := argocd.SetArgoCDAppRevision(ghPrClientDetails.Ctx, componentPath, ghPrClientDetails.PrSHA, ghPrClientDetails.RepoURL, config.UseSHALabelForArgoDicovery)
if err != nil {
ghPrClientDetails.PrLogger.Errorf("Failed to sync ArgoCD app from branch: err=%s\n", err)
for _, componentPath := range componentPathList {
if isSyncFromBranchAllowedForThisPath(config.AllowSyncArgoCDAppfromBranchPathRegex, componentPath) {
err := argocd.SetArgoCDAppRevision(ghPrClientDetails.Ctx, componentPath, ghPrClientDetails.PrSHA, ghPrClientDetails.RepoURL, config.UseSHALabelForArgoDicovery)
if err != nil {
ghPrClientDetails.PrLogger.Errorf("Failed to sync ArgoCD app from branch: err=%s\n", err)
}
}
}
}
}
}

// I should probably deprecated this in part altogether.
// I should probably deprecated this whole part altogether.
for commentSubstring, commitStatusContext := range config.ToggleCommitStatus {
if strings.Contains(*ce.Comment.Body, "/"+commentSubstring) {
err := ghPrClientDetails.ToggleCommitStatus(commitStatusContext, *ce.Sender.Name)
Expand Down

0 comments on commit 0c0d06f

Please sign in to comment.