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); } } } }
public async Task <InitContentItemsResult> InitContentItems(ContentItem markdownRepoContentItem) { var initContentItemsResult = new InitContentItemsResult(); var markdownRepoPart = markdownRepoContentItem.As <MarkdownRepoPart>(); if (markdownRepoPart == null) { initContentItemsResult.ErrorMessage = @"Can't initialize the content items because the repo content item doesn't contain MarkdownRepoPart."; return(initContentItemsResult); } var repoOwner = ""; if (!TryGetRepoOwner(markdownRepoPart.RepoUrl, out repoOwner)) { initContentItemsResult.ErrorMessage = @"Can't initialize 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(initContentItemsResult); } var repoName = ""; if (!TryGetRepoName(markdownRepoPart.RepoUrl, out repoName)) { initContentItemsResult.ErrorMessage = @"Can't initialize 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(initContentItemsResult); } try { var gitHubClient = GetGitHubClient(markdownRepoPart); // This is needed after the init, so we'll know what was the latest commit hash. var headCommitInBranch = await GetHeadCommit(repoOwner, repoName, markdownRepoPart.BranchName, gitHubClient); if (headCommitInBranch == null) { initContentItemsResult.ErrorMessage = @"Can't initialize the content items because there's no commit in the given branch: " + markdownRepoPart.BranchName; return(initContentItemsResult); } var treeResponse = await gitHubClient .Git .Tree .GetRecursive(repoOwner, repoName, markdownRepoPart.BranchName); var filesInFolder = markdownRepoPart.FolderName == "\\" ? treeResponse.Tree : treeResponse .Tree .Where(t => t.Path.StartsWith(markdownRepoPart.FolderName) && t.Path != markdownRepoPart.FolderName && t.Path.EndsWith(".md")); foreach (var treeItem in filesInFolder) { var decodedString = await GetFileContentByFileSha(treeItem.Sha, repoOwner, repoName, gitHubClient); _markdownContentItemManager.Create( markdownRepoPart, decodedString, treeItem.Path); } initContentItemsResult.LatestProcessedCommitHash = headCommitInBranch.Sha; initContentItemsResult.Success = true; return(initContentItemsResult); } catch (AuthorizationException ex) { Logger.Error(ex, "Can't initialize the content items because the given GitHub credentials are invalid."); throw; } catch (NotFoundException ex) { Logger.Error( ex, string.Format( "Can't initialize 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; } }