@@ -21,6 +21,11 @@ import (
2121// defined in settings.go.
2222type GitLabProvider struct {}
2323
24+ func gitlabUnauthorized (err error ) bool {
25+ var respErr * gitlab.ErrorResponse
26+ return errors .As (err , & respErr ) && respErr .Response != nil && respErr .Response .StatusCode == http .StatusUnauthorized
27+ }
28+
2429func init () { RegisterProvider (GitLabProvider {}) }
2530
2631func (GitLabProvider ) Name () string { return "gitlab" }
@@ -74,6 +79,9 @@ func (GitLabProvider) GetTags(ctx context.Context, user string, token *oauth2.To
7479 }
7580 tags , _ , err := c .Tags .ListTags (user + "/" + RepoName , & gitlab.ListTagsOptions {})
7681 if err != nil {
82+ if gitlabUnauthorized (err ) {
83+ return nil , ErrSignedOut
84+ }
7785 log .Printf ("gitlab GetTags: %v" , err )
7886 return nil , fmt .Errorf ("ListTags: %w" , err )
7987 }
@@ -92,6 +100,9 @@ func (GitLabProvider) GetBranches(ctx context.Context, user string, token *oauth
92100 }
93101 bs , _ , err := c .Branches .ListBranches (user + "/" + RepoName , & gitlab.ListBranchesOptions {})
94102 if err != nil {
103+ if gitlabUnauthorized (err ) {
104+ return nil , ErrSignedOut
105+ }
95106 log .Printf ("gitlab GetBranches: %v" , err )
96107 return nil , fmt .Errorf ("ListBranches: %w" , err )
97108 }
@@ -110,6 +121,9 @@ func (GitLabProvider) GetCommits(ctx context.Context, user string, token *oauth2
110121 }
111122 cs , _ , err := c .Commits .ListCommits (user + "/" + RepoName , & gitlab.ListCommitsOptions {})
112123 if err != nil {
124+ if gitlabUnauthorized (err ) {
125+ return nil , ErrSignedOut
126+ }
113127 log .Printf ("gitlab GetCommits: %v" , err )
114128 return nil , fmt .Errorf ("ListCommits: %w" , err )
115129 }
@@ -144,9 +158,15 @@ func (GitLabProvider) GetBookmarks(ctx context.Context, user, ref string, token
144158 if respErr .Response != nil && respErr .Response .StatusCode == http .StatusNotFound {
145159 return "" , "" , nil
146160 }
161+ if gitlabUnauthorized (err ) {
162+ return "" , "" , ErrSignedOut
163+ }
147164 log .Printf ("gitlab GetBookmarks get file: %v" , err )
148165 return "" , "" , nil
149166 }
167+ if gitlabUnauthorized (err ) {
168+ return "" , "" , ErrSignedOut
169+ }
150170 log .Printf ("gitlab GetBookmarks: %v" , err )
151171 return "" , "" , err
152172 }
@@ -161,8 +181,16 @@ func (GitLabProvider) GetBookmarks(ctx context.Context, user, ref string, token
161181func (GitLabProvider ) getDefaultBranch (ctx context.Context , user string , client * gitlab.Client , branch string ) (string , error ) {
162182 p , _ , err := client .Projects .GetProject (user + "/" + RepoName , nil )
163183 if err != nil {
164- if respErr , ok := err .(* gitlab.ErrorResponse ); ok && respErr .Response != nil && respErr .Response .StatusCode == http .StatusNotFound {
165- return "" , ErrRepoNotFound
184+ if respErr , ok := err .(* gitlab.ErrorResponse ); ok {
185+ if respErr .Response != nil && respErr .Response .StatusCode == http .StatusNotFound {
186+ return "" , ErrRepoNotFound
187+ }
188+ if gitlabUnauthorized (err ) {
189+ return "" , ErrSignedOut
190+ }
191+ }
192+ if gitlabUnauthorized (err ) {
193+ return "" , ErrSignedOut
166194 }
167195 log .Printf ("gitlab getDefaultBranch: %v" , err )
168196 return "" , err
@@ -202,9 +230,15 @@ func (GitLabProvider) UpdateBookmarks(ctx context.Context, user string, token *o
202230 if respErr .Response != nil && respErr .Response .StatusCode == http .StatusNotFound {
203231 return ErrRepoNotFound
204232 }
233+ if gitlabUnauthorized (err ) {
234+ return ErrSignedOut
235+ }
205236 log .Printf ("gitlab UpdateBookmarks update file: %v" , err )
206237 return err
207238 }
239+ if gitlabUnauthorized (err ) {
240+ return ErrSignedOut
241+ }
208242 if err .Error () == "404 Not Found" {
209243 return ErrRepoNotFound
210244 }
@@ -240,9 +274,15 @@ func (GitLabProvider) CreateBookmarks(ctx context.Context, user string, token *o
240274 if respErr .Response != nil && respErr .Response .StatusCode == http .StatusNotFound {
241275 return ErrRepoNotFound
242276 }
277+ if gitlabUnauthorized (err ) {
278+ return ErrSignedOut
279+ }
243280 log .Printf ("gitlab CreateBookmarks create file: %v" , err )
244281 return err
245282 }
283+ if gitlabUnauthorized (err ) {
284+ return ErrSignedOut
285+ }
246286 log .Printf ("gitlab CreateBookmarks: %v" , err )
247287 return err
248288 }
@@ -261,5 +301,8 @@ func (p GitLabProvider) CreateRepo(ctx context.Context, user string, token *oaut
261301 Visibility : gitlab .Ptr (gitlab .PrivateVisibility ),
262302 InitializeWithReadme : gitlab .Ptr (true ),
263303 })
304+ if err != nil && gitlabUnauthorized (err ) {
305+ return ErrSignedOut
306+ }
264307 return err
265308}
0 commit comments