diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2024-12-20 10:05:29 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-20 18:05:29 +0000 |
commit | 751fe8b714f8ebd5fa915e893402776c4856d6c2 (patch) | |
tree | 04c16b9574ef8ea394ac2565ed2efb18728ce85c /modules | |
parent | 4774151e5339431dcd7fde70f084e7a0ff0b6cf6 (diff) | |
download | gitea-751fe8b714f8ebd5fa915e893402776c4856d6c2.tar.gz gitea-751fe8b714f8ebd5fa915e893402776c4856d6c2.zip |
Move some errors to their own sub packages (#32880)
Diffstat (limited to 'modules')
-rw-r--r-- | modules/git/remote.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/modules/git/remote.go b/modules/git/remote.go index 7b10e6b663..de8d74eded 100644 --- a/modules/git/remote.go +++ b/modules/git/remote.go @@ -5,8 +5,12 @@ package git import ( "context" + "fmt" + "net/url" + "strings" giturl "code.gitea.io/gitea/modules/git/url" + "code.gitea.io/gitea/modules/util" ) // GetRemoteAddress returns remote url of git repository in the repoPath with special remote name @@ -37,3 +41,61 @@ func GetRemoteURL(ctx context.Context, repoPath, remoteName string) (*giturl.Git } return giturl.Parse(addr) } + +// ErrInvalidCloneAddr represents a "InvalidCloneAddr" kind of error. +type ErrInvalidCloneAddr struct { + Host string + IsURLError bool + IsInvalidPath bool + IsProtocolInvalid bool + IsPermissionDenied bool + LocalPath bool +} + +// IsErrInvalidCloneAddr checks if an error is a ErrInvalidCloneAddr. +func IsErrInvalidCloneAddr(err error) bool { + _, ok := err.(*ErrInvalidCloneAddr) + return ok +} + +func (err *ErrInvalidCloneAddr) Error() string { + if err.IsInvalidPath { + return fmt.Sprintf("migration/cloning from '%s' is not allowed: the provided path is invalid", err.Host) + } + if err.IsProtocolInvalid { + return fmt.Sprintf("migration/cloning from '%s' is not allowed: the provided url protocol is not allowed", err.Host) + } + if err.IsPermissionDenied { + return fmt.Sprintf("migration/cloning from '%s' is not allowed.", err.Host) + } + if err.IsURLError { + return fmt.Sprintf("migration/cloning from '%s' is not allowed: the provided url is invalid", err.Host) + } + + return fmt.Sprintf("migration/cloning from '%s' is not allowed", err.Host) +} + +func (err *ErrInvalidCloneAddr) Unwrap() error { + return util.ErrInvalidArgument +} + +// ParseRemoteAddr checks if given remote address is valid, +// and returns composed URL with needed username and password. +func ParseRemoteAddr(remoteAddr, authUsername, authPassword string) (string, error) { + remoteAddr = strings.TrimSpace(remoteAddr) + // Remote address can be HTTP/HTTPS/Git URL or local path. + if strings.HasPrefix(remoteAddr, "http://") || + strings.HasPrefix(remoteAddr, "https://") || + strings.HasPrefix(remoteAddr, "git://") { + u, err := url.Parse(remoteAddr) + if err != nil { + return "", &ErrInvalidCloneAddr{IsURLError: true, Host: remoteAddr} + } + if len(authUsername)+len(authPassword) > 0 { + u.User = url.UserPassword(authUsername, authPassword) + } + remoteAddr = u.String() + } + + return remoteAddr, nil +} |