Skip to content
This repository has been archived by the owner on Nov 28, 2024. It is now read-only.

Commit

Permalink
feat: add polling the pull request for the validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso committed Sep 18, 2023
1 parent bdce5ae commit 7f11cee
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion controllers/productdeploymentgenerator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func (r *ProductDeploymentGeneratorReconciler) reconcile(ctx context.Context, ob
Spec: v1alpha1.ValidationSpec{
ValidationRules: validationRules,
ServiceAccountName: obj.Spec.ServiceAccountName,
Interval: metav1.Duration{Duration: 10 * time.Second},
Interval: metav1.Duration{Duration: 30 * time.Second},
SyncRef: meta.NamespacedObjectReference{
Name: sync.Name,
Namespace: sync.Namespace,
Expand Down
26 changes: 19 additions & 7 deletions controllers/validation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@ func (r *ValidationReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, err
}

if conditions.IsTrue(obj, meta.ReadyCondition) {
merged, err := r.Validator.IsMerged(ctx, *repository, *sync)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to fetch pull request status: %w", err)
}

if merged {
if err := r.deleteGitRepository(ctx, obj.Status.GitRepositoryRef); err != nil {
conditions.MarkFalse(obj, meta.ReadyCondition, mpasv1alpha1.GitRepositoryCleanUpFailedReason, err.Error())

return ctrl.Result{}, fmt.Errorf("failed to delete GitRepository tracking the values file: %w", err)
}

// Stop reconciling this validation any further.
return ctrl.Result{}, nil
}
}

if obj.Status.GitRepositoryRef == nil {
logger.Info("creating git repository to track value changes")
// create gitrepository to track values file and immediately requeue
Expand Down Expand Up @@ -232,12 +250,6 @@ func (r *ValidationReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
}

if err := r.deleteGitRepository(ctx, obj.Status.GitRepositoryRef); err != nil {
conditions.MarkFalse(obj, meta.ReadyCondition, mpasv1alpha1.GitRepositoryCleanUpFailedReason, err.Error())

return ctrl.Result{}, fmt.Errorf("failed to delete GitRepository tracking the values file: %w", err)
}

if err := r.Validator.PassValidation(ctx, *repository, *sync); err != nil {
conditions.MarkFalse(obj, meta.ReadyCondition, mpasv1alpha1.ValidationFailedReason, err.Error())

Expand All @@ -247,7 +259,7 @@ func (r *ValidationReconciler) Reconcile(ctx context.Context, req ctrl.Request)
conditions.MarkTrue(obj, meta.ReadyCondition, meta.SucceededReason, "Reconciliation success")
obj.Status.LastValidatedDigestOutcome = mpasv1alpha1.SuccessValidationOutcome

return ctrl.Result{}, nil
return ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}, nil
}

// createValueFileGitRepository creates a GitRepository that tracks changes on a branch.
Expand Down
26 changes: 26 additions & 0 deletions pkg/validators/gitea/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,32 @@ func (v *Validator) PassValidation(ctx context.Context, repository gitv1alpha1.R
return v.createCheckRunStatus(ctx, repository, sync.Status.PullRequestID, gitea.StatusSuccess)
}

func (v *Validator) IsMerged(ctx context.Context, repository gitv1alpha1.Repository, sync deliveryv1alpha1.Sync) (bool, error) {
pullRequestID := sync.Status.PullRequestID

token, err := v.retrieveAccessToken(ctx, repository)
if err != nil {
return false, fmt.Errorf("failed to retrieve token: %w", err)
}

domain := defaultDomain
if repository.Spec.Domain != "" {
domain = repository.Spec.Domain
}

client, err := gitea.NewClient(domain, gitea.SetToken(string(token)))
if err != nil {
return false, fmt.Errorf("failed to create gitea client: %w", err)
}

pr, _, err := client.GetPullRequest(repository.Spec.Owner, repository.Name, int64(pullRequestID))
if err != nil {
return false, fmt.Errorf("failed to find PR: %w", err)
}

return pr.HasMerged, nil
}

func (v *Validator) createCheckRunStatus(ctx context.Context, repository gitv1alpha1.Repository, pullRequestID int, status gitea.StatusState) error {
logger := log.FromContext(ctx)

Expand Down
20 changes: 20 additions & 0 deletions pkg/validators/github/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"golang.org/x/oauth2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

Expand Down Expand Up @@ -64,6 +65,25 @@ func (v *Validator) PassValidation(ctx context.Context, repository gitv1alpha1.R
return v.createCheckRunStatus(ctx, repository, sync.Status.PullRequestID, "success")
}

func (v *Validator) IsMerged(ctx context.Context, repository gitv1alpha1.Repository, sync deliveryv1alpha1.Sync) (bool, error) {
token, err := v.retrieveAccessToken(ctx, repository)
if err != nil {
return false, fmt.Errorf("failed to retrieve token: %w", err)
}

ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: string(token)})
tc := oauth2.NewClient(context.Background(), ts)

g := ggithub.NewClient(tc)

pr, _, err := g.PullRequests.Get(ctx, repository.Spec.Owner, repository.Name, sync.Status.PullRequestID)
if err != nil {
return false, fmt.Errorf("failed to find PR: %w", err)
}

return pointer.BoolDeref(pr.Merged, false), nil
}

func (v *Validator) createCheckRunStatus(ctx context.Context, repository gitv1alpha1.Repository, pullRequestID int, status string) error {
logger := log.FromContext(ctx)

Expand Down
1 change: 1 addition & 0 deletions pkg/validators/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ import (
type Validator interface {
FailValidation(ctx context.Context, repository gitv1alpha1.Repository, sync deliveryv1alpha1.Sync) error
PassValidation(ctx context.Context, repository gitv1alpha1.Repository, sync deliveryv1alpha1.Sync) error
IsMerged(ctx context.Context, repository gitv1alpha1.Repository, sync deliveryv1alpha1.Sync) (bool, error)
}

0 comments on commit 7f11cee

Please sign in to comment.