generated from block/oss-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Private gomod repo support #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2c69152
feat: Private gomod repos
js-murph 0a1e0d5
fix: Cleanup a little
js-murph 7d9cf4c
fix: More cleanup
js-murph 41d8720
Merge branch 'main' into johnm/gomod-private-attempt-three
js-murph 5669257
fix: Comment for fetcher behaviour
js-murph c1ba40c
fix: Move matcher to fetcher module
js-murph ea3cbd8
fix: Linting issues
js-murph d0ab47b
Update internal/strategy/gomod/gomod.go
js-murph e2b01e0
fix: getting logger from a different object
js-murph 453bc28
Merge branch 'johnm/gomod-private-attempt-three' of https://github.co…
js-murph f351f16
fix: Use json marshalling instead of constructing as a string
js-murph af37b6f
fix: linting
js-murph 84b74a0
Merge branch 'main' into johnm/gomod-private-attempt-three
js-murph a962701
fix: Missing imports
js-murph e3dc51e
fix: Bug with empty cache
js-murph File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,73 @@ | ||
| package gomod | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "path" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/alecthomas/errors" | ||
| "github.com/goproxy/goproxy" | ||
| ) | ||
|
|
||
| // CompositeFetcher routes module requests to either public or private fetchers based on module path patterns. | ||
| type CompositeFetcher struct { | ||
| publicFetcher goproxy.Fetcher | ||
| privateFetcher goproxy.Fetcher | ||
| patterns []string | ||
| } | ||
|
|
||
| func NewCompositeFetcher( | ||
| publicFetcher goproxy.Fetcher, | ||
| privateFetcher goproxy.Fetcher, | ||
| patterns []string, | ||
| ) *CompositeFetcher { | ||
| return &CompositeFetcher{ | ||
| publicFetcher: publicFetcher, | ||
| privateFetcher: privateFetcher, | ||
| patterns: patterns, | ||
| } | ||
| } | ||
|
|
||
| func (c *CompositeFetcher) IsPrivate(modulePath string) bool { | ||
| for _, pattern := range c.patterns { | ||
| matched, err := path.Match(pattern, modulePath) | ||
| if err == nil && matched { | ||
| return true | ||
| } | ||
|
|
||
| if strings.HasPrefix(modulePath, pattern+"/") || modulePath == pattern { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| func (c *CompositeFetcher) Query(ctx context.Context, path, query string) (version string, t time.Time, err error) { | ||
| if c.IsPrivate(path) { | ||
| v, tm, err := c.privateFetcher.Query(ctx, path, query) | ||
| return v, tm, errors.Wrap(err, "private fetcher query") | ||
| } | ||
| v, tm, err := c.publicFetcher.Query(ctx, path, query) | ||
| return v, tm, errors.Wrap(err, "public fetcher query") | ||
| } | ||
|
|
||
| func (c *CompositeFetcher) List(ctx context.Context, path string) (versions []string, err error) { | ||
| if c.IsPrivate(path) { | ||
| v, err := c.privateFetcher.List(ctx, path) | ||
| return v, errors.Wrap(err, "private fetcher list") | ||
| } | ||
| v, err := c.publicFetcher.List(ctx, path) | ||
| return v, errors.Wrap(err, "public fetcher list") | ||
| } | ||
|
|
||
| func (c *CompositeFetcher) Download(ctx context.Context, path, version string) (info, mod, zip io.ReadSeekCloser, err error) { | ||
| if c.IsPrivate(path) { | ||
| i, m, z, err := c.privateFetcher.Download(ctx, path, version) | ||
| return i, m, z, errors.Wrap(err, "private fetcher download") | ||
| } | ||
| i, m, z, err := c.publicFetcher.Download(ctx, path, version) | ||
| return i, m, z, errors.Wrap(err, "public fetcher download") | ||
| } |
This file contains hidden or 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,123 @@ | ||
| package gomod_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/block/cachew/internal/strategy/gomod" | ||
| ) | ||
|
|
||
| func TestCompositeFetcher_isPrivate(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| patterns []string | ||
| modulePath string | ||
| want bool | ||
| }{ | ||
| { | ||
| name: "exact match single pattern", | ||
| patterns: []string{"github.com/squareup"}, | ||
| modulePath: "github.com/squareup", | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "exact match with multiple patterns", | ||
| patterns: []string{"github.com/org1", "github.com/squareup", "github.com/org2"}, | ||
| modulePath: "github.com/squareup", | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "prefix match - one level deep", | ||
| patterns: []string{"github.com/squareup"}, | ||
| modulePath: "github.com/squareup/repo", | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "prefix match - two levels deep", | ||
| patterns: []string{"github.com/squareup"}, | ||
| modulePath: "github.com/squareup/repo/submodule", | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "prefix match with multiple patterns", | ||
| patterns: []string{"github.com/org1", "github.com/squareup"}, | ||
| modulePath: "github.com/squareup/repo", | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "wildcard match", | ||
| patterns: []string{"github.com/squareup/*"}, | ||
| modulePath: "github.com/squareup/repo", | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "wildcard match - multiple levels", | ||
| patterns: []string{"github.com/*/*"}, | ||
| modulePath: "github.com/squareup/repo", | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "no match - different org", | ||
| patterns: []string{"github.com/squareup"}, | ||
| modulePath: "github.com/other/repo", | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "no match - different host", | ||
| patterns: []string{"github.com/squareup"}, | ||
| modulePath: "gitlab.com/squareup/repo", | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "no match - prefix without slash", | ||
| patterns: []string{"github.com/square"}, | ||
| modulePath: "github.com/squareup/repo", | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "no match - empty patterns", | ||
| patterns: []string{}, | ||
| modulePath: "github.com/squareup/repo", | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "empty module path", | ||
| patterns: []string{"github.com/squareup"}, | ||
| modulePath: "", | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "multiple patterns with no match", | ||
| patterns: []string{"github.com/org1", "github.com/org2", "github.com/org3"}, | ||
| modulePath: "github.com/squareup/repo", | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "pattern with trailing slash", | ||
| patterns: []string{"github.com/squareup/"}, | ||
| modulePath: "github.com/squareup/repo", | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "gopkg.in pattern", | ||
| patterns: []string{"gopkg.in/square"}, | ||
| modulePath: "gopkg.in/square/go-jose.v2", | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "nested GitHub org pattern", | ||
| patterns: []string{"github.com/squareup/internal"}, | ||
| modulePath: "github.com/squareup/internal/auth", | ||
| want: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| fetcher := gomod.NewCompositeFetcher(nil, nil, tt.patterns) | ||
| got := fetcher.IsPrivate(tt.modulePath) | ||
| if got != tt.want { | ||
| t.Errorf("IsPrivate() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't tell why this exists. We should be constructing a single Manager in main, then passing it everywhere, so AFAICT we shouldn't need this?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of where things load and where things need to be constructed, this gets more complicated than you might think. I'll address this as a separate PR because I think it might be easier to illustrate the problem and then we can work out what to do.