Skip to content

Commit caa676b

Browse files
committed
1 parent b58a7a5 commit caa676b

File tree

2 files changed

+115
-31
lines changed

2 files changed

+115
-31
lines changed

modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
using Microsoft.Extensions.Hosting;
88
using Microsoft.Extensions.Logging;
99
using Microsoft.Extensions.Options;
10-
using Nest;
11-
using Newtonsoft.Json;
1210
using Volo.Abp;
1311
using Volo.Abp.Caching;
1412
using Volo.Docs.Caching;

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

+115-29
Original file line numberDiff line numberDiff line change
@@ -48,76 +48,162 @@ public virtual async Task<Document> GetDocumentAsync(Project project, string doc
4848
fileName = documentName.Substring(documentName.LastIndexOf('/') + 1);
4949
}
5050

51-
var fileCommits = await GetFileCommitsAsync(project, version, project.GetGitHubInnerUrl(languageCode, documentName));
52-
53-
var documentCreationTime = fileCommits.LastOrDefault()?.Commit.Author.Date.DateTime ?? DateTime.MinValue;
54-
55-
var lastSignificantUpdateTime = !isNavigationDocument && !isParameterDocument && version == project.LatestVersionBranchName ?
56-
await GetLastSignificantUpdateTime(
57-
fileCommits,
58-
project,
59-
project.GetGitHubInnerUrl(languageCode, documentName),
60-
lastKnownSignificantUpdateTime,
61-
documentCreationTime
62-
) ?? lastKnownSignificantUpdateTime
63-
: null;
51+
var content = await DownloadWebContentAsStringAsync(rawDocumentUrl, token, userAgent);
52+
var commits = await GetGitHubCommitsOrNull(project, documentName, languageCode, version);
53+
54+
var documentCreationTime = GetFirstCommitDate(commits);
55+
var lastUpdateTime = GetLastCommitDate(commits);
56+
var lastSignificantUpdateTime = await GetLastKnownSignificantUpdateTime(project, documentName, languageCode, version, lastKnownSignificantUpdateTime, isNavigationDocument, isParameterDocument, commits, documentCreationTime);
6457

65-
var document = new Document(GuidGenerator.Create(),
58+
var document = new Document
59+
(
60+
GuidGenerator.Create(),
6661
project.Id,
6762
documentName,
6863
version,
6964
languageCode,
7065
fileName,
71-
await DownloadWebContentAsStringAsync(rawDocumentUrl, token, userAgent),
66+
content,
7267
project.Format,
7368
editLink,
7469
rootUrl,
7570
rawRootUrl,
7671
localDirectory,
7772
documentCreationTime,
78-
fileCommits.FirstOrDefault()?.Commit.Author.Date.DateTime ?? DateTime.MinValue,
73+
lastUpdateTime,
7974
DateTime.Now,
80-
lastSignificantUpdateTime);
75+
lastSignificantUpdateTime
76+
);
77+
78+
if (isNavigationDocument || isParameterDocument)
79+
{
80+
return document;
81+
}
82+
83+
var authors = GetAuthors(commits);
84+
foreach (var author in authors)
85+
{
86+
document.AddContributor(author.Login, author.HtmlUrl, author.AvatarUrl);
87+
}
8188

82-
var authors = fileCommits
89+
return document;
90+
}
91+
92+
private async Task<DateTime?> GetLastKnownSignificantUpdateTime(
93+
Project project,
94+
string documentName,
95+
string languageCode,
96+
string version,
97+
DateTime? lastKnownSignificantUpdateTime,
98+
bool isNavigationDocument,
99+
bool isParameterDocument,
100+
IReadOnlyList<GitHubCommit> commits,
101+
DateTime documentCreationTime)
102+
{
103+
return !isNavigationDocument && !isParameterDocument && version == project.LatestVersionBranchName
104+
? await GetLastSignificantUpdateTime(
105+
commits,
106+
project,
107+
project.GetGitHubInnerUrl(languageCode, documentName),
108+
lastKnownSignificantUpdateTime,
109+
documentCreationTime
110+
) ?? lastKnownSignificantUpdateTime
111+
: null;
112+
}
113+
114+
private static List<Author> GetAuthors(IReadOnlyList<GitHubCommit> commits)
115+
{
116+
if (commits == null || !commits.Any())
117+
{
118+
return new List<Author>();
119+
}
120+
121+
return commits
83122
.Where(x => x.Author != null)
84123
.Select(x => x.Author)
85124
.GroupBy(x => x.Id)
86125
.OrderByDescending(x => x.Count())
87-
.Select(x => x.FirstOrDefault()).ToList();
126+
.Select(x => x.FirstOrDefault())
127+
.ToList();
128+
}
129+
130+
private static DateTime GetLastCommitDate(IReadOnlyList<GitHubCommit> commits)
131+
{
132+
return GetCommitDate(commits, false);
133+
}
88134

