Files
zeekay 31c64ef661 test, applicationset: drop Argo branding, fix bugs it was hiding
Rename test names, comments, fixture data, and mock GitHub/webhook
responses that still said Argo/ArgoCD/argocd/argoproj in our own
voice, matching the hanzocd-* binary/configmap names and the
apps.hanzo.ai CRD/annotation domain already established elsewhere
in the fork. Also drop the dated argov1alpha1/argoprojiov1alpha1
import aliases for our own v1alpha1 API package.

Restore the CRD groups a prior blind rename had mangled on a
vendored third-party Argo Workflows example manifest back to the
real argoproj.io, and fix several places where an earlier
half-finished rename had gone silently wrong instead of just being
cosmetic: stale CD_BINARY_NAME values and a stale
--argocd-repo-server flag no longer recognized by the binary, cert
filenames and an ssh_known_hosts alias that didn't match what's
actually on disk, a namespace and a kustomize image substitution
that were silent no-ops, and a test assertion regex that could
never match its own fixture.

External lookup keys we don't control are left byte-exact:
quay.io/argoprojlabs images, the docker.io/argoproj CI builder
image, live argoproj/argo-workflows release URLs, the generated
ArgoprojV1alpha1() clientset method, and the Makefile-coupled
/tmp/argo-e2e and GOPATH mount conventions.
2026-09-05 01:07:59 -07:00

81 lines
2.2 KiB
Go

package pull_request
import (
"context"
"fmt"
"regexp"
"github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
)
func compileFilters(filters []v1alpha1.PullRequestGeneratorFilter) ([]*Filter, error) {
outFilters := make([]*Filter, 0, len(filters))
for _, filter := range filters {
outFilter := &Filter{}
var err error
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)
}
}
if filter.TargetBranchMatch != nil {
outFilter.TargetBranchMatch, err = regexp.Compile(*filter.TargetBranchMatch)
if err != nil {
return nil, fmt.Errorf("error compiling TargetBranchMatch regexp %q: %w", *filter.TargetBranchMatch, err)
}
}
if filter.TitleMatch != nil {
outFilter.TitleMatch, err = regexp.Compile(*filter.TitleMatch)
if err != nil {
return nil, fmt.Errorf("error compiling TitleMatch regexp %q: %w", *filter.TitleMatch, err)
}
}
outFilters = append(outFilters, outFilter)
}
return outFilters, nil
}
func matchFilter(pullRequest *PullRequest, filter *Filter) bool {
if filter.BranchMatch != nil && !filter.BranchMatch.MatchString(pullRequest.Branch) {
return false
}
if filter.TargetBranchMatch != nil && !filter.TargetBranchMatch.MatchString(pullRequest.TargetBranch) {
return false
}
if filter.TitleMatch != nil && !filter.TitleMatch.MatchString(pullRequest.Title) {
return false
}
return true
}
func ListPullRequests(ctx context.Context, provider PullRequestService, filters []v1alpha1.PullRequestGeneratorFilter) ([]*PullRequest, error) {
compiledFilters, err := compileFilters(filters)
if err != nil {
return nil, err
}
pullRequests, err := provider.List(ctx)
if err != nil {
return nil, err
}
if len(compiledFilters) == 0 {
return pullRequests, nil
}
filteredPullRequests := make([]*PullRequest, 0, len(pullRequests))
for _, pullRequest := range pullRequests {
for _, filter := range compiledFilters {
matches := matchFilter(pullRequest, filter)
if matches {
filteredPullRequests = append(filteredPullRequests, pullRequest)
break
}
}
}
return filteredPullRequests, nil
}