-
Notifications
You must be signed in to change notification settings - Fork 270
[plugins] Fix github canonical name #1897
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,22 +5,49 @@ import ( | |
"io" | ||
"net/http" | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/samber/lo" | ||
"go.jetpack.io/devbox/internal/boxcli/usererr" | ||
"go.jetpack.io/devbox/internal/cachehash" | ||
"go.jetpack.io/devbox/nix/flake" | ||
) | ||
|
||
type githubPlugin struct { | ||
ref flake.Ref | ||
ref flake.Ref | ||
name string | ||
} | ||
|
||
// Github only allows alphanumeric, hyphen, underscore, and period in repo names. | ||
// but we clean up just in case. | ||
var githubNameRegexp = regexp.MustCompile("[^a-zA-Z0-9-_.]+") | ||
|
||
func newGithubPlugin(ref flake.Ref) (*githubPlugin, error) { | ||
plugin := &githubPlugin{ref: ref} | ||
// For backward compatibility, we don't strictly require name to be present | ||
// in github plugins. If it's missing, we just use the directory as the name. | ||
name, err := getPluginNameFromContent(plugin) | ||
if err != nil && !errors.Is(err, errNameMissing) { | ||
return nil, err | ||
} | ||
if name == "" { | ||
name = strings.ReplaceAll(ref.Dir, "/", "-") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens if the plugin's devbox.json config is in the root of the github repo? Can we default ref.Dir to "root-folder" or something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there's no name and it's in the root, the qualified name becomes |
||
} | ||
plugin.name = githubNameRegexp.ReplaceAllString( | ||
strings.Join(lo.Compact([]string{ref.Owner, ref.Repo, name}), "."), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
" ", | ||
) | ||
return plugin, nil | ||
} | ||
|
||
func (p *githubPlugin) Fetch() ([]byte, error) { | ||
return p.FileContent(pluginConfigName) | ||
} | ||
|
||
func (p *githubPlugin) CanonicalName() string { | ||
return p.ref.Owner + "-" + p.ref.Repo | ||
return p.name | ||
} | ||
|
||
func (p *githubPlugin) Hash() string { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
package plugin | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"regexp" | ||
|
||
"go.jetpack.io/devbox/internal/boxcli/usererr" | ||
"go.jetpack.io/devbox/nix/flake" | ||
) | ||
|
||
type Includable interface { | ||
CanonicalName() string | ||
Hash() string | ||
FileContent(subpath string) ([]byte, error) | ||
Hash() string | ||
LockfileKey() string | ||
} | ||
|
||
|
@@ -22,8 +25,41 @@ func parseIncludable(includableRef, workingDir string) (Includable, error) { | |
case flake.TypePath: | ||
return newLocalPlugin(ref, workingDir) | ||
case flake.TypeGitHub: | ||
return &githubPlugin{ref: ref}, nil | ||
return newGithubPlugin(ref) | ||
default: | ||
return nil, fmt.Errorf("unsupported ref type %q", ref.Type) | ||
} | ||
} | ||
|
||
type fetcher interface { | ||
Includable | ||
Fetch() ([]byte, error) | ||
} | ||
|
||
var ( | ||
nameRegex = regexp.MustCompile(`^[a-zA-Z0-9_\- ]+$`) | ||
errNameMissing = usererr.New("'name' is missing") | ||
) | ||
|
||
func getPluginNameFromContent(plugin fetcher) (string, error) { | ||
content, err := plugin.Fetch() | ||
if err != nil { | ||
return "", err | ||
} | ||
m := map[string]any{} | ||
if err := json.Unmarshal(content, &m); err != nil { | ||
return "", err | ||
} | ||
name, ok := m["name"].(string) | ||
if !ok || name == "" { | ||
return "", | ||
fmt.Errorf("%w in plugin %s", errNameMissing, plugin.LockfileKey()) | ||
} | ||
if !nameRegex.MatchString(name) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: lets move |
||
return "", usererr.New( | ||
"plugin %s has an invalid name %q. Name must match %s", | ||
plugin.LockfileKey(), name, nameRegex, | ||
) | ||
} | ||
return name, nil | ||
} |
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.
why is the "but we clean up just in case" comment needed? The regex checks for the all the cases that Github allows for, which is good. Not understanding the "but..." as if we are doing some further restrictions?
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 just meant that we should never encounter weird stuff (because it would not be a valid github repo) but we sanitize it anyway just in case. (for example we really don't want a slash in there)