static Post[] ToPosts(GitTree tree)
 {
     return tree
         .tree
         .Where(i => i.type == "blob")
         .Select(i => new Post
         {
             postid = i.sha,
             title = i.path,
             permalink = i.url
         })
         .ToArray();
 }
Beispiel #2
0
 static Post[] ToPosts(GitTree tree)
 {
     return(tree
            .tree
            .Where(i => i.type == "blob")
            .Select(i => new Post
     {
         postid = i.sha,
         title = i.path,
         permalink = i.url
     })
            .ToArray());
 }
        async Task<SaveResult> CreateOrUpdateGithubPost(string postTitle, string content, IEnumerable<FileReference> referencedFiles, BlogSetting blog)
        {
            var treeToUpload = new GitTree();
            var imagesToUpload = referencedFiles.Where(f=>!f.Saved).ToList();
            if (imagesToUpload.Count > 0)
            {
                foreach (var imageToUpload in imagesToUpload)
                {
                    var imageContent = Convert.ToBase64String(File.ReadAllBytes(imageToUpload.FullPath));
                    var item = new GitFile
                    {
                        type = "tree",
                        path = imageToUpload.FullPath,
                        mode = ((int)GitTreeMode.SubDirectory),
                        content = imageContent
                    };

                    treeToUpload.tree.Add(item);
                }
            }

            var gitFile = new GitFile
            {
                path = postTitle,
                content = content,
                mode = (int)GitTreeMode.File,
                type = "blob"
            };
            treeToUpload.tree.Add(gitFile);

            var newTree = await githubApi.NewTree(blog.Token, blog.Username, blog.WebAPI, blog.BlogInfo.blogid, treeToUpload);
            var uploadedFile = newTree.Item1.tree.Single(t => t.path == gitFile.path);
            foreach (var fileReference in imagesToUpload)
            {
                fileReference.Saved = true;
            }

            return new SaveResult
                   {
                       Id = uploadedFile.sha,
                       NewDocumentContent = content
                   };
        }
        async Task<string> CreateOrUpdateGithubPost(string postTitle, string content, 
            ICollection<string> imagesToUpload, BlogSetting blog)
        {
            var treeToUpload = new GitTree();
            if (imagesToUpload.Count > 0)
            {
                foreach (var imageToUpload in imagesToUpload)
                {
                    var imageContent = Convert.ToBase64String(File.ReadAllBytes(imageToUpload));
                    var item = new GitFile
                    {
                        type = "tree",
                        path = imageToUpload,
                        mode = ((int)GitTreeMode.SubDirectory),
                        content = imageContent
                    };
                    treeToUpload.tree.Add(item);
                }
            }

            var gitFile = new GitFile
            {
                path = postTitle,
                content = content,
                mode = (int)GitTreeMode.File,
                type = "blob"
            };
            treeToUpload.tree.Add(gitFile);

            var newTree = await githubApi.NewTree(blog.Token, blog.Username, blog.WebAPI, blog.BlogInfo.blogid, treeToUpload);
            var uploadedFile = newTree.Item1.tree.Single(t => t.path == gitFile.path);
            return uploadedFile.sha;
        }
        async Task<GitTree> GetGitTree(string token, string username, string repository, GitTree tree, HttpClient client)
        {
            var url = string.Format("/repos/{0}/{1}/git/trees", username, repository);
            var respose = await client.PostAsJsonAsync(GetUrl(url, token), tree);

            return await respose.Content.ReadAsAsync<GitTree>();
        }
        static async Task<GitCommit> CreateGitCommit(string token, string username, string repository, string shaLatestCommit, GitTree gitTree,
                                         HttpClient client)
        {
            var gitCommit = new GitCommit
            {
                message = "Update from Markpad",
                parents = new[] {shaLatestCommit},
                tree = gitTree.sha
            };
            var url = string.Format("/repos/{0}/{1}/git/commits", username, repository);
            var respose = await client.PostAsJsonAsync(GetUrl(url, token), gitCommit);

            var content = await respose.Content.ReadAsAsync<dynamic>();
            gitCommit.sha = content.sha;
            return gitCommit;
        }
        public async Task<Tuple<GitTree, GitCommit>> NewTree(string token, string username, string repository, string branch, GitTree tree)
        {
            var client = new HttpClient();
            //Get base commit ref
            var shaLatestCommit = await GetLatestCommitSha(token, username, repository, branch, client);

            tree.base_tree = await GetBaseTreeSha(token, username, repository, shaLatestCommit, client);

            var gitTree = await GetGitTree(token, username, repository, tree, client);

            var gitCommit = await CreateGitCommit(token, username, repository, shaLatestCommit, gitTree, client);

            LogResult(await FinaliseCommit(token, username, repository, branch, gitCommit.sha, client));

            return Tuple.Create(gitTree, gitCommit);
        }
Beispiel #8
0
        async Task <GitTree> GetGitTree(string token, string username, string repository, GitTree tree, HttpClient client)
        {
            var url     = string.Format("/repos/{0}/{1}/git/trees", username, repository);
            var respose = await client.PostAsJsonAsync(GetUrl(url, token), tree);

            return(await respose.Content.ReadAsAsync <GitTree>());
        }
Beispiel #9
0
        static async Task <GitCommit> CreateGitCommit(string token, string username, string repository, string shaLatestCommit, GitTree gitTree,
                                                      HttpClient client)
        {
            var gitCommit = new GitCommit
            {
                message = "Update from Markpad",
                parents = new[] { shaLatestCommit },
                tree    = gitTree.sha
            };
            var url     = string.Format("/repos/{0}/{1}/git/commits", username, repository);
            var respose = await client.PostAsJsonAsync(GetUrl(url, token), gitCommit);

            var content = await respose.Content.ReadAsAsync <dynamic>();

            gitCommit.sha = content.sha;
            return(gitCommit);
        }
Beispiel #10
0
        public async Task <Tuple <GitTree, GitCommit> > NewTree(string token, string username, string repository, string branch, GitTree tree)
        {
            var client = new HttpClient();
            //Get base commit ref
            var shaLatestCommit = await GetLatestCommitSha(token, username, repository, branch, client);

            tree.base_tree = await GetBaseTreeSha(token, username, repository, shaLatestCommit, client);

            var gitTree = await GetGitTree(token, username, repository, tree, client);

            var gitCommit = await CreateGitCommit(token, username, repository, shaLatestCommit, gitTree, client);

            LogResult(await FinaliseCommit(token, username, repository, branch, gitCommit.sha, client));

            return(Tuple.Create(gitTree, gitCommit));
        }