66 "context"
77 "encoding/base64"
88 "fmt"
9+ "log"
910 "strings"
1011
1112 "github.com/google/go-github/v55/github"
@@ -54,6 +55,7 @@ func (GitHubProvider) client(ctx context.Context, token *oauth2.Token) *github.C
5455func (p GitHubProvider ) CurrentUser (ctx context.Context , token * oauth2.Token ) (* User , error ) {
5556 u , _ , err := p .client (ctx , token ).Users .Get (ctx , "" )
5657 if err != nil {
58+ log .Printf ("github CurrentUser: %v" , err )
5759 return nil , err
5860 }
5961 user := & User {}
@@ -66,6 +68,7 @@ func (p GitHubProvider) CurrentUser(ctx context.Context, token *oauth2.Token) (*
6668func (p GitHubProvider ) GetTags (ctx context.Context , user string , token * oauth2.Token ) ([]* Tag , error ) {
6769 tags , _ , err := p .client (ctx , token ).Repositories .ListTags (ctx , user , RepoName , & github.ListOptions {})
6870 if err != nil {
71+ log .Printf ("github GetTags: %v" , err )
6972 return nil , fmt .Errorf ("ListTags: %w" , err )
7073 }
7174 res := make ([]* Tag , 0 , len (tags ))
@@ -78,6 +81,7 @@ func (p GitHubProvider) GetTags(ctx context.Context, user string, token *oauth2.
7881func (p GitHubProvider ) GetBranches (ctx context.Context , user string , token * oauth2.Token ) ([]* Branch , error ) {
7982 bs , _ , err := p .client (ctx , token ).Repositories .ListBranches (ctx , user , RepoName , & github.BranchListOptions {})
8083 if err != nil {
84+ log .Printf ("github GetBranches: %v" , err )
8185 return nil , fmt .Errorf ("ListBranches: %w" , err )
8286 }
8387 res := make ([]* Branch , 0 , len (bs ))
@@ -90,6 +94,7 @@ func (p GitHubProvider) GetBranches(ctx context.Context, user string, token *oau
9094func (p GitHubProvider ) GetCommits (ctx context.Context , user string , token * oauth2.Token ) ([]* Commit , error ) {
9195 cs , _ , err := p .client (ctx , token ).Repositories .ListCommits (ctx , user , RepoName , & github.CommitsListOptions {})
9296 if err != nil {
97+ log .Printf ("github GetCommits: %v" , err )
9398 return nil , fmt .Errorf ("ListCommits: %w" , err )
9499 }
95100 res := make ([]* Commit , 0 , len (cs ))
@@ -116,13 +121,15 @@ func (p GitHubProvider) GetBookmarks(ctx context.Context, user, ref string, toke
116121 return "" , "" , nil
117122 }
118123 if err != nil {
124+ log .Printf ("github GetBookmarks: %v" , err )
119125 return "" , "" , fmt .Errorf ("GetBookmarks: %w" , err )
120126 }
121127 if contents .Content == nil {
122128 return "" , "" , nil
123129 }
124130 b , err := base64 .StdEncoding .DecodeString (* contents .Content )
125131 if err != nil {
132+ log .Printf ("github GetBookmarks decode: %v" , err )
126133 return "" , "" , fmt .Errorf ("GetBookmarks: %w" , err )
127134 }
128135 sha := ""
@@ -140,11 +147,13 @@ func (p GitHubProvider) getDefaultBranch(ctx context.Context, user string, clien
140147 if resp != nil && resp .StatusCode == 404 {
141148 rep , err = p .createRepo (ctx , user , client )
142149 if err != nil {
150+ log .Printf ("github createRepo: %v" , err )
143151 return "" , created , err
144152 }
145153 created = true
146154 }
147155 if err != nil {
156+ log .Printf ("github getDefaultBranch: %v" , err )
148157 return "" , created , fmt .Errorf ("Repositories.Get: %w" , err )
149158 }
150159 if rep .DefaultBranch != nil {
@@ -159,6 +168,7 @@ func (p GitHubProvider) createRepo(ctx context.Context, user string, client *git
159168 rep := & github.Repository {Name : & RepoName , Description : SP ("Personal bookmarks" ), Private : BP (true )}
160169 rep , _ , err := client .Repositories .Create (ctx , "" , rep )
161170 if err != nil {
171+ log .Printf ("github createRepo: %v" , err )
162172 return nil , fmt .Errorf ("Repositories.Create: %w" , err )
163173 }
164174 _ , _ , err = client .Repositories .CreateFile (ctx , user , RepoName , "readme.md" , & github.RepositoryContentFileOptions {
@@ -169,6 +179,7 @@ See . https://github.com/arran4/gobookmarks `),
169179 Author : commitAuthor , Committer : commitAuthor ,
170180 })
171181 if err != nil {
182+ log .Printf ("github createRepo readme: %v" , err )
172183 return nil , fmt .Errorf ("CreateReadme: %w" , err )
173184 }
174185 return rep , nil
@@ -180,10 +191,12 @@ func (p GitHubProvider) createRef(ctx context.Context, user string, client *gith
180191 err = nil
181192 }
182193 if err != nil {
194+ log .Printf ("github createRef getRef: %v" , err )
183195 return fmt .Errorf ("GetRef: %w" , err )
184196 }
185197 _ , _ , err = client .Git .CreateRef (ctx , user , RepoName , & github.Reference {Ref : & branchRef , Object : gsref .Object })
186198 if err != nil {
199+ log .Printf ("github createRef create: %v" , err )
187200 return fmt .Errorf ("CreateRef: %w" , err )
188201 }
189202 return nil
@@ -207,21 +220,25 @@ func (p GitHubProvider) UpdateBookmarks(ctx context.Context, user string, token
207220 }
208221 _ , grefResp , err := client .Git .GetRef (ctx , user , RepoName , branchRef )
209222 if err != nil && grefResp .StatusCode != 404 {
223+ log .Printf ("github UpdateBookmarks getRef: %v" , err )
210224 return fmt .Errorf ("GetRef: %w" , err )
211225 }
212226 if grefResp .StatusCode == 404 {
213227 if err := p .createRef (ctx , user , client , sourceRef , branchRef ); err != nil {
228+ log .Printf ("github UpdateBookmarks create ref: %v" , err )
214229 return fmt .Errorf ("create ref: %w" , err )
215230 }
216231 }
217232 contents , _ , resp , err := client .Repositories .GetContents (ctx , user , RepoName , "bookmarks.txt" , & github.RepositoryContentGetOptions {Ref : branchRef })
218233 if resp != nil && resp .StatusCode == 404 {
219234 if _ , err := p .createRepo (ctx , user , client ); err != nil {
235+ log .Printf ("github UpdateBookmarks create repo: %v" , err )
220236 return fmt .Errorf ("CreateRepo: %w" , err )
221237 }
222238 return p .CreateBookmarks (ctx , user , token , branch , text )
223239 }
224240 if err != nil {
241+ log .Printf ("github UpdateBookmarks get contents: %v" , err )
225242 return fmt .Errorf ("GetContents: %w" , err )
226243 }
227244 if contents == nil || contents .Content == nil {
@@ -239,6 +256,7 @@ func (p GitHubProvider) UpdateBookmarks(ctx context.Context, user string, token
239256 Committer : commitAuthor ,
240257 })
241258 if err != nil {
259+ log .Printf ("github UpdateBookmarks update: %v" , err )
242260 return fmt .Errorf ("UpdateBookmarks: %w" , err )
243261 }
244262 return nil
@@ -250,6 +268,7 @@ func (p GitHubProvider) CreateBookmarks(ctx context.Context, user string, token
250268 var err error
251269 branch , _ , err = p .getDefaultBranch (ctx , user , client , branch )
252270 if err != nil {
271+ log .Printf ("github CreateBookmarks default branch: %v" , err )
253272 return err
254273 }
255274 }
@@ -261,6 +280,7 @@ func (p GitHubProvider) CreateBookmarks(ctx context.Context, user string, token
261280 Committer : commitAuthor ,
262281 })
263282 if err != nil {
283+ log .Printf ("github CreateBookmarks: %v" , err )
264284 return fmt .Errorf ("CreateBookmarks: %w" , err )
265285 }
266286 return nil
0 commit comments