Browse Source

Docs Update label improvements CONT.

pull/3079/head
Yunus Emre Kalkan 7 years ago
parent
commit
b29ef789ba
  1. 36
      modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentSource.cs
  2. 87
      modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubPatchAnalyzer.cs
  3. 10
      modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubRepositoryManager.cs
  4. 2
      modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/IGithubRepositoryManager.cs

36
modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentSource.cs

@ -52,22 +52,30 @@ namespace Volo.Docs.GitHub.Documents
var documentCreationTime = fileCommits.LastOrDefault()?.Commit.Author.Date.DateTime ?? DateTime.MinValue;
var document= new Document(GuidGenerator.Create(),
project.Id,
documentName,
version,
var lastSignificantUpdateTime = !isNavigationDocument && !isParameterDocument ?
await GetLastSignificantUpdateTime(fileCommits, project.GetGitHubInnerUrl(languageCode, documentName),
lastKnownSignificantUpdateTime, documentCreationTime,
GetOwnerNameFromUrl(project.GetGitHubUrl()),
GetRepositoryNameFromUrl(project.GetGitHubUrl()),
project.GetGitHubAccessTokenOrNull()) ?? lastKnownSignificantUpdateTime : null;
var document = new Document(GuidGenerator.Create(),
project.Id,
documentName,
version,
languageCode,
fileName,
fileName,
await DownloadWebContentAsStringAsync(rawDocumentUrl, token, userAgent),
project.Format,
editLink,
project.Format,
editLink,
rootUrl,
rawRootUrl,
rawRootUrl,
localDirectory,
documentCreationTime,
fileCommits.FirstOrDefault()?.Commit.Author.Date.DateTime ?? DateTime.MinValue,
DateTime.Now,
GetLastSignificantUpdateTime(fileCommits, project.GetGitHubInnerUrl(languageCode, documentName), lastKnownSignificantUpdateTime, documentCreationTime) ?? lastKnownSignificantUpdateTime);
lastSignificantUpdateTime);
var authors = fileCommits
.Where(x => x.Author != null)
@ -87,8 +95,8 @@ namespace Volo.Docs.GitHub.Documents
return document;
}
private DateTime? GetLastSignificantUpdateTime(IReadOnlyList<GitHubCommit> fileCommits, string fileName,
DateTime? lastKnownSignificantUpdateTime, DateTime documentCreationTime)
private async Task<DateTime?> GetLastSignificantUpdateTime(IReadOnlyList<GitHubCommit> fileCommits, string fileName,
DateTime? lastKnownSignificantUpdateTime, DateTime documentCreationTime, string repoOwnerName, string repoName, string token)
{
var commitsToEvaluate = (lastKnownSignificantUpdateTime != null
? fileCommits.Where(c => c.Commit.Author.Date.DateTime > lastKnownSignificantUpdateTime)
@ -96,7 +104,11 @@ namespace Volo.Docs.GitHub.Documents
foreach (var gitHubCommit in commitsToEvaluate)
{
if (_githubPatchAnalyzer.HasPatchSignificantChanges(gitHubCommit.Files.First(f=>f.Filename == fileName).Patch))
var fullCommit =
await _githubRepositoryManager.GetSingleCommitsAsync(repoOwnerName, repoName, gitHubCommit.Sha, token);
if (_githubPatchAnalyzer.HasPatchSignificantChanges(fullCommit.Files.First(f=>f.Filename == fileName).Patch))
{
return gitHubCommit.Commit.Author.Date.DateTime;
}

87
modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubPatchAnalyzer.cs

@ -15,36 +15,94 @@ namespace Volo.Docs.GitHub.Documents
{
var changes = GetChanges(patch);
foreach (var change in changes)
{
var isSignificant = IsChangeSignificant(change);
var mergedChanges = MergeChanges(changes);
if (isSignificant)
{
return true;
}
if (IsChangeSignificant(mergedChanges))
{
return true;
}
return false;
}
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)
{
throw new NotImplementedException();
if (CompareLineCount(change))
{
return true;
}
if (CompareWordCount(change))
{
return true;
}
if (CompareWords(change))
{
return true;
}
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;
}
private static bool CompareWordCount(CommitChanges change)
{
var wordCountInNewLines =
string.Join(" ", change.NewLines).Split(" ").Count(s => !string.IsNullOrWhiteSpace(s));
var wordCountInOldLines =
string.Join(" ", change.OldLines).Split(" ").Count(s => !string.IsNullOrWhiteSpace(s));
return Math.Abs(wordCountInNewLines - wordCountInOldLines) > 15;
}
private List<CommitChanges> GetChanges(string patch)
{
var changes = new List<CommitChanges>();
var changeList = patch.Split("@@").Where(s => !string.IsNullOrWhiteSpace(s)).Where((c, i) => i % 2 == 0).ToList();
var pathSplited = patch.Split("@@");
var changeList = pathSplited.Where(s => !string.IsNullOrWhiteSpace(s)).Where((c, i) => i % 2 == 1).ToList();
foreach (var change in changeList)
{
var commitChange = new CommitChanges();
var lines = change.Split("\\n");
var lines = change.Split("\n");
commitChange.OldLines.AddRange(lines.Where(l => l.StartsWith("-")).Select(l => l.Substring(1)).Where(l => !string.IsNullOrWhiteSpace(l)));
commitChange.NewLines.AddRange(lines.Where(l => l.StartsWith("+")).Select(l => l.Substring(1)).Where(l=>!string.IsNullOrWhiteSpace(l)));
commitChange.NewLines.AddRange(lines.Where(l => l.StartsWith("+")).Select(l => l.Substring(1)).Where(l => !string.IsNullOrWhiteSpace(l)));
changes.Add(commitChange);
}
@ -52,6 +110,11 @@ namespace Volo.Docs.GitHub.Documents
return changes;
}
private string TrimAndRemovePunctuation(string str)
{
return new string(str.Trim().ToCharArray().Where(c => !char.IsPunctuation(c)).ToArray());
}
private class CommitChanges
{
public List<string> OldLines { get; set; }
@ -61,7 +124,7 @@ namespace Volo.Docs.GitHub.Documents
public CommitChanges()
{
OldLines = new List<string>();
NewLines= new List<string>();
NewLines = new List<string>();
}
}
}

10
modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubRepositoryManager.cs

@ -75,5 +75,15 @@ namespace Volo.Docs.GitHub.Documents
var request = new CommitRequest { Path = filename, Sha = version };
return await client.Repository.Commit.GetAll(repo.Id, request);
}
public async Task<GitHubCommit> GetSingleCommitsAsync(string name, string repositoryName, string sha, string token)
{
var client = token.IsNullOrWhiteSpace()
? new GitHubClient(new ProductHeaderValue(name))
: new GitHubClient(new ProductHeaderValue(name), new InMemoryCredentialStore(new Credentials(token)));
var repo = await client.Repository.Get(name, repositoryName);
return await client.Repository.Commit.Get(repo.Id, sha);
}
}
}

2
modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/IGithubRepositoryManager.cs

@ -16,5 +16,7 @@ namespace Volo.Docs.GitHub.Documents
Task<IReadOnlyList<Release>> GetReleasesAsync(string name, string repositoryName, string token);
Task<IReadOnlyList<GitHubCommit>> GetFileCommitsAsync(string name, string repositoryName, string version, string filename, string token);
Task<GitHubCommit> GetSingleCommitsAsync(string name, string repositoryName, string sha, string token);
}
}

Loading…
Cancel
Save