@@ -141,35 +141,31 @@ func (p GitHubProvider) GetBookmarks(ctx context.Context, user, ref string, toke
141141
142142var commitAuthor = & github.CommitAuthor {Name : SP ("Gobookmarks" ), Email : SP ("Gobookmarks@arran.net.au" )}
143143
144- func (p GitHubProvider ) getDefaultBranch (ctx context.Context , user string , client * github.Client , branch string ) (string , bool , error ) {
144+ func (p GitHubProvider ) getDefaultBranch (ctx context.Context , user string , client * github.Client , branch string ) (string , error ) {
145145 rep , resp , err := client .Repositories .Get (ctx , user , RepoName )
146- created := false
147146 if resp != nil && resp .StatusCode == 404 {
148- rep , err = p .createRepo (ctx , user , client )
149- if err != nil {
150- log .Printf ("github createRepo: %v" , err )
151- return "" , created , err
152- }
153- created = true
147+ return "" , ErrRepoNotFound
154148 }
155149 if err != nil {
156150 log .Printf ("github getDefaultBranch: %v" , err )
157- return "" , created , fmt .Errorf ("Repositories.Get: %w" , err )
151+ return "" , fmt .Errorf ("Repositories.Get: %w" , err )
158152 }
159153 if rep .DefaultBranch != nil {
160154 branch = * rep .DefaultBranch
161155 } else {
162156 branch = "main"
163157 }
164- return branch , created , nil
158+ return branch , nil
165159}
166160
167- func (p GitHubProvider ) createRepo (ctx context.Context , user string , client * github.Client ) (* github.Repository , error ) {
161+ func (p GitHubProvider ) CreateRepo (ctx context.Context , user string , token * oauth2.Token , name string ) error {
162+ client := p .client (ctx , token )
163+ RepoName = name
168164 rep := & github.Repository {Name : & RepoName , Description : SP ("Personal bookmarks" ), Private : BP (true )}
169165 rep , _ , err := client .Repositories .Create (ctx , "" , rep )
170166 if err != nil {
171167 log .Printf ("github createRepo: %v" , err )
172- return nil , fmt .Errorf ("Repositories.Create: %w" , err )
168+ return fmt .Errorf ("Repositories.Create: %w" , err )
173169 }
174170 _ , _ , err = client .Repositories .CreateFile (ctx , user , RepoName , "readme.md" , & github.RepositoryContentFileOptions {
175171 Message : SP ("Auto create from web" ),
@@ -180,9 +176,10 @@ See . https://github.com/arran4/gobookmarks `),
180176 })
181177 if err != nil {
182178 log .Printf ("github createRepo readme: %v" , err )
183- return nil , fmt .Errorf ("CreateReadme: %w" , err )
179+ return fmt .Errorf ("CreateReadme: %w" , err )
184180 }
185- return rep , nil
181+ _ = rep
182+ return nil
186183}
187184
188185func (p GitHubProvider ) createRef (ctx context.Context , user string , client * github.Client , sourceRef , branchRef string ) error {
@@ -204,7 +201,7 @@ func (p GitHubProvider) createRef(ctx context.Context, user string, client *gith
204201
205202func (p GitHubProvider ) UpdateBookmarks (ctx context.Context , user string , token * oauth2.Token , sourceRef , branch , text , expectSHA string ) error {
206203 client := p .client (ctx , token )
207- defaultBranch , created , err := p .getDefaultBranch (ctx , user , client , branch )
204+ defaultBranch , err := p .getDefaultBranch (ctx , user , client , branch )
208205 if err != nil {
209206 return err
210207 }
@@ -215,9 +212,6 @@ func (p GitHubProvider) UpdateBookmarks(ctx context.Context, user string, token
215212 if sourceRef == "" {
216213 sourceRef = branchRef
217214 }
218- if created {
219- return p .CreateBookmarks (ctx , user , token , branch , text )
220- }
221215 _ , grefResp , err := client .Git .GetRef (ctx , user , RepoName , branchRef )
222216 if err != nil && grefResp .StatusCode != 404 {
223217 log .Printf ("github UpdateBookmarks getRef: %v" , err )
@@ -231,11 +225,7 @@ func (p GitHubProvider) UpdateBookmarks(ctx context.Context, user string, token
231225 }
232226 contents , _ , resp , err := client .Repositories .GetContents (ctx , user , RepoName , "bookmarks.txt" , & github.RepositoryContentGetOptions {Ref : branchRef })
233227 if resp != nil && resp .StatusCode == 404 {
234- if _ , err := p .createRepo (ctx , user , client ); err != nil {
235- log .Printf ("github UpdateBookmarks create repo: %v" , err )
236- return fmt .Errorf ("CreateRepo: %w" , err )
237- }
238- return p .CreateBookmarks (ctx , user , token , branch , text )
228+ return ErrRepoNotFound
239229 }
240230 if err != nil {
241231 log .Printf ("github UpdateBookmarks get contents: %v" , err )
@@ -266,7 +256,7 @@ func (p GitHubProvider) CreateBookmarks(ctx context.Context, user string, token
266256 client := p .client (ctx , token )
267257 if branch == "" {
268258 var err error
269- branch , _ , err = p .getDefaultBranch (ctx , user , client , branch )
259+ branch , err = p .getDefaultBranch (ctx , user , client , branch )
270260 if err != nil {
271261 log .Printf ("github CreateBookmarks default branch: %v" , err )
272262 return err
0 commit comments