From f35f4d67751b339a1fd4305a536bfd71ac668935 Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 24 Mar 2023 21:07:19 -0500 Subject: [PATCH] feat: Flag to disable custom `[Atlantis]` commit messages on PR merge (#3120) * Modify the automere commit message to include the PR number [DEVOPS-674] * Update server/events/vcs/common/common.go Co-authored-by: Ken Kaizu * added test for PR message func [DEVOPS-674] --------- Co-authored-by: Ken Kaizu --- server/events/vcs/azuredevops_client.go | 2 +- server/events/vcs/common/common.go | 8 +++++--- server/events/vcs/common/common_test.go | 22 +++++++++++++++++++++- server/events/vcs/gitlab_client.go | 2 +- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/server/events/vcs/azuredevops_client.go b/server/events/vcs/azuredevops_client.go index e9622e5aa..e12cd078b 100644 --- a/server/events/vcs/azuredevops_client.go +++ b/server/events/vcs/azuredevops_client.go @@ -325,7 +325,7 @@ func (g *AzureDevopsClient) MergePull(pull models.PullRequest, pullOptions model BypassPolicy: new(bool), BypassReason: azuredevops.String(""), DeleteSourceBranch: &pullOptions.DeleteSourceBranchOnMerge, - MergeCommitMessage: azuredevops.String(common.AutomergeCommitMsg), + MergeCommitMessage: azuredevops.String(common.AutomergeCommitMsg(pull.Num)), MergeStrategy: &mcm, SquashMerge: new(bool), TransitionWorkItems: twi, diff --git a/server/events/vcs/common/common.go b/server/events/vcs/common/common.go index 7a41b1d83..bb5004ed0 100644 --- a/server/events/vcs/common/common.go +++ b/server/events/vcs/common/common.go @@ -3,12 +3,14 @@ package common import ( + "fmt" "math" ) -// AutomergeCommitMsg is the commit message Atlantis will use when automatically -// merging pull requests. -const AutomergeCommitMsg = "[Atlantis] Automatically merging after successful apply" +// AutomergeCommitMsg returns the commit message to use when automerging. +func AutomergeCommitMsg(pullNum int) string { + return fmt.Sprintf("[Atlantis] Automatically merging after successful apply: PR #%d", pullNum) +} // SplitComment splits comment into a slice of comments that are under maxSize. // It appends sepEnd to all comments that have a following comment. diff --git a/server/events/vcs/common/common_test.go b/server/events/vcs/common/common_test.go index 79dea1cb7..246bd4985 100644 --- a/server/events/vcs/common/common_test.go +++ b/server/events/vcs/common/common_test.go @@ -18,7 +18,6 @@ import ( "testing" "github.com/runatlantis/atlantis/server/events/vcs/common" - . "github.com/runatlantis/atlantis/testing" ) @@ -61,3 +60,24 @@ func TestSplitComment_FourComments(t *testing.T) { sepStart + comment[expMax*2:expMax*3] + sepEnd, sepStart + comment[expMax*3:]}, split) } + +func TestAutomergeCommitMsg(t *testing.T) { + tests := []struct { + name string + pullNum int + want string + }{ + { + name: "Atlantis PR commit message should include PR number", + pullNum: 123, + want: "[Atlantis] Automatically merging after successful apply: PR #123", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := common.AutomergeCommitMsg(tt.pullNum); got != tt.want { + t.Errorf("AutomergeCommitMsg() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/server/events/vcs/gitlab_client.go b/server/events/vcs/gitlab_client.go index 84ef44fcb..9767f944f 100644 --- a/server/events/vcs/gitlab_client.go +++ b/server/events/vcs/gitlab_client.go @@ -313,7 +313,7 @@ func (g *GitlabClient) WaitForSuccessPipeline(ctx context.Context, pull models.P // MergePull merges the merge request. func (g *GitlabClient) MergePull(pull models.PullRequest, pullOptions models.PullRequestOptions) error { - commitMsg := common.AutomergeCommitMsg + commitMsg := common.AutomergeCommitMsg(pull.Num) mr, err := g.GetMergeRequest(pull.BaseRepo.FullName, pull.Num) if err != nil {