-
Notifications
You must be signed in to change notification settings - Fork 560
/
Copy pathallowlist.go
56 lines (45 loc) · 1.4 KB
/
allowlist.go
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
48
49
50
51
52
53
54
55
56
package utils
import (
"log/slog"
"net/url"
"os"
"strings"
"github.com/samber/lo"
)
func ExtractCleanRepoName(gitlabURL string) (string, error) {
// Parse the URL
parsedURL, err := url.Parse(gitlabURL)
if err != nil {
slog.Error("Failed to parse URL", "url", gitlabURL, "error", err)
return "", err
}
// The repository name is typically the last part of the path
// We use path.Base to handle cases where there might be a trailing slash
repoName := parsedURL.Hostname() + parsedURL.Path
// If the URL ends with .git, remove it
repoName = strings.TrimSuffix(repoName, ".git")
slog.Debug("Extracted clean repo name", "originalUrl", gitlabURL, "cleanName", repoName)
return repoName, nil
}
func IsInRepoAllowList(repoUrl string) bool {
allowList := os.Getenv("DIGGER_REPO_ALLOW_LIST")
if allowList == "" {
slog.Debug("No repo allow list defined, allowing all repos")
return true
}
allowedReposUrls := strings.Split(allowList, ",")
// gitlab.com/diggerhq/test
// https://gitlab.com/diggerhq/test
repoName, err := ExtractCleanRepoName(repoUrl)
if err != nil {
slog.Warn("Could not parse repository URL", "url", repoUrl, "error", err)
return false
}
exists := lo.Contains(allowedReposUrls, repoName)
if exists {
slog.Debug("Repository is in allow list", "repo", repoName)
} else {
slog.Info("Repository is not in allow list", "repo", repoName, "allowList", allowList)
}
return exists
}