236 lines
8.6 KiB
Go
236 lines
8.6 KiB
Go
package domain
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"regexp"
|
|
"strings"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
// ErrInvalidGitHubRepository reports a repository address that is not a
|
|
// GitHub repository address of the accepted form.
|
|
var ErrInvalidGitHubRepository = errors.New("invalid GitHub repository address")
|
|
|
|
const (
|
|
maxProfileProjects = 10
|
|
maxProfileProjectTitleRunes = 100
|
|
maxProfileProjectDescription = 500
|
|
maxProfileProjectStackItems = 10
|
|
maxRepositoryURLRunes = 160
|
|
githubRepositoryPrefix = "https://github.com/"
|
|
// projectStackSeparator joins the tags of a project into one comparable
|
|
// string. A tag is one line, so a line break never occurs inside one.
|
|
projectStackSeparator = "\n"
|
|
)
|
|
|
|
var (
|
|
// GitHub owner names: letters, digits, and single hyphens, at most 39
|
|
// characters, not starting with a hyphen.
|
|
githubOwnerPattern = regexp.MustCompile(`^[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})$`)
|
|
// GitHub repository names: letters, digits, '.', '_', and '-'.
|
|
githubNamePattern = regexp.MustCompile(`^[A-Za-z0-9._-]{1,100}$`)
|
|
)
|
|
|
|
// GitHubRepository is a validated public repository coordinate. Owner and
|
|
// Name keep the letter case the candidate typed; Key is the lower-case form
|
|
// that GitHub treats as the same repository.
|
|
type GitHubRepository struct {
|
|
owner string
|
|
name string
|
|
}
|
|
|
|
// ParseGitHubRepositoryURL accepts only https://github.com/<owner>/<name>
|
|
// with an optional trailing slash or .git suffix. Any other scheme, host,
|
|
// port, user information, query, fragment, or path is rejected, so the
|
|
// address can never point the server at another host.
|
|
func ParseGitHubRepositoryURL(raw string) (GitHubRepository, error) {
|
|
const location = "domain.ParseGitHubRepositoryURL"
|
|
fail := func(reason string) (GitHubRepository, error) {
|
|
return GitHubRepository{}, fmt.Errorf("%s - %s: %w", location, reason, ErrInvalidGitHubRepository)
|
|
}
|
|
if !utf8.ValidString(raw) || utf8.RuneCountInString(raw) > maxRepositoryURLRunes {
|
|
return fail("address is too long or not UTF-8")
|
|
}
|
|
if !strings.HasPrefix(raw, githubRepositoryPrefix) {
|
|
return fail("address must start with https://github.com/")
|
|
}
|
|
path := strings.TrimPrefix(raw, githubRepositoryPrefix)
|
|
path = strings.TrimSuffix(path, "/")
|
|
parts := strings.Split(path, "/")
|
|
if len(parts) != 2 {
|
|
return fail("address must name exactly an owner and a repository")
|
|
}
|
|
owner, name := parts[0], strings.TrimSuffix(parts[1], ".git")
|
|
switch {
|
|
case !githubOwnerPattern.MatchString(owner) || strings.Contains(owner, "--"):
|
|
return fail("owner is not a GitHub account name")
|
|
case !githubNamePattern.MatchString(name) || name == "." || name == "..":
|
|
return fail("repository is not a GitHub repository name")
|
|
}
|
|
return GitHubRepository{owner: owner, name: name}, nil
|
|
}
|
|
|
|
// Owner returns the account part of the address.
|
|
func (repository GitHubRepository) Owner() string { return repository.owner }
|
|
|
|
// Name returns the repository part of the address.
|
|
func (repository GitHubRepository) Name() string { return repository.name }
|
|
|
|
// URL returns the canonical address https://github.com/<owner>/<name>.
|
|
func (repository GitHubRepository) URL() string {
|
|
return githubRepositoryPrefix + repository.owner + "/" + repository.name
|
|
}
|
|
|
|
// Key is "<owner>/<name>" in lower case: the identity of the repository for
|
|
// deduplication and for its stored check.
|
|
func (repository GitHubRepository) Key() string {
|
|
return strings.ToLower(repository.owner + "/" + repository.name)
|
|
}
|
|
|
|
// IsZero reports a repository that was not parsed.
|
|
func (repository GitHubRepository) IsZero() bool { return repository.owner == "" }
|
|
|
|
// The coordinate names an account, so formatting redacts it like profile data.
|
|
func (GitHubRepository) String() string { return redactedProfile }
|
|
func (GitHubRepository) GoString() string { return redactedProfile }
|
|
func (GitHubRepository) LogValue() slog.Value {
|
|
return slog.StringValue(redactedProfile)
|
|
}
|
|
func (GitHubRepository) Format(state fmt.State, _ rune) { writeRedacted(state, redactedProfile) }
|
|
|
|
// ProfileProject is one project of the profile. Description and
|
|
// RepositoryURL are "" when not given. The stack is kept as one string so the
|
|
// value stays comparable; StackTags returns the tags. The project is personal
|
|
// data: formatting redacts it.
|
|
type ProfileProject struct {
|
|
Title string
|
|
Description string
|
|
RepositoryURL string
|
|
stack string
|
|
}
|
|
|
|
// NewProfileProject builds a project value from request parts. Validation
|
|
// happens when the profile resolves it.
|
|
func NewProfileProject(title, description string, stack []string, repositoryURL string) ProfileProject {
|
|
return ProfileProject{
|
|
Title: title, Description: description, RepositoryURL: repositoryURL,
|
|
stack: strings.Join(stack, projectStackSeparator),
|
|
}
|
|
}
|
|
|
|
// StackTags returns the stack tags in the order the candidate gave them.
|
|
func (project ProfileProject) StackTags() []string {
|
|
if project.stack == "" {
|
|
return nil
|
|
}
|
|
return strings.Split(project.stack, projectStackSeparator)
|
|
}
|
|
|
|
// Repository returns the parsed repository and whether the project links one.
|
|
func (project ProfileProject) Repository() (GitHubRepository, bool) {
|
|
if project.RepositoryURL == "" {
|
|
return GitHubRepository{}, false
|
|
}
|
|
repository, err := ParseGitHubRepositoryURL(project.RepositoryURL)
|
|
if err != nil {
|
|
return GitHubRepository{}, false
|
|
}
|
|
return repository, true
|
|
}
|
|
|
|
func (ProfileProject) String() string { return redactedProfile }
|
|
func (ProfileProject) GoString() string { return redactedProfile }
|
|
func (ProfileProject) LogValue() slog.Value {
|
|
return slog.StringValue(redactedProfile)
|
|
}
|
|
func (ProfileProject) Format(state fmt.State, _ rune) { writeRedacted(state, redactedProfile) }
|
|
|
|
// projectRules validates and normalizes one project. The repository address
|
|
// becomes canonical, so a trailing slash or a .git suffix never creates a
|
|
// second value for the same repository.
|
|
func projectRules(value ProfileProject) (ProfileProject, string, string) {
|
|
value.Description = strings.ReplaceAll(value.Description, "\r\n", "\n")
|
|
if problem := singleLineTextProblem(value.Title, maxProfileProjectTitleRunes); problem != "" {
|
|
return value, "/title", problem
|
|
}
|
|
if value.Description != "" {
|
|
if problem := multiLineTextProblem(value.Description, maxProfileProjectDescription); problem != "" {
|
|
return value, "/description", problem
|
|
}
|
|
}
|
|
tags := value.StackTags()
|
|
if len(tags) > maxProfileProjectStackItems {
|
|
return value, "/stack", fmt.Sprintf("must have at most %d items", maxProfileProjectStackItems)
|
|
}
|
|
seen := make(map[string]struct{}, len(tags))
|
|
for index, tag := range tags {
|
|
if problem := singleLineTextProblem(tag, maxProfileTagRunes); problem != "" {
|
|
return value, fmt.Sprintf("/stack/%d", index), problem
|
|
}
|
|
key := strings.ToLower(tag)
|
|
if _, repeated := seen[key]; repeated {
|
|
return value, fmt.Sprintf("/stack/%d", index), "must not repeat an earlier item"
|
|
}
|
|
seen[key] = struct{}{}
|
|
}
|
|
if value.RepositoryURL != "" {
|
|
repository, err := ParseGitHubRepositoryURL(value.RepositoryURL)
|
|
if err != nil {
|
|
return value, "/repository_url", "must be a GitHub repository address https://github.com/<owner>/<repository>"
|
|
}
|
|
value.RepositoryURL = repository.URL()
|
|
}
|
|
return value, "", ""
|
|
}
|
|
|
|
// projectKey identifies a project for the repeat rule: two projects may not
|
|
// link the same repository. A project without a repository never repeats.
|
|
func projectKey(value ProfileProject) string {
|
|
repository, ok := value.Repository()
|
|
if !ok {
|
|
return "\x00" + value.Title + "\x00" + value.Description + "\x00" + value.stack
|
|
}
|
|
return repository.Key()
|
|
}
|
|
|
|
// storedProjectValue is the JSON shape of a project claim that the CHECK of
|
|
// candidate_profile_claims requires.
|
|
type storedProjectValue struct {
|
|
Title string `json:"title"`
|
|
Description *string `json:"description"`
|
|
Stack []string `json:"stack"`
|
|
RepositoryURL *string `json:"repository_url"`
|
|
}
|
|
|
|
func projectStoredValue(project ProfileProject) any {
|
|
stack := project.StackTags()
|
|
if stack == nil {
|
|
stack = []string{}
|
|
}
|
|
return storedProjectValue{
|
|
Title: project.Title, Description: optionalString(project.Description), Stack: stack,
|
|
RepositoryURL: optionalString(project.RepositoryURL),
|
|
}
|
|
}
|
|
|
|
// ProjectRepositories returns the distinct repositories that the projects of
|
|
// the content link, in project order.
|
|
func (content CandidateProfileContent) ProjectRepositories() []GitHubRepository {
|
|
var repositories []GitHubRepository
|
|
seen := make(map[string]struct{})
|
|
for _, claim := range content.projects {
|
|
repository, ok := claim.value.Repository()
|
|
if !ok {
|
|
continue
|
|
}
|
|
if _, repeated := seen[repository.Key()]; repeated {
|
|
continue
|
|
}
|
|
seen[repository.Key()] = struct{}{}
|
|
repositories = append(repositories, repository)
|
|
}
|
|
return repositories
|
|
}
|