mirror of
https://github.com/hanzoai/deploy.git
synced 2026-10-07 08:44:50 +00:00
go.mod still declared `module github.com/hanzoai/deploy` after the repo was renamed. A Go module path is not cosmetic — it is the resolvable location. So `go get github.com/hanzoai/cd` could not work, and anything importing this module had to spell a name that no longer exists at that address. The build log of v3.6.0 is where it surfaced: the compiler was still emitting github.com/hanzoai/deploy/cmd/cd/commands. 3541 import paths across 690 .go, 19 .proto, 4 .yaml, 3 .sh, 2 .mod, Procfile and Makefile. Plus the 62 ghcr.io/hanzoai/deploy image references, moved to ghcr.io/hanzoai/cd to match hanzo.yml's build target — a distinct string from the module path, replaced separately so neither could clobber the other. go mod tidy clean, CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./... clean. Co-authored-by: Hanzo Dev <[email protected]>
179 lines
4.9 KiB
Go
179 lines
4.9 KiB
Go
package scm_provider
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"regexp"
|
|
"slices"
|
|
"strings"
|
|
|
|
argoprojiov1alpha1 "github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
|
|
)
|
|
|
|
func compileFilters(filters []argoprojiov1alpha1.SCMProviderGeneratorFilter) ([]*Filter, error) {
|
|
outFilters := make([]*Filter, 0, len(filters))
|
|
for _, filter := range filters {
|
|
outFilter := &Filter{}
|
|
var err error
|
|
if filter.RepositoryMatch != nil {
|
|
outFilter.RepositoryMatch, err = regexp.Compile(*filter.RepositoryMatch)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error compiling RepositoryMatch regexp %q: %w", *filter.RepositoryMatch, err)
|
|
}
|
|
outFilter.FilterType = FilterTypeRepo
|
|
}
|
|
if filter.LabelMatch != nil {
|
|
outFilter.LabelMatch, err = regexp.Compile(*filter.LabelMatch)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error compiling LabelMatch regexp %q: %w", *filter.LabelMatch, err)
|
|
}
|
|
outFilter.FilterType = FilterTypeRepo
|
|
}
|
|
if filter.PathsExist != nil {
|
|
outFilter.PathsExist = filter.PathsExist
|
|
outFilter.FilterType = FilterTypeBranch
|
|
}
|
|
if filter.PathsDoNotExist != nil {
|
|
outFilter.PathsDoNotExist = filter.PathsDoNotExist
|
|
outFilter.FilterType = FilterTypeBranch
|
|
}
|
|
if filter.BranchMatch != nil {
|
|
outFilter.BranchMatch, err = regexp.Compile(*filter.BranchMatch)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error compiling BranchMatch regexp %q: %w", *filter.BranchMatch, err)
|
|
}
|
|
outFilter.FilterType = FilterTypeBranch
|
|
}
|
|
outFilters = append(outFilters, outFilter)
|
|
}
|
|
return outFilters, nil
|
|
}
|
|
|
|
func matchFilter(ctx context.Context, provider SCMProviderService, repo *Repository, filter *Filter) (bool, error) {
|
|
if filter.RepositoryMatch != nil && !filter.RepositoryMatch.MatchString(repo.Repository) {
|
|
return false, nil
|
|
}
|
|
|
|
if filter.BranchMatch != nil && !filter.BranchMatch.MatchString(repo.Branch) {
|
|
return false, nil
|
|
}
|
|
|
|
if filter.LabelMatch != nil {
|
|
found := slices.ContainsFunc(repo.Labels, filter.LabelMatch.MatchString)
|
|
if !found {
|
|
return false, nil
|
|
}
|
|
}
|
|
|
|
if len(filter.PathsExist) != 0 {
|
|
for _, path := range filter.PathsExist {
|
|
path = strings.TrimRight(path, "/")
|
|
hasPath, err := provider.RepoHasPath(ctx, repo, path)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if !hasPath {
|
|
return false, nil
|
|
}
|
|
}
|
|
}
|
|
if len(filter.PathsDoNotExist) != 0 {
|
|
for _, path := range filter.PathsDoNotExist {
|
|
path = strings.TrimRight(path, "/")
|
|
hasPath, err := provider.RepoHasPath(ctx, repo, path)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if hasPath {
|
|
return false, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
func ListRepos(ctx context.Context, provider SCMProviderService, filters []argoprojiov1alpha1.SCMProviderGeneratorFilter, cloneProtocol string) ([]*Repository, error) {
|
|
compiledFilters, err := compileFilters(filters)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
repos, err := provider.ListRepos(ctx, cloneProtocol)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
repoFilters := getApplicableFilters(compiledFilters)[FilterTypeRepo]
|
|
if len(repoFilters) == 0 {
|
|
repos, err := getBranches(ctx, provider, repos, compiledFilters)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return repos, nil
|
|
}
|
|
filteredRepos := make([]*Repository, 0, len(repos))
|
|
for _, repo := range repos {
|
|
for _, filter := range repoFilters {
|
|
matches, err := matchFilter(ctx, provider, repo, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if matches {
|
|
filteredRepos = append(filteredRepos, repo)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
repos, err = getBranches(ctx, provider, filteredRepos, compiledFilters)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return repos, nil
|
|
}
|
|
|
|
func getBranches(ctx context.Context, provider SCMProviderService, repos []*Repository, compiledFilters []*Filter) ([]*Repository, error) {
|
|
reposWithBranches := []*Repository{}
|
|
for _, repo := range repos {
|
|
reposFilled, err := provider.GetBranches(ctx, repo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
reposWithBranches = append(reposWithBranches, reposFilled...)
|
|
}
|
|
branchFilters := getApplicableFilters(compiledFilters)[FilterTypeBranch]
|
|
if len(branchFilters) == 0 {
|
|
return reposWithBranches, nil
|
|
}
|
|
filteredRepos := make([]*Repository, 0, len(reposWithBranches))
|
|
for _, repo := range reposWithBranches {
|
|
for _, filter := range branchFilters {
|
|
matches, err := matchFilter(ctx, provider, repo, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if matches {
|
|
filteredRepos = append(filteredRepos, repo)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return filteredRepos, nil
|
|
}
|
|
|
|
// getApplicableFilters returns a map of filters separated by type.
|
|
func getApplicableFilters(filters []*Filter) map[FilterType][]*Filter {
|
|
filterMap := map[FilterType][]*Filter{
|
|
FilterTypeBranch: {},
|
|
FilterTypeRepo: {},
|
|
}
|
|
for _, filter := range filters {
|
|
switch filter.FilterType {
|
|
case FilterTypeBranch:
|
|
filterMap[FilterTypeBranch] = append(filterMap[FilterTypeBranch], filter)
|
|
case FilterTypeRepo:
|
|
filterMap[FilterTypeRepo] = append(filterMap[FilterTypeRepo], filter)
|
|
}
|
|
}
|
|
return filterMap
|
|
}
|