Exemplo n.º 1
0
        public void Process(ScheduledTaskContext context)
        {
            if (!context.Task.TaskType.StartsWith(TaskTypes.MarkdownContentUpdaterPrefix))
            {
                return;
            }

            Renew(true, context.Task.ContentItem);

            var markdownRepoContentItem = context.Task.ContentItem;

            if (markdownRepoContentItem == null || markdownRepoContentItem.ContentType != ContentTypes.MarkdownRepo)
            {
                return;
            }

            var markdownRepoPart = markdownRepoContentItem.As <MarkdownRepoPart>();

            // This is the first run, so we want to create content items from all the md files.
            if (string.IsNullOrEmpty(markdownRepoPart.LatestProcessedCommitHash))
            {
                var initResult = new InitContentItemsResult {
                };
                if (RepoIsGitHubRepo(markdownRepoPart.RepoUrl))
                {
                    var initContentItemsTask = _gitHubRepoService.InitContentItems(markdownRepoContentItem);
                    initContentItemsTask.Wait();
                    initResult = initContentItemsTask.Result;
                }

                if (initResult.Success)
                {
                    if (!string.IsNullOrEmpty(initResult.LatestProcessedCommitHash))
                    {
                        markdownRepoPart.LatestProcessedCommitHash = initResult.LatestProcessedCommitHash;
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(initResult.ErrorMessage))
                    {
                        Logger.Error("Error during initializing repo content items. Message: " + initResult.ErrorMessage);
                    }
                }
            }
            else
            {
                var updateResult = new UpdateContentItemsResult {
                };
                if (RepoIsGitHubRepo(markdownRepoPart.RepoUrl))
                {
                    var updateContentItemsTask = _gitHubRepoService.UpdateContentItems(markdownRepoContentItem);
                    updateContentItemsTask.Wait();
                    updateResult = updateContentItemsTask.Result;
                }

                if (updateResult.Success)
                {
                    if (!string.IsNullOrEmpty(updateResult.LatestProcessedCommitHash))
                    {
                        markdownRepoPart.LatestProcessedCommitHash = updateResult.LatestProcessedCommitHash;
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(updateResult.ErrorMessage))
                    {
                        Logger.Error("Error during updating repo content items. Message: " + updateResult.ErrorMessage);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public async Task <UpdateContentItemsResult> UpdateContentItems(ContentItem markdownRepoContentItem)
        {
            var updateContentItemsResult = new UpdateContentItemsResult();

            var markdownRepoPart = markdownRepoContentItem.As <MarkdownRepoPart>();

            if (markdownRepoPart == null)
            {
                updateContentItemsResult.ErrorMessage = "Can't update the content items because the repo content item doesn't contain MarkdownRepoPart.";
                return(updateContentItemsResult);
            }

            var repoOwner = "";

            if (!TryGetRepoOwner(markdownRepoPart.RepoUrl, out repoOwner))
            {
                updateContentItemsResult.ErrorMessage = @"Can't update the content items because can't get the repo owner from the given GitHub url. Set a vaild GitHub repo url. E.g. https://github.com/username/reponame";
                return(updateContentItemsResult);
            }

            var repoName = "";

            if (!TryGetRepoName(markdownRepoPart.RepoUrl, out repoName))
            {
                updateContentItemsResult.ErrorMessage = @"Can't update the content items because can't get the repo name from the given GitHub url. Set a vaild GitHub repo url. E.g. https://github.com/username/reponame";
                return(updateContentItemsResult);
            }

            try
            {
                var gitHubClient = GetGitHubClient(markdownRepoPart);

                var headCommitInBranch = await GetHeadCommit(repoOwner, repoName, markdownRepoPart.BranchName, gitHubClient);

                if (headCommitInBranch == null)
                {
                    return(updateContentItemsResult);
                }

                // The latest commit is the latest processed commit.
                if (headCommitInBranch.Sha == markdownRepoPart.LatestProcessedCommitHash)
                {
                    updateContentItemsResult.Success = true;
                    return(updateContentItemsResult);
                }

                var commits = await gitHubClient
                              .Repository
                              .Commit
                              .GetAll(repoOwner, repoName, new CommitRequest { Sha = markdownRepoPart.BranchName });

                var newCommits = new List <GitHubCommit>();
                foreach (var commit in commits)
                {
                    if (commit.Sha == markdownRepoPart.LatestProcessedCommitHash)
                    {
                        break;
                    }

                    // So the latest will be the last.
                    newCommits.Insert(0, commit);
                }

                var changedFiles = new List <GitHubCommitFile>();
                foreach (var commit in newCommits)
                {
                    var detailedCommit = await gitHubClient.Repository.Commit.Get(repoOwner, repoName, commit.Sha);

                    foreach (var file in detailedCommit.Files)
                    {
                        if (!MarkdownRepoPartHelpers.FolderOrFileIsInRepoFolder(markdownRepoPart.FolderName, file.Filename))
                        {
                            continue;
                        }

                        // If the file was modifyed in a previous commit, then this logic will take care of the status changes.
                        var sameFileThatChangedPreviously = changedFiles
                                                            .FirstOrDefault(changedFile => changedFile.Sha == file.Sha);
                        if (sameFileThatChangedPreviously != null)
                        {
                            if (file.Status == GitHubCommitStatus.Removed)
                            {
                                if (sameFileThatChangedPreviously.Status == GitHubCommitStatus.Added)
                                {
                                    changedFiles.Remove(sameFileThatChangedPreviously);
                                }
                                else if (sameFileThatChangedPreviously.Status == GitHubCommitStatus.Modified)
                                {
                                    changedFiles.Remove(sameFileThatChangedPreviously);
                                    changedFiles.Add(file);
                                }
                            }
                        }
                        else
                        {
                            changedFiles.Add(file);
                        }
                    }
                }

                foreach (var changedFile in changedFiles)
                {
                    var decodedString = await GetFileContentByFileSha(changedFile.Sha, repoOwner, repoName, gitHubClient);

                    switch (changedFile.Status)
                    {
                    case GitHubCommitStatus.Added:
                        _markdownContentItemManager.Create(
                            markdownRepoPart,
                            decodedString,
                            changedFile.Filename);
                        break;

                    case GitHubCommitStatus.Removed:
                        _markdownContentItemManager.Delete(markdownRepoPart, changedFile.Filename);
                        break;

                    case GitHubCommitStatus.Modified:
                        _markdownContentItemManager.Modify(markdownRepoPart, decodedString, changedFile.Filename);
                        break;
                    }
                }

                updateContentItemsResult.LatestProcessedCommitHash = headCommitInBranch.Sha;
                updateContentItemsResult.Success = true;
                return(updateContentItemsResult);
            }
            catch (AuthorizationException ex)
            {
                Logger.Error(ex, "Can't update the content items because the given GitHub credentials are invalid.");
                throw;
            }
            catch (NotFoundException ex)
            {
                Logger.Error(
                    ex,
                    string.Format(
                        "Can't update the content items because can't get the repository with the given data. Repo owner: {0}, repo name: {1}, branch name: {2}.",
                        repoOwner,
                        repoName,
                        markdownRepoPart.BranchName));
                throw;
            }
        }