Skip to content

Commit

Permalink
clean up feature allocation since its always true now
Browse files Browse the repository at this point in the history
  • Loading branch information
smonero committed Jul 10, 2024
1 parent 0c16f78 commit c890c9c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 299 deletions.
42 changes: 0 additions & 42 deletions server/legacy/events/policy_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {

Check failure on line 81 in server/legacy/events/policy_filter.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

func `(*ApprovedPolicyFilter).approverIsOwner` is unused (unused)
if approval.GetUser() == nil {
return false, errors.New("failed to identify approver")
Expand Down
31 changes: 10 additions & 21 deletions server/legacy/events/pr_project_context_builder.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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,
)
}
142 changes: 2 additions & 140 deletions server/legacy/events/vcs/github_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"encoding/base64"
"fmt"
"net/http"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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
Expand Down
43 changes: 5 additions & 38 deletions server/legacy/lyft/command/feature_runner.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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,
Expand All @@ -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)
}

Expand Down
26 changes: 1 addition & 25 deletions server/neptune/gateway/event/pr_error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down
Loading

0 comments on commit c890c9c

Please sign in to comment.