2

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

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

https://learn.microsoft.com/en-us/rest/api/azure/devops/git/repositories?view=azure-devops-rest-5.1

Is there any option available via REST API?.

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

1 Answers1

5

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

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

Follow below steps in your C# code

  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

  2. 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);
    
  • Where are the types such as "ChangeToAdd" defined... can you provide an actual complete (i.e. including all usings, etc...) sample? – David V. Corbin Oct 22 '20 at 13:59
  • code is available here https://gist.github.com/krishrocks1904/a0727291801eb837d4f691ea997fdc4c – Rakesh Suryawanshi Oct 23 '20 at 09:23
  • @RakeshSuryawanshi Thanks for the answer and code sample. If we need to make an initial commit with the basic code structure for application using REST API, do we need to go on a loop and add all the files to 'changes' list (variable as per your answer). That seems like a tedious process. Are there any other approach if we need to make a bulk commit? – Sneha Dominic Aug 09 '21 at 11:57