@@ -10,6 +10,8 @@ import (
1010 "github.com/creydr/func-operator/internal/monitoring"
1111 "github.com/go-git/go-git/v6"
1212 "github.com/go-git/go-git/v6/plumbing"
13+ "github.com/go-git/go-git/v6/plumbing/transport"
14+ "github.com/go-git/go-git/v6/plumbing/transport/http"
1315 "github.com/prometheus/client_golang/prometheus"
1416)
1517
@@ -18,7 +20,7 @@ const (
1820)
1921
2022type Manager interface {
21- CloneRepository (ctx context.Context , url , reference string ) (* Repository , error )
23+ CloneRepository (ctx context.Context , url , reference string , auth map [ string ][] byte ) (* Repository , error )
2224}
2325
2426func NewManager () Manager {
@@ -27,7 +29,7 @@ func NewManager() Manager {
2729
2830type managerImpl struct {}
2931
30- func (* managerImpl ) CloneRepository (ctx context.Context , repoUrl , reference string ) (* Repository , error ) {
32+ func (m * managerImpl ) CloneRepository (ctx context.Context , repoUrl , reference string , auth map [ string ][] byte ) (* Repository , error ) {
3133 timer := prometheus .NewTimer (monitoring .GitCloneDuration )
3234 defer timer .ObserveDuration ()
3335
@@ -47,6 +49,7 @@ func (*managerImpl) CloneRepository(ctx context.Context, repoUrl, reference stri
4749 ReferenceName : plumbing .ReferenceName (reference ),
4850 SingleBranch : true ,
4951 Depth : 1 ,
52+ Auth : m .getAuth (auth ),
5053 })
5154 if err != nil {
5255 return nil , fmt .Errorf ("failed to clone repo: %w" , err )
@@ -56,3 +59,23 @@ func (*managerImpl) CloneRepository(ctx context.Context, repoUrl, reference stri
5659 CloneDir : targetDir ,
5760 }, nil
5861}
62+
63+ func (m * managerImpl ) getAuth (authSecret map [string ][]byte ) transport.AuthMethod {
64+ if len (authSecret ) == 0 {
65+ return nil
66+ } else if token , ok := authSecret ["token" ]; ok {
67+ return & http.BasicAuth {
68+ Username : "empty" , // can be anything except an empty string
69+ Password : string (token ),
70+ }
71+ } else if username , ok := authSecret ["username" ]; ok {
72+ if password , ok := authSecret ["password" ]; ok {
73+ return & http.BasicAuth {
74+ Username : string (username ),
75+ Password : string (password ),
76+ }
77+ }
78+ return nil
79+ } // add other auth methods when needed
80+ return nil
81+ }
0 commit comments