簡體   English   中英

需要在 Azure DevOps 存儲庫中使用 C# 創建一個文件夾(以及其中的一個文件) - 無論是 Git 還是 TFVC

[英]Need to create a folder(and a file inside it) using C# inside Azure DevOps repository - be it Git or TFVC

從 Azure DevOps 門戶,無論源代碼是否被克隆,我都可以手動將文件/文件夾添加到存儲庫中 -用於說明的圖像

但是,我想從我的 ASP .NET 核心應用程序中的 c# 代碼以編程方式在存儲庫內的文件夾中創建一個文件夾和一個文件。

是否有 Azure DevOps 服務 REST API 或任何其他方式來做到這一點? 我將僅通過 PAT 令牌使用 BASIC 身份驗證。

注意:我只能在本地存儲庫中克隆源代碼。

早日回復真的很感激。

我嘗試了 HttpClient、GitHttpClient 和 LibGit2Sharp 但失敗了。

在您的 C# 代碼中執行以下步驟

  1. 調用 GetRef REST https://dev.azure.com/{0}/{1}/_apis/git/repositories/{2}/refs{3}這應該返回您可以用來推送的存儲庫分支的對象你的改變

  2. 接下來,調用 Push REST API 將文件夾或文件創建到您的存儲庫https://dev.azure.com/{0}/{1}/_apis/git/repositories/{2}/pushes{3}

     var changes = new List<ChangeToAdd>(); //Add Files //pnp_structure.yml var jsonContent = File.ReadAllText(@"./static-files/somejsonfile.json"); ChangeToAdd changeJson = new ChangeToAdd() { changeType = "add", item = new ItemBase() { path = string.Concat(path, "/[your-folder-name]/somejsonfile.json") }, newContent = new Newcontent() { contentType = "rawtext", content = jsonContent } }; changes.Add(changeJson); CommitToAdd commit = new CommitToAdd(); commit.comment = "commit from code"; commit.changes = changes.ToArray(); var content = new List<CommitToAdd>() { commit }; var request = new { refUpdates = refs, commits = content }; var personalaccesstoken = _configuration["azure-devOps-configuration-token"]; var authorization = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalaccesstoken))); _logger.LogInformation($"[HTTP REQUEST] make a http call with uri: {uri} "); //here I making http client call // https://dev.azure.com/{orgnizationName}/{projectName}/_apis/git/repositories/{repositoryId}/pushes{?api-version} var result = _httpClient.SendHttpWebRequest(uri, method, data, authorization);

暫無
暫無

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

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