Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.

Commit f828dd8

Browse files
committed
create a pull request status check
1 parent 13f5672 commit f828dd8

File tree

6 files changed

+45
-87
lines changed

6 files changed

+45
-87
lines changed

apis/delivery/v1alpha1/sync_types.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ type SyncStatus struct {
5959
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].message",description=""
6060
Conditions []metav1.Condition `json:"conditions,omitempty"`
6161

62-
// +optional
63-
ValuesGitRepositoryRef *meta.NamespacedObjectReference `json:"valuesGitRepositoryRef,omitempty"`
64-
6562
// +optional
6663
PullRequestID int `json:"pullRequestID,omitempty"`
6764
}

apis/delivery/v1alpha1/zz_generated.deepcopy.go

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/delivery.ocm.software_syncs.yaml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,20 +184,6 @@ spec:
184184
type: integer
185185
pullRequestID:
186186
type: integer
187-
valuesGitRepositoryRef:
188-
description: NamespacedObjectReference contains enough information
189-
to locate the referenced Kubernetes resource object in any namespace.
190-
properties:
191-
name:
192-
description: Name of the referent.
193-
type: string
194-
namespace:
195-
description: Namespace of the referent, when not specified it
196-
acts as LocalObjectReference.
197-
type: string
198-
required:
199-
- name
200-
type: object
201187
type: object
202188
type: object
203189
served: true

controllers/delivery/sync_controller.go

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@ import (
1313
"github.com/fluxcd/pkg/apis/meta"
1414
"github.com/fluxcd/pkg/runtime/conditions"
1515
"github.com/fluxcd/pkg/runtime/patch"
16-
sourcebeta2 "github.com/fluxcd/source-controller/api/v1beta2"
1716
corev1 "k8s.io/api/core/v1"
1817
apierrors "k8s.io/apimachinery/pkg/api/errors"
1918
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2019
"k8s.io/apimachinery/pkg/runtime"
2120
"k8s.io/apimachinery/pkg/types"
22-
"k8s.io/utils/pointer"
2321
ctrl "sigs.k8s.io/controller-runtime"
2422
"sigs.k8s.io/controller-runtime/pkg/builder"
2523
"sigs.k8s.io/controller-runtime/pkg/client"
26-
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2724
"sigs.k8s.io/controller-runtime/pkg/log"
2825
"sigs.k8s.io/controller-runtime/pkg/predicate"
2926

@@ -232,17 +229,6 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resul
232229
}
233230

234231
obj.Status.PullRequestID = id
235-
236-
// Create GitRepository to track values file changes.
237-
repo, err := r.createValueFileGitRepository(ctx, *obj, *repository, targetBranch)
238-
if err != nil {
239-
err = fmt.Errorf("failed to create value tracking git repository object: %w", err)
240-
conditions.MarkFalse(obj, meta.ReadyCondition, v1alpha1.GitRepositoryCreateFailedReason, err.Error())
241-
242-
return ctrl.Result{}, err
243-
}
244-
245-
obj.Status.ValuesGitRepositoryRef = &repo
246232
}
247233

248234
// Remove any stale Ready condition, most likely False, set above. Its value
@@ -281,50 +267,3 @@ func (r *SyncReconciler) parseAuthSecret(secret *corev1.Secret, opts *pkg.PushOp
281267
},
282268
}
283269
}
284-
285-
// createValueFileGitRepository creates a GitRepository that tracks changes on a branch.
286-
func (r *SyncReconciler) createValueFileGitRepository(ctx context.Context, sync v1alpha1.Sync, repository mpasv1alpha1.Repository, branch string) (meta.NamespacedObjectReference, error) {
287-
owners := sync.GetOwnerReferences()
288-
if len(owners) != 1 {
289-
return meta.NamespacedObjectReference{}, fmt.Errorf("expected exactly one owner, got: %d", len(owners))
290-
}
291-
292-
repo := &sourcebeta2.GitRepository{
293-
ObjectMeta: metav1.ObjectMeta{
294-
Name: repository.Name + "values-repo",
295-
Namespace: repository.Namespace,
296-
},
297-
Spec: sourcebeta2.GitRepositorySpec{
298-
URL: repository.GetRepositoryURL(),
299-
SecretRef: &meta.LocalObjectReference{
300-
Name: repository.Spec.Credentials.SecretRef.Name,
301-
},
302-
Interval: metav1.Duration{Duration: 5 * time.Second},
303-
Reference: &sourcebeta2.GitRepositoryRef{
304-
Branch: branch,
305-
},
306-
Ignore: pointer.String(fmt.Sprintf(`# exclude all
307-
/*
308-
# include values.yaml
309-
!./products/%s/values.yaml
310-
`, owners[0].Name)),
311-
},
312-
}
313-
314-
if _, err := controllerutil.CreateOrUpdate(ctx, r.Client, repo, func() error {
315-
if repo.ObjectMeta.CreationTimestamp.IsZero() {
316-
if err := controllerutil.SetOwnerReference(&repository, repo, r.Scheme); err != nil {
317-
return fmt.Errorf("failed to set owner to git repository object: %w", err)
318-
}
319-
}
320-
321-
return nil
322-
}); err != nil {
323-
return meta.NamespacedObjectReference{}, fmt.Errorf("failed to create git repository: %w", err)
324-
}
325-
326-
return meta.NamespacedObjectReference{
327-
Name: repo.Name,
328-
Namespace: repo.Namespace,
329-
}, nil
330-
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ require (
1919
k8s.io/api v0.26.2
2020
k8s.io/apimachinery v0.26.2
2121
k8s.io/client-go v0.26.2
22-
k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5
2322
sigs.k8s.io/controller-runtime v0.14.4
2423
)
2524

@@ -175,6 +174,7 @@ require (
175174
k8s.io/component-base v0.26.2 // indirect
176175
k8s.io/klog/v2 v2.90.0 // indirect
177176
k8s.io/kube-openapi v0.0.0-20230228151317-19cbebb19cb7 // indirect
177+
k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5 // indirect
178178
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
179179
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
180180
sigs.k8s.io/yaml v1.3.0 // indirect

pkg/providers/github/github.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,51 @@ func (c *Client) CreatePullRequest(ctx context.Context, branch string, sync deli
154154
return -1, fmt.Errorf("failed to create github client: %w", err)
155155
}
156156

157+
var (
158+
id int
159+
createPullRequestFunc = gogit.CreateUserPullRequest
160+
)
161+
157162
if repository.Spec.IsOrganization {
158-
return gogit.CreateOrganizationPullRequest(ctx, gc, domain, branch, sync.Spec.PullRequestTemplate, repository)
163+
createPullRequestFunc = gogit.CreateOrganizationPullRequest
164+
}
165+
166+
if id, err = createPullRequestFunc(ctx, gc, domain, branch, sync.Spec.PullRequestTemplate, repository); err != nil {
167+
return 0, fmt.Errorf("failed to create pull request: %w", err)
168+
}
169+
170+
if err := c.createCheckRun(ctx, repository, id); err != nil {
171+
return 0, fmt.Errorf("failed to create check run: %w", err)
172+
}
173+
174+
return id, nil
175+
}
176+
177+
func (c *Client) createCheckRun(ctx context.Context, repository mpasv1alpha1.Repository, prID int) error {
178+
token, err := c.retrieveAccessToken(ctx, repository)
179+
if err != nil {
180+
return fmt.Errorf("failed to retrieve token: %w", err)
159181
}
160182

161-
return gogit.CreateUserPullRequest(ctx, gc, domain, branch, sync.Spec.PullRequestTemplate, repository)
183+
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: string(token)})
184+
tc := oauth2.NewClient(context.Background(), ts)
185+
186+
g := ggithub.NewClient(tc)
187+
//
188+
pr, _, err := g.PullRequests.Get(ctx, repository.Spec.Owner, repository.Name, prID)
189+
if err != nil {
190+
return fmt.Errorf("failed to find PR: %w", err)
191+
}
192+
193+
_, _, err = g.Repositories.CreateStatus(ctx, repository.Spec.Owner, repository.Name, *pr.Head.SHA, &ggithub.RepoStatus{
194+
State: ggithub.String("error"),
195+
Description: ggithub.String("MPAS Validation Check"),
196+
Context: ggithub.String(statusCheckName),
197+
})
198+
199+
if err != nil {
200+
return fmt.Errorf("failed to create status for pr: %w", err)
201+
}
202+
203+
return nil
162204
}

0 commit comments

Comments
 (0)