简体   繁体   English

在编写 Azure Devops / TFS API 脚本时 - 有没有办法在不退出的情况下处理“获取仓库不存在”错误响应?

[英]When scripting Azure Devops / TFS API - Is there a way to handle “Get Repo does not exist” error response without exiting?

Inside of a TFS pipeline, I am using the Get Repo API within a Powershell script to validate whether a GIT repo exists within TFS before the script initiates the creation of a new GIT repo within TFS. Inside of a TFS pipeline, I am using the Get Repo API within a Powershell script to validate whether a GIT repo exists within TFS before the script initiates the creation of a new GIT repo within TFS.

I have validated that the Invoke-Method syntax is good.我已经验证 Invoke-Method 语法很好。 When the TFS GIT Repo exists, it returns the values without issue.当 TFS GIT 存储库存在时,它会毫无问题地返回值。

When a repo does not exist, the API responds with the following error and exits with code '1'.当存储库不存在时,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'.

Currently, the Invoke-RestMethod line is structured like this:目前,Invoke-RestMethod 行的结构如下:

$results= Invoke-RestMethod @args

I would like the script to be able to process this case as "does not exist" without crashing.我希望脚本能够将这种情况处理为“不存在”而不会崩溃。

Can anyone suggest ways for me to ignore this error code?谁能建议我忽略此错误代码的方法? Any constructive suggestions are welcome!欢迎任何建设性的建议! Thanks!谢谢!

Based on the context of your answer you are wanting a try-catch block.根据您回答的上下文,您需要一个 try-catch 块。 This means that if an error happens within the try part it will not crash the program but catch the error and let you decide what to do in the catch part of the block.这意味着如果在 try 部分发生错误,它不会使程序崩溃,而是会捕获错误并让您决定在块的 catch 部分中做什么。 If you want more information go here .如果您想了解更多信息 go在这里 There is also one other section to this called a finally block which executes not matter whether you code fails or not.还有另一个部分称为 finally 块,无论您的代码是否失败,它都会执行。 This section is completely optional though.这部分是完全可选的。

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" }

I used the Get Repo List API to harvest all repos within a project.我使用 Get Repo List API 来收集项目中的所有 repo。 Documentation here. 文档在这里。

... /_apis/git/repositories?api-version=5.1

From there, I checked to see if the Get Repo List results matched any of the repo names that i was looking for.从那里,我检查了 Get Repo List 结果是否与我正在寻找的任何 repo 名称匹配。 If there was a match, I wrote a notification to the screen, and removed the repo name from my query array.如果有匹配,我会在屏幕上写一个通知,并从我的查询数组中删除 repo 名称。

        $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}
                    }
                }
            }

Then I iterated through each of the remaining entries in the query array, wrote a notification to the screen, and returned null for each.然后我遍历查询数组中剩余的每个条目,向屏幕写入通知,并为每个条目返回 null。

            if (-! [string]::IsNullOrWhiteSpace($repoCheckArray)) {
                foreach ($repoCheckName in $repoCheckArray) {
                    write-host "Repo does not exist - $repoCheckName"
                }
                return $null,$null
            }
        }

For the names that exist in the repo list, they will then kick off the Get Repo API and gather the necessary data.对于 repo 列表中存在的名称,他们将启动 Get Repo API 并收集必要的数据。 Documentation here. 文档在这里。

... /_apis/git/repositories/" + $repoName+ "?api-version=5.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Invalid Json error when Uploading/pushing a file to Azure Devops Repo using Pushes Create Api with Python - Invalid Json error when Uploading/pushing a file to Azure Devops Repo using Pushes Create Api with Python 获取 Azure DevOps 存储库的引用日志? - Get reflog of Azure devops repo? 将 Gitlab repo 迁移到 TFS (Azure DevOps) 包括历史和分支 - Migrate Gitlab repo to TFS (Azure DevOps) include history and branches Azure DevOps:如何使用 REST ZDB974238714CA8ADE4FZA repo 在 GIT 存储库中创建文件夹 - Azure DevOps: How to create a folder in GIT repo with REST API repo Azure Devops 指定登录 session 不存在 - Azure Devops specified logon session does not exist 使用推送将文件上传/推送到 Azure Devops Repo 时如何找到 oldobjectid - How to find oldobjectid when Uploading/pushing a file to Azure Devops Repo using Pushes Create Api with Python TFS到Azure DevOps的迁移 - TFS to Azure DevOps Migration 从 Azure DevOps 存储库导入时,go 获取私有仓库失败。 - go get private repo fails due to missing .git when importing from Azure DevOps repositories DevOps (Azure) 存储库配置 - DevOps (Azure) Repo Configuration Azure DevOps: HTTP 503 curl 22 推送到远程仓库时出错 - Azure DevOps: HTTP 503 curl 22 error when pushing to remote repo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM