Пример #1
0
        public GitCommit GetCommit(string org, string repo, string sha)
        {
            var commit = new GitCommit();

            try
            {
                var jss = new JavaScriptSerializer();

                var url = Resources.GitApiUrl + Resources.GitRepos + "/" + org + "/" + repo + Resources.GitCommits + "/" + sha;

                var request = (HttpWebRequest)WebRequest.Create(url);

                var responseString = new StreamReader(GetHttpWebResponse(request).GetResponseStream()).ReadToEnd();

                commit = jss.Deserialize <GitCommit>(responseString);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }

            return(commit);
        }
Пример #2
0
        /**
         * org - repo author name
         * repo - repository name
         * username - github login
         * password - github password
         * filepath - path to the file which you want to commit
         * branch - git branch name (will be commited to the head)
         */
        public string CommitFile(string org, string repo, string username, string password, string filepath, string branch, string commitMessage)
        {
            var            jss = new JavaScriptSerializer();
            string         url;
            HttpWebRequest request;

            try
            {
                var newFile = Convert.ToBase64String(File.ReadAllBytes(filepath));

                url = Resources.GitApiUrl + Resources.GitRepos + "/" + org + "/" + repo + Resources.GitGit + Resources.GitReferences +
                      Resources.GitHeads + "/" + branch;

                request = (HttpWebRequest)WebRequest.Create(url);
                var responseString = "";
                using (var response = GetHttpWebResponse(request))
                {
                    responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                }

                var headRef = jss.Deserialize <GitRef>(responseString);

                var headCommit = GetCommit(org, repo, headRef.Object.sha);

                url = headCommit.commit.tree.url;

                request = (HttpWebRequest)WebRequest.Create(url);

                using (var response = GetHttpWebResponse(request))
                {
                    responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                }

                var headTree = jss.Deserialize <GitTree>(responseString);

                var oldFileUrl = headTree.tree.Single(file => file.path == filepath).url;

                var json = new JavaScriptSerializer().Serialize(new
                {
                    content  = newFile,
                    encoding = "base64"
                });

                url = Resources.GitApiUrl + Resources.GitRepos + "/" + org + "/" + repo + Resources.GitGit + Resources.GitBlobs;
                var requestPost = (HttpWebRequest)WebRequest.Create(url);

                requestPost.Method = "POST";

                var newBlob = new GitBlob();
                using (var streamWriter = new StreamWriter(requestPost.GetRequestStream()))
                {
                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();

                    var httpResponse = GetHttpWebResponse(requestPost);
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();
                        newBlob = jss.Deserialize <GitBlob>(result);
                    }
                }

                var newTree = new GitNewTree {
                    base_tree = headCommit.commit.tree.sha
                };
                newTree.tree.Add(new GitNewTreeTree()
                {
                    mode = "100644", path = filepath, sha = newBlob.sha, type = "blob"
                });

                json = new JavaScriptSerializer().Serialize(newTree);

                url = Resources.GitApiUrl + Resources.GitRepos + "/" + org + "/" + repo + Resources.GitGit + Resources.GitTrees;
                var requestPostTree = (HttpWebRequest)WebRequest.Create(url);

                requestPostTree.Method = "POST";

                var newCreatedTree = new GitTree();
                using (var streamWriter = new StreamWriter(requestPostTree.GetRequestStream()))
                {
                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();

                    var httpResponse = GetHttpWebResponse(requestPostTree);
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();
                        newCreatedTree = jss.Deserialize <GitTree>(result);
                    }
                }

                var newCommit = new GitNewCommit();
                newCommit.message = "Test Msg";
                newCommit.author  = new GitCommitCommitAuthor()
                {
                    date = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssK"), email = "*****@*****.**", name = "MSiliunas"
                };
                newCommit.parents.Add(headCommit.sha);
                newCommit.tree = newCreatedTree.sha;

                json = new JavaScriptSerializer().Serialize(newCommit);

                url = Resources.GitApiUrl + Resources.GitRepos + "/" + org + "/" + repo + Resources.GitGit + Resources.GitCommits;
                var requestPostCommit = (HttpWebRequest)WebRequest.Create(url);

                requestPostCommit.Method      = "POST";
                requestPostCommit.ContentType = "application/json";

                var newCreatedCommit = new GitCommit();
                using (var streamWriter = new StreamWriter(requestPostCommit.GetRequestStream()))
                {
                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();

                    var httpResponse = GetHttpWebResponse(requestPostCommit);
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();
                        newCreatedCommit = jss.Deserialize <GitCommit>(result);
                    }
                }

                url = Resources.GitApiUrl + Resources.GitRepos + "/" + org + "/" + repo + Resources.GitGit + Resources.GitReferences +
                      Resources.GitHeads + "/" + branch;

                var requestPostRef = (HttpWebRequest)WebRequest.Create(url);

                requestPostRef.Method      = "PATCH";
                requestPostRef.ContentType = "application/json";

                json = new JavaScriptSerializer().Serialize(new
                {
                    sha   = newCreatedCommit.sha,
                    force = true
                });

                using (var streamWriter = new StreamWriter(requestPostRef.GetRequestStream()))
                {
                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();

                    var httpResponse = GetHttpWebResponse(requestPostRef);
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();
                        return(result);
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return("");
        }