[英]How to list the files with content that are in azure repo using REST API
[英]How to commit a list of folders and files to Azure DevOps Repository using Rest API?
我正在尝试使用 Azure DevOps REST API 自动创建存储库及其初始化。 我能够使用 API 成功创建存储库。
我们如何使用 REST API 提交构成基本代码结构的批量数据,比如文件夹和文件列表? 在Pushes - Create的请求体中,contentType 可以是 base64encoded 或 rawtext。 我已经使用 rawtext 来测试单个文件的提交并且它成功运行。 现在,我必须同时提交文件和文件夹。
实际上,Rest API 总是用来提交与项目相关的文件。
如果要提交文件夹中的所有文件,则应在更改中定义所有文件的路径。 Shayki Abramczyk 的评论非常有帮助。 注意: Git文件夹不能为空。
例如,这两个路径将提交文件夹“content”。
"item": {
"path": "/tasks/content/newtasks.md"
}
"item": {
"path": "/tasks/content/inactivetasks.md"
}
请参考this similar issue ,Rakesh创建了一个function和C#来自动推送文件。
public class Refs
{
public string name { get; set; }
public string objectId { get; set; }
public string oldObjectId { get; set; }
public Creator creator { get; set; }
public string url { get; set; }
}
public class Change
{
public string changeType { get; set; }
public Item item { get; set; }
public Newcontent newContent { get; set; }
}
public class CommitToAdd
{
public string comment { get; set; }
public ChangeToAdd[] changes { get; set; }
}
public class ChangeToAdd
{
public string changeType { get; set; }
public ItemBase item { get; set; }
public Newcontent newContent { get; set; }
}
public class ItemBase
{
public string path { get; set; }
}
public class Newcontent
{
public string content { get; set; }
public string contentType { get; set; }
}
// ### Implementation
//on your Program.cs file
public static class program
{
public async Task AddFileToRepository(string projectName, string repositoryId, Dictionary<string, Task<string>> blobContainer)
{
var refs = new List<Refs>() { new Refs { oldObjectId = "0000000000000000000000000000000000000000", name = Constants.DevOps.MASTER_REPO_REF_NAME } };
var changes = new List<ChangeToAdd>();
foreach (var blob in blobContainer)
{
if (!blob.Key.StartsWith(".git"))
{
ChangeToAdd changeJson = new ChangeToAdd()
{
changeType = "add",
item = new ItemBase() { path = blob.Key },
newContent = new Newcontent()
{
contentType = "rawtext",
content = blob.Value.Result
}
};
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 uri = $"https://dev.azure.com/{_orgnizationName}/{projectName}/_apis/git/repositories/{repositoryId}/pushes{Constants.DevOps.API_VERSION}";
using (var client = this.HttpClient)
{
var authorizationToken = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalaccessToken)));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authorizationToken);
var requestJson = JsonConvert.SerializeObject(request);
var httpContent = new StringContent(requestJson, Encoding.ASCII, "application/json");
var response = await client.PostAsync(uri, httpContent);
if (!response.IsSuccessStatusCode)
{
throw new Exception(ApplicationMessages.FailedToAddFilesToRepository);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.