@@ -48,76 +48,162 @@ public virtual async Task<Document> GetDocumentAsync(Project project, string doc
48
48
fileName = documentName . Substring ( documentName . LastIndexOf ( '/' ) + 1 ) ;
49
49
}
50
50
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 ) ;
64
57
65
- var document = new Document ( GuidGenerator . Create ( ) ,
58
+ var document = new Document
59
+ (
60
+ GuidGenerator . Create ( ) ,
66
61
project . Id ,
67
62
documentName ,
68
63
version ,
69
64
languageCode ,
70
65
fileName ,
71
- await DownloadWebContentAsStringAsync ( rawDocumentUrl , token , userAgent ) ,
66
+ content ,
72
67
project . Format ,
73
68
editLink ,
74
69
rootUrl ,
75
70
rawRootUrl ,
76
71
localDirectory ,
77
72
documentCreationTime ,
78
- fileCommits . FirstOrDefault ( ) ? . Commit . Author . Date . DateTime ?? DateTime . MinValue ,
73
+ lastUpdateTime ,
79
74
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
+ }
81
88
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
83
122
. Where ( x => x . Author != null )
84
123
. Select ( x => x . Author )
85
124
. GroupBy ( x => x . Id )
86
125
. 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
+ }
88
134
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 )
90
143
{
91
- foreach ( var author in authors )
92
- {
93
- document . AddContributor ( author . Login , author . HtmlUrl , author . AvatarUrl ) ;
94
- }
144
+ return DateTime . MinValue ;
95
145
}
96
146
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
+ }
98
185
}
99
186
100
187
private async Task < DateTime ? > GetLastSignificantUpdateTime (
101
- IReadOnlyList < GitHubCommit > fileCommits ,
188
+ IReadOnlyList < GitHubCommit > commits ,
102
189
Project project ,
103
190
string fileName ,
104
191
DateTime ? lastKnownSignificantUpdateTime ,
105
192
DateTime documentCreationTime )
106
193
{
107
- if ( ! fileCommits . Any ( ) )
194
+ if ( commits == null || ! commits . Any ( ) )
108
195
{
109
196
return null ;
110
197
}
111
198
112
- var fileCommitsAfterCreation = fileCommits . Take ( fileCommits . Count - 1 ) ;
199
+ var fileCommitsAfterCreation = commits . Take ( commits . Count - 1 ) ;
113
200
114
201
var commitsToEvaluate = ( lastKnownSignificantUpdateTime != null
115
202
? fileCommitsAfterCreation . Where ( c => c . Commit . Author . Date . DateTime > lastKnownSignificantUpdateTime )
116
203
: fileCommitsAfterCreation ) . Where ( c => c . Commit . Author . Date . DateTime > DateTime . Now . AddDays ( - 14 ) ) ;
117
204
118
205
foreach ( var gitHubCommit in commitsToEvaluate )
119
206
{
120
-
121
207
var fullCommit = await _githubRepositoryManager . GetSingleCommitsAsync (
122
208
GetOwnerNameFromUrl ( project . GetGitHubUrl ( ) ) ,
123
209
GetRepositoryNameFromUrl ( project . GetGitHubUrl ( ) ) ,
0 commit comments