Skip to content

Commit

Permalink
add check on project and global levels if auto merge is enabled
Browse files Browse the repository at this point in the history
Signed-off-by: davidwin93 <dave.winiarski@zapier.com>
  • Loading branch information
davidwin93 committed Oct 16, 2023
1 parent 45797cf commit 9f7e0e2
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/runstream/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type RunMetadata interface {
GetCommitSHA() string
GetOrganization() string
GetVcsProvider() string
GetAutoMerge() bool
}

type RunPollingTask interface {
Expand Down
5 changes: 5 additions & 0 deletions pkg/runstream/run_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type TFRunMetadata struct {
RootNoteID int64

VcsProvider string

AutoMerge bool
}

func (r *TFRunMetadata) GetAction() string {
Expand Down Expand Up @@ -75,6 +77,9 @@ func (r *TFRunMetadata) GetOrganization() string {
func (r *TFRunMetadata) GetVcsProvider() string {
return r.VcsProvider
}
func (r *TFRunMetadata) GetAutoMerge() bool {
return r.AutoMerge
}
func (s *Stream) AddRunMeta(rmd RunMetadata) error {
b, err := encodeTFRunMetadata(rmd)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/tfc_trigger/project_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type TFCWorkspace struct {
Dir string `yaml:"dir"`
Mode string `yaml:"mode" default:"apply-before-merge" validate:"one_of=apply-before-merge,merge-before-apply,tfc-vcs-repo"`
TriggerDirs []string `yaml:"triggerDirs"`
AutoMerge bool `yaml:"autoMerge" default:"true"`
}

func getProjectConfigFile(ctx context.Context, gl vcs.GitClient, trigger *TFCTrigger) (*ProjectConfig, error) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/tfc_trigger/project_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func Test_loadProjectConfig(t *testing.T) {
Dir: "terraform/dev/",
Mode: "apply-before-merge",
TriggerDirs: nil,
AutoMerge: true,
},
}},
wantErr: false,
Expand All @@ -202,6 +203,7 @@ func Test_loadProjectConfig(t *testing.T) {
Dir: "terraform/dev/",
Mode: "apply-before-merge",
TriggerDirs: nil,
AutoMerge: true,
},
}},
wantErr: false,
Expand All @@ -215,6 +217,7 @@ func Test_loadProjectConfig(t *testing.T) {
Organization: "foo-corp",
Dir: "terraform/dev/",
Mode: "apply-before-merge",
AutoMerge: true,
TriggerDirs: []string{
"modules/**",
},
Expand All @@ -231,6 +234,7 @@ func Test_loadProjectConfig(t *testing.T) {
Organization: "foo-corp",
Dir: "terraform/dev/",
Mode: "apply-before-merge",
AutoMerge: true,
TriggerDirs: []string{
"modules/database/",
},
Expand All @@ -247,6 +251,7 @@ func Test_loadProjectConfig(t *testing.T) {
Organization: "foo-corp",
Dir: "terraform/dev/",
Mode: "apply-before-merge",
AutoMerge: true,
},
}},
wantErr: false,
Expand All @@ -266,12 +271,14 @@ func Test_loadProjectConfig(t *testing.T) {
Organization: "foo-corp",
Dir: "terraform/dev/",
Mode: "apply-before-merge",
AutoMerge: true,
},
{
Name: "service-tfbuddy-tooling",
Organization: "foo-corp",
Dir: "terraform/tooling/",
Mode: "apply-before-merge",
AutoMerge: true,
},
}},
wantErr: false,
Expand Down
6 changes: 4 additions & 2 deletions pkg/tfc_trigger/tfc_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,10 @@ func (t *TFCTrigger) triggerRunForWorkspace(ctx context.Context, cfgWS *TFCWorks
Bool("speculative", run.ConfigurationVersion.Speculative).
Msg("created TFC run")

return t.publishRunToStream(ctx, run)
return t.publishRunToStream(ctx, run, cfgWS)
}

func (t *TFCTrigger) publishRunToStream(ctx context.Context, run *tfe.Run) error {
func (t *TFCTrigger) publishRunToStream(ctx context.Context, run *tfe.Run, cfgWS *TFCWorkspace) error {
ctx, span := otel.Tracer("TFC").Start(ctx, "publishRunToStream")
defer span.End()

Expand All @@ -639,6 +639,8 @@ func (t *TFCTrigger) publishRunToStream(ctx context.Context, run *tfe.Run) error
DiscussionID: t.GetMergeRequestDiscussionID(),
RootNoteID: t.GetMergeRequestRootNoteID(),
VcsProvider: t.GetVcsProvider(),
//set Auto Merge if both conditions are met.
AutoMerge: cfgWS.AutoMerge && cfgWS.Mode == "apply-before-merge",
}
err := t.runstream.AddRunMeta(rmd)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions pkg/vcs/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package vcs

import "os"

const TF_BUDDY_AUTO_MERGE = "TFBUDDY_ENABLE_AUTO_MERGE"

func IsGlobalAutoMergeEnabled() bool {
//empty or true will permit auto merge.
return os.Getenv(TF_BUDDY_AUTO_MERGE) != "false"
}
8 changes: 7 additions & 1 deletion pkg/vcs/github/run_events_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func (w *RunEventsWorker) postRunStatusComment(ctx context.Context, run *tfe.Run
return
}
// The applying phase of a run has completed.
w.client.MergeMR(ctx, rmd.GetMRInternalID(), rmd.GetMRProjectNameWithNamespace())
w.mergePRIfPossible(ctx, rmd)
}
}
func (w *RunEventsWorker) mergePRIfPossible(ctx context.Context, rmd runstream.RunMetadata) {
if !rmd.GetAutoMerge() || !vcs.IsGlobalAutoMergeEnabled() {
return
}
w.client.MergeMR(ctx, rmd.GetMRInternalID(), rmd.GetMRProjectNameWithNamespace())
}
15 changes: 13 additions & 2 deletions pkg/vcs/gitlab/mr_status_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/rs/zerolog/log"
gogitlab "github.com/xanzy/go-gitlab"
"github.com/zapier/tfbuddy/pkg/runstream"
"github.com/zapier/tfbuddy/pkg/vcs"
"go.opentelemetry.io/otel"
)

Expand Down Expand Up @@ -197,10 +198,20 @@ func (p *RunStatusUpdater) getLatestPipelineID(ctx context.Context, rmd runstrea
return nil
}

func (p *RunStatusUpdater) mergeMRIfPossible(ctx context.Context, rmd runstream.RunMetadata) error {
func (p *RunStatusUpdater) mergeMRIfPossible(ctx context.Context, rmd runstream.RunMetadata) {
ctx, span := otel.Tracer("TFC").Start(ctx, "mergeMRIfPossible")
defer span.End()

if !rmd.GetAutoMerge() || !vcs.IsGlobalAutoMergeEnabled() {
return
}

err := p.client.MergeMR(ctx, rmd.GetMRInternalID(), rmd.GetMRProjectNameWithNamespace())
if err != nil {
span.RecordError(err)
}
log.Debug().AnErr("err", err).Msg("merge MR")
return err

}

// configureBackOff returns a backoff configuration to use to retry requests
Expand Down

0 comments on commit 9f7e0e2

Please sign in to comment.