Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit98830f1

Browse files
authored
Azure - omit empty values in UpdatePullRequest (#123)
1 parent8722fd2 commit98830f1

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

‎vcsclient/azurerepos.go‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,26 +181,23 @@ func (client *AzureReposClient) CreatePullRequest(ctx context.Context, _, reposi
181181
}
182182

183183
// UpdatePullRequest on Azure Repos
184-
func (client*AzureReposClient)UpdatePullRequest(ctx context.Context,owner,repository,title,body,targetBranchNamestring,prIdint,state vcsutils.PullRequestState)error {
184+
func (client*AzureReposClient)UpdatePullRequest(ctx context.Context,_,repository,title,body,targetBranchNamestring,prIdint,state vcsutils.PullRequestState)error {
185185
azureReposGitClient,err:=client.buildAzureReposClient(ctx)
186186
iferr!=nil {
187187
returnerr
188188
}
189-
// If the string is empty,do not add a prefix,as it indicates that the user does not intend to update the target branch.
190-
iftargetBranchName!="" {
191-
targetBranchName=vcsutils.AddBranchPrefix(targetBranchName)
192-
}
189+
targetBranchName=vcsutils.AddBranchPrefix(targetBranchName)
193190
client.logger.Debug(updatingPullRequest,prId)
194191
_,err=azureReposGitClient.UpdatePullRequest(ctx, git.UpdatePullRequestArgs{
195192
GitPullRequestToUpdate:&git.GitPullRequest{
196-
Description:&body,
193+
Description:vcsutils.GetNilIfZeroVal(body),
197194
Status:azureMapPullRequestState(state),
198-
TargetRefName:&targetBranchName,
199-
Title:&title,
195+
TargetRefName:vcsutils.GetNilIfZeroVal(targetBranchName),
196+
Title:vcsutils.GetNilIfZeroVal(title),
200197
},
201-
RepositoryId:&repository,
202-
PullRequestId:&prId,
203-
Project:&client.vcsInfo.Project,
198+
RepositoryId:vcsutils.GetNilIfZeroVal(repository),
199+
PullRequestId:vcsutils.GetNilIfZeroVal(prId),
200+
Project:vcsutils.GetNilIfZeroVal(client.vcsInfo.Project),
204201
})
205202
returnerr
206203
}

‎vcsutils/utils.go‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ func GetZeroValue[T any]() T {
157157
return*new(T)
158158
}
159159

160+
funcisZeroValue[Tcomparable](valT)bool {
161+
zeroValue:=GetZeroValue[T]()
162+
returnzeroValue==val
163+
}
164+
160165
// DefaultIfNotNil checks:
161166
// 1. If the pointer is nil, return the zero value of the type
162167
// 2. If the pointer isn't nil, return the value of the pointer.
@@ -167,14 +172,21 @@ func DefaultIfNotNil[T any](val *T) T {
167172
return*val
168173
}
169174

175+
funcGetNilIfZeroVal[Tcomparable](valT)*T {
176+
ifisZeroValue(val) {
177+
returnnil
178+
}
179+
return&val
180+
}
181+
170182
// PointerOf returns pointer to the provided value if it is not nil.
171183
funcPointerOf[Tany](vT)*T {
172184
return&v
173185
}
174186

175187
// AddBranchPrefix adds a branchPrefix to a branch name if it is not already present.
176188
funcAddBranchPrefix(branchstring)string {
177-
if!strings.HasPrefix(branch,branchPrefix) {
189+
ifbranch!=""&&!strings.HasPrefix(branch,branchPrefix) {
178190
branch=fmt.Sprintf("%s%s",branchPrefix,branch)
179191
}
180192
returnbranch

‎vcsutils/utils_test.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,14 @@ func TestGetPullRequestFilePath(t *testing.T) {
256256
assert.Equal(t,test.expected,result)
257257
}
258258
}
259+
260+
funcTestGetNilIfZeroVal(t*testing.T) {
261+
integerZeroVal:=0
262+
stringZeroVal:=""
263+
integerVal:=42
264+
stringVal:="Hello"
265+
assert.Nil(t,GetNilIfZeroVal(integerZeroVal))
266+
assert.Nil(t,GetNilIfZeroVal(stringZeroVal))
267+
assert.Equal(t,integerVal,*GetNilIfZeroVal(integerVal))
268+
assert.Equal(t,stringVal,*GetNilIfZeroVal(stringVal))
269+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp