예제 #1
0
        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();
        }
예제 #2
0
        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();
        }
        /// <summary>
        /// Create a new tree by using additional properties in the request.
        /// </summary>
        /// <param name="forestID">The ID of the forest in which to create the tree.</param>
        /// <returns>The created tree.</returns>
        private async Task <Tree> CreateTreeUsingAdditionalProperties(string forestID)
        {
            var createTreeRequest = new CreateTreeRequest
            {
                ForestId             = forestID,
                AdditionalProperties = new Dictionary <string, JToken>
                {
                    { "Name", "Demo tree " },
                    { "Description", "Demo tree" },
                    { "Type", "DemoTree" },
                },
            };

            Console.WriteLine($"Creating tree (using additional properties) with ForestId={createTreeRequest.ForestId}, Name={createTreeRequest.AdditionalProperties["Name"]}, " +
                              $"Description={createTreeRequest.AdditionalProperties["Description"]}, Type={createTreeRequest.AdditionalProperties["Type"]}...");

            Tree createdTree = await this.orgClient.CreateTreeAsync(createTreeRequest).ConfigureAwait(false);

            Console.Write("Created tree: ");
            this.PrintTree(createdTree);
            Console.WriteLine();

            return(createdTree);
        }
예제 #4
0
        /// <summary>
        /// Create a tree.
        /// </summary>
        /// <param name="forestID">The ID of the forest in which to create the tree.</param>
        /// <returns>The created tree.</returns>
        private async Task <Tree> CreateTree(string forestID)
        {
            var userID = await this.GetUserID().ConfigureAwait(false);

            var createTreeRequest = new CreateTreeRequest
            {
                ForestId    = forestID,
                Id          = $"CDMServicesDemo:Org:Tree-{Guid.NewGuid().ToString()}", // Optional (if not specified, the service will auto-generate it)
                Name        = "DemoTree",                                              // Optional
                Description = "A demo tree",                                           // Optional
                Type        = "DemoTree",                                              // Optional
                Policy      = new Policy                                               // Optional
                {
                    Statements = new PolicyStatement[]
                    {
                        new PolicyStatement
                        {
                            Effect    = "Allow",
                            Principal = new JValue($"user:{userID}"),
                            Action    = new JValue("*"),
                            Resource  = new JValue("*"),
                        },
                    },
                },
            };

            Console.WriteLine($"Creating tree with ForestId={createTreeRequest.ForestId}, Id={createTreeRequest.Id}, Name={createTreeRequest.Name}, Description={createTreeRequest.Description}, Type={createTreeRequest.Type}...");

            Tree tree = await this.orgClient.CreateTreeAsync(createTreeRequest).ConfigureAwait(false);

            Console.Write($"Created tree: ");
            this.PrintTree(tree);
            Console.WriteLine();

            return(tree);
        }