-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.go
More file actions
47 lines (39 loc) · 1.14 KB
/
functions.go
File metadata and controls
47 lines (39 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// SPDX-FileCopyrightText: Copyright 2025 Carabiner Systems, Inc
// SPDX-License-Identifier: Apache-2.0
package github
import (
"fmt"
"net/url"
"strings"
)
// RepoFromString returns a repository URL from a string. It supports
// full URLs, repo slugs (org/repo), VCS locators and possibly more
// input types will be added in the future.
func RepoFromString(str string) (string, error) {
// String is a github URL without scheme
if strings.HasPrefix(str, "github.com") {
str = "https://" + str
}
u, err := url.Parse(str)
if err != nil {
return "", fmt.Errorf("parsing url string: %w", err)
}
host := u.Hostname()
if host == "" {
host = "github.com"
}
parts := strings.Split(strings.TrimPrefix(u.Path, "/"), "/")
if len(parts) < 2 {
return "", fmt.Errorf("URL path needs to have an org and repo")
}
// If it's a locator, trim the branch (if present)
if strings.HasPrefix(u.Scheme, "git+") {
f, _, _ := strings.Cut(parts[len(parts)-1], "@")
parts[len(parts)-1] = f
}
scheme := strings.TrimPrefix(u.Scheme, "git+")
if scheme == "" {
scheme = "https"
}
return fmt.Sprintf("%s://%s/%s/%s", scheme, host, parts[0], parts[1]), nil
}