Skip to content

Commit

Permalink
Creates repo properly
Browse files Browse the repository at this point in the history
  • Loading branch information
arran4 committed Oct 22, 2023
1 parent 350aa29 commit d23dace
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 24 deletions.
138 changes: 117 additions & 21 deletions bookmarkAccess.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,70 @@ import (
"github.com/google/go-github/v55/github"
"golang.org/x/oauth2"
"net/http"
"os"
)

var (
commitAuthor = &github.CommitAuthor{
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,
Expand All @@ -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,
Expand All @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions gitAccess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -18,15 +18,15 @@ 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)
}
return branches, nil
}
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)
}
Expand Down

0 comments on commit d23dace

Please sign in to comment.