1package utils
 2
 3import (
 4	"fmt"
 5	"path"
 6	"strings"
 7	"unicode"
 8)
 9
10// SanitizeRepo returns a sanitized version of the given repository name.
11func SanitizeRepo(repo string) string {
12	// We need to use an absolute path for the path to be cleaned correctly.
13	repo = strings.TrimPrefix(repo, "/")
14	repo = "/" + repo
15
16	// We're using path instead of filepath here because this is not OS dependent
17	// looking at you Windows
18	repo = path.Clean(repo)
19	repo = strings.TrimSuffix(repo, ".git")
20	return repo[1:]
21}
22
23// ValidateUsername returns an error if any of the given usernames are invalid.
24func ValidateUsername(username string) error {
25	if username == "" {
26		return fmt.Errorf("username cannot be empty")
27	}
28
29	if !unicode.IsLetter(rune(username[0])) {
30		return fmt.Errorf("username must start with a letter")
31	}
32
33	for _, r := range username {
34		if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' {
35			return fmt.Errorf("username can only contain letters, numbers, and hyphens")
36		}
37	}
38
39	return nil
40}
41
42// ValidateRepo returns an error if the given repository name is invalid.
43func ValidateRepo(repo string) error {
44	if repo == "" {
45		return fmt.Errorf("repo cannot be empty")
46	}
47
48	for _, r := range repo {
49		if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' && r != '_' && r != '.' && r != '/' {
50			return fmt.Errorf("repo can only contain letters, numbers, hyphens, underscores, periods, and slashes")
51		}
52	}
53
54	return nil
55}