编写 Azure Devops / TFS 脚本时 API - 有没有办法在不退出的情况下处理 "Get Repo does not exist" 错误响应?
When scripting Azure Devops / TFS API - Is there a way to handle "Get Repo does not exist" error response without exiting?
在 TFS 管道内部,我在 Powershell 脚本中使用 Get Repo API 来在脚本启动新 GIT TFS 内的回购。
我已经验证了 Invoke-Method 语法是好的。当 TFS GIT Repo 存在时,它 returns 没有问题的值。
当回购不存在时,API 响应以下错误并以代码“1”退出。
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"TF401019: The
Git repository with name or identifier TFS-GIT-REPO-NAME-GOES-HERE does not
exist or you do not have permissions for the operation you are attempting.","ty
peName":"Microsoft.TeamFoundation.Git.Server.GitRepositoryNotFoundException,
Microsoft.TeamFoundation.Git.Server, Version=14.0.0.0, Culture=neutral, PublicK
eyToken=b03f5f7f11d50a3a","typeKey":"GitRepositoryNotFoundException","errorCode
":0,"eventId":3000}
At //filepath/scriptName.ps1
+ $results= Invoke-RestMethod @args
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:Htt
pWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShe
ll.Commands.InvokeRestMethodCommand
##[error]PowerShell exited with code '1'.
目前,Invoke-RestMethod 行的结构如下:
$results= Invoke-RestMethod @args
我希望脚本能够将这种情况处理为 "does not exist" 而不会崩溃。
任何人都可以建议我忽略此错误代码的方法吗?欢迎任何建设性的建议!谢谢!
根据您回答的上下文,您需要一个 try-catch 块。这意味着如果在 try 部分发生错误,它不会使程序崩溃但会捕获错误并让您决定在块的 catch 部分做什么。如果您想了解更多信息,请转到 here。还有另外一个部分称为 finally 块,无论您的代码是否失败,它都会执行。不过,此部分完全是可选的。
try { $results= Invoke-RestMethod @args }
catch { "do nothing or record the error out to a log" }
try { $results= Invoke-RestMethod @args }
catch { "do nothing or record the error out to a log" }
finally { "do something else" }
我使用获取回购列表 API 来获取项目中的所有回购。 Documentation here.
... /_apis/git/repositories?api-version=5.1
从那里,我检查了获取回购列表结果是否与我正在寻找的任何回购名称相匹配。如果匹配,我会在屏幕上写一条通知,并从我的查询数组中删除回购名称。
$getRepoListResultsTemp = tfsRepoAPI $BuildPathStage $apiModeTemp $DIRdata
$repoCheckArray = $newRepoName.split(",")
$repoCheckCount = $repoCheckArray.count
if ($repoCheckCount -gt 0) {
foreach ($tfsRepoName in $getRepoListResultsTemp[0].value.name) {
foreach ($repoCheckName in $repoCheckArray) {
if ($tfsRepoName -eq $repoCheckName) {
write-host "The repo exists. The repo name is $tfsRepoName"
$repoCheckArray = $repoCheckArray | ? {$_ -ne $tfsRepoName}
}
}
}
然后我遍历了查询数组中的每个剩余条目,在屏幕上写了一条通知,并为每个条目返回了 null。
if (-! [string]::IsNullOrWhiteSpace($repoCheckArray)) {
foreach ($repoCheckName in $repoCheckArray) {
write-host "Repo does not exist - $repoCheckName"
}
return $null,$null
}
}
对于 repo 列表中存在的名称,他们将启动 Get Repo API 并收集必要的数据。 Documentation here.
... /_apis/git/repositories/" + $repoName+ "?api-version=5.1
在 TFS 管道内部,我在 Powershell 脚本中使用 Get Repo API 来在脚本启动新 GIT TFS 内的回购。
我已经验证了 Invoke-Method 语法是好的。当 TFS GIT Repo 存在时,它 returns 没有问题的值。
当回购不存在时,API 响应以下错误并以代码“1”退出。
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"TF401019: The
Git repository with name or identifier TFS-GIT-REPO-NAME-GOES-HERE does not
exist or you do not have permissions for the operation you are attempting.","ty
peName":"Microsoft.TeamFoundation.Git.Server.GitRepositoryNotFoundException,
Microsoft.TeamFoundation.Git.Server, Version=14.0.0.0, Culture=neutral, PublicK
eyToken=b03f5f7f11d50a3a","typeKey":"GitRepositoryNotFoundException","errorCode
":0,"eventId":3000}
At //filepath/scriptName.ps1
+ $results= Invoke-RestMethod @args
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:Htt
pWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShe
ll.Commands.InvokeRestMethodCommand
##[error]PowerShell exited with code '1'.
目前,Invoke-RestMethod 行的结构如下:
$results= Invoke-RestMethod @args
我希望脚本能够将这种情况处理为 "does not exist" 而不会崩溃。
任何人都可以建议我忽略此错误代码的方法吗?欢迎任何建设性的建议!谢谢!
根据您回答的上下文,您需要一个 try-catch 块。这意味着如果在 try 部分发生错误,它不会使程序崩溃但会捕获错误并让您决定在块的 catch 部分做什么。如果您想了解更多信息,请转到 here。还有另外一个部分称为 finally 块,无论您的代码是否失败,它都会执行。不过,此部分完全是可选的。
try { $results= Invoke-RestMethod @args }
catch { "do nothing or record the error out to a log" }
try { $results= Invoke-RestMethod @args }
catch { "do nothing or record the error out to a log" }
finally { "do something else" }
我使用获取回购列表 API 来获取项目中的所有回购。 Documentation here.
... /_apis/git/repositories?api-version=5.1
从那里,我检查了获取回购列表结果是否与我正在寻找的任何回购名称相匹配。如果匹配,我会在屏幕上写一条通知,并从我的查询数组中删除回购名称。
$getRepoListResultsTemp = tfsRepoAPI $BuildPathStage $apiModeTemp $DIRdata
$repoCheckArray = $newRepoName.split(",")
$repoCheckCount = $repoCheckArray.count
if ($repoCheckCount -gt 0) {
foreach ($tfsRepoName in $getRepoListResultsTemp[0].value.name) {
foreach ($repoCheckName in $repoCheckArray) {
if ($tfsRepoName -eq $repoCheckName) {
write-host "The repo exists. The repo name is $tfsRepoName"
$repoCheckArray = $repoCheckArray | ? {$_ -ne $tfsRepoName}
}
}
}
然后我遍历了查询数组中的每个剩余条目,在屏幕上写了一条通知,并为每个条目返回了 null。
if (-! [string]::IsNullOrWhiteSpace($repoCheckArray)) {
foreach ($repoCheckName in $repoCheckArray) {
write-host "Repo does not exist - $repoCheckName"
}
return $null,$null
}
}
对于 repo 列表中存在的名称,他们将启动 Get Repo API 并收集必要的数据。 Documentation here.
... /_apis/git/repositories/" + $repoName+ "?api-version=5.1