rm legacy GH integration

This commit is contained in:
Collin M. Barrett 2019-07-02 12:19:01 -05:00
parent dc6605374e
commit 5dfe568144
4 changed files with 0 additions and 132 deletions

View file

@ -31,7 +31,6 @@
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.3" />
<PackageReference Include="Microsoft.DotNet.Analyzers.Compatibility" Version="0.2.12-alpha" />
<PackageReference Include="morelinq" Version="3.1.1" />
<PackageReference Include="Octokit" Version="0.32.0" />
<PackageReference Include="SharpCompress" Version="0.23.0" />
</ItemGroup>

View file

@ -1,70 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FilterLists.Services.GitHub.Models;
using JetBrains.Annotations;
using Octokit;
namespace FilterLists.Services.GitHub
{
[UsedImplicitly]
public class GitHubService
{
private const string RawFileUrlRoot = "raw.githubusercontent.com";
private const string ProductHeader = "FilterLists";
private const string ReadOnlyPersonalAccessToken = "25ef3c8e20d2fcacf1583e7650b63cfa3e784fec";
private readonly GitHubClient client;
public GitHubService() =>
client = new GitHubClient(new ProductHeaderValue(ProductHeader))
{
Credentials = new Credentials(ReadOnlyPersonalAccessToken)
};
public async Task<CommitDates> GetCommitDatesAsync(string rawFileUrl)
{
if (!IsValidUrl(rawFileUrl))
return null;
var commits = await GetAllCommits(rawFileUrl);
return new CommitDates
{
First = commits.LastOrDefault()?.Commit.Author.Date.DateTime,
Last = commits.FirstOrDefault()?.Commit.Author.Date.DateTime
};
}
private static bool IsValidUrl(string fileRawUrl) => fileRawUrl.Contains(RawFileUrlRoot);
private async Task<IReadOnlyList<GitHubCommit>> GetAllCommits(string rawFileUrl)
{
var meta = ParseMeta(rawFileUrl);
return await client.Repository.Commit.GetAll(
meta.Owner,
meta.RepositoryName,
new CommitRequest {Path = meta.FilePath});
}
private static FileMeta ParseMeta(string rawFileUrl)
{
var urlPath = rawFileUrl.Split(new[] {RawFileUrlRoot}, StringSplitOptions.None)[1];
var urlPathSlugs = urlPath.Split('/');
var filePath = string.Empty;
for (var i = 4; i < urlPathSlugs.Length; i++)
filePath = filePath + '/' + urlPathSlugs[i];
return new FileMeta
{
Owner = urlPathSlugs[1],
RepositoryName = urlPathSlugs[2],
FilePath = filePath.Substring(1)
};
}
private class FileMeta
{
public string Owner { get; set; }
public string RepositoryName { get; set; }
public string FilePath { get; set; }
}
}
}

View file

@ -1,10 +0,0 @@
using System;
namespace FilterLists.Services.GitHub.Models
{
public class CommitDates
{
public DateTime? First { get; set; }
public DateTime? Last { get; set; }
}
}

View file

@ -1,51 +0,0 @@
using System;
using System.Threading.Tasks;
using FilterLists.Services.GitHub;
using FilterLists.Services.GitHub.Models;
using Xunit;
namespace FilterLists.Services.Tests
{
public class GitHubServiceTests
{
[Fact]
public async Task GetCommitDatesAsync_InvalidGitHubRawUrl_ReturnsNull()
{
const string url = "https://github.com/collinbarrett/FilterLists/blob/master/.gitattributes";
var actualDates = await new GitHubService().GetCommitDatesAsync(url);
Assert.Null(actualDates);
}
[Fact]
public async Task GetCommitDatesAsync_ValidGitHubRawUrl_ReturnsCorrectFirstAndLastCommitDates()
{
const string url = "https://raw.githubusercontent.com/collinbarrett/FilterLists/master/LICENSE";
var expectedDates = new CommitDates
{
First = DateTime.Parse("2016-12-10T20:28:16.0000000"),
Last = DateTime.Parse("2018-01-24T00:08:21.0000000")
};
var actualDates = await new GitHubService().GetCommitDatesAsync(url);
Assert.Equal(expectedDates.First, actualDates.First);
Assert.Equal(expectedDates.Last, actualDates.Last);
}
[Fact]
public async Task GetCommitDatesAsync_ValidGitHubRawUrlToNonexistentFile_ReturnsNull()
{
const string url = "https://github.com/collinbarrett/FilterLists/blob/master/doesnotexist";
var actualDates = await new GitHubService().GetCommitDatesAsync(url);
Assert.Null(actualDates);
}
[Fact]
public async Task GetCommitDatesAsync_ValidGitHubRawUrlWithOnlyOneCommit_ReturnsSameFirstAndLastCommitDates()
{
const string url = "https://raw.githubusercontent.com/collinbarrett/FilterLists/master/.gitattributes";
var expectedDate = DateTime.Parse("2017-04-08T00:05:26.0000000");
var actualDates = await new GitHubService().GetCommitDatesAsync(url);
Assert.Equal(expectedDate, actualDates.First);
Assert.Equal(expectedDate, actualDates.Last);
}
}
}