use*_*420 2 azure-devops azure-devops-rest-api
如何使用 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# 代码中执行以下步骤
调用 GetRef REST https://dev.azure.com/{0}/{1}/_apis/git/repositories/{2}/refs{3} 这应该返回您可以用来推送的存储库分支的对象你的改变
接下来,调用 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8739 次 |
| 最近记录: |