Skip to content

Commit

Permalink
feat: add notLabels to applicationset pullrequest generator (argoproj…
Browse files Browse the repository at this point in the history
…#12300)

Signed-off-by: Mikhail Nacharov <mike.nacharov@gmail.com>
  • Loading branch information
mnacharov committed Feb 8, 2023
1 parent 2a58483 commit 3ca5631
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
6 changes: 3 additions & 3 deletions applicationset/generators/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (g *PullRequestGenerator) selectServiceProvider(ctx context.Context, genera
if err != nil {
return nil, fmt.Errorf("error fetching Secret token: %v", err)
}
return pullrequest.NewGitLabService(ctx, token, providerConfig.API, providerConfig.Project, providerConfig.Labels, providerConfig.PullRequestState)
return pullrequest.NewGitLabService(ctx, token, providerConfig.API, providerConfig.Project, providerConfig.Labels, providerConfig.NotLabels, providerConfig.PullRequestState)
}
if generatorConfig.Gitea != nil {
providerConfig := generatorConfig.Gitea
Expand Down Expand Up @@ -144,15 +144,15 @@ func (g *PullRequestGenerator) github(ctx context.Context, cfg *argoprojiov1alph
if err != nil {
return nil, fmt.Errorf("error getting GitHub App secret: %v", err)
}
return pullrequest.NewGithubAppService(*auth, cfg.API, cfg.Owner, cfg.Repo, cfg.Labels)
return pullrequest.NewGithubAppService(*auth, cfg.API, cfg.Owner, cfg.Repo, cfg.Labels, cfg.NotLabels)
}

// always default to token, even if not set (public access)
token, err := g.getSecretRef(ctx, cfg.TokenRef, applicationSetInfo.Namespace)
if err != nil {
return nil, fmt.Errorf("error fetching Secret token: %v", err)
}
return pullrequest.NewGithubService(ctx, token, cfg.API, cfg.Owner, cfg.Repo, cfg.Labels)
return pullrequest.NewGithubService(ctx, token, cfg.API, cfg.Owner, cfg.Repo, cfg.Labels, cfg.NotLabels)
}

// getSecretRef gets the value of the key for the specified Secret resource.
Expand Down
43 changes: 34 additions & 9 deletions applicationset/services/pull_request/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import (
)

type GithubService struct {
client *github.Client
owner string
repo string
labels []string
client *github.Client
owner string
repo string
labels []string
notLabels []string
}

var _ PullRequestService = (*GithubService)(nil)

func NewGithubService(ctx context.Context, token, url, owner, repo string, labels []string) (PullRequestService, error) {
func NewGithubService(ctx context.Context, token, url, owner, repo string, labels []string, notLabels []string) (PullRequestService, error) {
var ts oauth2.TokenSource
// Undocumented environment variable to set a default token, to be used in testing to dodge anonymous rate limits.
if token == "" {
Expand All @@ -41,10 +42,11 @@ func NewGithubService(ctx context.Context, token, url, owner, repo string, label
}
}
return &GithubService{
client: client,
owner: owner,
repo: repo,
labels: labels,
client: client,
owner: owner,
repo: repo,
labels: labels,
notLabels: notLabels,
}, nil
}

Expand All @@ -64,6 +66,9 @@ func (g *GithubService) List(ctx context.Context) ([]*PullRequest, error) {
if !containLabels(g.labels, pull.Labels) {
continue
}
if !notContainLabels(g.notLabels, pull.Labels) {
continue
}
pullRequests = append(pullRequests, &PullRequest{
Number: *pull.Number,
Branch: *pull.Head.Ref,
Expand Down Expand Up @@ -97,3 +102,23 @@ func containLabels(expectedLabels []string, gotLabels []*github.Label) bool {
}
return true
}

// notContainLabels returns true if gotLabels not contain exceptLabels
func notContainLabels(notLabels []string, gotLabels []*github.Label) bool {
for _, except := range notLabels {
found := false
for _, got := range gotLabels {
if got.Name == nil {
continue
}
if except == *got.Name {
found = true
break
}
}
if found {
return false
}
}
return true
}
11 changes: 6 additions & 5 deletions applicationset/services/pull_request/github_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import (
"github.com/argoproj/argo-cd/v2/applicationset/services/internal/github_app"
)

func NewGithubAppService(g github_app_auth.Authentication, url, owner, repo string, labels []string) (PullRequestService, error) {
func NewGithubAppService(g github_app_auth.Authentication, url, owner, repo string, labels []string, notLabels []string) (PullRequestService, error) {
client, err := github_app.Client(g, url)
if err != nil {
return nil, err
}
return &GithubService{
client: client,
owner: owner,
repo: repo,
labels: labels,
client: client,
owner: owner,
repo: repo,
labels: labels,
notLabels: notLabels,
}, nil
}
12 changes: 9 additions & 3 deletions applicationset/services/pull_request/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ type GitLabService struct {
client *gitlab.Client
project string
labels []string
notLabels []string
pullRequestState string
}

var _ PullRequestService = (*GitLabService)(nil)

func NewGitLabService(ctx context.Context, token, url, project string, labels []string, pullRequestState string) (PullRequestService, error) {
func NewGitLabService(ctx context.Context, token, url, project string, labels []string, notLabels []string, pullRequestState string) (PullRequestService, error) {
var clientOptionFns []gitlab.ClientOptionFunc

// Set a custom Gitlab base URL if one is provided
Expand All @@ -38,23 +39,28 @@ func NewGitLabService(ctx context.Context, token, url, project string, labels []
client: client,
project: project,
labels: labels,
notLabels: notLabels,
pullRequestState: pullRequestState,
}, nil
}

func (g *GitLabService) List(ctx context.Context) ([]*PullRequest, error) {

// Filter the merge requests on labels, if they are specified.
var labels *gitlab.Labels
var labels, notLabels *gitlab.Labels
if len(g.labels) > 0 {
labels = (*gitlab.Labels)(&g.labels)
}
if len(g.notLabels) > 0 {
notLabels = (*gitlab.Labels)(&g.notLabels)
}

opts := &gitlab.ListProjectMergeRequestsOptions{
ListOptions: gitlab.ListOptions{
PerPage: 100,
},
Labels: labels,
Labels: labels,
NotLabels: notLabels,
}

if g.pullRequestState != "" {
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/application/v1alpha1/applicationset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ type PullRequestGeneratorGithub struct {
AppSecretName string `json:"appSecretName,omitempty" protobuf:"bytes,5,opt,name=appSecretName"`
// Labels is used to filter the PRs that you want to target
Labels []string `json:"labels,omitempty" protobuf:"bytes,6,rep,name=labels"`
// Labels is used to filter out the PRs that you don't want to target
NotLabels []string `json:"notLabels,omitempty" protobuf:"bytes,6,rep,name=notLabels"`
}

// PullRequestGeneratorGitLab defines connection info specific to GitLab.
Expand All @@ -470,6 +472,8 @@ type PullRequestGeneratorGitLab struct {
TokenRef *SecretRef `json:"tokenRef,omitempty" protobuf:"bytes,3,opt,name=tokenRef"`
// Labels is used to filter the MRs that you want to target
Labels []string `json:"labels,omitempty" protobuf:"bytes,4,rep,name=labels"`
// Labels is used to filter out the PRs that you don't want to target
NotLabels []string `json:"notLabels,omitempty" protobuf:"bytes,4,rep,name=notLabels"`
// PullRequestState is an additional MRs filter to get only those with a certain state. Default: "" (all states)
PullRequestState string `json:"pullRequestState,omitempty" protobuf:"bytes,5,rep,name=pullRequestState"`
}
Expand Down

0 comments on commit 3ca5631

Please sign in to comment.