Docs Update label improvements CONT.

pull/3079/head
Yunus Emre Kalkan 6 years ago
parent b29ef789ba
commit 1f1e2c193d

@ -28,7 +28,7 @@ namespace Volo.Docs.GitHub.Documents
_githubRepositoryManager = githubRepositoryManager;
_githubPatchAnalyzer = githubPatchAnalyzer;
}
public virtual async Task<Document> 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<DateTime?> GetLastSignificantUpdateTime(IReadOnlyList<GitHubCommit> fileCommits, string fileName,
DateTime? lastKnownSignificantUpdateTime, DateTime documentCreationTime, string repoOwnerName, string repoName, string token)
private async Task<DateTime?> GetLastSignificantUpdateTime(
IReadOnlyList<GitHubCommit> 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;
}

@ -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<CommitChanges> 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<string> GetDistinctWordsFromLineList(List<string> 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<CommitChanges> GetChanges(string patch)
private CommitChanges GetChanges(string patch)
{
var changes = new List<CommitChanges>();
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<CommitChanges> changes)
{
var mergedChanges = new CommitChanges();
foreach (var commitChanges in changes)
{
mergedChanges.NewLines.AddRange(commitChanges.NewLines);
mergedChanges.OldLines.AddRange(commitChanges.OldLines);
}
return mergedChanges;
}
private List<string> GetDistinctWordsFromLineList(List<string> lines)
{
return string.Join(" ", lines).Split(" ").Where(s => !string.IsNullOrWhiteSpace(s))
.Select(TrimAndRemovePunctuation).Distinct().ToList();
}
private string TrimAndRemovePunctuation(string str)

Loading…
Cancel
Save