Skip to content

Commit

Permalink
feat: allow limiting MRs to specific labels when doing async evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
jippi committed Aug 30, 2024
1 parent f8322c0 commit c2f21dc
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
35 changes: 18 additions & 17 deletions cmd/conventions.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package cmd

const (
FlagAPIToken = "api-token"
FlagCommitSHA = "commit"
FlagConfigFile = "config"
FlagDryRun = "dry-run"
FlagMergeRequestID = "id"
FlagSCMBaseURL = "base-url"
FlagSCMProject = "project"
FlagServerListenHost = "listen-host"
FlagServerListenPort = "listen-port"
FlagServerTimeout = "timeout"
FlagUpdatePipeline = "update-pipeline"
FlagUpdatePipelineURL = "update-pipeline-url"
FlagPeriodicEvaluationInterval = "periodic-evaluation-interval"
FlagPeriodicEvaluationIgnoreMergeRequestsWithLabel = "periodic-evaluation-ignore-mr-labels"
FlagPeriodicEvaluationOnlyProjectsWithTopics = "periodic-evaluation-project-topics"
FlagPeriodicEvaluationOnlyProjectsWithMembership = "periodic-evaluation-only-project-membership"
FlagWebhookSecret = "webhook-secret"
FlagAPIToken = "api-token"
FlagCommitSHA = "commit"
FlagConfigFile = "config"
FlagDryRun = "dry-run"
FlagMergeRequestID = "id"
FlagSCMBaseURL = "base-url"
FlagSCMProject = "project"
FlagServerListenHost = "listen-host"
FlagServerListenPort = "listen-port"
FlagServerTimeout = "timeout"
FlagUpdatePipeline = "update-pipeline"
FlagUpdatePipelineURL = "update-pipeline-url"
FlagPeriodicEvaluationInterval = "periodic-evaluation-interval"
FlagPeriodicEvaluationIgnoreMergeRequestsWithLabel = "periodic-evaluation-ignore-mr-labels"
FlagPeriodicEvaluationRequireMergeRequestsWithLabel = "periodic-evaluation-require-mr-labels"
FlagPeriodicEvaluationOnlyProjectsWithTopics = "periodic-evaluation-project-topics"
FlagPeriodicEvaluationOnlyProjectsWithMembership = "periodic-evaluation-only-project-membership"
FlagWebhookSecret = "webhook-secret"
)
7 changes: 7 additions & 0 deletions cmd/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ var GitLab = &cli.Command{
"SCM_ENGINE_PERIODIC_EVALUATION_IGNORE_MR_WITH_LABELS",
},
},
&cli.StringSliceFlag{
Name: FlagPeriodicEvaluationRequireMergeRequestsWithLabel,
Usage: "(Optional) Only process MR with these labels",
EnvVars: []string{
"SCM_ENGINE_PERIODIC_EVALUATION_REQUIRE_MR_WITH_LABELS",
},
},
&cli.StringSliceFlag{
Name: FlagPeriodicEvaluationOnlyProjectsWithTopics,
Usage: "(Optional) Only evaluate projects with these topics",
Expand Down
1 change: 1 addition & 0 deletions cmd/gitlab_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func Server(cCtx *cli.Context) error {

filter := scm.MergeRequestListFilters{
IgnoreMergeRequestWithLabels: cCtx.StringSlice(FlagPeriodicEvaluationIgnoreMergeRequestsWithLabel),
OnlyMergeRequestsWithLabels: cCtx.StringSlice(FlagPeriodicEvaluationRequireMergeRequestsWithLabel),
OnlyProjectsWithMembership: cCtx.Bool(FlagPeriodicEvaluationOnlyProjectsWithMembership),
OnlyProjectsWithTopics: cCtx.StringSlice(FlagPeriodicEvaluationOnlyProjectsWithTopics),
SCMConfigurationFilePath: cCtx.String(FlagConfigFile),
Expand Down
5 changes: 5 additions & 0 deletions cmd/gitlab_server_periodic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"fmt"
"log/slog"
"strings"
"sync"
Expand Down Expand Up @@ -71,6 +72,8 @@ func startPeriodicEvaluation(ctx context.Context, interval time.Duration, filter
continue
}

slogctx.Info(ctx, fmt.Sprintf("Found %d merge requests to evaluate", len(results)), slog.Int("number_of_projects", len(results)))

for _, mergeRequest := range results {
ctx := ctx // make sure we define a fresh GC-able context per merge request so we don't append to the existing forever
ctx = state.WithCommitSHA(ctx, mergeRequest.SHA)
Expand Down Expand Up @@ -98,6 +101,8 @@ func startPeriodicEvaluation(ctx context.Context, interval time.Duration, filter
continue
}
} // end loop results

slogctx.Info(ctx, "Completed periodic evaluation cycle")
} // end select
} // end loop
}(wg)
Expand Down
2 changes: 1 addition & 1 deletion pkg/scm/gitlab/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type PeriodicEvaluationProjectNode struct {
FullPath string `graphql:"fullPath"`

// MergeRequests contains up to 100 merge requests, sorted by oldest update/last change first
MergeRequests graphqlNodesOf[PeriodicEvaluationMergeRequestNode] `graphql:"mergeRequests(first: 100, state: opened, not: {labels: $mr_ignore_labels}, sort: UPDATED_ASC)"`
MergeRequests graphqlNodesOf[PeriodicEvaluationMergeRequestNode] `graphql:"mergeRequests(first: 100, state: opened, not: {labels: $mr_ignore_labels}, labels: $mr_require_labels, sort: UPDATED_ASC)"`

// Repository contains information about the git repository
Repository PeriodicEvaluationRepository `graphql:"repository"`
Expand Down
14 changes: 10 additions & 4 deletions pkg/scm/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,27 +204,33 @@ type MergeRequestListFilters struct {
IgnoreMergeRequestWithLabels []string
OnlyProjectsWithMembership bool
OnlyProjectsWithTopics []string
OnlyMergeRequestsWithLabels []string
SCMConfigurationFilePath string
}

func (filter *MergeRequestListFilters) AsGraphqlVariables() map[string]any {
output := map[string]any{
"mr_ignore_labels": filter.IgnoreMergeRequestWithLabels,
"mr_require_labels": filter.OnlyMergeRequestsWithLabels,
"project_membership": filter.OnlyProjectsWithMembership,
"project_topics": filter.OnlyProjectsWithTopics,
"scm_config_file_path": filter.SCMConfigurationFilePath,
}

if len(filter.SCMConfigurationFilePath) == 0 {
output["scm_config_file_path"] = ".scm-engine.yml"
if len(filter.IgnoreMergeRequestWithLabels) == 0 {
output["mr_ignore_labels"] = []string{}
}

if len(filter.OnlyMergeRequestsWithLabels) == 0 {
output["mr_require_labels"] = []string{}
}

if len(filter.OnlyProjectsWithTopics) == 0 {
output["project_topics"] = []string{}
}

if len(filter.IgnoreMergeRequestWithLabels) == 0 {
output["mr_ignore_labels"] = []string{}
if len(filter.SCMConfigurationFilePath) == 0 {
output["scm_config_file_path"] = ".scm-engine.yml"
}

return output
Expand Down

0 comments on commit c2f21dc

Please sign in to comment.