Example #1
0
        private async Task <IReadOnlyList <GitHubCommit> > GetGitHubCommitsOrNull(Project project, string documentName, string languageCode, string version)
        {
            /*
             * Getting file commits usually throws "Resource temporarily unavailable" or "Network is unreachable"
             * This is a trival information and running this inside try-catch is safer.
             */

            try
            {
                return(await GetFileCommitsAsync(project, version, project.GetGitHubInnerUrl(languageCode, documentName)));
            }
            catch (Exception e)
            {
                Logger.LogError(e.ToString());
                return(null);
            }
        }
Example #2
0
 private async Task <DateTime?> GetLastKnownSignificantUpdateTime(
     Project project,
     string documentName,
     string languageCode,
     string version,
     DateTime?lastKnownSignificantUpdateTime,
     bool isNavigationDocument,
     bool isParameterDocument,
     IReadOnlyList <GitHubCommit> commits,
     DateTime documentCreationTime)
 {
     return(!isNavigationDocument && !isParameterDocument && version == project.LatestVersionBranchName
         ? await GetLastSignificantUpdateTime(
                commits,
                project,
                project.GetGitHubInnerUrl(languageCode, documentName),
                lastKnownSignificantUpdateTime,
                documentCreationTime
                ) ?? lastKnownSignificantUpdateTime
         : null);
 }
Example #3
0
        public virtual async Task <Document> GetDocumentAsync(Project project, string documentName, string languageCode, string version, DateTime?lastKnownSignificantUpdateTime = null)
        {
            var token                = project.GetGitHubAccessTokenOrNull();
            var rootUrl              = project.GetGitHubUrl(version);
            var userAgent            = project.GetGithubUserAgentOrNull();
            var rawRootUrl           = CalculateRawRootUrlWithLanguageCode(rootUrl, languageCode);
            var rawDocumentUrl       = rawRootUrl + documentName;
            var isNavigationDocument = documentName == project.NavigationDocumentName;
            var isParameterDocument  = documentName == project.ParametersDocumentName;
            var editLink             = rootUrl.ReplaceFirst("/tree/", "/blob/") + languageCode + "/" + documentName;
            var localDirectory       = "";
            var fileName             = documentName;

            if (documentName.Contains("/"))
            {
                localDirectory = documentName.Substring(0, documentName.LastIndexOf('/'));
                fileName       = documentName.Substring(documentName.LastIndexOf('/') + 1);
            }

            var fileCommits = await GetFileCommitsAsync(project, version, project.GetGitHubInnerUrl(languageCode, documentName));

            var documentCreationTime = fileCommits.LastOrDefault()?.Commit.Author.Date.DateTime ?? DateTime.MinValue;

            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,
                                        documentName,
                                        version,
                                        languageCode,
                                        fileName,
                                        await DownloadWebContentAsStringAsync(rawDocumentUrl, token, userAgent),
                                        project.Format,
                                        editLink,
                                        rootUrl,
                                        rawRootUrl,
                                        localDirectory,
                                        documentCreationTime,
                                        fileCommits.FirstOrDefault()?.Commit.Author.Date.DateTime ?? DateTime.MinValue,
                                        DateTime.Now,
                                        lastSignificantUpdateTime);

            var authors = fileCommits
                          .Where(x => x.Author != null)
                          .Select(x => x.Author)
                          .GroupBy(x => x.Id)
                          .OrderByDescending(x => x.Count())
                          .Select(x => x.FirstOrDefault()).ToList();

            if (!isNavigationDocument && !isParameterDocument)
            {
                foreach (var author in authors)
                {
                    document.AddContributor(author.Login, author.HtmlUrl, author.AvatarUrl);
                }
            }

            return(document);
        }