Skip to content

Commit

Permalink
push mod fetch details into remote types
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Worm <tony@hofstadter.io>
  • Loading branch information
verdverm committed Aug 8, 2021
1 parent fe4fbb8 commit f202e6a
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 162 deletions.
143 changes: 16 additions & 127 deletions lib/mod/cache/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import (
"fmt"
"os"

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
googithub "github.com/google/go-github/v30/github"

"github.com/hofstadter-io/hof/lib/yagu"
"github.com/hofstadter-io/hof/lib/yagu/repos/git"
"github.com/hofstadter-io/hof/lib/yagu/repos/github"
"github.com/hofstadter-io/hof/lib/yagu/repos/gitlab"
Expand All @@ -33,144 +30,36 @@ func Fetch(lang, mod, ver, pev string) (err error) {
}

// else we have it already
// fmt.Println("Found in cache")

return nil
}

func fetch(lang, mod, ver string, private bool) error {
remote, owner, repo := parseModURL(mod)
tag := ver

if private {
return fetchGit(lang, remote, owner, repo, tag, true)
} else if remote == "github.com" {
return fetchGitHub(lang, owner, repo, tag)
} else if remote == "gitlab.com" {
return fetchGitLab(lang, owner, repo, tag)
}
return fetchGit(lang, remote, owner, repo, tag, private)
}

func fetchGit(lang, remote, owner, repo, tag string, private bool) error {
FS := memfs.New()

if err := git.FetchGit(FS, remote, owner, repo, tag, private); err != nil {
return fmt.Errorf("While fetching from git\n%w\n", err)
}

if err := Write(lang, remote, owner, repo, tag, FS); err != nil {
return fmt.Errorf("While writing to cache\n%w\n", err)
}

return nil
}

func fetchGitLab(lang, owner, repo, tag string) (err error) {
FS := memfs.New()
client, err := gitlab.NewClient()
if err != nil {
return err
}

zReader, err := gitlab.FetchZip(client, owner, repo, tag)
if err != nil {
return fmt.Errorf("While fetching from GitLab\n%w\n", err)
}

if err := yagu.BillyLoadFromZip(zReader, FS, true); err != nil {
return fmt.Errorf("While reading zipfile\n%w\n", err)
}

if err := Write(lang, "gitlab.com", owner, repo, tag, FS); err != nil {
return fmt.Errorf("While writing to cache\n%w\n", err)
}

return nil
}

