public static void Commit(string originmdPath, string modifiedPath, string repoName) { Console.WriteLine("******************Six steps for Commit***************************"); //get reference & tree to commit string url_getRef = url_Head + repoName + url_getRefTail; string url_getCommit = url_Head + repoName + url_getCommitTail; string parent_sha = JObject.Parse(Get(url_getRef, new Dictionary <string, string>()))["object"]["sha"].ToString(); string baseTree_sha = JObject.Parse(Get(url_getCommit + parent_sha, new Dictionary <string, string>()))["tree"]["sha"].ToString(); //create a blob string url_createBlob = url_Head + repoName + url_createBlobTail; StreamReader sr = new StreamReader(originmdPath, Encoding.GetEncoding("utf-8")); CreateBlobRequest createBlobRequest = new CreateBlobRequest { Content = sr.ReadToEnd(), Encoding = "utf-8" }; string createBlobBody = JsonConvert.SerializeObject(createBlobRequest); string blob_sha = JObject.Parse(Post(url_createBlob, createBlobBody))["sha"].ToString(); //create a new tree for commit string url_createTree = url_Head + repoName + url_createTreeTail; CreateTreeRequest createTreeRequest = new CreateTreeRequest { BaseTree = baseTree_sha, Tree = new TreeNode[] { new TreeNode { Path = modifiedPath, Mode = "100644", Type = "blob", Sha = blob_sha } } }; string createTreeBody = JsonConvert.SerializeObject(createTreeRequest); string treeSubmit_sha = JObject.Parse(Post(url_createTree, createTreeBody))["sha"].ToString(); //create a new commit string url_createCommit = url_Head + repoName + url_createCommitTail; CreateCommitRequest createCommitRequest = new CreateCommitRequest { Message = "Commit automatically!", Parents = new string[] { parent_sha }, Tree = treeSubmit_sha }; string createCommitBody = JsonConvert.SerializeObject(createCommitRequest); string createSubmit_sha = JObject.Parse(Post(url_createCommit, createCommitBody))["sha"].ToString(); //update reference string url_updateRef = url_Head + repoName + url_updateRefTail; UpdateReferenceRequest updateReferenceRequest = new UpdateReferenceRequest { Sha = createSubmit_sha, Force = true }; string updateReferenceBody = JsonConvert.SerializeObject(updateReferenceRequest); string updateRef_res = Post(url_updateRef, updateReferenceBody).ToString(); }
/// <summary> /// Creates a commit with multiple files and actions. /// </summary> /// <param name="projectId">The ID, path or <see cref="Project"/> of the project.</param> /// <param name="request">Create commit request.</param> /// <param name="autoEncodeToBase64">Automatically encode contents to base64 (default false).</param> public async Task <Commit> CreateAsync(ProjectId projectId, CreateCommitRequest request, bool autoEncodeToBase64 = false) { if (autoEncodeToBase64) { foreach (var action in request.Actions.Where(action => !string.IsNullOrEmpty(action.Content))) { action.Encoding = CreateCommitRequestActionEncoding.Base64; action.Content = Convert.ToBase64String(Encoding.UTF8.GetBytes(action.Content)); } } return(await _httpFacade.Post <Commit>($"projects/{projectId}/repository/commits", request)); }
private static void Commit() { Console.WriteLine("******************Six steps for Commit***************************"); //get reference & tree to commit string parent_sha = JObject.Parse(Get(url_getRef, new Dictionary <string, string>()))["object"]["sha"].ToString(); string baseTree_sha = JObject.Parse(Get(url_getCommit + parent_sha, new Dictionary <string, string>()))["tree"]["sha"].ToString(); //create a blob StreamReader sr = new StreamReader(sourceFile, Encoding.GetEncoding("utf-8")); CreateBlobRequest createBlobRequest = new CreateBlobRequest { Content = sr.ReadToEnd(), Encoding = "utf-8" }; string createBlobBody = JsonConvert.SerializeObject(createBlobRequest); string blob_sha = JObject.Parse(Post(url_createBlob, createBlobBody))["sha"].ToString(); //create a new tree for commit CreateTreeRequest createTreeRequest = new CreateTreeRequest { BaseTree = baseTree_sha, Tree = new TreeNode[] { new TreeNode { Path = @"node-azure-tools.md", Mode = "100644", Type = "blob", Sha = blob_sha } } }; string createTreeBody = JsonConvert.SerializeObject(createTreeRequest); string treeSubmit_sha = JObject.Parse(Post(url_createTree, createTreeBody))["sha"].ToString(); //create a new commit CreateCommitRequest createCommitRequest = new CreateCommitRequest { Message = "Commit automatically!", Parents = new string[] { parent_sha }, Tree = treeSubmit_sha }; string createCommitBody = JsonConvert.SerializeObject(createCommitRequest); string createSubmit_sha = JObject.Parse(Post(url_createCommit, createCommitBody))["sha"].ToString(); //update reference UpdateReferenceRequest updateReferenceRequest = new UpdateReferenceRequest { Sha = createSubmit_sha, Force = true }; string updateReferenceBody = JsonConvert.SerializeObject(updateReferenceRequest); string updateRef_res = Post(url_updateRef, updateReferenceBody).ToString(); }
public async Task CreateCommitRequest(bool autoEncodeToBase64) { //We need a distinction as otherwise the create won't work for the second run (already exists). string suffix = autoEncodeToBase64 ? "_autoEncoded" : ""; //Create var actions = new List <CreateCommitRequestAction> { new CreateCommitRequestAction(CreateCommitRequestActionType.Create, "file" + suffix) { Content = "content" } }; var createCommitRequest = new CreateCommitRequest("master", "create", actions); var commit = await _sut.CreateAsync(TestProjectId, createCommitRequest, autoEncodeToBase64); commit.Id.Should().NotBeNullOrEmpty(); //Update actions = new List <CreateCommitRequestAction> { new CreateCommitRequestAction(CreateCommitRequestActionType.Update, "file" + suffix) { Content = "new content" } }; createCommitRequest = new CreateCommitRequest("master", "update", actions); commit = await _sut.CreateAsync(TestProjectId, createCommitRequest, autoEncodeToBase64); commit.Id.Should().NotBeNullOrEmpty(); //Move actions = new List <CreateCommitRequestAction> { new CreateCommitRequestAction(CreateCommitRequestActionType.Move, "subfolder/file" + suffix) { PreviousPath = "file" + suffix } }; createCommitRequest = new CreateCommitRequest("master", "move", actions); commit = await _sut.CreateAsync(TestProjectId, createCommitRequest, autoEncodeToBase64); commit.Id.Should().NotBeNullOrEmpty(); commit.AuthorEmail.Should().Be(TestUserEmail); commit.AuthorName.Should().Be(TestName); commit.CommitStats.Should().NotBeNull(); //Delete actions = new List <CreateCommitRequestAction> { new CreateCommitRequestAction(CreateCommitRequestActionType.Delete, "subfolder/file" + suffix) }; createCommitRequest = new CreateCommitRequest("master", "delete", actions) { AuthorEmail = TestExtraUserEmail, AuthorName = TestExtraName, Stats = false }; commit = await _sut.CreateAsync(TestProjectId, createCommitRequest, autoEncodeToBase64); commit.Id.Should().NotBeNullOrEmpty(); commit.AuthorEmail.Should().Be(TestExtraUserEmail); commit.AuthorName.Should().Be(TestExtraName); commit.CommitStats.Should().BeNull(); }
public Task CreateCommit(int id, CreateCommitRequest commit) => _httpFacade.Post($"projects/{id}/repository/commits", commit);