簡體   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.

我已經驗證 Invoke-Method 語法很好。 當 TFS GIT 存儲庫存在時,它會毫無問題地返回值。

當存儲庫不存在時,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

我希望腳本能夠將這種情況處理為“不存在”而不會崩潰。

誰能建議我忽略此錯誤代碼的方法? 歡迎任何建設性的建議! 謝謝!

根據您回答的上下文,您需要一個 try-catch 塊。 這意味着如果在 try 部分發生錯誤,它不會使程序崩潰,而是會捕獲錯誤並讓您決定在塊的 catch 部分中做什么。 如果您想了解更多信息 go在這里 還有另一個部分稱為 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" }

我使用 Get Repo List API 來收集項目中的所有 repo。 文檔在這里。

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

從那里,我檢查了 Get Repo List 結果是否與我正在尋找的任何 repo 名稱匹配。 如果有匹配,我會在屏幕上寫一個通知,並從我的查詢數組中刪除 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}
                    }
                }
            }

然后我遍歷查詢數組中剩余的每個條目,向屏幕寫入通知,並為每個條目返回 null。

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

對於 repo 列表中存在的名稱,他們將啟動 Get Repo API 並收集必要的數據。 文檔在這里。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM