114 lines
2.7 KiB
Go
114 lines
2.7 KiB
Go
package common
|
|
|
|
/*
|
|
* This file is part of Autogits.
|
|
*
|
|
* Copyright © 2024 SUSE LLC
|
|
*
|
|
* Autogits is free software: you can redistribute it and/or modify it under
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
* Foundation, either version 2 of the License, or (at your option) any later
|
|
* version.
|
|
*
|
|
* Autogits is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with
|
|
* Foobar. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"regexp"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
func SplitLines(str string) []string {
|
|
return SplitStringNoEmpty(str, "\n")
|
|
}
|
|
|
|
func SplitStringNoEmpty(str, sep string) []string {
|
|
ret := slices.DeleteFunc(strings.Split(str, sep), func(s string) bool {
|
|
return len(strings.TrimSpace(s)) == 0
|
|
})
|
|
for i := range ret {
|
|
ret[i] = strings.TrimSpace(ret[i])
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func TranslateHttpsToSshUrl(url string) (string, error) {
|
|
const (
|
|
url1 = "https://src.opensuse.org/"
|
|
url2 = "https://src.suse.de/"
|
|
|
|
url1_len = len(url1)
|
|
url2_len = len(url2)
|
|
)
|
|
|
|
if len(url) > url1_len && url[0:url1_len] == url1 {
|
|
return "ssh://gitea@src.opensuse.org/" + url[url1_len:], nil
|
|
}
|
|
if len(url) > url2_len && url[0:url2_len] == url2 {
|
|
return "ssh://gitea@src.suse.de/" + url[url2_len:], nil
|
|
}
|
|
|
|
return "", fmt.Errorf("Unknown input url %s", url)
|
|
}
|
|
|
|
func TranslateSshNativeToUrl(urlString string) (string, error) {
|
|
rx := regexp.MustCompile("^([^:@]+)@?([^:]*):(.+)$")
|
|
m := rx.FindAllStringSubmatch(urlString, -1)
|
|
if m == nil {
|
|
return "", fmt.Errorf("Cannot match expected native SSH schema: %s", urlString)
|
|
}
|
|
|
|
if len(m[0][2]) > 0 {
|
|
// with user
|
|
return "ssh://" + m[0][1] + "@" + m[0][2] + "/" + m[0][3], nil
|
|
}
|
|
// without user
|
|
return "ssh://" + m[0][1] + "/" + m[0][3], nil
|
|
}
|
|
|
|
type GitUrl struct {
|
|
Org string
|
|
Repo string
|
|
Commit string
|
|
}
|
|
|
|
var valid_schemas []string = []string { "https", "ssh", "http" }
|
|
|
|
func ParseGitRemoteUrl(urlString string) (*GitUrl, error) {
|
|
url, err := url.Parse(urlString)
|
|
if err != nil || !slices.Contains(valid_schemas, url.Scheme) {
|
|
u, err := TranslateSshNativeToUrl(urlString)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Unable to parse url: %w", err)
|
|
}
|
|
return ParseGitRemoteUrl(u)
|
|
}
|
|
|
|
e := SplitStringNoEmpty(url.Path, "/")
|
|
if len(e) != 2 {
|
|
return nil, fmt.Errorf("Unexpected format for Gitea URL: %s", e)
|
|
}
|
|
|
|
org := e[0]
|
|
repo := e[1]
|
|
if len(repo) > 4 && repo[len(repo)-4:] == ".git" {
|
|
repo = repo[0 : len(repo)-4]
|
|
}
|
|
|
|
u := GitUrl{
|
|
Org: org,
|
|
Repo: repo,
|
|
Commit: url.Fragment,
|
|
}
|
|
|
|
return &u, nil
|
|
}
|