Skip to content

Commit

Permalink
add merge MR support to Gitlab
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 13, 2023
1 parent 412247a commit 540a921
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/vcs/gitlab/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ func (c *GitlabClient) GetCommitStatuses(ctx context.Context, projectID, commitS

return statuses
}
func (c *GitlabClient) MergeMR(ctx context.Context, mrIID int, project string) error {
_, span := otel.Tracer("TFC").Start(ctx, "MergeMR")
defer span.End()
_, _, err := c.client.MergeRequests.AcceptMergeRequest(project, mrIID, &gogitlab.AcceptMergeRequestOptions{})
return err
}

// Crawl the comments on this MR for tfbuddy comments, grab any TFC urls out of them, and delete them.
func (c *GitlabClient) GetOldRunUrls(ctx context.Context, mrIID int, project string, rootNoteID int) (string, error) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/vcs/gitlab/mr_status_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (p *RunStatusUpdater) updateCommitStatusForRun(ctx context.Context, run *tf
}
// The applying phase of a run has completed.
p.updateStatus(ctx, gogitlab.Success, "apply", rmd)
p.mergeMRIfPossible(ctx, rmd)

case tfe.RunCanceled:
// The run has been discarded. This is a final state.
Expand Down Expand Up @@ -196,6 +197,12 @@ func (p *RunStatusUpdater) getLatestPipelineID(ctx context.Context, rmd runstrea
return nil
}

func (p *RunStatusUpdater) mergeMRIfPossible(ctx context.Context, rmd runstream.RunMetadata) error {
err := p.client.MergeMR(ctx, rmd.GetMRInternalID(), rmd.GetMRProjectNameWithNamespace())
log.Debug().AnErr("err", err).Msg("merge MR")
return err
}

// configureBackOff returns a backoff configuration to use to retry requests
func configureBackOff() *backoff.ExponentialBackOff {

Expand Down
1 change: 1 addition & 0 deletions pkg/vcs/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type GitClient interface {
SetCommitStatus(ctx context.Context, projectWithNS string, commitSHA string, status CommitStatusOptions) (CommitStatus, error)
GetPipelinesForCommit(ctx context.Context, projectWithNS string, commitSHA string) ([]ProjectPipeline, error)
GetOldRunUrls(ctx context.Context, mrIID int, project string, rootCommentID int) (string, error)
MergeMR(ctx context.Context, mrIID int, project string) error
}
type GitRepo interface {
FetchUpstreamBranch(string) error
Expand Down

0 comments on commit 540a921

Please sign in to comment.