@@ -9,11 +9,13 @@ import (
99	"fmt" 
1010	"io" 
1111	"os" 
12+ 	"os/exec" 
1213	"path/filepath" 
1314	"regexp" 
1415	"sort" 
1516	"strings" 
1617
18+ 	"github.com/cloudbees-io/configure-git-global-credentials/internal" 
1719	"github.com/go-git/go-git/v5/config" 
1820	format "github.com/go-git/go-git/v5/plumbing/format/config" 
1921	"github.com/go-git/go-git/v5/plumbing/transport" 
@@ -46,7 +48,12 @@ type Config struct {
4648	GitLabServerURL  string  `mapstructure:"gitlab-server-url"` 
4749}
4850
49- func  loadConfig (scope  config.Scope ) (_  * format.Config , _  string , retErr  error ) {
51+ const  (
52+ 	tokenEnv                    =  "CLOUDBEES_API_TOKEN" 
53+ 	cbGitCredentialsHelperPath  =  "git-credential-cloudbees" 
54+ )
55+ 
56+ var  loadConfig  =  func (scope  config.Scope ) (_  * format.Config , _  string , retErr  error ) {
5057	paths , err  :=  config .Paths (scope )
5158	if  err  !=  nil  {
5259		return  nil , "" , err 
@@ -153,6 +160,15 @@ func (c *Config) Apply(ctx context.Context) error {
153160		return  err 
154161	}
155162
163+ 	gitCredCloudbeesExists  :=  true 
164+ 	cbGitCredentialsHelperPath , err  :=  exec .LookPath (cbGitCredentialsHelperPath )
165+ 	if  err  !=  nil  {
166+ 		internal .Debug ("Could not find git-credential-cloudbees on the path, falling back to old-style helper" )
167+ 		gitCredCloudbeesExists  =  false 
168+ 	} else  {
169+ 		internal .Debug ("Found git-credential-cloudbees on the path at %s" , cbGitCredentialsHelperPath )
170+ 	}
171+ 
156172	homePath  :=  os .Getenv ("HOME" )
157173	actionPath  :=  filepath .Join (homePath , ".cloudbees-configure-git-global-credentials" , c .uniqueId ())
158174	if  err  :=  os .MkdirAll (actionPath , os .ModePerm ); err  !=  nil  {
@@ -164,35 +180,44 @@ func (c *Config) Apply(ctx context.Context) error {
164180	var  helperConfigFile  string 
165181
166182	if  ! c .ssh () {
167- 		fmt .Println ("🔄 Installing credentials helper ..." )
183+ 		if  ! gitCredCloudbeesExists  {
184+ 			fmt .Println ("🔄 Installing credentials helper ..." )
168185
169- 		self , err  :=  os .Executable ()
170- 		if  err  !=  nil  {
171- 			return  err 
172- 		}
186+ 			 self , err  :=  os .Executable ()
187+ 			 if  err  !=  nil  {
188+ 				 return  err 
189+ 			 }
173190
174- 		helperExecutable  :=  filepath .Join (actionPath , "git-credential-helper" )
175- 		if  a , err  :=  filepath .Abs (helperExecutable ); err  !=  nil  {
176- 			helperExecutable  =  a 
177- 		}
191+ 			 helperExecutable  :=  filepath .Join (actionPath , "git-credential-helper" )
192+ 			 if  a , err  :=  filepath .Abs (helperExecutable ); err  !=  nil  {
193+ 				 helperExecutable  =  a 
194+ 			 }
178195
179- 		err  =  copyFileHelper (helperExecutable , self )
180- 		if  err  !=  nil  {
181- 			return  err 
182- 		}
196+ 			 err  =  copyFileHelper (helperExecutable , self )
197+ 			 if  err  !=  nil  {
198+ 				 return  err 
199+ 			 }
183200
184- 		fmt .Println ("✅ Credentials helper installed" )
201+ 			 fmt .Println ("✅ Credentials helper installed" )
185202
186- 		helperConfig  =  & format.Config {}
187- 		helperConfigFile  =  helperExecutable  +  ".cfg" 
188- 		helper  =  fmt .Sprintf ("%s credential-helper --config-file %s" , helperExecutable , helperConfigFile )
203+ 			 helperConfig  =  & format.Config {}
204+ 			 helperConfigFile  =  helperExecutable  +  ".cfg" 
205+ 			 helper  =  fmt .Sprintf ("%s credential-helper --config-file %s" , helperExecutable , helperConfigFile )
189206
190- 		if  _ , err  :=  os .Stat (helperConfigFile ); err  !=  nil  {
191- 			b , err  :=  os .ReadFile (helperConfigFile )
192- 			if  err  ==  nil  {
193- 				// make best effort to merge existing, if it fails we will overwrite the whole 
194- 				_  =  format .NewDecoder (bytes .NewReader (b )).Decode (helperConfig )
207+ 			if  _ , err  :=  os .Stat (helperConfigFile ); err  !=  nil  {
208+ 				b , err  :=  os .ReadFile (helperConfigFile )
209+ 				if  err  ==  nil  {
210+ 					// make best effort to merge existing, if it fails we will overwrite the whole 
211+ 					_  =  format .NewDecoder (bytes .NewReader (b )).Decode (helperConfig )
212+ 				}
195213			}
214+ 		} else  {
215+ 			filterUrl  :=  make ([]string , 0 , len (aliases ))
216+ 			for  url  :=  range  aliases  {
217+ 				filterUrl  =  append (filterUrl , url )
218+ 			}
219+ 
220+ 			return  invokeGitCredentialsHelper (ctx , cbGitCredentialsHelperPath , cfgPath , c .CloudBeesApiURL , c .CloudBeesApiToken , filterUrl )
196221		}
197222	} else  {
198223		// check if the SSH key looks to be a base64 encoded private key that the user forgot to decode 
@@ -308,6 +333,36 @@ func (c *Config) Apply(ctx context.Context) error {
308333	return  nil 
309334}
310335
336+ var  invokeGitCredentialsHelper  =  func (ctx  context.Context , path , gitConfigPath , cloudbeesApiURL , cloudbeesApiToken  string , filterGitUrls  []string ) error  {
337+ 
338+ 	homeDir , err  :=  os .UserHomeDir ()
339+ 	if  err  !=  nil  {
340+ 		return  err 
341+ 	}
342+ 	helperConfig  :=  filepath .Join (homeDir , ".git-credential-cloudbees-config" )
343+ 
344+ 	filterUrlArgs  :=  []string {}
345+ 
346+ 	filterUrlArgs  =  append (filterUrlArgs , "init" )
347+ 	filterUrlArgs  =  append (filterUrlArgs , "--config" , helperConfig )
348+ 	filterUrlArgs  =  append (filterUrlArgs , "--cloudbees-api-token-env-var" , tokenEnv )
349+ 	filterUrlArgs  =  append (filterUrlArgs , "--cloudbees-api-url" , cloudbeesApiURL )
350+ 	filterUrlArgs  =  append (filterUrlArgs , "--git-config-file-path" , gitConfigPath )
351+ 	for  _ , filterGitUrl  :=  range  filterGitUrls  {
352+ 		filterUrlArgs  =  append (filterUrlArgs , "--filter-git-urls" , filterGitUrl )
353+ 	}
354+ 	cmd  :=  exec .CommandContext (ctx , path , filterUrlArgs ... )
355+ 
356+ 	cmd .Stdout  =  os .Stdout 
357+ 	cmd .Stderr  =  os .Stderr 
358+ 
359+ 	internal .Debug ("%s" , cmd .String ())
360+ 
361+ 	cmd .Env  =  append (os .Environ (), fmt .Sprintf ("%s=%s" , tokenEnv , cloudbeesApiToken ))
362+ 
363+ 	return  cmd .Run ()
364+ }
365+ 
311366func  (c  * Config ) providerUsername () string  {
312367	switch  c .Provider  {
313368	case  "github" :
0 commit comments