gpt4 book ai didi

azure-devops - 使用 Azure DevOps REST API 将新文件和文件夹添加到 Azure Git 存储库

转载 作者:行者123 更新时间:2023-12-02 19:19:17 25 4
gpt4 key购买 nike

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

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

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

是否有通过 REST API 提供的选项?

或者任何其他可用的自动化方式,无论是 CICD 还是通过 c#?

最佳答案

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

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

在您的 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);

关于azure-devops - 使用 Azure DevOps REST API 将新文件和文件夹添加到 Azure Git 存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63262865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com