[Azure] C# 如何控制 Azure DevOps - 新增/刪除/修改 上面的檔案

2020-12-11

上一篇文章 ( [Azure] C# 如何控制 Azure DevOps - 上面的專案 取得檔案內容跟所有 Repository資訊 ) 簡單的讓你可以取得  Azure 上面 的Respository  以及他的檔案,今天我們要來聊一下,關於如何上傳/修改/刪除上面的檔案




介紹一下今天要做的事情,上面已經有既有的 Respository ,然後我要對他上傳一個本地的檔案上去,之後對他修改,再來就是刪除。


Step1. 申請 https://dev.azure.com/ 帳號

Step2. 開一個專案

Step3. 申請自己的的 Personal Access Tokens,請注意最多只有 90 天,所以如果你有 production 要記得要做更換機制。




Step4. 安裝  Microsoft.TeamFoundationServer.Client

網址:  https://www.nuget.org/packages/Microsoft.TeamFoundationServer.Client/16.153.0   


 


新增檔案上去,細節我就寫在 code 裡面

var orgUrl = new Uri("https://dev.azure.com/your_project"); //if you dont have your_personal_access_token //reference : https://blog.no2don.com/2020/12/azure-c-azure-devops-repository.html var personalAccessToken = "your_personal_access_token";; VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken)); GitHttpClient gitClient = connection.GetClient<GitHttpClient>(); //663d14ea-6b23-4c1f-a083-ec38e2812320 is repo id //if you dont have that //reference : https://blog.no2don.com/2020/12/azure-c-azure-devops-repository.html GitRef defaultBranch = gitClient.GetRefsAsync("663d14ea-6b23-4c1f-a083-ec38e2812320", "", false).Result.First(); //先準備好一個 branch , commit 會用到 GitRefUpdate newBranch = new GitRefUpdate() { Name = $"refs/heads/main", OldObjectId = defaultBranch.ObjectId, }; //開始準備 commit GitCommitRef newCommit = new GitCommitRef() { //新增註解 Comment = "增加一個檔案 , " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Changes = new GitChange[] { new GitChange() { // xxx.jpg 是上去後的檔名 ChangeType = VersionControlChangeType.Add, Item = new GitItem() { Path = $"xxx.jpg" }, NewContent = new ItemContent() { //s1.jpg 是本地檔案 Content =Convert.ToBase64String(System.IO.File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory+"s1.jpg")), ContentType = ItemContentType.Base64Encoded, }, } }, }; //開始 push 上去 GitPush push = gitClient.CreatePushAsync(new GitPush() { RefUpdates = new GitRefUpdate[] { newBranch }, Commits = new GitCommitRef[] { newCommit }, }, "663d14ea-6b23-4c1f-a083-ec38e2812320").Result;



修改檔案,細節我就寫在 code 裡面

var orgUrl = new Uri("https://dev.azure.com/your_project"); //if you dont have your_personal_access_token //reference : https://blog.no2don.com/2020/12/azure-c-azure-devops-repository.html var personalAccessToken = "your_personal_access_token";; VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken)); GitHttpClient gitClient = connection.GetClient<GitHttpClient>(); //663d14ea-6b23-4c1f-a083-ec38e2812320 is repo id //if you dont have that //reference : https://blog.no2don.com/2020/12/azure-c-azure-devops-repository.html GitRef defaultBranch = gitClient.GetRefsAsync("663d14ea-6b23-4c1f-a083-ec38e2812320", "", false).Result.First(); //先準備好一個 branch , commit 會用到 GitRefUpdate newBranch = new GitRefUpdate() { Name = $"refs/heads/main", OldObjectId = defaultBranch.ObjectId, }; //開始準備 commit GitCommitRef newCommit = new GitCommitRef() { //新增註解 Comment = "修改一個檔案 , " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Changes = new GitChange[] { new GitChange() { // 跟新增不一樣的部分就是將 add => edit //upload new file to modify xxx.jpg to new. ChangeType = VersionControlChangeType.Edit, Item = new GitItem() { Path = $"xxx.jpg" }, NewContent = new ItemContent() { //s1.jpg 是本地檔案 Content =Convert.ToBase64String(System.IO.File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory+"s2.jpg")), ContentType = ItemContentType.Base64Encoded, }, } }, }; //開始 push 上去 GitPush push = gitClient.CreatePushAsync(new GitPush() { RefUpdates = new GitRefUpdate[] { newBranch }, Commits = new GitCommitRef[] { newCommit }, }, "663d14ea-6b23-4c1f-a083-ec38e2812320").Result;


刪除檔案,細節我就註解在 code 裡面

var orgUrl = new Uri("https://dev.azure.com/your_project"); //if you dont have your_personal_access_token //reference : https://blog.no2don.com/2020/12/azure-c-azure-devops-repository.html var personalAccessToken = "your_personal_access_token";; VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken)); GitHttpClient gitClient = connection.GetClient<GitHttpClient>(); //663d14ea-6b23-4c1f-a083-ec38e2812320 is repo id //if you dont have that //reference : https://blog.no2don.com/2020/12/azure-c-azure-devops-repository.html GitRef defaultBranch = gitClient.GetRefsAsync("663d14ea-6b23-4c1f-a083-ec38e2812320", "", false).Result.First(); GitRefUpdate newBranch = new GitRefUpdate() { Name = $"refs/heads/main", OldObjectId = defaultBranch.ObjectId, }; GitCommitRef newCommit = new GitCommitRef() { Comment = "delete file xxx.jpg", Changes = new GitChange[] { new GitChange() { ChangeType = VersionControlChangeType.Delete, Item = new GitItem() { Path = $"xxx.jpg" }, } }, }; GitPush push = gitClient.CreatePushAsync(new GitPush() { RefUpdates = new GitRefUpdate[] { newBranch }, Commits = new GitCommitRef[] { newCommit }, }, "663d14ea-6b23-4c1f-a083-ec38e2812320").Result; }

結論,只看我 code 不會太困難,但是裡面有些東西是從那個大案例中找出來的花了一點時間所以就筆記一下,給之後碰到的人 :)


reference:
https://github.com/microsoft/azure-devops-dotnet-samples


當麻許的碎念筆記 2014 | Donma Hsu Design.