func fetchGitHub(lang, owner, repo, tag string) (err error) {
FS := memfs.New()

if tag == "v0.0.0" {
err = fetchGitHubBranch(FS, lang, owner, repo, "")
} else {
err = fetchGitHubTag(FS, lang, owner, repo, tag)
}
if err != nil {
return fmt.Errorf("While fetching from github\n%w\n", err)
}

err = Write(lang, "github.com", owner, repo, tag, FS)
if err != nil {
return fmt.Errorf("While writing to cache\n%w\n", err)
}
remote, owner, repo := parseModURL(mod)
tag := ver

return nil
}
func fetchGitHubBranch(FS billy.Filesystem, lang, owner, repo, branch string) error {
client, err := github.NewClient()
if err != nil {
return err
}
// TODO, how to deal with self-hosted / enterprise repos?

if branch == "" {
r, err := github.GetRepo(client, owner, repo)
if err != nil {
return err
switch remote {
case "github.com":
if err := github.Fetch(FS, owner, repo, tag); err != nil {
return fmt.Errorf("While fetching from github\n%w\n", err)
}
branch = *r.DefaultBranch

fmt.Printf("%#+v\n", *r)
}

// fmt.Println("Fetch github BRANCH", lang, owner, repo, branch)

zReader, err := github.FetchBranchZip(client, owner, repo, branch)
if err != nil {
return fmt.Errorf("While fetching branch zipfile for %s/%s@%s\n%w\n", owner, repo, branch, err)
}

err = yagu.BillyLoadFromZip(zReader, FS, true)
if err != nil {
return fmt.Errorf("While reading branch zipfile\n%w\n", err)
}

return nil
}
func fetchGitHubTag(FS billy.Filesystem, lang, owner, repo, tag string) error {
// fmt.Println("Fetch github TAG", lang, owner, repo, tag)
client, err := github.NewClient()
if err != nil {
return err
}

tags, err := github.GetTags(client, owner, repo)
if err != nil {
return err
}

// The tag we are looking for
var T *googithub.RepositoryTag
for _, t := range tags {
if tag != "" && tag == *t.Name {
T = t
// fmt.Printf("FOUND %v\n", *t.Name)
case "gitlab.com":
if err := gitlab.Fetch(FS, owner, repo, tag); err != nil {
return fmt.Errorf("While fetching from gitlab\n%w\n", err)
}
}
if T == nil {
return fmt.Errorf("Did not find tag %q for 'https://github.com/%s/%s' @%s", tag, owner, repo, tag)
}

zReader, err := github.FetchTagZip(client, T)
if err != nil {
fmt.Printf("Error: %v\n", err)
return fmt.Errorf("While fetching tag zipfile\n%w\n", err)
default:
if err := git.Fetch(FS, remote, owner, repo, tag, private); err != nil {
return fmt.Errorf("While fetching from git\n%w\n", err)
}
}

err = yagu.BillyLoadFromZip(zReader, FS, true)
if err != nil {
return fmt.Errorf("While reading tag zipfile\n%w\n", err)
if err := Write(lang, remote, owner, repo, tag, FS); err != nil {
return fmt.Errorf("While writing to cache\n%w\n", err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion lib/yagu/repos/git/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func CloneRepoRef(srcUrl string, ref *plumbing.Reference) (*GitRepo, error) {

// FetchGit clone the repository inside FS.
// If private flag is set, it will look for netrc credentials, fallbacking to SSH
func FetchGit(FS billy.Filesystem, remote, owner, repo, tag string, private bool) error {
func Fetch(FS billy.Filesystem, remote, owner, repo, tag string, private bool) error {
srcRepo := path.Join(owner, repo)
gco := &gogit.CloneOptions{
URL: fmt.Sprintf("https://%s/%s", remote, srcRepo),
Expand Down
2 changes: 2 additions & 0 deletions lib/yagu/repos/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"context"
"fmt"
"os"

"golang.org/x/oauth2"
Expand All @@ -15,6 +16,7 @@ func NewClient() (client *github.Client, err error) {
ctx := context.Background()

if token := os.Getenv(TokenEnv); token != "" {
fmt.Println("GITHUB OAUTH")
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
Expand Down
78 changes: 49 additions & 29 deletions lib/yagu/repos/github/tags.go → lib/yagu/repos/github/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,69 @@ package github
import (
"archive/zip"
"bytes"
"context"
"fmt"
"strings"

"github.com/go-git/go-billy/v5"
"github.com/google/go-github/v30/github"
"github.com/parnurzeal/gorequest"

"github.com/hofstadter-io/hof/lib/yagu"
)

func GetTagsSplit(client *github.Client, module string) ([]*github.RepositoryTag, error) {
flds := strings.SplitN(module, "/", 1)
domain, rest := flds[0], flds[1]
func Fetch(FS billy.Filesystem, owner, repo, tag string) (error) {
client, err := NewClient()
if err != nil {
return err
}

if domain != "github.com" {
return nil, fmt.Errorf("Github Tags Fetch called with non 'github.com' domain %q", module)
var zReader *zip.Reader

if tag == "v0.0.0" {
r, err := GetRepo(client, owner, repo)
if err != nil {
return err
}

zReader, err = FetchBranchZip(client, owner, repo, *r.DefaultBranch)
if err != nil {
return fmt.Errorf("While fetching branch zipfile for %s/%s@%s\n%w\n", owner, repo, *r.DefaultBranch, err)
}
} else {
tags, err := GetTags(client, owner, repo)
if err != nil {
return err
}

// The tag we are looking for
var T *github.RepositoryTag
for _, t := range tags {
if tag != "" && tag == *t.Name {
T = t
// fmt.Printf("FOUND %v\n", *t.Name)
}
}

if T == nil {
return fmt.Errorf("Did not find tag %q for 'https://github.com/%s/%s' @%s", tag, owner, repo, tag)
}

zReader, err = FetchTagZip(client, T)
if err != nil {
fmt.Printf("Error: %v\n", err)
return fmt.Errorf("While fetching tag zipfile\n%w\n", err)
}
}

flds = strings.Split(rest, "/")
owner, repo := flds[0], flds[1]
tags, _, err := client.Repositories.ListTags(context.Background(), owner, repo, nil)
if err != nil {
return nil, err
return fmt.Errorf("While fetching from github\n%w\n", err)
}
return tags, nil
}

func GetRepo(client *github.Client, owner, repo string) (*github.Repository, error) {
r, _, err := client.Repositories.Get(context.Background(), owner, repo)
return r, err
}

func GetBranch(client *github.Client, owner, repo, branch string) (*github.Branch, error) {
b, _, err := client.Repositories.GetBranch(context.Background(), owner, repo, branch)
return b, err
}

func GetBranches(client *github.Client, owner, repo, branch string) ([]*github.Branch, error) {
bs, _, err := client.Repositories.ListBranches(context.Background(), owner, repo, nil)
return bs, err
}
if err := yagu.BillyLoadFromZip(zReader, FS, true); err != nil {
return fmt.Errorf("While reading zipfile\n%w\n", err)
}

func GetTags(client *github.Client, owner, repo string) ([]*github.RepositoryTag, error) {
tags, _, err := client.Repositories.ListTags(context.Background(), owner, repo, nil)
return tags, err
return nil
}

func FetchTagZip(client *github.Client, tag *github.RepositoryTag) (*zip.Reader, error) {
Expand Down
47 changes: 47 additions & 0 deletions lib/yagu/repos/github/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package github

import (
"context"
"fmt"
"strings"

"github.com/google/go-github/v30/github"
)

func GetTagsSplit(client *github.Client, module string) ([]*github.RepositoryTag, error) {
flds := strings.SplitN(module, "/", 1)
domain, rest := flds[0], flds[1]

if domain != "github.com" {
return nil, fmt.Errorf("Github Tags Fetch called with non 'github.com' domain %q", module)
}

flds = strings.Split(rest, "/")
owner, repo := flds[0], flds[1]
tags, _, err := client.Repositories.ListTags(context.Background(), owner, repo, nil)
if err != nil {
return nil, err
}
return tags, nil
}

func GetRepo(client *github.Client, owner, repo string) (*github.Repository, error) {
r, _, err := client.Repositories.Get(context.Background(), owner, repo)
return r, err
}

func GetBranch(client *github.Client, owner, repo, branch string) (*github.Branch, error) {
b, _, err := client.Repositories.GetBranch(context.Background(), owner, repo, branch)
return b, err
}

func GetBranches(client *github.Client, owner, repo, branch string) ([]*github.Branch, error) {
bs, _, err := client.Repositories.ListBranches(context.Background(), owner, repo, nil)
return bs, err
}

func GetTags(client *github.Client, owner, repo string) ([]*github.RepositoryTag, error) {
tags, _, err := client.Repositories.ListTags(context.Background(), owner, repo, nil)
return tags, err
}

Loading

0 comments on commit f202e6a

Please sign in to comment.