繁体   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