@@ -2,6 +2,7 @@ package avancement
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "log"
78 "net/url"
@@ -97,7 +98,7 @@ func (s *ServiceManager) Promote(serviceName, fromURL, toURL, newBranchName, mes
9798 } else {
9899 source , errorSource = s .checkoutSourceRepo (fromURL , fromBranch )
99100 if errorSource != nil {
100- return fmt . Errorf ( "failed to checkout repo: %w " , errorSource )
101+ return git . GitError ( "error checking out source repository from Git " , fromURL )
101102 }
102103 reposToDelete = append (reposToDelete , source )
103104 if newBranchName == "" {
@@ -107,15 +108,19 @@ func (s *ServiceManager) Promote(serviceName, fromURL, toURL, newBranchName, mes
107108
108109 destination , err := s .checkoutDestinationRepo (toURL , newBranchName )
109110 if err != nil {
110- return fmt .Errorf ("failed to checkout repo: %w" , err )
111+ if git .IsGitError (err ) {
112+ return git .GitError (fmt .Sprintf ("failed to clone destination repository, error: %s" , err .Error ()), toURL )
113+ }
114+ // This would be a checkout error as the clone error gives us the above gitError instead
115+ return fmt .Errorf ("failed to checkout destination repository, error: %w" , err )
111116 }
112117 reposToDelete = append (reposToDelete , destination )
113118
114119 var copied []string
115120 if isLocal {
116121 copied , err = local .CopyConfig (serviceName , localSource , destination )
117122 if err != nil {
118- return fmt .Errorf ("failed to setup local repo : %w" , err )
123+ return fmt .Errorf ("failed to set up local repository : %w" , err )
119124 }
120125 } else {
121126 copied , err = git .CopyService (serviceName , source , destination )
@@ -141,13 +146,14 @@ func (s *ServiceManager) Promote(serviceName, fromURL, toURL, newBranchName, mes
141146 }
142147
143148 if err := destination .Push (newBranchName ); err != nil {
144- return fmt .Errorf ("failed to push: %w" , err )
149+ return fmt .Errorf ("failed to push to Git repository - check the access token is correct with sufficient permissions : %w" , err )
145150 }
146151
147152 ctx := context .Background ()
148153 pr , err := createPullRequest (ctx , fromURL , toURL , newBranchName , commitMsg , s .clientFactory (s .author .Token ), isLocal )
149154 if err != nil {
150- return fmt .Errorf ("failed to create a pull-request for branch %s in %v: %w" , newBranchName , toURL , err )
155+ message := fmt .Sprintf ("failed to create a pull-request for branch %s, error: %s" , newBranchName , err )
156+ return git .GitError (message , toURL )
151157 }
152158 log .Printf ("created PR %d" , pr .Number )
153159 return nil
@@ -156,23 +162,26 @@ func (s *ServiceManager) Promote(serviceName, fromURL, toURL, newBranchName, mes
156162func (s * ServiceManager ) checkoutSourceRepo (repoURL , branch string ) (git.Repo , error ) {
157163 repo , err := s .cloneRepo (repoURL , branch )
158164 if err != nil {
159- return nil , fmt .Errorf ("failed to clone source repo %s: %w" , repoURL , err )
165+ if git .IsGitError (err ) {
166+ return nil , git .GitError ("failed to clone source repository" , repoURL )
167+ }
168+ return nil , err
160169 }
161170 err = repo .Checkout (branch )
162171 if err != nil {
163- return nil , fmt .Errorf ("failed to checkout branch %s in repo %s : %w " , branch , repoURL , err )
172+ return nil , git . GitError ( fmt .Sprintf ("failed to checkout branch %s, error : %s " , branch , err . Error ()), repoURL )
164173 }
165174 return repo , nil
166175}
167176
168177func (s * ServiceManager ) checkoutDestinationRepo (repoURL , branch string ) (git.Repo , error ) {
169178 repo , err := s .cloneRepo (repoURL , branch )
170179 if err != nil {
171- return nil , fmt . Errorf ( "failed to clone destination repo %s: %w" , repoURL , err )
180+ return nil , git . GitError ( err . Error () , repoURL )
172181 }
173182 err = repo .CheckoutAndCreate (branch )
174183 if err != nil {
175- return nil , fmt .Errorf ("failed to checkout branch %s in repo %s : %w" , branch , repoURL , err )
184+ return nil , fmt .Errorf ("failed to checkout branch %s: %w" , branch , err )
176185 }
177186 return repo , nil
178187}
@@ -185,7 +194,8 @@ func (s *ServiceManager) cloneRepo(repoURL, branch string) (git.Repo, error) {
185194 }
186195 repo , err := s .repoFactory (repoURL , path .Join (s .cacheDir , encode (repoURL , branch )), s .debug )
187196 if err != nil {
188- return nil , fmt .Errorf ("failed to create repo for URL %s: %w" , repoURL , err )
197+ message := fmt .Sprintf ("failed to clone repository, error is: %s" , err .Error ())
198+ return nil , git .GitError (message , repoURL )
189199 }
190200 err = repo .Clone ()
191201 if err != nil {
@@ -205,7 +215,7 @@ func createPullRequest(ctx context.Context, fromURL, toURL, newBranchName, commi
205215 // TODO: improve this error message
206216 return nil , err
207217 }
208- // TODO: come up with a better way of generating the repo URL (this
218+ // TODO: come up with a better way of generating the repository URL (this
209219 // only works for GitHub)
210220 pr , _ , err := client .PullRequests .Create (ctx , fmt .Sprintf ("%s/%s" , user , repo ), prInput )
211221 return pr , err
@@ -217,8 +227,11 @@ func encode(gitURL, branch string) string {
217227
218228func addCredentialsIfNecessary (s string , a * git.Author ) (string , error ) {
219229 parsed , err := url .Parse (s )
230+ // If this does error (e.g. if there's a leading ":" character") one would see
231+ // parse ":[email protected] ": missing protocol scheme 232+ // thus surfacing the token. So intentionally display a generic parse problem
220233 if err != nil {
221- return "" , fmt . Errorf ("failed to parse git repo url %v: %w" , s , err )
234+ return "" , errors . New ("failed to parse git repository url" )
222235 }
223236 if parsed .Scheme != "https" || parsed .User != nil {
224237 return s , nil
0 commit comments