From c890c9cc55679db642f03a05ac3b86fb78e56f65 Mon Sep 17 00:00:00 2001 From: Shawna Monero Date: Wed, 10 Jul 2024 09:32:51 -0700 Subject: [PATCH] clean up feature allocation since its always true now --- server/legacy/events/policy_filter.go | 42 ------ .../events/pr_project_context_builder.go | 31 ++-- server/legacy/events/vcs/github_client.go | 142 +----------------- server/legacy/lyft/command/feature_runner.go | 43 +----- .../neptune/gateway/event/pr_error_handler.go | 26 +--- server/neptune/workflows/activities/github.go | 34 +---- 6 files changed, 19 insertions(+), 299 deletions(-) diff --git a/server/legacy/events/policy_filter.go b/server/legacy/events/policy_filter.go index 85ea132f6..676c54b39 100644 --- a/server/legacy/events/policy_filter.go +++ b/server/legacy/events/policy_filter.go @@ -58,15 +58,6 @@ func (p *ApprovedPolicyFilter) Filter(ctx context.Context, installationToken int return failedPolicies, nil } - // Dismiss PR reviews when event came from pull request change/atlantis plan comment - if trigger == command.AutoTrigger || trigger == command.CommentTrigger { - err := p.dismissStalePRReviews(ctx, installationToken, repo, prNum) - if err != nil { - return failedPolicies, errors.Wrap(err, "failed to dismiss stale PR reviews") - } - return failedPolicies, nil - } - // Fetch reviewers who approved the PR approvedReviewers, err := p.prReviewFetcher.ListLatestApprovalUsernames(ctx, installationToken, repo, prNum) if err != nil { @@ -87,39 +78,6 @@ func (p *ApprovedPolicyFilter) Filter(ctx context.Context, installationToken int return filteredFailedPolicies, nil } -func (p *ApprovedPolicyFilter) dismissStalePRReviews(ctx context.Context, installationToken int64, repo models.Repo, prNum int) error { - shouldAllocate, err := p.allocator.ShouldAllocate(feature.LegacyDeprecation, feature.FeatureContext{ - RepoName: repo.FullName, - }) - if err != nil { - return errors.Wrap(err, "unable to allocate legacy deprecation feature flag") - } - // if legacy deprecation is enabled, don't dismiss stale PR reviews in legacy workflow - if shouldAllocate { - p.logger.InfoContext(ctx, "legacy deprecation feature flag enabled, not dismissing stale PR reviews") - return nil - } - - approvalReviews, err := p.prReviewFetcher.ListApprovalReviews(ctx, installationToken, repo, prNum) - if err != nil { - return errors.Wrap(err, "failed to fetch GH PR reviews") - } - - for _, approval := range approvalReviews { - isOwner, err := p.approverIsOwner(ctx, installationToken, approval) - if err != nil { - return errors.Wrap(err, "failed to validate approver is owner") - } - if isOwner { - err = p.prReviewDismisser.Dismiss(ctx, installationToken, repo, prNum, approval.GetID()) - if err != nil { - return errors.Wrap(err, "failed to dismiss GH PR reviews") - } - } - } - return nil -} - func (p *ApprovedPolicyFilter) approverIsOwner(ctx context.Context, installationToken int64, approval *gh.PullRequestReview) (bool, error) { if approval.GetUser() == nil { return false, errors.New("failed to identify approver") diff --git a/server/legacy/events/pr_project_context_builder.go b/server/legacy/events/pr_project_context_builder.go index d339c28d9..8b2ad46f8 100644 --- a/server/legacy/events/pr_project_context_builder.go +++ b/server/legacy/events/pr_project_context_builder.go @@ -1,8 +1,6 @@ package events import ( - "fmt" - "github.com/runatlantis/atlantis/server/config/valid" "github.com/runatlantis/atlantis/server/legacy/events/command" "github.com/runatlantis/atlantis/server/logging" @@ -38,23 +36,14 @@ func (p *PlatformModeProjectContextBuilder) BuildProjectContext( repoDir string, contextFlags *command.ContextFlags, ) []command.ProjectContext { - shouldAllocate, err := p.allocator.ShouldAllocate(feature.PlatformMode, feature.FeatureContext{RepoName: ctx.HeadRepo.FullName}) - if err != nil { - p.Logger.ErrorContext(ctx.RequestCtx, fmt.Sprintf("unable to allocate for feature: %s, error: %s", feature.PlatformMode, err)) - } - - if shouldAllocate { - return buildContext( - ctx, - cmdName, - getSteps(cmdName, prjCfg.PullRequestWorkflow, contextFlags.LogLevel), - p.CommentBuilder, - prjCfg, - commentArgs, - repoDir, - contextFlags, - ) - } - - return p.delegate.BuildProjectContext(ctx, cmdName, prjCfg, commentArgs, repoDir, contextFlags) + return buildContext( + ctx, + cmdName, + getSteps(cmdName, prjCfg.PullRequestWorkflow, contextFlags.LogLevel), + p.CommentBuilder, + prjCfg, + commentArgs, + repoDir, + contextFlags, + ) } diff --git a/server/legacy/events/vcs/github_client.go b/server/legacy/events/vcs/github_client.go index 13d3213d3..52ccf4b60 100644 --- a/server/legacy/events/vcs/github_client.go +++ b/server/legacy/events/vcs/github_client.go @@ -18,7 +18,6 @@ import ( "encoding/base64" "fmt" "net/http" - "strconv" "strings" "time" @@ -497,145 +496,8 @@ func (g *GithubClient) GetRepoStatuses(repo models.Repo, pull models.PullRequest // UpdateStatus updates the status badge on the pull request. // See https://github.com/blog/1227-commit-status-api. func (g *GithubClient) UpdateStatus(ctx context.Context, request types.UpdateStatusRequest) (string, error) { - shouldAllocate, err := g.allocator.ShouldAllocate(feature.LegacyDeprecation, feature.FeatureContext{ - RepoName: request.Repo.FullName, - }) - if err != nil { - return "", errors.Wrap(err, "unable to allocate legacy deprecation feature flag") - } - // if legacy deprecation is enabled, don't mutate check runs in legacy workflow - if shouldAllocate { - g.logger.InfoContext(ctx, "legacy deprecation feature flag enabled, not updating check runs") - return "", nil - } - - // Empty status ID means we create a new check run - if request.StatusID == "" { - return g.createCheckRun(ctx, request) - } - return request.StatusID, g.updateCheckRun(ctx, request, request.StatusID) -} - -func (g *GithubClient) createCheckRun(ctx context.Context, request types.UpdateStatusRequest) (string, error) { - status, conclusion := g.resolveChecksStatus(request.State) - createCheckRunOpts := github.CreateCheckRunOptions{ - Name: request.StatusName, - HeadSHA: request.Ref, - Status: &status, - Output: g.createCheckRunOutput(request), - } - - if request.DetailsURL != "" { - createCheckRunOpts.DetailsURL = &request.DetailsURL - } - - // Conclusion is required if status is Completed - if status == Completed.String() { - createCheckRunOpts.Conclusion = &conclusion - } - - checkRun, _, err := g.client.Checks.CreateCheckRun(ctx, request.Repo.Owner, request.Repo.Name, createCheckRunOpts) - if err != nil { - return "", err - } - - return strconv.FormatInt(*checkRun.ID, 10), nil -} - -func (g *GithubClient) updateCheckRun(ctx context.Context, request types.UpdateStatusRequest, checkRunID string) error { - status, conclusion := g.resolveChecksStatus(request.State) - updateCheckRunOpts := github.UpdateCheckRunOptions{ - Name: request.StatusName, - Status: &status, - Output: g.createCheckRunOutput(request), - } - - if request.DetailsURL != "" { - updateCheckRunOpts.DetailsURL = &request.DetailsURL - } - - // Conclusion is required if status is Completed - if status == Completed.String() { - updateCheckRunOpts.Conclusion = &conclusion - } - - checkRunIDInt, err := strconv.ParseInt(checkRunID, 10, 64) - if err != nil { - return err - } - - _, _, err = g.client.Checks.UpdateCheckRun(ctx, request.Repo.Owner, request.Repo.Name, checkRunIDInt, updateCheckRunOpts) - return err -} - -func (g *GithubClient) resolveState(state models.VCSStatus) string { - switch state { - case models.QueuedVCSStatus: - return "Queued" - case models.PendingVCSStatus: - return "In Progress" - case models.SuccessVCSStatus: - return "Success" - case models.FailedVCSStatus: - return "Failed" - } - return "Failed" -} - -func (g *GithubClient) createCheckRunOutput(request types.UpdateStatusRequest) *github.CheckRunOutput { - var summary string - - // Project command - if strings.Contains(request.StatusName, ":") { - // plan/apply command - if request.DetailsURL != "" { - summary = fmt.Sprintf(projectCommandTemplateWithLogs, - request.CommandName, - request.Project, - request.Workspace, - g.resolveState(request.State), - fmt.Sprintf("[Logs](%s)", request.DetailsURL), - ) - } else { - summary = fmt.Sprintf(projectCommandTemplate, - request.CommandName, - request.Project, - request.Workspace, - g.resolveState(request.State), - ) - } - } else { - if request.NumSuccess != "" && request.NumTotal != "" { - summary = fmt.Sprintf(commandTemplateWithCount, - request.CommandName, - request.NumTotal, - request.NumSuccess, - g.resolveState(request.State)) - } else { - summary = fmt.Sprintf(commandTemplate, - request.CommandName, - g.resolveState(request.State)) - } - } - - // Add formatting to summary - summary = strings.ReplaceAll(strings.ReplaceAll(summary, "{", "`"), "}", "`") - - checkRunOutput := github.CheckRunOutput{ - Title: &request.StatusName, - Summary: &summary, - } - - if request.Output == "" { - return &checkRunOutput - } - if len(request.Output) > maxChecksOutputLength { - terraformOutputTooLong := "Terraform output is too long for Github UI, please review the above link to view detailed logs." - checkRunOutput.Text = &terraformOutputTooLong - } else { - checkRunOutput.Text = &request.Output - } - return &checkRunOutput + // since legacy deprecation feature flag was enabled (2024), we don't need to do the updating of check runs + return "", nil } // Github Checks uses Status and Conclusion to report status of the check run. Need to map models.VcsStatus to Status and Conclusion diff --git a/server/legacy/lyft/command/feature_runner.go b/server/legacy/lyft/command/feature_runner.go index 69d5122df..5af75256d 100644 --- a/server/legacy/lyft/command/feature_runner.go +++ b/server/legacy/lyft/command/feature_runner.go @@ -1,8 +1,6 @@ package command import ( - "fmt" - "github.com/runatlantis/atlantis/server/config/valid" "github.com/runatlantis/atlantis/server/legacy/events" "github.com/runatlantis/atlantis/server/legacy/events/command" @@ -33,20 +31,9 @@ func (a *PlatformModeRunner) Run(ctx *command.Context, cmd *command.Comment) { return } - shouldAllocate, err := a.Allocator.ShouldAllocate(feature.PlatformMode, feature.FeatureContext{RepoName: ctx.HeadRepo.FullName}) - if err != nil { - a.Logger.ErrorContext(ctx.RequestCtx, fmt.Sprintf("unable to allocate for feature: %s, error: %s", feature.PlatformMode, err)) - } - - // if this isn't allocated don't worry about the rest - if !shouldAllocate { - a.Runner.Run(ctx, cmd) - return - } - // now let's determine whether the repo is configured for platform mode by building commands var projectCmds []command.ProjectContext - projectCmds, err = a.Builder.BuildApplyCommands(ctx, cmd) + projectCmds, err := a.Builder.BuildApplyCommands(ctx, cmd) if err != nil { a.Logger.ErrorContext(ctx.RequestCtx, err.Error()) return @@ -74,12 +61,7 @@ type PlatformModeProjectRunner struct { //create object and test // Plan runs terraform plan for the project described by ctx. func (p *PlatformModeProjectRunner) Plan(ctx command.ProjectContext) command.ProjectResult { - shouldAllocate, err := p.Allocator.ShouldAllocate(feature.PlatformMode, feature.FeatureContext{RepoName: ctx.HeadRepo.FullName}) - if err != nil { - p.Logger.ErrorContext(ctx.RequestCtx, fmt.Sprintf("unable to allocate for feature: %s, error: %s", feature.PlatformMode, err)) - } - - if shouldAllocate && (ctx.WorkflowModeType == valid.PlatformWorkflowMode) { + if ctx.WorkflowModeType == valid.PlatformWorkflowMode { return p.PlatformModeRunner.Plan(ctx) } @@ -88,12 +70,7 @@ func (p *PlatformModeProjectRunner) Plan(ctx command.ProjectContext) command.Pro // PolicyCheck evaluates policies defined with Rego for the project described by ctx. func (p *PlatformModeProjectRunner) PolicyCheck(ctx command.ProjectContext) command.ProjectResult { - shouldAllocate, err := p.Allocator.ShouldAllocate(feature.PlatformMode, feature.FeatureContext{RepoName: ctx.HeadRepo.FullName}) - if err != nil { - p.Logger.ErrorContext(ctx.RequestCtx, fmt.Sprintf("unable to allocate for feature: %s, error: %s", feature.PlatformMode, err)) - } - - if shouldAllocate && (ctx.WorkflowModeType == valid.PlatformWorkflowMode) { + if ctx.WorkflowModeType == valid.PlatformWorkflowMode { return p.PlatformModeRunner.PolicyCheck(ctx) } @@ -102,12 +79,7 @@ func (p *PlatformModeProjectRunner) PolicyCheck(ctx command.ProjectContext) comm // Apply runs terraform apply for the project described by ctx. func (p *PlatformModeProjectRunner) Apply(ctx command.ProjectContext) command.ProjectResult { - shouldAllocate, err := p.Allocator.ShouldAllocate(feature.PlatformMode, feature.FeatureContext{RepoName: ctx.HeadRepo.FullName}) - if err != nil { - p.Logger.ErrorContext(ctx.RequestCtx, fmt.Sprintf("unable to allocate for feature: %s, error: %s", feature.PlatformMode, err)) - } - - if shouldAllocate && (ctx.WorkflowModeType == valid.PlatformWorkflowMode) { + if ctx.WorkflowModeType == valid.PlatformWorkflowMode { return command.ProjectResult{ Command: command.Apply, RepoRelDir: ctx.RepoRelDir, @@ -122,12 +94,7 @@ func (p *PlatformModeProjectRunner) Apply(ctx command.ProjectContext) command.Pr } func (p *PlatformModeProjectRunner) Version(ctx command.ProjectContext) command.ProjectResult { - shouldAllocate, err := p.Allocator.ShouldAllocate(feature.PlatformMode, feature.FeatureContext{RepoName: ctx.HeadRepo.FullName}) - if err != nil { - p.Logger.ErrorContext(ctx.RequestCtx, fmt.Sprintf("unable to allocate for feature: %s, error: %s", feature.PlatformMode, err)) - } - - if shouldAllocate && (ctx.WorkflowModeType == valid.PlatformWorkflowMode) { + if ctx.WorkflowModeType == valid.PlatformWorkflowMode { return p.PlatformModeRunner.Version(ctx) } diff --git a/server/neptune/gateway/event/pr_error_handler.go b/server/neptune/gateway/event/pr_error_handler.go index 237e569e6..4345a7d4a 100644 --- a/server/neptune/gateway/event/pr_error_handler.go +++ b/server/neptune/gateway/event/pr_error_handler.go @@ -53,19 +53,7 @@ type LegacyPREventErrorHandler struct { } func (p *LegacyPREventErrorHandler) WrapWithHandling(ctx context.Context, event PREvent, commandName string, executor sync.Executor) sync.Executor { - allocation, err := p.allocator.ShouldAllocate(feature.LegacyDeprecation, feature.FeatureContext{ - RepoName: event.GetRepo().FullName, - }) - - if err != nil { - return p.delegate.WrapWithHandling(ctx, event, commandName, executor) - } - - if allocation { - return executor - } - - return p.delegate.WrapWithHandling(ctx, event, commandName, executor) + return executor } type NeptunePREventErrorHandler struct { @@ -74,18 +62,6 @@ type NeptunePREventErrorHandler struct { } func (p *NeptunePREventErrorHandler) WrapWithHandling(ctx context.Context, event PREvent, commandName string, executor sync.Executor) sync.Executor { - allocation, err := p.allocator.ShouldAllocate(feature.LegacyDeprecation, feature.FeatureContext{ - RepoName: event.GetRepo().FullName, - }) - - if err != nil { - return p.delegate.WrapWithHandling(ctx, event, commandName, executor) - } - - if !allocation { - return executor - } - return p.delegate.WrapWithHandling(ctx, event, commandName, executor) } diff --git a/server/neptune/workflows/activities/github.go b/server/neptune/workflows/activities/github.go index 66101ecf8..c87b0b8a0 100644 --- a/server/neptune/workflows/activities/github.go +++ b/server/neptune/workflows/activities/github.go @@ -95,17 +95,6 @@ type UpdateCheckRunResponse struct { } func (a *githubActivities) GithubUpdateCheckRun(ctx context.Context, request UpdateCheckRunRequest) (UpdateCheckRunResponse, error) { - shouldAllocate, err := a.Allocator.ShouldAllocate(feature.LegacyDeprecation, feature.FeatureContext{ - RepoName: request.Repo.GetFullName(), - }) - if err != nil { - activity.GetLogger(ctx).Error("unable to allocate legacy deprecation feature flag", key.ErrKey, err) - } - // skip check run mutation if we're in PR mode and legacy deprecation is not enabled - if request.Mode == terraform.PR && !shouldAllocate { - return UpdateCheckRunResponse{}, nil - } - output := github.CheckRunOutput{ Title: &request.Title, Text: &request.Title, @@ -152,17 +141,6 @@ func (a *githubActivities) GithubUpdateCheckRun(ctx context.Context, request Upd } func (a *githubActivities) GithubCreateCheckRun(ctx context.Context, request CreateCheckRunRequest) (CreateCheckRunResponse, error) { - shouldAllocate, err := a.Allocator.ShouldAllocate(feature.LegacyDeprecation, feature.FeatureContext{ - RepoName: request.Repo.GetFullName(), - }) - if err != nil { - activity.GetLogger(ctx).Error("unable to allocate legacy deprecation feature flag", key.ErrKey, err) - } - // skip check run mutation if we're in PR mode and legacy deprecation is not enabled - if request.Mode == terraform.PR && !shouldAllocate { - return CreateCheckRunResponse{}, nil - } - output := github.CheckRunOutput{ Title: &request.Title, Text: &request.Title, @@ -412,20 +390,10 @@ type DismissRequest struct { type DismissResponse struct{} func (a *githubActivities) GithubDismiss(ctx context.Context, request DismissRequest) (DismissResponse, error) { - shouldAllocate, err := a.Allocator.ShouldAllocate(feature.LegacyDeprecation, feature.FeatureContext{ - RepoName: request.Repo.GetFullName(), - }) - if err != nil { - return DismissResponse{}, errors.Wrap(err, "unable to allocate legacy deprecation feature flag") - } - // skip PR dismissals if we're in PR mode and legacy deprecation is not enabled - if !shouldAllocate { - return DismissResponse{}, nil - } dismissRequest := &github.PullRequestReviewDismissalRequest{ Message: github.String(request.DismissReason), } - _, _, err = a.Client.DismissReview( + _, _, err := a.Client.DismissReview( ctx, request.Repo.Owner, request.Repo.Name,