From 98830f1be81504987593dc61a61b5aa79c0572fa Mon Sep 17 00:00:00 2001 From: Omer Zidkoni <50792403+omerzi@users.noreply.github.com> Date: Wed, 22 Nov 2023 10:16:13 +0200 Subject: [PATCH] Azure - omit empty values in UpdatePullRequest (#123) --- vcsclient/azurerepos.go | 19 ++++++++----------- vcsutils/utils.go | 14 +++++++++++++- vcsutils/utils_test.go | 11 +++++++++++ 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/vcsclient/azurerepos.go b/vcsclient/azurerepos.go index 9631c6e3..3291786a 100644 --- a/vcsclient/azurerepos.go +++ b/vcsclient/azurerepos.go @@ -181,26 +181,23 @@ func (client *AzureReposClient) CreatePullRequest(ctx context.Context, _, reposi } // UpdatePullRequest on Azure Repos -func (client *AzureReposClient) UpdatePullRequest(ctx context.Context, owner, repository, title, body, targetBranchName string, prId int, state vcsutils.PullRequestState) error { +func (client *AzureReposClient) UpdatePullRequest(ctx context.Context, _, repository, title, body, targetBranchName string, prId int, state vcsutils.PullRequestState) error { azureReposGitClient, err := client.buildAzureReposClient(ctx) if err != nil { return err } - // If the string is empty,do not add a prefix,as it indicates that the user does not intend to update the target branch. - if targetBranchName != "" { - targetBranchName = vcsutils.AddBranchPrefix(targetBranchName) - } + targetBranchName = vcsutils.AddBranchPrefix(targetBranchName) client.logger.Debug(updatingPullRequest, prId) _, err = azureReposGitClient.UpdatePullRequest(ctx, git.UpdatePullRequestArgs{ GitPullRequestToUpdate: &git.GitPullRequest{ - Description: &body, + Description: vcsutils.GetNilIfZeroVal(body), Status: azureMapPullRequestState(state), - TargetRefName: &targetBranchName, - Title: &title, + TargetRefName: vcsutils.GetNilIfZeroVal(targetBranchName), + Title: vcsutils.GetNilIfZeroVal(title), }, - RepositoryId: &repository, - PullRequestId: &prId, - Project: &client.vcsInfo.Project, + RepositoryId: vcsutils.GetNilIfZeroVal(repository), + PullRequestId: vcsutils.GetNilIfZeroVal(prId), + Project: vcsutils.GetNilIfZeroVal(client.vcsInfo.Project), }) return err } diff --git a/vcsutils/utils.go b/vcsutils/utils.go index c2f52e5e..b2b60a41 100644 --- a/vcsutils/utils.go +++ b/vcsutils/utils.go @@ -157,6 +157,11 @@ func GetZeroValue[T any]() T { return *new(T) } +func isZeroValue[T comparable](val T) bool { + zeroValue := GetZeroValue[T]() + return zeroValue == val +} + // DefaultIfNotNil checks: // 1. If the pointer is nil, return the zero value of the type // 2. If the pointer isn't nil, return the value of the pointer. @@ -167,6 +172,13 @@ func DefaultIfNotNil[T any](val *T) T { return *val } +func GetNilIfZeroVal[T comparable](val T) *T { + if isZeroValue(val) { + return nil + } + return &val +} + // PointerOf returns pointer to the provided value if it is not nil. func PointerOf[T any](v T) *T { return &v @@ -174,7 +186,7 @@ func PointerOf[T any](v T) *T { // AddBranchPrefix adds a branchPrefix to a branch name if it is not already present. func AddBranchPrefix(branch string) string { - if !strings.HasPrefix(branch, branchPrefix) { + if branch != "" && !strings.HasPrefix(branch, branchPrefix) { branch = fmt.Sprintf("%s%s", branchPrefix, branch) } return branch diff --git a/vcsutils/utils_test.go b/vcsutils/utils_test.go index 644dcb2d..414dd5eb 100644 --- a/vcsutils/utils_test.go +++ b/vcsutils/utils_test.go @@ -256,3 +256,14 @@ func TestGetPullRequestFilePath(t *testing.T) { assert.Equal(t, test.expected, result) } } + +func TestGetNilIfZeroVal(t *testing.T) { + integerZeroVal := 0 + stringZeroVal := "" + integerVal := 42 + stringVal := "Hello" + assert.Nil(t, GetNilIfZeroVal(integerZeroVal)) + assert.Nil(t, GetNilIfZeroVal(stringZeroVal)) + assert.Equal(t, integerVal, *GetNilIfZeroVal(integerVal)) + assert.Equal(t, stringVal, *GetNilIfZeroVal(stringVal)) +}