Skip to content

Commit dea01e8

Browse files
authored
Merge pull request #35 from arran4/codex/improve-logging-and-error-handling-for-gitlab-repository
Improve gitlab handling
2 parents 34418c1 + 1de8f8d commit dea01e8

File tree

4 files changed

+78
-9
lines changed

4 files changed

+78
-9
lines changed

data_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func testFuncMap() template.FuncMap {
7878
}, nil
7979
},
8080
"bookmarksOrEditBookmarks": func() (string, error) { return "Category: Demo\nhttps://example.com Home", nil },
81+
"bookmarksExist": func() (bool, error) { return true, nil },
8182
"bookmarksSHA": func() (string, error) { return "sha", nil },
8283
"branchOrEditBranch": func() (string, error) { return "main", nil },
8384
"tags": func() ([]*Tag, error) {

funcs.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gobookmarks
22

33
import (
4+
"errors"
45
"fmt"
56
"github.com/gorilla/sessions"
67
"golang.org/x/oauth2"
@@ -72,6 +73,9 @@ func NewFuncs(r *http.Request) template.FuncMap {
7273
}
7374
return Bookmarks(r)
7475
},
76+
"bookmarksExist": func() (bool, error) {
77+
return BookmarksExist(r)
78+
},
7579
"bookmarksSHA": func() (string, error) {
7680
session := r.Context().Value(ContextValues("session")).(*sessions.Session)
7781
githubUser, _ := session.Values["GithubUser"].(*User)
@@ -216,3 +220,24 @@ func Bookmarks(r *http.Request) (string, error) {
216220
}
217221
return bookmarks, nil
218222
}
223+
224+
func BookmarksExist(r *http.Request) (bool, error) {
225+
session := r.Context().Value(ContextValues("session")).(*sessions.Session)
226+
githubUser, _ := session.Values["GithubUser"].(*User)
227+
token, _ := session.Values["Token"].(*oauth2.Token)
228+
ref := r.URL.Query().Get("ref")
229+
230+
login := ""
231+
if githubUser != nil {
232+
login = githubUser.Login
233+
}
234+
235+
bookmarks, _, err := GetBookmarks(r.Context(), login, ref, token)
236+
if err != nil {
237+
if errors.Is(err, ErrRepoNotFound) {
238+
return false, nil
239+
}
240+
return false, fmt.Errorf("bookmarks exist: %w", err)
241+
}
242+
return bookmarks != "", nil
243+
}

provider_gitlab.go

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
154173
func (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

204244
func (p GitLabProvider) CreateRepo(ctx context.Context, user string, token *oauth2.Token, name string) error {

templates/indexPage.gohtml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
You will need to login to see this page:
44
<a href="{{ OAuth2URL }}">Login</a><br>
55
{{else}}
6+
{{- if not bookmarksExist }}
7+
<p>Your bookmarks repository was not found. Click <a href="/edit">here</a> to create it.</p>
8+
{{- end }}
69
{{- range bookmarkPages }}
710
<div class="bookmarkPage{{ if useCssColumns }} cssColumns{{ end }}">
811
{{- range .Blocks }}

0 commit comments

Comments
 (0)