Skip to content

Commit

Permalink
Refactor: extract commenter usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 committed Feb 5, 2021
1 parent 342a76e commit cc0064a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
32 changes: 5 additions & 27 deletions controllers/application_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package controllers

import (
"context"
"fmt"

argocdv1alpha1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/go-logr/logr"
Expand All @@ -27,6 +26,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"

"github.com/int128/argocd-commenter/pkg/commenter"
"github.com/int128/argocd-commenter/pkg/github"
)

Expand All @@ -49,41 +49,19 @@ func (r *ApplicationReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error)
return ctrl.Result{}, client.IgnoreNotFound(err)
}

commitComment, err := commitCommentFor(application)
if err != nil {
log.Error(err, "Skipped the event", "Application", req.NamespacedName)
return ctrl.Result{}, nil
cmt := commenter.ApplicationOperationState{
Log: log,
}
log.Info("Creating a commit comment", "commitComment", commitComment)
if err := github.CreateCommitComment(ctx, *commitComment); err != nil {
if err := cmt.Do(ctx, application); err != nil {
if github.IsRetryableError(err) {
return ctrl.Result{}, err
}
log.Error(err, "Ignored the permanent error")
log.Error(err, "unable to add a comment")
return ctrl.Result{}, nil
}
return ctrl.Result{}, nil
}

func commitCommentFor(application argocdv1alpha1.Application) (*github.CommitComment, error) {
repository, err := github.ParseRepositoryURL(application.Spec.Source.RepoURL)
if err != nil {
return nil, fmt.Errorf("non-GitHub URL: %w", err)
}
if application.Status.OperationState == nil {
return nil, fmt.Errorf("status.operationState is nil") // should not reach here
}
return &github.CommitComment{
Repository: *repository,
CommitSHA: application.Status.Sync.Revision,
Body: fmt.Sprintf("ArgoCD: %s: %s: %s",
application.Name,
application.Status.OperationState.Phase,
application.Status.OperationState.Message,
),
}, nil
}

func (r *ApplicationReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&argocdv1alpha1.Application{}).
Expand Down
43 changes: 43 additions & 0 deletions pkg/commenter/application_operation_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package commenter

import (
"context"
"fmt"

argocdv1alpha1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/go-logr/logr"

"github.com/int128/argocd-commenter/pkg/github"
)

type ApplicationOperationState struct {
Log logr.Logger
}

func (cmt *ApplicationOperationState) Do(ctx context.Context, application argocdv1alpha1.Application) error {
if application.Status.OperationState == nil {
cmt.Log.Info("application.status.operationState is nil (should not reach here)", "application.status", application.Status)
return nil
}
repository, err := github.ParseRepositoryURL(application.Spec.Source.RepoURL)
if err != nil {
cmt.Log.Info("skip non-GitHub URL", "error", err)
return nil
}

commitComment := github.CommitComment{
Repository: *repository,
CommitSHA: application.Status.Sync.Revision,
Body: fmt.Sprintf("ArgoCD: %s: %s: %s",
application.Name,
application.Status.OperationState.Phase,
application.Status.OperationState.Message,
),
}

cmt.Log.Info("creating a commit comment", "commitComment", commitComment)
if err := github.CreateCommitComment(ctx, commitComment); err != nil {
return fmt.Errorf("could not add a comment: %w", err)
}
return nil
}

0 comments on commit cc0064a

Please sign in to comment.