mirror of https://github.com/abpframework/abp.git
7 changed files with 214 additions and 60 deletions
@ -0,0 +1,63 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Net.Http; |
|||
using System.Net.Http.Headers; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Octokit; |
|||
using Octokit.Internal; |
|||
using ProductHeaderValue = Octokit.ProductHeaderValue; |
|||
|
|||
namespace Volo.Docs.GitHub.Documents |
|||
{ |
|||
public class GithubRepositoryManager : IGithubRepositoryManager |
|||
{ |
|||
public const string HttpClientName = "GithubRepositoryManagerHttpClientName"; |
|||
|
|||
private readonly IHttpClientFactory _clientFactory; |
|||
|
|||
public GithubRepositoryManager(IHttpClientFactory clientFactory) |
|||
{ |
|||
_clientFactory = clientFactory; |
|||
} |
|||
|
|||
public async Task<string> GetFileRawStringContentAsync(string rawUrl, string token, string userAgent) |
|||
{ |
|||
var httpClient = _clientFactory.CreateClient(HttpClientName); |
|||
if (!token.IsNullOrWhiteSpace()) |
|||
{ |
|||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", token); |
|||
} |
|||
|
|||
httpClient.DefaultRequestHeaders.Add("User-Agent", userAgent ?? ""); |
|||
|
|||
return await httpClient.GetStringAsync(new Uri(rawUrl)); |
|||
} |
|||
|
|||
public async Task<byte[]> GetFileRawByteArrayContentAsync(string rawUrl, string token, string userAgent) |
|||
{ |
|||
var httpClient = _clientFactory.CreateClient(HttpClientName); |
|||
if (!token.IsNullOrWhiteSpace()) |
|||
{ |
|||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", token); |
|||
} |
|||
|
|||
httpClient.DefaultRequestHeaders.Add("User-Agent", userAgent ?? ""); |
|||
|
|||
return await httpClient.GetByteArrayAsync(new Uri(rawUrl)); |
|||
} |
|||
|
|||
public async Task<IReadOnlyList<Release>> GetReleasesAsync(string name, string repositoryName, string token) |
|||
{ |
|||
var client = token.IsNullOrWhiteSpace() |
|||
? new GitHubClient(new ProductHeaderValue(name)) |
|||
: new GitHubClient(new ProductHeaderValue(name), new InMemoryCredentialStore(new Credentials(token))); |
|||
|
|||
return (await client |
|||
.Repository |
|||
.Release |
|||
.GetAll(name, repositoryName)).ToList(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Octokit; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Docs.GitHub.Documents |
|||
{ |
|||
public interface IGithubRepositoryManager : ITransientDependency |
|||
{ |
|||
Task<string> GetFileRawStringContentAsync(string rawUrl, string token, string userAgent); |
|||
|
|||
Task<byte[]> GetFileRawByteArrayContentAsync(string rawUrl, string token, string userAgent); |
|||
|
|||
Task<IReadOnlyList<Release>> GetReleasesAsync(string name, string repositoryName, string token); |
|||
|
|||
} |
|||
} |
|||
@ -1,7 +1,43 @@ |
|||
namespace Volo.Docs |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using NSubstitute; |
|||
using Octokit; |
|||
using Volo.Docs.GitHub.Documents; |
|||
|
|||
namespace Volo.Docs |
|||
{ |
|||
public abstract class DocsDomainTestBase : DocsTestBase<DocsDomainTestModule> |
|||
{ |
|||
|
|||
protected override void AfterAddApplication(IServiceCollection services) |
|||
{ |
|||
var repositoryManager = Substitute.For<IGithubRepositoryManager>(); |
|||
repositoryManager.GetFileRawStringContentAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>()) |
|||
.Returns("stringContent"); |
|||
repositoryManager.GetFileRawByteArrayContentAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>()) |
|||
.Returns(new byte[] { 0x01, 0x02, 0x03 }); |
|||
repositoryManager.GetReleasesAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>()) |
|||
.Returns(new List<Release> |
|||
{ |
|||
new Release("https://api.github.com/repos/abpframework/abp/releases/16293679", |
|||
"https://github.com/abpframework/abp/releases/tag/0.15.0", |
|||
"https://api.github.com/repos/abpframework/abp/releases/16293679/assets", |
|||
"https://uploads.github.com/repos/abpframework/abp/releases/16293679/assets{?name,label}", |
|||
16293679, |
|||
"0.15.0", |
|||
"master", |
|||
"0.15.0", |
|||
"0.15.0 already release", |
|||
false, |
|||
false, |
|||
DateTimeOffset.Parse("2019-03-22T18:43:58Z"), |
|||
DateTimeOffset.Parse("2019-03-22T19:44:25Z"), |
|||
null, |
|||
"https://api.github.com/repos/abpframework/abp/tarball/0.15.0", |
|||
"https://api.github.com/repos/abpframework/abp/zipball/0.15.0", |
|||
null) |
|||
}); |
|||
services.AddSingleton(repositoryManager); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,73 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using NSubstitute; |
|||
using Octokit; |
|||
using Shouldly; |
|||
using Volo.Docs.Documents; |
|||
using Volo.Docs.GitHub.Documents; |
|||
using Volo.Docs.Projects; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Docs |
|||
{ |
|||
public class GithubDocumentStore_Tests : DocsDomainTestBase |
|||
{ |
|||
private readonly IDocumentStoreFactory _documentStoreFactory; |
|||
private readonly IProjectRepository _projectRepository; |
|||
private readonly DocsTestData _testData; |
|||
|
|||
public GithubDocumentStore_Tests() |
|||
{ |
|||
_documentStoreFactory = GetRequiredService<IDocumentStoreFactory>(); |
|||
_projectRepository = GetRequiredService<IProjectRepository>(); |
|||
_testData = GetRequiredService<DocsTestData>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task GetDocumentAsync() |
|||
{ |
|||
var store = _documentStoreFactory.Create(GithubDocumentStore.Type); |
|||
var project = await _projectRepository.FindAsync(_testData.PorjectId); |
|||
project.ShouldNotBeNull(); |
|||
var document = await store.GetDocumentAsync(project, "index2", "0.123.0"); |
|||
document.ShouldNotBeNull(); |
|||
|
|||
document.Title.ShouldBe("index2"); |
|||
document.FileName.ShouldBe("index2"); |
|||
document.Version.ShouldBe("0.123.0"); |
|||
document.Content.ShouldBe("stringContent"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task GetVersionsAsync() |
|||
{ |
|||
var store = _documentStoreFactory.Create(GithubDocumentStore.Type); |
|||
var project = await _projectRepository.FindAsync(_testData.PorjectId); |
|||
project.ShouldNotBeNull(); |
|||
|
|||
var document = await store.GetVersionsAsync(project); |
|||
document.ShouldNotBeNull(); |
|||
|
|||
document.Count.ShouldBe(1); |
|||
document.ShouldContain(x => x.Name == "0.15.0" && x.DisplayName == "0.15.0"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task GetResource() |
|||
{ |
|||
var store = _documentStoreFactory.Create(GithubDocumentStore.Type); |
|||
var project = await _projectRepository.FindAsync(_testData.PorjectId); |
|||
project.ShouldNotBeNull(); |
|||
|
|||
var documentResource = await store.GetResource(project, "index.md", "0.123.0"); |
|||
documentResource.ShouldNotBeNull(); |
|||
|
|||
documentResource.Content.ShouldBe(new byte[] |
|||
{ |
|||
0x01, 0x02, 0x03 |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue