@@ -8,10 +8,12 @@ import (
88 "context"
99 "errors"
1010 "fmt"
11+ "time"
1112
1213 "github.com/fluxcd/pkg/apis/meta"
1314 "github.com/fluxcd/pkg/runtime/conditions"
1415 "github.com/fluxcd/pkg/runtime/patch"
16+ "github.com/open-component-model/git-controller/pkg/providers"
1517 corev1 "k8s.io/api/core/v1"
1618 apierrors "k8s.io/apimachinery/pkg/api/errors"
1719 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -25,15 +27,16 @@ import (
2527
2628 "github.com/open-component-model/git-controller/apis/delivery/v1alpha1"
2729 mpasv1alpha1 "github.com/open-component-model/git-controller/apis/mpas/v1alpha1"
28- providers "github.com/open-component-model/git-controller/pkg"
30+ "github.com/open-component-model/git-controller/pkg"
2931)
3032
3133// SyncReconciler reconciles a Sync object
3234type SyncReconciler struct {
3335 client.Client
3436 Scheme * runtime.Scheme
3537
36- Git providers.Git
38+ Git pkg.Git
39+ Provider providers.Provider
3740}
3841
3942//+kubebuilder:rbac:groups=delivery.ocm.software,resources=syncs,verbs=get;list;watch;create;update;patch;delete
@@ -161,15 +164,25 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
161164 return ctrl.Result {}, retErr
162165 }
163166
167+ branch := obj .Spec .Branch
168+ if branch == "" && obj .Spec .AutomaticPullRequestCreation {
169+ branch = fmt .Sprintf ("branch-%d" , time .Now ().Unix ())
170+ } else if branch == "" && ! obj .Spec .AutomaticPullRequestCreation {
171+ retErr = fmt .Errorf ("branch cannot be empty if automatic pull request creation is not enabled" )
172+ conditions .MarkFalse (obj , meta .ReadyCondition , v1alpha1 .GitRepositoryPushFailedReason , retErr .Error ())
173+
174+ return ctrl.Result {}, retErr
175+ }
176+
164177 // trim any trailing `/` and then just add.
165178 log .V (4 ).Info ("crafting artifact URL to download from" , "url" , snapshot .Status .RepositoryURL )
166- opts := & providers .PushOptions {
179+ opts := & pkg .PushOptions {
167180 URL : repository .GetRepositoryURL (),
168181 Message : obj .Spec .CommitTemplate .Message ,
169182 Name : obj .Spec .CommitTemplate .Name ,
170183 Email : obj .Spec .CommitTemplate .Email ,
171184 Snapshot : snapshot ,
172- Branch : obj . Spec . Branch ,
185+ Branch : branch ,
173186 SubPath : obj .Spec .SubPath ,
174187 }
175188
@@ -185,6 +198,15 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
185198
186199 obj .Status .Digest = digest
187200
201+ if obj .Spec .AutomaticPullRequestCreation {
202+ if err := r .Provider .CreatePullRequest (ctx , branch , * obj , * repository ); err != nil {
203+ retErr = fmt .Errorf ("failed to create pull request: %w" , err )
204+ conditions .MarkFalse (obj , meta .ReadyCondition , v1alpha1 .CreatePullRequestFailedReason , retErr .Error ())
205+
206+ return ctrl.Result {}, retErr
207+ }
208+ }
209+
188210 // Remove any stale Ready condition, most likely False, set above. Its value
189211 // is derived from the overall result of the reconciliation in the deferred
190212 // block at the very end.
@@ -200,10 +222,10 @@ func (r *SyncReconciler) SetupWithManager(mgr ctrl.Manager) error {
200222 Complete (r )
201223}
202224
203- func (r * SyncReconciler ) parseAuthSecret (secret * corev1.Secret , opts * providers .PushOptions ) {
225+ func (r * SyncReconciler ) parseAuthSecret (secret * corev1.Secret , opts * pkg .PushOptions ) {
204226 if _ , ok := secret .Data ["identity" ]; ok {
205- opts .Auth = & providers .Auth {
206- SSH : & providers .SSH {
227+ opts .Auth = & pkg .Auth {
228+ SSH : & pkg .SSH {
207229 PemBytes : secret .Data ["identity" ],
208230 User : string (secret .Data ["username" ]),
209231 Password : string (secret .Data ["password" ]),
@@ -212,8 +234,8 @@ func (r *SyncReconciler) parseAuthSecret(secret *corev1.Secret, opts *providers.
212234 return
213235 }
214236 // default to basic auth.
215- opts .Auth = & providers .Auth {
216- BasicAuth : & providers .BasicAuth {
237+ opts .Auth = & pkg .Auth {
238+ BasicAuth : & pkg .BasicAuth {
217239 Username : string (secret .Data ["username" ]),
218240 Password : string (secret .Data ["password" ]),
219241 },
0 commit comments