|
|
|
@ -10,6 +10,7 @@ using Volo.Abp.Domain.Services; |
|
|
|
using Volo.Docs.Documents; |
|
|
|
using Volo.Docs.GitHub.Projects; |
|
|
|
using Volo.Docs.Projects; |
|
|
|
using Newtonsoft.Json.Linq; |
|
|
|
using ProductHeaderValue = Octokit.ProductHeaderValue; |
|
|
|
using Project = Volo.Docs.Projects.Project; |
|
|
|
|
|
|
|
@ -23,9 +24,11 @@ namespace Volo.Docs.GitHub.Documents |
|
|
|
|
|
|
|
public virtual async Task<Document> GetDocument(Project project, string documentName, string version) |
|
|
|
{ |
|
|
|
var token = project.GetGitHubAccessTokenOrNull(); |
|
|
|
var rootUrl = project.GetGitHubUrl(version); |
|
|
|
var rawRootUrl = CalculateRawRootUrl(rootUrl); |
|
|
|
var rawDocumentUrl = rawRootUrl + documentName; |
|
|
|
var commitHistoryUrl = project.GetGitHubUrlForCommitHistory() + documentName; |
|
|
|
var editLink = rootUrl.ReplaceFirst("/tree/", "/blob/") + documentName; |
|
|
|
var localDirectory = ""; |
|
|
|
var fileName = documentName; |
|
|
|
@ -45,18 +48,12 @@ namespace Volo.Docs.GitHub.Documents |
|
|
|
Format = project.Format, |
|
|
|
LocalDirectory = localDirectory, |
|
|
|
FileName = fileName, |
|
|
|
Contributors = await GetContributors(commitHistoryUrl, token), |
|
|
|
Version = version, |
|
|
|
Content = await DownloadWebContentAsStringAsync(rawDocumentUrl, project.GetGitHubAccessTokenOrNull()) |
|
|
|
Content = await DownloadWebContentAsStringAsync(rawDocumentUrl, token) |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
private static string CalculateRawRootUrl(string rootUrl) |
|
|
|
{ |
|
|
|
return rootUrl |
|
|
|
.Replace("github.com", "raw.githubusercontent.com") |
|
|
|
.ReplaceFirst("/tree/", "/"); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<List<VersionInfo>> GetVersions(Project project) |
|
|
|
{ |
|
|
|
try |
|
|
|
@ -146,6 +143,7 @@ namespace Volo.Docs.GitHub.Documents |
|
|
|
{ |
|
|
|
webClient.Headers.Add("Authorization", "token " + token); |
|
|
|
} |
|
|
|
webClient.Headers.Add("User-Agent", "request"); |
|
|
|
|
|
|
|
return await webClient.DownloadStringTaskAsync(new Uri(rawUrl)); |
|
|
|
} |
|
|
|
@ -179,5 +177,48 @@ namespace Volo.Docs.GitHub.Documents |
|
|
|
throw new ResourceNotFoundException(rawUrl); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private async Task<List<DocumentContributor>> GetContributors(string url, string token) |
|
|
|
{ |
|
|
|
var contributors = new List<DocumentContributor>(); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
var commitsJsonAsString = await DownloadWebContentAsStringAsync(url, token); |
|
|
|
|
|
|
|
var commits = JArray.Parse(commitsJsonAsString); |
|
|
|
|
|
|
|
foreach (var commit in commits) |
|
|
|
{ |
|
|
|
var author = commit["author"]; |
|
|
|
|
|
|
|
if (contributors.All(c => c.Username != (string) author["login"])) |
|
|
|
{ |
|
|
|
contributors.Add(new DocumentContributor |
|
|
|
{ |
|
|
|
Username = (string)author["login"], |
|
|
|
UserProfileUrl = (string)author["html_url"], |
|
|
|
AvatarUrl = (string)author["avatar_url"] |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
contributors.Reverse(); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
Logger.LogWarning(ex.Message); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return contributors; |
|
|
|
} |
|
|
|
|
|
|
|
private static string CalculateRawRootUrl(string rootUrl) |
|
|
|
{ |
|
|
|
return rootUrl |
|
|
|
.Replace("github.com", "raw.githubusercontent.com") |
|
|
|
.ReplaceFirst("/tree/", "/"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |