Skip to content

Commit

Permalink
feat: add handler func for comment needed
Browse files Browse the repository at this point in the history
  • Loading branch information
spoukke committed Apr 18, 2023
1 parent 3f38328 commit 2e53785
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions internal/controllers/terraformpullrequest/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *DiscoveryNeeded) getHandler() func(ctx context.Context, r *Reconciler,
return func(ctx context.Context, r *Reconciler, repository *configv1alpha1.TerraformRepository, pr *configv1alpha1.TerraformPullRequest) ctrl.Result {
layers, err := r.getAffectedLayers(repository, pr)
if err != nil {
log.Errorf("failed to get affected layers for pull request %s: %v", pr.Name, err)
log.Errorf("failed to get affected layers for pull request %s: %s", pr.Name, err)
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.OnError}
}
newLayers := generateTempLayers(pr, layers)
Expand All @@ -75,7 +75,7 @@ func (s *DiscoveryNeeded) getHandler() func(ctx context.Context, r *Reconciler,
}
err = annotations.Add(ctx, r.Client, pr, map[string]string{annotations.LastDiscoveredCommit: pr.Annotations[annotations.LastBranchCommit]})
if err != nil {
log.Errorf("failed to add annotation %s to pull request %s: %v", annotations.LastDiscoveredCommit, pr.Name, err)
log.Errorf("failed to add annotation %s to pull request %s: %s", annotations.LastDiscoveredCommit, pr.Name, err)
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.OnError}
}
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.WaitAction}
Expand All @@ -88,11 +88,35 @@ func (s *CommentNeeded) getHandler() func(ctx context.Context, r *Reconciler, re
return func(ctx context.Context, r *Reconciler, repository *configv1alpha1.TerraformRepository, pr *configv1alpha1.TerraformPullRequest) ctrl.Result {
layers, err := getLinkedLayers(r.Client, pr)
if err != nil {
log.Errorf("failed to get linked layers for pull request %s: %v", pr.Name, err)
log.Errorf("failed to get linked layers for pull request %s: %s", pr.Name, err)
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.OnError}
}

var provider Provider
found := false
for _, p := range r.Providers {
if provider.IsFromProvider(pr) {
provider = p
found = true
}
}
if !found {
log.Infof("failed to get pull request provider. Requeuing")
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.WaitAction}
}

comment := comment.NewDefaultComment(layers, r.Storage)
return ctrl.Result{}
err = provider.Comment(repository, pr, comment)
if err != nil {
log.Errorf("an error occured while commenting pull request: %s", err)
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.OnError}
}
err = annotations.Add(ctx, r.Client, pr, map[string]string{annotations.LastCommentedCommit: pr.Annotations[annotations.LastDiscoveredCommit]})
if err != nil {
log.Errorf("failed to add annotation %s to pull request %s: %s", annotations.LastCommentedCommit, pr.Name, err)
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.OnError}
}
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.WaitAction}
}
}

Expand Down

0 comments on commit 2e53785

Please sign in to comment.