Esempio n. 1
0
        public async Task DeploySite()
        {
            StatusUpdate?.Invoke("Starting deploy...", StatusType.Status);
            if (_githubToken == string.Empty)
            {
                // TODO: Send an error message
                StatusUpdate?.Invoke("No GitHub token configured.", StatusType.Error);
                return;
            }

            if (_repoName == string.Empty)
            {
                StatusUpdate?.Invoke("No repository configured.", StatusType.Error);
                return;
            }

            string[] splitRepoName = _repoName.Split('/');
            if (splitRepoName.Length != 2)
            {
                StatusUpdate?.Invoke("Repository name format invalid.", StatusType.Error);
                return;
            }

            if (_repoDestination == string.Empty)
            {
                StatusUpdate?.Invoke("No destination configured.", StatusType.Error);
                return;
            }

            StatusUpdate?.Invoke("Getting repository...", StatusType.Status);
            long repoId = (await _github.Repository.Get(splitRepoName[0], splitRepoName[1])).Id;

            string    headMasterRef = "heads/" + _repoBranch;
            Reference masterReference;

            try
            {
                masterReference = await _github.Git.Reference.Get(repoId, headMasterRef);
            }
            catch (Exception ex)
            {
                if (ex is NotFoundException)
                {
                    // This probably means the branch hasn't been created yet
                    // https://github.com/octokit/octokit.net/issues/1098
                    StatusUpdate?.Invoke("Creating branch " + _repoBranch + "...", StatusType.Status);
                    Reference master = await _github.Git.Reference.Get(repoId, "heads/master");

                    masterReference = await _github.Git.Reference.Create(repoId,
                                                                         new NewReference("refs/" + headMasterRef, master.Object.Sha));
                }
                else
                {
                    throw;
                }
            }

            StatusUpdate?.Invoke("Getting current commit...", StatusType.Status);
            GitHubCommit currentCommit  = (await _github.Repository.Commit.GetAll(repoId)).First();
            string       currentTreeSha = currentCommit.Commit.Tree.Sha;

            NewTree newTree = new NewTree();

            if (_repoDirectory != string.Empty)
            {
                TreeResponse currentTree = await _github.Git.Tree.Get(repoId, currentTreeSha);

                // Copy the current tree to the new tree
                StatusUpdate?.Invoke("Copying current tree to new tree...", StatusType.Status);
                newTree = (await CopySubtree(_github, currentTree, repoId, _repoDirectory)).Tree;
            }

            // Now, create all of the files in the tree
            StatusUpdate?.Invoke("Uploading compiled pages and adding to tree...", StatusType.Status);
            await _context.CompiledPages.ToList().ForEachAsync(async page =>
            {
                NewBlob newBlob = new NewBlob
                {
                    Content  = MiscUtils.Base64Encode(page.Contents),
                    Encoding = EncodingType.Base64
                };
                BlobReference newBlobCreated = await _github.Git.Blob.Create(repoId, newBlob);
                string newBlobSha            = newBlobCreated.Sha;
                newTree.Tree.Add(new NewTreeItem
                {
                    Mode = "100644",
                    Type = TreeType.Blob,
                    Sha  = newBlobSha,
                    Path = Path.Join(_repoDirectory, page.Title + ".html")
                });
            });

            // Upload all of the files from the wwwroot directory
            StatusUpdate?.Invoke("Uploading other files and adding to tree...", StatusType.Status);
            (await CopyDirectoryIntoTree(_github, repoId, _repoDirectory)).ForEach(item => { newTree.Tree.Add(item); });

            StatusUpdate?.Invoke("Creating commit...", StatusType.Status);
            string    newTreeSha    = (await _github.Git.Tree.Create(repoId, newTree)).Sha;
            NewCommit newCommit     = new NewCommit("Automated deploy from webweb", newTreeSha, masterReference.Object.Sha);
            Commit    createdCommit = await _github.Git.Commit.Create(repoId, newCommit);

            await _github.Git.Reference.Update(repoId, headMasterRef, new ReferenceUpdate(createdCommit.Sha));

            StatusUpdate?.Invoke("Deploy complete.", StatusType.Success);
        }