From d23dace84af465a62f2dd12066ef6a2cc7cd23bf Mon Sep 17 00:00:00 2001 From: Arran Ubels Date: Sun, 22 Oct 2023 23:32:16 +1100 Subject: [PATCH] Creates repo properly --- bookmarkAccess.go | 138 +++++++++++++++++++++++++++++++++++++++------- gitAccess.go | 6 +- 2 files changed, 120 insertions(+), 24 deletions(-) diff --git a/bookmarkAccess.go b/bookmarkAccess.go index c9670a7..57b318b 100644 --- a/bookmarkAccess.go +++ b/bookmarkAccess.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-github/v55/github" "golang.org/x/oauth2" "net/http" + "os" ) var ( @@ -14,46 +15,62 @@ var ( Name: SP("Gobookmarks"), Email: SP("Gobookmarks@arran.net.au"), } + RepoName = GetBookmarksRepoName() ) +func GetBookmarksRepoName() string { + ns := os.Getenv("GBM_NAMESPACE") + if ns != "" { + return fmt.Sprintf("MyBookmarks-%s", ns) + } + return "MyBookmarks" +} + func UpdateBookmarks(ctx context.Context, githubUser string, userToken *oauth2.Token, sourceRef, branch, text string) error { + client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(userToken))) + defaultBranch, created, err := GetDefaultBranch(ctx, githubUser, client, branch) + if err != nil { + return err + } if branch == "" { - branch = "main" + branch = defaultBranch } branchRef := "refs/heads/" + branch if sourceRef == "" { sourceRef = branchRef } - client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(userToken))) - - _, grefResp, err := client.Git.GetRef(ctx, githubUser, "MyBookmarks", branchRef) + if created { + return CreateBookmarks(ctx, githubUser, userToken, branch, text) + } + _, grefResp, err := client.Git.GetRef(ctx, githubUser, RepoName, branchRef) if err != nil && grefResp.StatusCode != http.StatusNotFound { return fmt.Errorf("UpdateBookmarks: client.Git.GetRef: %w", err) } if grefResp.StatusCode == http.StatusNotFound { - gsref, _, err := client.Git.GetRef(ctx, githubUser, "MyBookmarks", sourceRef) + err := CreateRef(ctx, githubUser, client, sourceRef, branchRef) if err != nil { - return fmt.Errorf("UpdateBookmarks: client.Git.GetRef sRef: %w", err) - } - _, _, err = client.Git.CreateRef(ctx, githubUser, "MyBookmarks", &github.Reference{ - Ref: &branchRef, - Object: gsref.Object, - }) - if err != nil { - return fmt.Errorf("UpdateBookmarks: client.Git.CreateRef sRef: %w", err) + return fmt.Errorf("create ref: %w", err) } } - contents, _, _, err := client.Repositories.GetContents(ctx, githubUser, "MyBookmarks", "bookmarks.txt", &github.RepositoryContentGetOptions{ + contents, _, resp, err := client.Repositories.GetContents(ctx, githubUser, RepoName, "bookmarks.txt", &github.RepositoryContentGetOptions{ Ref: branchRef, }) + switch resp.StatusCode { + case http.StatusNotFound: + if _, err := CreateRepo(ctx, githubUser, client); err != nil { + return fmt.Errorf("CreateRepo: %w", err) + } + return CreateBookmarks(ctx, githubUser, userToken, branch, text) + err = nil + } if err != nil { return fmt.Errorf("UpdateBookmarks: client.Repositories.GetContents: %w", err) } - if contents.Content == nil { + if contents == nil || contents.Content == nil { return nil } - _, _, err = client.Repositories.UpdateFile(ctx, githubUser, "MyBookmarks", "bookmarks.txt", &github.RepositoryContentFileOptions{ + _, _, err = client.Repositories.UpdateFile(ctx, githubUser, RepoName, "bookmarks.txt", &github.RepositoryContentFileOptions{ Message: SP("Auto change from web"), Content: []byte(text), Branch: &branch, @@ -68,17 +85,92 @@ func UpdateBookmarks(ctx context.Context, githubUser string, userToken *oauth2.T return nil } +func GetDefaultBranch(ctx context.Context, githubUser string, client *github.Client, branch string) (string, bool, error) { + rep, resp, err := client.Repositories.Get(ctx, githubUser, RepoName) + created := false + switch resp.StatusCode { + case http.StatusNotFound: + rep, err = CreateRepo(ctx, githubUser, client) + if err != nil { + return "", created, err + } + created = true + default: + } + if err != nil { + return "", created, fmt.Errorf("Repositories.Get: %w", err) + } + if rep.DefaultBranch != nil { + branch = *rep.DefaultBranch + } else { + branch = "main" + } + return branch, created, nil +} + +func CreateRepo(ctx context.Context, githubUser string, client *github.Client) (*github.Repository, error) { + rep := &github.Repository{ + Name: &RepoName, + Description: SP("Personal bookmarks"), + Private: BP(true), + } + var err error + rep, _, err = client.Repositories.Create(ctx, "", rep) + if err != nil { + return nil, fmt.Errorf("Repositories.Create: %w", err) + } + if _, _, err = client.Repositories.CreateFile(ctx, githubUser, RepoName, "readme.md", &github.RepositoryContentFileOptions{ + Message: SP("Auto create from web"), + Content: []byte(`# Your bookmarks + +See . https://github.com/arran4/gobookmarks `), + Author: commitAuthor, + Committer: commitAuthor, + }); err != nil { + return nil, fmt.Errorf("CreateReadme: %w", err) + } + + return rep, err +} + +func CreateRef(ctx context.Context, githubUser string, client *github.Client, sourceRef string, branchRef string) error { + gsref, resp, err := client.Git.GetRef(ctx, githubUser, RepoName, sourceRef) + switch resp.StatusCode { + case http.StatusNotFound: + err = nil + } + if err != nil { + return fmt.Errorf("UpdateBookmarks: client.Git.GetRef sRef: %w", err) + } + _, _, err = client.Git.CreateRef(ctx, githubUser, RepoName, &github.Reference{ + Ref: &branchRef, + Object: gsref.Object, + }) + if err != nil { + return fmt.Errorf("UpdateBookmarks: client.Git.CreateRef sRef: %w", err) + } + return nil +} + func SP(s string) *string { return &s } +func BP(b bool) *bool { + return &b +} + func CreateBookmarks(ctx context.Context, githubUser string, userToken *oauth2.Token, branch, text string) error { + client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(userToken))) if branch == "" { - branch = "main" + var err error + branch, _, err = GetDefaultBranch(ctx, githubUser, client, branch) + if err != nil { + return err + } } - client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(userToken))) - _, _, err := client.Repositories.CreateFile(ctx, githubUser, "MyBookmarks", "bookmarks.txt", &github.RepositoryContentFileOptions{ - Message: SP("Auto change from web"), + _, _, err := client.Repositories.CreateFile(ctx, githubUser, RepoName, "bookmarks.txt", &github.RepositoryContentFileOptions{ + Message: SP("Auto create from web"), Content: []byte(text), Branch: &branch, Author: commitAuthor, @@ -92,9 +184,13 @@ func CreateBookmarks(ctx context.Context, githubUser string, userToken *oauth2.T func GetBookmarks(ctx context.Context, githubUser string, ref string, userToken *oauth2.Token) (string, error) { client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(userToken))) - contents, _, _, err := client.Repositories.GetContents(ctx, githubUser, "MyBookmarks", "bookmarks.txt", &github.RepositoryContentGetOptions{ + contents, _, resp, err := client.Repositories.GetContents(ctx, githubUser, RepoName, "bookmarks.txt", &github.RepositoryContentGetOptions{ Ref: ref, }) + switch resp.StatusCode { + case http.StatusNotFound: + return "", nil + } if err != nil { return "", fmt.Errorf("GetBookmarks: %w", err) } diff --git a/gitAccess.go b/gitAccess.go index f5c8fd1..8ff41c9 100644 --- a/gitAccess.go +++ b/gitAccess.go @@ -9,7 +9,7 @@ import ( func GetTags(ctx context.Context, githubUser string, userToken *oauth2.Token) ([]*github.RepositoryTag, error) { client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(userToken))) - tags, _, err := client.Repositories.ListTags(ctx, githubUser, "MyBookmarks", &github.ListOptions{}) + tags, _, err := client.Repositories.ListTags(ctx, githubUser, RepoName, &github.ListOptions{}) if err != nil { return nil, fmt.Errorf("ListTags: %w", err) } @@ -18,7 +18,7 @@ func GetTags(ctx context.Context, githubUser string, userToken *oauth2.Token) ([ func GetBranches(ctx context.Context, githubUser string, userToken *oauth2.Token) ([]*github.Branch, error) { client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(userToken))) - branches, _, err := client.Repositories.ListBranches(ctx, githubUser, "MyBookmarks", &github.BranchListOptions{}) + branches, _, err := client.Repositories.ListBranches(ctx, githubUser, RepoName, &github.BranchListOptions{}) if err != nil { return nil, fmt.Errorf("ListBranches: %w", err) } @@ -26,7 +26,7 @@ func GetBranches(ctx context.Context, githubUser string, userToken *oauth2.Token } func GetCommits(ctx context.Context, githubUser string, userToken *oauth2.Token) ([]*github.RepositoryCommit, error) { client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(userToken))) - commits, _, err := client.Repositories.ListCommits(ctx, githubUser, "MyBookmarks", &github.CommitsListOptions{}) + commits, _, err := client.Repositories.ListCommits(ctx, githubUser, RepoName, &github.CommitsListOptions{}) if err != nil { return nil, fmt.Errorf("ListCommits: %w", err) }