Skip to content

Commit

Permalink
Azure - omit empty values in UpdatePullRequest (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerzi committed Nov 22, 2023
1 parent 8722fd2 commit 98830f1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
19 changes: 8 additions & 11 deletions vcsclient/azurerepos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 13 additions & 1 deletion vcsutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -167,14 +172,21 @@ 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
}

// 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
Expand Down
11 changes: 11 additions & 0 deletions vcsutils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 98830f1

Please sign in to comment.