Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
164 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package cache | ||
|
||
import "strings" | ||
|
||
func splitMod(mod string) (remote, owner, repo string) { | ||
var flds []string | ||
if i := strings.Index(mod, ".git"); i > -1 { | ||
flds = strings.SplitN(mod[:i], "/", 3) | ||
} else { | ||
flds = strings.Split(mod, "/") | ||
} | ||
|
||
return flds[0], flds[1], flds[2] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package cache | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestSplitMod(t *testing.T) { | ||
tests := map[string]struct { | ||
mod string | ||
expected string | ||
}{ | ||
"simple": {mod: "github.com/owner/repo", expected: "github.com/owner/repo"}, | ||
"submodule": {mod: "github.com/owner/repo/submodule", expected: "github.com/owner/repo"}, | ||
"complex": {mod: "gitlab.com/owner/repo.git/submodule", expected: "gitlab.com/owner/repo"}, | ||
"subgroup": {mod: "gitlab.com/owner/subgroup/repo.git", expected: "gitlab.com/owner/subgroup/repo"}, | ||
"subgroup+submodule": {mod: "gitlab.com/owner/subgroup/repo.git/submodule", expected: "gitlab.com/owner/subgroup/repo"}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
rm, o, rp := splitMod(tc.mod) | ||
if fmt.Sprintf("%s/%s/%s", rm, o, rp) != tc.expected { | ||
t.Fatalf("expected: %v, got: %s/%s/%s", tc.expected, rm, o, rp) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package gitlab | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/xanzy/go-gitlab" | ||
) | ||
|
||
func NewClient() (client *gitlab.Client, err error) { | ||
return gitlab.NewClient(os.Getenv("GITLAB_TOKEN")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package gitlab | ||
|
||
import ( | ||
"archive/zip" | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/xanzy/go-gitlab" | ||
) | ||
|
||
func fetchShaZip(client *gitlab.Client, pid interface{}, sha string) (*zip.Reader, error) { | ||
format := "zip" | ||
data, _, err := client.Repositories.Archive(pid, &gitlab.ArchiveOptions{ | ||
Format: &format, | ||
SHA: &sha, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
r := bytes.NewReader(data) | ||
|
||
return zip.NewReader(r, int64(len(data))) | ||
} | ||
|
||
func FetchZip(client *gitlab.Client, owner, repo, tag string) (*zip.Reader, error) { | ||
pid := fmt.Sprintf("%s/%s", owner, repo) | ||
|
||
var sha string | ||
|
||
if tag == "v0.0.0" { | ||
bs, _, err := client.Branches.ListBranches(pid, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var branch *gitlab.Branch | ||
|
||
for _, candidate := range bs { | ||
if candidate.Default { | ||
branch = candidate | ||
|
||
break | ||
} | ||
} | ||
|
||
if branch == nil { | ||
return nil, fmt.Errorf("Could not find default branch for repository %s", pid) | ||
} | ||
|
||
sha = branch.Commit.ID | ||
} else { | ||
t, _, err := client.Tags.GetTag(pid, tag) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sha = t.Commit.ID | ||
} | ||
|
||
return fetchShaZip(client, pid, sha) | ||
} |