89-
if (!isNavigationDocument && !isParameterDocument)
135+
private static DateTime GetFirstCommitDate(IReadOnlyList<GitHubCommit> commits)
136+
{
137+
return GetCommitDate(commits, true);
138+
}
139+
140+
private static DateTime GetCommitDate(IReadOnlyList<GitHubCommit> commits, bool isFirstCommit)
141+
{
142+
if (commits == null)
90143
{
91-
foreach (var author in authors)
92-
{
93-
document.AddContributor(author.Login, author.HtmlUrl, author.AvatarUrl);
94-
}
144+
return DateTime.MinValue;
95145
}
96146

97-
return document;
147+
var gitHubCommit = isFirstCommit ?
148+
commits.LastOrDefault() : //first commit
149+
commits.FirstOrDefault(); //last commit
150+
151+
if (gitHubCommit == null)
152+
{
153+
return DateTime.MinValue;
154+
}
155+
156+
if (gitHubCommit.Commit == null)
157+
{
158+
return DateTime.MinValue;
159+
}
160+
161+
if (gitHubCommit.Commit.Author == null)
162+
{
163+
return DateTime.MinValue;
164+
}
165+
166+
return gitHubCommit.Commit.Author.Date.DateTime;
167+
}
168+
169+
private async Task<IReadOnlyList<GitHubCommit>> GetGitHubCommitsOrNull(Project project, string documentName, string languageCode, string version)
170+
{
171+
/*
172+
* Getting file commits usually throws "Resource temporarily unavailable" or "Network is unreachable"
173+
* This is a trival information and running this inside try-catch is safer.
174+
*/
175+
176+
try
177+
{
178+
return await GetFileCommitsAsync(project, version, project.GetGitHubInnerUrl(languageCode, documentName));
179+
}
180+
catch (Exception e)
181+
{
182+
Logger.LogError(e.ToString());
183+
return null;
184+
}
98185
}
99186

100187
private async Task<DateTime?> GetLastSignificantUpdateTime(
101-
IReadOnlyList<GitHubCommit> fileCommits,
188+
IReadOnlyList<GitHubCommit> commits,
102189
Project project,
103190
string fileName,
104191
DateTime? lastKnownSignificantUpdateTime,
105192
DateTime documentCreationTime)
106193
{
107-
if (!fileCommits.Any())
194+
if (commits == null || !commits.Any())
108195
{
109196
return null;
110197
}
111198

112-
var fileCommitsAfterCreation = fileCommits.Take(fileCommits.Count - 1);
199+
var fileCommitsAfterCreation = commits.Take(commits.Count - 1);
113200

114201
var commitsToEvaluate = (lastKnownSignificantUpdateTime != null
115202
? fileCommitsAfterCreation.Where(c => c.Commit.Author.Date.DateTime > lastKnownSignificantUpdateTime)
116203
: fileCommitsAfterCreation).Where(c => c.Commit.Author.Date.DateTime > DateTime.Now.AddDays(-14));
117204

118205
foreach (var gitHubCommit in commitsToEvaluate)
119206
{
120-
121207
var fullCommit = await _githubRepositoryManager.GetSingleCommitsAsync(
122208
GetOwnerNameFromUrl(project.GetGitHubUrl()),
123209
GetRepositoryNameFromUrl(project.GetGitHubUrl()),

0 commit comments

Comments
 (0)