使用Azure DevOps REST API将新文件和文件夹添加到Azure Git存储库 [英] Add new Files and Folders to Azure Git Repository with Azure DevOps REST API

查看:218
本文介绍了使用Azure DevOps REST API将新文件和文件夹添加到Azure Git存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Azure DevOps REST API将新文件和文件夹添加到Azure Git存储库中?

How to Add new Files and Folders to Azure Git Repository with Azure DevOps REST API?

我想使用Azure DevOps REST API将一些静态文件添加到我的存储库中.

I want to add some static files to my repository using Azure DevOps REST APIs.

https://docs.microsoft.com/zh-CN/rest/api/azure/devops/git/repositories?view=azure-devops-rest-5.1

是否可以通过REST API使用任何选项?

Is there any option available via REST API?.

或CICD或通过c#提供的其他任何自动化方式?

or anyother automated way available, either CICD or through c#?

推荐答案

我找到了答案,我们可以使用Git Push REST API uri

I found the answer, we can use the Git Push REST API uri

https://docs.microsoft.com/zh-cn/rest/api/azure/devops/git/pushes/create?view=azure-devops-rest-5.1

https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pushes/create?view=azure-devops-rest-5.1

按照您的C#代码中的以下步骤操作

Follow below steps in your C# code

  1. 调用GetRef REST

  1. call GetRef REST https://dev.azure.com/{0}/{1}/_apis/git/repositories/{2}/refs{3} this should return the object of your repository branch which you can use to push your changes

接下来,调用Push REST API在文件夹中创建文件夹或文件 https://dev.azure.com/{0}/{1}/_apis/git/repositories/{2}/推送{3}

Next, call Push REST API to create folder or file into your repository 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);

这篇关于使用Azure DevOps REST API将新文件和文件夹添加到Azure Git存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