@@ -131,13 +131,16 @@ func (GitLabProvider) GetBookmarks(ctx context.Context, user, ref string, token
131131 log .Printf ("gitlab GetBookmarks client: %v" , err )
132132 return "" , "" , err
133133 }
134+ if ref == "" {
135+ ref = "HEAD"
136+ }
134137 f , _ , err := c .RepositoryFiles .GetFile (user + "/" + RepoName , "bookmarks.txt" , & gitlab.GetFileOptions {Ref : gitlab .String (ref )})
135138 if err != nil {
136139 if respErr , ok := err .(* gitlab.ErrorResponse ); ok {
137140 if respErr .Response != nil && respErr .Response .StatusCode == http .StatusNotFound {
138141 return "" , "" , nil
139142 }
140- log .Printf ("gitlab GetBookmarks: %v" , err )
143+ log .Printf ("gitlab GetBookmarks get file : %v" , err )
141144 return "" , "" , nil
142145 }
143146 log .Printf ("gitlab GetBookmarks: %v" , err )
@@ -151,12 +154,35 @@ func (GitLabProvider) GetBookmarks(ctx context.Context, user, ref string, token
151154 return string (data ), f .LastCommitID , nil
152155}
153156
157+ func (GitLabProvider ) getDefaultBranch (ctx context.Context , user string , client * gitlab.Client , branch string ) (string , error ) {
158+ p , _ , err := client .Projects .GetProject (user + "/" + RepoName , nil )
159+ if err != nil {
160+ if respErr , ok := err .(* gitlab.ErrorResponse ); ok && respErr .Response != nil && respErr .Response .StatusCode == http .StatusNotFound {
161+ return "" , ErrRepoNotFound
162+ }
163+ log .Printf ("gitlab getDefaultBranch: %v" , err )
164+ return "" , err
165+ }
166+ if p .DefaultBranch != "" {
167+ branch = p .DefaultBranch
168+ } else {
169+ branch = "main"
170+ }
171+ return branch , nil
172+ }
154173func (GitLabProvider ) UpdateBookmarks (ctx context.Context , user string , token * oauth2.Token , sourceRef , branch , text , expectSHA string ) error {
155174 c , err := GitLabProvider {}.client (token )
156175 if err != nil {
157176 log .Printf ("gitlab UpdateBookmarks client: %v" , err )
158177 return err
159178 }
179+ if branch == "" {
180+ branch , err = GitLabProvider {}.getDefaultBranch (ctx , user , c , branch )
181+ if err != nil {
182+ log .Printf ("gitlab UpdateBookmarks default branch: %v" , err )
183+ return err
184+ }
185+ }
160186 opt := & gitlab.UpdateFileOptions {
161187 Branch : gitlab .String (branch ),
162188 Content : gitlab .String (text ),
@@ -167,8 +193,12 @@ func (GitLabProvider) UpdateBookmarks(ctx context.Context, user string, token *o
167193 }
168194 _ , _ , err = c .RepositoryFiles .UpdateFile (user + "/" + RepoName , "bookmarks.txt" , opt )
169195 if err != nil {
170- if respErr , ok := err .(* gitlab.ErrorResponse ); ok && respErr .Response != nil && respErr .Response .StatusCode == http .StatusNotFound {
171- return ErrRepoNotFound
196+ if respErr , ok := err .(* gitlab.ErrorResponse ); ok {
197+ if respErr .Response != nil && respErr .Response .StatusCode == http .StatusNotFound {
198+ return ErrRepoNotFound
199+ }
200+ log .Printf ("gitlab UpdateBookmarks update file: %v" , err )
201+ return err
172202 }
173203 log .Printf ("gitlab UpdateBookmarks: %v" , err )
174204 return err
@@ -182,6 +212,13 @@ func (GitLabProvider) CreateBookmarks(ctx context.Context, user string, token *o
182212 log .Printf ("gitlab CreateBookmarks client: %v" , err )
183213 return err
184214 }
215+ if branch == "" {
216+ branch , err = GitLabProvider {}.getDefaultBranch (ctx , user , c , branch )
217+ if err != nil {
218+ log .Printf ("gitlab CreateBookmarks default branch: %v" , err )
219+ return err
220+ }
221+ }
185222 opt := & gitlab.CreateFileOptions {
186223 Branch : gitlab .String (branch ),
187224 Content : gitlab .String (text ),
@@ -191,14 +228,17 @@ func (GitLabProvider) CreateBookmarks(ctx context.Context, user string, token *o
191228 }
192229 _ , _ , err = c .RepositoryFiles .CreateFile (user + "/" + RepoName , "bookmarks.txt" , opt )
193230 if err != nil {
194- if respErr , ok := err .(* gitlab.ErrorResponse ); ok && respErr .Response != nil && respErr .Response .StatusCode == http .StatusNotFound {
195- return ErrRepoNotFound
196- }
197- if err != nil {
198- log .Printf ("gitlab CreateBookmarks: %v" , err )
231+ if respErr , ok := err .(* gitlab.ErrorResponse ); ok {
232+ if respErr .Response != nil && respErr .Response .StatusCode == http .StatusNotFound {
233+ return ErrRepoNotFound
234+ }
235+ log .Printf ("gitlab CreateBookmarks create file: %v" , err )
236+ return err
199237 }
238+ log .Printf ("gitlab CreateBookmarks: %v" , err )
239+ return err
200240 }
201- return err
241+ return nil
202242}
203243
204244func (p GitLabProvider ) CreateRepo (ctx context.Context , user string , token * oauth2.Token , name string ) error {
0 commit comments