From 1f1e2c193dcccc80d0e3b80efe3eadd94d4ca6a8 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Wed, 11 Mar 2020 16:05:25 +0300 Subject: [PATCH] Docs Update label improvements CONT. --- .../GitHub/Documents/GithubDocumentSource.cs | 39 +++++---- .../GitHub/Documents/GithubPatchAnalyzer.cs | 80 ++++++++----------- 2 files changed, 59 insertions(+), 60 deletions(-) diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentSource.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentSource.cs index d1239b0603..ad96d48838 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentSource.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentSource.cs @@ -28,7 +28,7 @@ namespace Volo.Docs.GitHub.Documents _githubRepositoryManager = githubRepositoryManager; _githubPatchAnalyzer = githubPatchAnalyzer; } - + public virtual async Task GetDocumentAsync(Project project, string documentName, string languageCode, string version, DateTime? lastKnownSignificantUpdateTime = null) { var token = project.GetGitHubAccessTokenOrNull(); @@ -52,13 +52,15 @@ namespace Volo.Docs.GitHub.Documents var documentCreationTime = fileCommits.LastOrDefault()?.Commit.Author.Date.DateTime ?? DateTime.MinValue; - - var lastSignificantUpdateTime = !isNavigationDocument && !isParameterDocument ? - await GetLastSignificantUpdateTime(fileCommits, project.GetGitHubInnerUrl(languageCode, documentName), - lastKnownSignificantUpdateTime, documentCreationTime, - GetOwnerNameFromUrl(project.GetGitHubUrl()), - GetRepositoryNameFromUrl(project.GetGitHubUrl()), - project.GetGitHubAccessTokenOrNull()) ?? lastKnownSignificantUpdateTime : null; + var lastSignificantUpdateTime = !isNavigationDocument && !isParameterDocument && version == project.LatestVersionBranchName ? + await GetLastSignificantUpdateTime( + fileCommits, + project, + project.GetGitHubInnerUrl(languageCode, documentName), + lastKnownSignificantUpdateTime, + documentCreationTime + ) ?? lastKnownSignificantUpdateTime + : null; var document = new Document(GuidGenerator.Create(), project.Id, @@ -77,7 +79,7 @@ namespace Volo.Docs.GitHub.Documents DateTime.Now, lastSignificantUpdateTime); - var authors = fileCommits + var authors = fileCommits .Where(x => x.Author != null) .Select(x => x.Author) .GroupBy(x => x.Id) @@ -95,20 +97,27 @@ namespace Volo.Docs.GitHub.Documents return document; } - private async Task GetLastSignificantUpdateTime(IReadOnlyList fileCommits, string fileName, - DateTime? lastKnownSignificantUpdateTime, DateTime documentCreationTime, string repoOwnerName, string repoName, string token) + private async Task GetLastSignificantUpdateTime( + IReadOnlyList fileCommits, + Project project, + string fileName, + DateTime? lastKnownSignificantUpdateTime, + DateTime documentCreationTime) { var commitsToEvaluate = (lastKnownSignificantUpdateTime != null ? fileCommits.Where(c => c.Commit.Author.Date.DateTime > lastKnownSignificantUpdateTime) - :fileCommits).Where(c=>c.Commit.Author.Date.DateTime > DateTime.Now.AddDays(-14) && c.Commit.Author.Date.DateTime > documentCreationTime); + : fileCommits).Where(c => c.Commit.Author.Date.DateTime > DateTime.Now.AddDays(-14) && c.Commit.Author.Date.DateTime > documentCreationTime); foreach (var gitHubCommit in commitsToEvaluate) { - var fullCommit = - await _githubRepositoryManager.GetSingleCommitsAsync(repoOwnerName, repoName, gitHubCommit.Sha, token); + var fullCommit = await _githubRepositoryManager.GetSingleCommitsAsync( + GetOwnerNameFromUrl(project.GetGitHubUrl()), + GetRepositoryNameFromUrl(project.GetGitHubUrl()), + gitHubCommit.Sha, + project.GetGitHubAccessTokenOrNull()); - if (_githubPatchAnalyzer.HasPatchSignificantChanges(fullCommit.Files.First(f=>f.Filename == fileName).Patch)) + if (_githubPatchAnalyzer.HasPatchSignificantChanges(fullCommit.Files.First(f => f.Filename == fileName).Patch)) { return gitHubCommit.Commit.Author.Date.DateTime; } diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubPatchAnalyzer.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubPatchAnalyzer.cs index c3bf8cc7c2..091c2fb42f 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubPatchAnalyzer.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubPatchAnalyzer.cs @@ -8,37 +8,15 @@ namespace Volo.Docs.GitHub.Documents { public class GithubPatchAnalyzer : DomainService, IGithubPatchAnalyzer { - private const string OldChangeStart = "\\n-"; - private const string NewChangeStart = "\\n+"; - public bool HasPatchSignificantChanges(string patch) { var changes = GetChanges(patch); - var mergedChanges = MergeChanges(changes); - - if (IsChangeSignificant(mergedChanges)) - { - return true; - } - - return false; + return IsChangesSignificant(changes); } - private CommitChanges MergeChanges(List changes) - { - var mergedChanges = new CommitChanges(); - - foreach (var commitChanges in changes) - { - mergedChanges.NewLines.AddRange(commitChanges.NewLines); - mergedChanges.OldLines.AddRange(commitChanges.OldLines); - } - - return mergedChanges; - } - private bool IsChangeSignificant(CommitChanges change) + private bool IsChangesSignificant(CommitChanges change) { if (CompareLineCount(change)) { @@ -58,26 +36,9 @@ namespace Volo.Docs.GitHub.Documents return false; } - private bool CompareWords(CommitChanges change) - { - var wordsInNewLines = GetDistinctWordsFromLineList(change.NewLines); - var wordsInOldLines = GetDistinctWordsFromLineList(change.OldLines); - - var differentWordsInNewLines = wordsInNewLines.Except(wordsInOldLines).Count(); - var differentWordsInOldLines = wordsInOldLines.Except(wordsInNewLines).Count(); - - return differentWordsInNewLines + differentWordsInOldLines > 10; - } - - private List GetDistinctWordsFromLineList(List lines) - { - return string.Join(" ", lines).Split(" ").Where(s => !string.IsNullOrWhiteSpace(s)) - .Select(TrimAndRemovePunctuation).Distinct().ToList(); - } - private static bool CompareLineCount(CommitChanges change) { - return Math.Abs(change.NewLines.Count - change.OldLines.Count) > 3; + return Math.Abs(change.NewLines.Count - change.OldLines.Count) >= 3; } private static bool CompareWordCount(CommitChanges change) @@ -87,10 +48,21 @@ namespace Volo.Docs.GitHub.Documents var wordCountInOldLines = string.Join(" ", change.OldLines).Split(" ").Count(s => !string.IsNullOrWhiteSpace(s)); - return Math.Abs(wordCountInNewLines - wordCountInOldLines) > 15; + return Math.Abs(wordCountInNewLines - wordCountInOldLines) >= 15; + } + + private bool CompareWords(CommitChanges change) + { + var wordsInNewLines = GetDistinctWordsFromLineList(change.NewLines); + var wordsInOldLines = GetDistinctWordsFromLineList(change.OldLines); + + var differentWordsInNewLines = wordsInNewLines.Except(wordsInOldLines).Count(); + var differentWordsInOldLines = wordsInOldLines.Except(wordsInNewLines).Count(); + + return differentWordsInNewLines + differentWordsInOldLines >= 10; } - private List GetChanges(string patch) + private CommitChanges GetChanges(string patch) { var changes = new List(); var pathSplited = patch.Split("@@"); @@ -107,7 +79,25 @@ namespace Volo.Docs.GitHub.Documents changes.Add(commitChange); } - return changes; + return MergeChanges(changes); + } + private CommitChanges MergeChanges(List changes) + { + var mergedChanges = new CommitChanges(); + + foreach (var commitChanges in changes) + { + mergedChanges.NewLines.AddRange(commitChanges.NewLines); + mergedChanges.OldLines.AddRange(commitChanges.OldLines); + } + + return mergedChanges; + } + + private List GetDistinctWordsFromLineList(List lines) + { + return string.Join(" ", lines).Split(" ").Where(s => !string.IsNullOrWhiteSpace(s)) + .Select(TrimAndRemovePunctuation).Distinct().ToList(); } private string TrimAndRemovePunctuation(string str)