1
0
mirror of https://github.com/helm/chart-testing.git synced 2026-02-05 09:45:14 +01:00
Files
chart-testing/pkg/tool/account.go
Rickard von Essen 75d3cfc4f1 bug(493): Improved git remote parsing (#726)
* bug(493): Improved git remote parsing

Previously this would fail to parse git remotes like:

ssh://github.com/foo/bar
ssh://github.com:2222/foo/bar

This properly parses hostname from both URLs and scp style remotes.

Signed-off-by: Rickard von Essen <rickard.von.essen@gmail.com>

* Fix lint issue

Signed-off-by: Rickard von Essen <rickard.von.essen@gmail.com>

---------

Signed-off-by: Rickard von Essen <rickard.von.essen@gmail.com>
2025-06-02 15:49:32 +02:00

57 lines
1.7 KiB
Go

// Copyright The Helm Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tool
import (
"fmt"
"net/http"
"net/url"
"regexp"
)
type AccountValidator struct{}
var scpStylePattern = regexp.MustCompile("^(?:[^@]+@)?(?<host>[^@/:]+):.+$")
func (v AccountValidator) Validate(repoURL string, account string) error {
domain, err := parseOutGitRepoDomain(repoURL)
if err != nil {
return err
}
url := fmt.Sprintf("https://%s/%s", domain, account)
response, err := http.Head(url) // nolint: gosec
if err != nil {
return fmt.Errorf("failed validating maintainers: %w", err)
}
if response.StatusCode != 200 {
return fmt.Errorf("failed validating maintainer %q: %s", account, response.Status)
}
return nil
}
func parseOutGitRepoDomain(repoURL string) (string, error) {
// Git remotes can be either URLs or scp style remotes
parsedURL, err := url.Parse(repoURL)
if err != nil || len(parsedURL.Hostname()) < 1 {
submatch := scpStylePattern.FindStringSubmatch(repoURL)
if len(submatch) < 2 || len(submatch[1]) < 1 {
return "", fmt.Errorf("could not parse git repository domain for %q", repoURL)
}
return submatch[1], nil
}
return parsedURL.Hostname(), nil
}