Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub api refactoring #33

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Annoy-o-Bot.AcceptanceTests/AcceptanceTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Security.Cryptography;
using System.Text;
using Annoy_o_Bot.CosmosDB;
using Annoy_o_Bot.GitHub;
using Annoy_o_Bot.GitHub.Api;
using Annoy_o_Bot.GitHub.Callbacks;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.Configuration;
Expand All @@ -12,7 +13,7 @@
namespace Annoy_o_Bot.AcceptanceTests;

[Collection("CosmosDB")]
public class AcceptanceTest(CosmosFixture cosmosFixture) : IAsyncLifetime

Check warning on line 16 in Annoy-o-Bot.AcceptanceTests/AcceptanceTest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'configurationBuilder' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 16 in Annoy-o-Bot.AcceptanceTests/AcceptanceTest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'container' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
{
protected const string SignatureKey = "mysecretkey";

Expand Down Expand Up @@ -67,11 +68,11 @@
await timeoutHandler.Run(null!, reminders, container);
}

protected static HttpRequest CreateCallbackHttpRequest(CallbackModel callback)
protected static HttpRequest CreateCallbackHttpRequest(GitPushCallbackModel gitPushCallback)
{
var httpContext = new DefaultHttpContext();
var request = httpContext.Request;
var messageContent = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(callback, Formatting.None));
var messageContent = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(gitPushCallback, Formatting.None));
request.Body = new MemoryStream(messageContent);
request.Headers.Add("X-GitHub-Event", "push");
request.Headers.Add("X-Hub-Signature-256",
Expand Down
8 changes: 5 additions & 3 deletions Annoy-o-Bot.AcceptanceTests/Fakes/CallbackModelHelper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
namespace Annoy_o_Bot.AcceptanceTests.Fakes;
using Annoy_o_Bot.GitHub.Callbacks;

namespace Annoy_o_Bot.AcceptanceTests.Fakes;

class CallbackModelHelper
{
public static CallbackModel.CommitModel CreateCommitModel(string? added = null, string? modified = null, string? removed = null)
public static GitPushCallbackModel.CommitModel CreateCommitModel(string? added = null, string? modified = null, string? removed = null)
{
var commit = new CallbackModel.CommitModel
var commit = new GitPushCallbackModel.CommitModel
{
Id = Guid.NewGuid().ToString(),
Added = added != null ? new[] { added } : Array.Empty<string>(),
Expand Down
6 changes: 1 addition & 5 deletions Annoy-o-Bot.AcceptanceTests/Fakes/FakeGitHubApi.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using Annoy_o_Bot.GitHub;
using Annoy_o_Bot.GitHub.Api;

namespace Annoy_o_Bot.AcceptanceTests.Fakes;

class FakeGitHubApi : IGitHubApi
{
private Dictionary<(long, long), IGitHubRepository> registeredRepos = new();

public FakeGitHubApi()
{
}

public FakeGitHubRepository CreateNewRepository()
{
var repository = new FakeGitHubRepository(Random.Shared.NextInt64(), Random.Shared.NextInt64());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Annoy_o_Bot.GitHub;
using Annoy_o_Bot.GitHub.Api;

namespace Annoy_o_Bot.AcceptanceTests.Fakes;

Expand Down
2 changes: 1 addition & 1 deletion Annoy-o-Bot.AcceptanceTests/Fakes/FakeGitHubRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Annoy_o_Bot.GitHub;
using Annoy_o_Bot.GitHub.Api;
using Octokit;

namespace Annoy_o_Bot.AcceptanceTests.Fakes;
Expand Down
14 changes: 7 additions & 7 deletions Annoy-o-Bot.AcceptanceTests/Fakes/RepoHelperExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Newtonsoft.Json;
using Octokit;
using Annoy_o_Bot.GitHub.Callbacks;
using Newtonsoft.Json;

namespace Annoy_o_Bot.AcceptanceTests.Fakes;

static class RepoHelperExtensions
{
private const string DefaultBranch = "main";

public static CallbackModel CommitNewReminder(this FakeGitHubRepository repo, ReminderDefinition reminderDefinition,
public static GitPushCallbackModel CommitNewReminder(this FakeGitHubRepository repo, ReminderDefinition reminderDefinition,
string? branch = null)
{
var filename = Guid.NewGuid().ToString("N");
Expand All @@ -18,13 +18,13 @@ public static CallbackModel CommitNewReminder(this FakeGitHubRepository repo, Re
return repo.Commit(commit, branch);
}

public static CallbackModel Commit(this FakeGitHubRepository repo, CallbackModel.CommitModel commit,
public static GitPushCallbackModel Commit(this FakeGitHubRepository repo, GitPushCallbackModel.CommitModel commit,
string? branch = null)
{
return new CallbackModel
return new GitPushCallbackModel
{
Installation = new CallbackModel.InstallationModel() { Id = repo.InstallationId },
Repository = new CallbackModel.RepositoryModel() { Id = repo.RepositoryId, DefaultBranch = DefaultBranch },
Installation = new GitPushCallbackModel.InstallationModel() { Id = repo.InstallationId },
Repository = new GitPushCallbackModel.RepositoryModel() { Id = repo.RepositoryId, DefaultBranch = DefaultBranch },
Ref = $"refs/heads/{branch ?? DefaultBranch}",
HeadCommit = commit,
Commits = new []{ commit }
Expand Down
1 change: 0 additions & 1 deletion Annoy-o-Bot.AcceptanceTests/When_callback_type_not_push.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Annoy_o_Bot.AcceptanceTests.Fakes;
using Annoy_o_Bot.GitHub;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text.Json;
using Annoy_o_Bot.AcceptanceTests.Fakes;
using Annoy_o_Bot.CosmosDB;
using Annoy_o_Bot.AcceptanceTests.Fakes;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Annoy_o_Bot.AcceptanceTests.Fakes;
using Annoy_o_Bot.CosmosDB;
using Annoy_o_Bot.CosmosDB.Tests;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ public class When_request_signature_does_not_match : AcceptanceTest
[Fact]
public async Task Should_return_error()
{
const string InvalidSignature = "sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
var gitHubApi = new FakeGitHubApi();
var repository = gitHubApi.CreateNewRepository();

var request = CreateCallbackHttpRequest(repository.Commit(CallbackModelHelper.CreateCommitModel()));
var requestHash = request.Headers["X-Hub-Signature-256"];
request.Headers["X-Hub-Signature-256"] = "1234567890"; // this is not the incorrect but the expected signature in this test
request.Headers["X-Hub-Signature-256"] = InvalidSignature; // this is not the incorrect but the expected signature in this test

var handler = new CallbackHandler(gitHubApi, configurationBuilder.Build(), NullLogger<CallbackHandler>.Instance);

var exception = await Assert.ThrowsAnyAsync<Exception>(() => handler.Run(request, container));

Assert.Contains(
$"Computed request payload signature ('{requestHash}') does not match provided signature ('{request.Headers["X-Hub-Signature-256"]}')",
$"Computed request payload signature ('{requestHash}') does not match provided signature ('{InvalidSignature}')",
exception.Message);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Annoy_o_Bot.AcceptanceTests.Fakes;
using Annoy_o_Bot.CosmosDB;
using Annoy_o_Bot.GitHub.Callbacks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
Expand All @@ -24,7 +25,7 @@ public async Task Should_create_reminder_in_database()
};
var createCallback = appInstallation.CommitNewReminder(updatedReminder);

var updateCommit = new CallbackModel.CommitModel
var updateCommit = new GitPushCallbackModel.CommitModel
{
Id = Guid.NewGuid().ToString(),
Modified =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json;
using Annoy_o_Bot.AcceptanceTests.Fakes;
using Annoy_o_Bot.CosmosDB;
using Annoy_o_Bot.GitHub.Callbacks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
Expand Down Expand Up @@ -37,7 +37,7 @@ public async Task Should_update_reminder_in_database()
};
appInstallation.AddFileContent(createCallback.Commits[0].Added[0], JsonSerializer.Serialize(updatedReminder));

var updateCommit = new CallbackModel.CommitModel
var updateCommit = new GitPushCallbackModel.CommitModel
{
Id = Guid.NewGuid().ToString(),
Modified = new[]
Expand Down
2 changes: 1 addition & 1 deletion Annoy-o-Bot.Tests/Annoy-o-Bot.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Annoy_o_Bot.Tests</RootNamespace>
<RootNamespace>Annoy_o_Bot</RootNamespace>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
42 changes: 21 additions & 21 deletions Annoy-o-Bot.Tests/FileChangesTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
namespace Annoy_o_Bot.Tests
{
using Annoy_o_Bot.GitHub;
using Xunit;
using Annoy_o_Bot.GitHub.Callbacks;
using Xunit;

namespace Annoy_o_Bot
{
public class FileChangesTests
{
[Fact]
public void Should_return_empty_changes_when_no_commits()
{
var result = CommitParser.GetChanges(new CallbackModel.CommitModel[0]);
var result = CommitParser.GetChanges(new GitPushCallbackModel.CommitModel[0]);

Assert.Empty(result.New);
Assert.Empty(result.Deleted);
Expand All @@ -20,19 +20,19 @@ public void Should_aggregate_changes_from_all_commits()
{
var commitModel = new[]
{
new CallbackModel.CommitModel
new GitPushCallbackModel.CommitModel
{
Added = new []{ "a1" },
Modified = new []{ "m1" },
Removed = new [] { "r1"}
},
new CallbackModel.CommitModel
new GitPushCallbackModel.CommitModel
{
Added = new []{ "a2" },
Modified = new []{ "m2" },
Removed = new [] { "r2"}
},
new CallbackModel.CommitModel
new GitPushCallbackModel.CommitModel
{
Added = new []{ "a3" },
Modified = new []{ "m3" },
Expand All @@ -58,11 +58,11 @@ public void Should_handle_multiple_updates()
{
var commitModel = new[]
{
new CallbackModel.CommitModel
new GitPushCallbackModel.CommitModel
{
Modified = new []{ "file1" },
},
new CallbackModel.CommitModel
new GitPushCallbackModel.CommitModel
{
Modified = new [] { "file1"}
}
Expand All @@ -79,11 +79,11 @@ public void Should_handle_update_delete()
{
var commitModel = new[]
{
new CallbackModel.CommitModel
new GitPushCallbackModel.CommitModel
{
Modified = new []{ "file1" },
},
new CallbackModel.CommitModel
new GitPushCallbackModel.CommitModel
{
Removed = new [] { "file1"}
}
Expand All @@ -101,15 +101,15 @@ public void Should_handle_new_delete()
{
var commitModel = new[]
{
new CallbackModel.CommitModel
new GitPushCallbackModel.CommitModel
{
Added = new []{ "file1" },
},
new CallbackModel.CommitModel
new GitPushCallbackModel.CommitModel
{
Modified = new []{ "file1" },
},
new CallbackModel.CommitModel
new GitPushCallbackModel.CommitModel
{
Removed = new [] { "file1"}
}
Expand All @@ -128,15 +128,15 @@ public void Should_handle_new_update()
{
var commitModel = new[]
{
new CallbackModel.CommitModel
new GitPushCallbackModel.CommitModel
{
Added = new []{ "file1" },
},
new CallbackModel.CommitModel
new GitPushCallbackModel.CommitModel
{
Modified = new []{ "file1" },
},
new CallbackModel.CommitModel
new GitPushCallbackModel.CommitModel
{
Modified = new [] { "file1"}
}
Expand All @@ -155,15 +155,15 @@ public void Should_handle_delete_new()
{
var commitModel = new[]
{
new CallbackModel.CommitModel
new GitPushCallbackModel.CommitModel
{
Modified = new []{ "file1" },
},
new CallbackModel.CommitModel
new GitPushCallbackModel.CommitModel
{
Removed = new []{ "file1" },
},
new CallbackModel.CommitModel
new GitPushCallbackModel.CommitModel
{
Added = new [] { "file1"}
}
Expand Down
69 changes: 69 additions & 0 deletions Annoy-o-Bot.Tests/GitHub/Callbacks/GitPushCallbackModelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System.IO;
using Newtonsoft.Json;
using Xunit;

namespace Annoy_o_Bot.GitHub.Callbacks;

public class GitPushCallbackModelTests
{
[Fact]
public void NewFileAdded()
{
var result = JsonConvert.DeserializeObject<GitPushCallbackModel>(File.ReadAllText("requests/fileAdded.json"));

Assert.Equal("refs/heads/master", result.Ref);

Assert.Equal(7498230L, result.Installation.Id);

Assert.Equal(179716425L, result.Repository.Id);
Assert.Equal("master", result.Repository.DefaultBranch);
Assert.Equal("TitleTest", result.Repository.Name);

var commit = Assert.Single(result.Commits);
Assert.Equal("ba7c6f17f5beaafc603eca52b864356848865fec", commit.Id);
var addedFile = Assert.Single(commit.Added);
Assert.Equal(".reminder/testReminder.json", addedFile);
Assert.Equal("Create trigger4.json", commit.Message);

Assert.Equal("timbussmann", result.Pusher.Name);
}

[Fact]
public void MultiCommit()
{
var result = JsonConvert.DeserializeObject<GitPushCallbackModel>(File.ReadAllText("requests/multiCommitFileHistory.json"));

Assert.Equal(4, result.Commits.Length);

Assert.Equal(".reminder/newFile.json", Assert.Single(result.Commits[0].Added));
Assert.Empty(result.Commits[0].Modified);
Assert.Empty(result.Commits[0].Removed);
Assert.Equal("Create newFile.json", result.Commits[0].Message);

Assert.Equal(".reminder/newFile.json", Assert.Single(result.Commits[1].Modified));
Assert.Empty(result.Commits[1].Added);
Assert.Empty(result.Commits[1].Removed);
Assert.Equal("Update newFile.json", result.Commits[1].Message);

Assert.Equal(".reminder/newFile.json", Assert.Single(result.Commits[2].Removed));
Assert.Empty(result.Commits[2].Added);
Assert.Empty(result.Commits[2].Modified);
Assert.Equal("Delete newFile.json", result.Commits[2].Message);

Assert.Empty(result.Commits[3].Added);
Assert.Empty(result.Commits[3].Modified);
Assert.Empty(result.Commits[3].Removed);
Assert.Equal("Merge pull request #15 from timbussmann/file-ops-history\n\nCreate newFile.json", result.Commits[3].Message);

Assert.Equal("cb1ec97f51657c2718ab4e0b1d0bf2656aeb3127", result.HeadCommit.Id);
}

[Fact]
public void BranchDeleted()
{
var result = JsonConvert.DeserializeObject<GitPushCallbackModel>(File.ReadAllText("requests/branchDeleted.json"));

Assert.Null(result.HeadCommit);
Assert.Empty(result.Commits);
}
}
Loading
Loading