66 "context"
77 "encoding/base64"
88 "fmt"
9+ "log"
910
1011 "github.com/google/go-github/v55/github"
1112 "golang.org/x/oauth2"
@@ -36,6 +37,7 @@ func (GitHubProvider) client(ctx context.Context, token *oauth2.Token) *github.C
3637func (p GitHubProvider ) CurrentUser (ctx context.Context , token * oauth2.Token ) (* User , error ) {
3738 u , _ , err := p .client (ctx , token ).Users .Get (ctx , "" )
3839 if err != nil {
40+ log .Printf ("github CurrentUser: %v" , err )
3941 return nil , err
4042 }
4143 user := & User {}
@@ -48,6 +50,7 @@ func (p GitHubProvider) CurrentUser(ctx context.Context, token *oauth2.Token) (*
4850func (p GitHubProvider ) GetTags (ctx context.Context , user string , token * oauth2.Token ) ([]* Tag , error ) {
4951 tags , _ , err := p .client (ctx , token ).Repositories .ListTags (ctx , user , RepoName , & github.ListOptions {})
5052 if err != nil {
53+ log .Printf ("github GetTags: %v" , err )
5154 return nil , fmt .Errorf ("ListTags: %w" , err )
5255 }
5356 res := make ([]* Tag , 0 , len (tags ))
@@ -60,6 +63,7 @@ func (p GitHubProvider) GetTags(ctx context.Context, user string, token *oauth2.
6063func (p GitHubProvider ) GetBranches (ctx context.Context , user string , token * oauth2.Token ) ([]* Branch , error ) {
6164 bs , _ , err := p .client (ctx , token ).Repositories .ListBranches (ctx , user , RepoName , & github.BranchListOptions {})
6265 if err != nil {
66+ log .Printf ("github GetBranches: %v" , err )
6367 return nil , fmt .Errorf ("ListBranches: %w" , err )
6468 }
6569 res := make ([]* Branch , 0 , len (bs ))
@@ -72,6 +76,7 @@ func (p GitHubProvider) GetBranches(ctx context.Context, user string, token *oau
7276func (p GitHubProvider ) GetCommits (ctx context.Context , user string , token * oauth2.Token ) ([]* Commit , error ) {
7377 cs , _ , err := p .client (ctx , token ).Repositories .ListCommits (ctx , user , RepoName , & github.CommitsListOptions {})
7478 if err != nil {
79+ log .Printf ("github GetCommits: %v" , err )
7580 return nil , fmt .Errorf ("ListCommits: %w" , err )
7681 }
7782 res := make ([]* Commit , 0 , len (cs ))
@@ -98,13 +103,15 @@ func (p GitHubProvider) GetBookmarks(ctx context.Context, user, ref string, toke
98103 return "" , "" , nil
99104 }
100105 if err != nil {
106+ log .Printf ("github GetBookmarks: %v" , err )
101107 return "" , "" , fmt .Errorf ("GetBookmarks: %w" , err )
102108 }
103109 if contents .Content == nil {
104110 return "" , "" , nil
105111 }
106112 b , err := base64 .StdEncoding .DecodeString (* contents .Content )
107113 if err != nil {
114+ log .Printf ("github GetBookmarks decode: %v" , err )
108115 return "" , "" , fmt .Errorf ("GetBookmarks: %w" , err )
109116 }
110117 sha := ""
@@ -122,11 +129,13 @@ func (p GitHubProvider) getDefaultBranch(ctx context.Context, user string, clien
122129 if resp != nil && resp .StatusCode == 404 {
123130 rep , err = p .createRepo (ctx , user , client )
124131 if err != nil {
132+ log .Printf ("github createRepo: %v" , err )
125133 return "" , created , err
126134 }
127135 created = true
128136 }
129137 if err != nil {
138+ log .Printf ("github getDefaultBranch: %v" , err )
130139 return "" , created , fmt .Errorf ("Repositories.Get: %w" , err )
131140 }
132141 if rep .DefaultBranch != nil {
@@ -141,6 +150,7 @@ func (p GitHubProvider) createRepo(ctx context.Context, user string, client *git
141150 rep := & github.Repository {Name : & RepoName , Description : SP ("Personal bookmarks" ), Private : BP (true )}
142151 rep , _ , err := client .Repositories .Create (ctx , "" , rep )
143152 if err != nil {
153+ log .Printf ("github createRepo: %v" , err )
144154 return nil , fmt .Errorf ("Repositories.Create: %w" , err )
145155 }
146156 _ , _ , err = client .Repositories .CreateFile (ctx , user , RepoName , "readme.md" , & github.RepositoryContentFileOptions {
@@ -151,6 +161,7 @@ See . https://github.com/arran4/gobookmarks `),
151161 Author : commitAuthor , Committer : commitAuthor ,
152162 })
153163 if err != nil {
164+ log .Printf ("github createRepo readme: %v" , err )
154165 return nil , fmt .Errorf ("CreateReadme: %w" , err )
155166 }
156167 return rep , nil
@@ -162,10 +173,12 @@ func (p GitHubProvider) createRef(ctx context.Context, user string, client *gith
162173 err = nil
163174 }
164175 if err != nil {
176+ log .Printf ("github createRef getRef: %v" , err )
165177 return fmt .Errorf ("GetRef: %w" , err )
166178 }
167179 _ , _ , err = client .Git .CreateRef (ctx , user , RepoName , & github.Reference {Ref : & branchRef , Object : gsref .Object })
168180 if err != nil {
181+ log .Printf ("github createRef create: %v" , err )
169182 return fmt .Errorf ("CreateRef: %w" , err )
170183 }
171184 return nil
@@ -189,21 +202,25 @@ func (p GitHubProvider) UpdateBookmarks(ctx context.Context, user string, token
189202 }
190203 _ , grefResp , err := client .Git .GetRef (ctx , user , RepoName , branchRef )
191204 if err != nil && grefResp .StatusCode != 404 {
205+ log .Printf ("github UpdateBookmarks getRef: %v" , err )
192206 return fmt .Errorf ("GetRef: %w" , err )
193207 }
194208 if grefResp .StatusCode == 404 {
195209 if err := p .createRef (ctx , user , client , sourceRef , branchRef ); err != nil {
210+ log .Printf ("github UpdateBookmarks create ref: %v" , err )
196211 return fmt .Errorf ("create ref: %w" , err )
197212 }
198213 }
199214 contents , _ , resp , err := client .Repositories .GetContents (ctx , user , RepoName , "bookmarks.txt" , & github.RepositoryContentGetOptions {Ref : branchRef })
200215 if resp != nil && resp .StatusCode == 404 {
201216 if _ , err := p .createRepo (ctx , user , client ); err != nil {
217+ log .Printf ("github UpdateBookmarks create repo: %v" , err )
202218 return fmt .Errorf ("CreateRepo: %w" , err )
203219 }
204220 return p .CreateBookmarks (ctx , user , token , branch , text )
205221 }
206222 if err != nil {
223+ log .Printf ("github UpdateBookmarks get contents: %v" , err )
207224 return fmt .Errorf ("GetContents: %w" , err )
208225 }
209226 if contents == nil || contents .Content == nil {
@@ -221,6 +238,7 @@ func (p GitHubProvider) UpdateBookmarks(ctx context.Context, user string, token
221238 Committer : commitAuthor ,
222239 })
223240 if err != nil {
241+ log .Printf ("github UpdateBookmarks update: %v" , err )
224242 return fmt .Errorf ("UpdateBookmarks: %w" , err )
225243 }
226244 return nil
@@ -232,6 +250,7 @@ func (p GitHubProvider) CreateBookmarks(ctx context.Context, user string, token
232250 var err error
233251 branch , _ , err = p .getDefaultBranch (ctx , user , client , branch )
234252 if err != nil {
253+ log .Printf ("github CreateBookmarks default branch: %v" , err )
235254 return err
236255 }
237256 }
@@ -243,6 +262,7 @@ func (p GitHubProvider) CreateBookmarks(ctx context.Context, user string, token
243262 Committer : commitAuthor ,
244263 })
245264 if err != nil {
265+ log .Printf ("github CreateBookmarks: %v" , err )
246266 return fmt .Errorf ("CreateBookmarks: %w" , err )
247267 }
248268 return nil
0 commit comments