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

added support for DateTime serialized as FileTime #1731

Closed
wants to merge 10 commits into from
9 changes: 9 additions & 0 deletions Octokit.Reactive/Clients/IObservableAccessTokensClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Octokit.Reactive
{
public interface IObservableAccessTokensClient
{
IObservable<AccessToken> Create(int installationId);
}
}
7 changes: 7 additions & 0 deletions Octokit.Reactive/Clients/IObservableInstallationsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Octokit.Reactive
{
public interface IObservableInstallationsClient
{
IObservableAccessTokensClient AccessTokens { get; }
}
}
24 changes: 24 additions & 0 deletions Octokit.Reactive/Clients/ObservableAccessTokensClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Reactive.Threading.Tasks;

namespace Octokit.Reactive
{
class ObservableAccessTokensClient : IObservableAccessTokensClient
{
readonly IAccessTokensClient _client;
readonly IConnection _connection;

public ObservableAccessTokensClient(IGitHubClient client)
{
Ensure.ArgumentNotNull(client, "client");

_client = client.Installations.AccessTokens;
_connection = client.Connection;
}

public IObservable<AccessToken> Create(int installationId)
{
return _client.Create(installationId).ToObservable();
}
}
}
19 changes: 19 additions & 0 deletions Octokit.Reactive/Clients/ObservableInstallationsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Octokit.Reactive
{
class ObservableInstallationsClient : IObservableInstallationsClient
{
readonly IInstallationsClient _client;
readonly IConnection _connection;

public ObservableInstallationsClient(IGitHubClient client)
{
Ensure.ArgumentNotNull(client, "client");

_client = client.Installations;
_connection = client.Connection;
AccessTokens = new ObservableAccessTokensClient(client);
}

public IObservableAccessTokensClient AccessTokens { get; }
}
}
1 change: 1 addition & 0 deletions Octokit.Reactive/IObservableGitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public interface IObservableGitHubClient : IApiInfoProvider

IObservableAuthorizationsClient Authorization { get; }
IObservableActivitiesClient Activity { get; }
IObservableInstallationsClient Installations { get; }
IObservableIssuesClient Issue { get; }
IObservableMiscellaneousClient Miscellaneous { get; }
IObservableOauthClient Oauth { get; }
Expand Down
2 changes: 2 additions & 0 deletions Octokit.Reactive/ObservableGitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public ObservableGitHubClient(IGitHubClient gitHubClient)
_gitHubClient = gitHubClient;
Authorization = new ObservableAuthorizationsClient(gitHubClient);
Activity = new ObservableActivitiesClient(gitHubClient);
Installations = new ObservableInstallationsClient(gitHubClient);
Issue = new ObservableIssuesClient(gitHubClient);
Miscellaneous = new ObservableMiscellaneousClient(gitHubClient);
Oauth = new ObservableOauthClient(gitHubClient);
Expand Down Expand Up @@ -69,6 +70,7 @@ public void SetRequestTimeout(TimeSpan timeout)

public IObservableAuthorizationsClient Authorization { get; private set; }
public IObservableActivitiesClient Activity { get; private set; }
public IObservableInstallationsClient Installations { get; private set; }
public IObservableIssuesClient Issue { get; private set; }
public IObservableMiscellaneousClient Miscellaneous { get; private set; }
public IObservableOauthClient Oauth { get; private set; }
Expand Down
50 changes: 50 additions & 0 deletions Octokit.Tests/Clients/EventsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ public async Task EnsuresNonNullArguments()
{"PullRequestEvent", typeof(PullRequestEventPayload)},
{"PullRequestReviewCommentEvent", typeof(PullRequestCommentPayload)},
{"PushEvent", typeof(PushEventPayload)},
{"StatusEvent", typeof(StatusEventPayload)},
{"WatchEvent", typeof(StarredEventPayload)},
{"unknown", typeof(ActivityPayload)}
};
Expand Down Expand Up @@ -810,6 +811,55 @@ public async Task DeserializesPushEventCorrectly()
Assert.Equal("message", payload.Commits.FirstOrDefault().Message);
}

[Fact]
public async Task DeserializesStatusEventCorrectly()
{
var jsonObj = new JsonObject
{
{ "type", "StatusEvent" },
{
"payload", new
{
id = 214015194,
sha = "9049f1265b7d61be4a8904a9a27120d2064dab3b",
name = "baxterthehacker/public-repo",
target_url = "https://www.some_target_url.com",
context = "default",
description = "some human readable text",
state = "success",
branches = new []
{
new
{
name = "master",
commit = new
{
sha = "9049f1265b7d61be4a8904a9a27120d2064dab3b",
url = "https://api.github.com/repos/baxterthehacker/public-repo/commits/9049f1265b7d61be4a8904a9a27120d2064dab3b"
}
}
},
created_at = "2015-05-05T23:40:39Z"
}
}
};

var client = GetTestingEventsClient(jsonObj);
var activities = await client.GetAll();
Assert.Equal(1, activities.Count);

var payload = activities.FirstOrDefault().Payload as StatusEventPayload;
Assert.Equal(214015194, payload.Id);
Assert.Equal("9049f1265b7d61be4a8904a9a27120d2064dab3b", payload.Sha);
Assert.Equal("baxterthehacker/public-repo", payload.Name);
Assert.Equal("https://www.some_target_url.com", payload.TargetUrl);
Assert.Equal("default", payload.Context);
Assert.Equal("some human readable text", payload.Description);
Assert.Equal(CommitState.Success, payload.State.Value);
Assert.Equal(1, payload.Branches.Count);
Assert.Equal(new DateTimeOffset(2015, 05, 05, 23, 40, 39, TimeSpan.Zero), payload.CreatedAt);
}

[Fact]
public async Task DeserializesStarredEventCorrectly()
{
Expand Down
6 changes: 5 additions & 1 deletion Octokit/Authentication/AuthenticationType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public enum AuthenticationType
/// <summary>
/// Delegated access to a third party
/// </summary>
Oauth
Oauth,
/// <summary>
/// Credential for GitHub App using signed JWT
/// </summary>
Bearer
}
}
3 changes: 2 additions & 1 deletion Octokit/Authentication/Authenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class Authenticator
{
{ AuthenticationType.Anonymous, new AnonymousAuthenticator() },
{ AuthenticationType.Basic, new BasicAuthenticator() },
{ AuthenticationType.Oauth, new TokenAuthenticator() }
{ AuthenticationType.Oauth, new TokenAuthenticator() },
{ AuthenticationType.Bearer, new BearerTokenAuthenticator() }
};

public Authenticator(ICredentialStore credentialStore)
Expand Down
23 changes: 23 additions & 0 deletions Octokit/Authentication/BearerTokenAuthenticator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Globalization;

namespace Octokit.Internal
{
class BearerTokenAuthenticator: IAuthenticationHandler
{
public void Authenticate(IRequest request, Credentials credentials)
{
Ensure.ArgumentNotNull(request, "request");
Ensure.ArgumentNotNull(credentials, "credentials");
Ensure.ArgumentNotNull(credentials.Password, "credentials.Password");

if (credentials.Login != null)
{
throw new InvalidOperationException("The Login is not null for a token authentication request. You " +
"probably did something wrong.");
}

request.Headers["Authorization"] = string.Format(CultureInfo.InvariantCulture, "Bearer {0}", credentials.Password);
}
}
}
18 changes: 18 additions & 0 deletions Octokit/Clients/AccessTokensClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Threading.Tasks;

namespace Octokit
{
class AccessTokensClient : ApiClient, IAccessTokensClient
{
private const string AcceptHeader = "application/vnd.github.machine-man-preview+json";

public AccessTokensClient(IApiConnection apiConnection) : base(apiConnection)
{
}

public Task<AccessToken> Create(int installationId)
{
return ApiConnection.Post<AccessToken>(ApiUrls.AccessTokens(installationId), null, AcceptHeader);
}
}
}
16 changes: 16 additions & 0 deletions Octokit/Clients/ApplicationClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Threading.Tasks;

namespace Octokit
{
public class ApplicationClient : ApiClient, IApplicationClient
{
public ApplicationClient(IApiConnection apiConnection) : base(apiConnection)
{
}

public Task<Application> Create()
{
return ApiConnection.Get<Application>(ApiUrls.App(), null, AcceptHeaders.MachineManPreview);
}
}
}
9 changes: 9 additions & 0 deletions Octokit/Clients/IAccessTokensClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace Octokit
{
public interface IAccessTokensClient
{
Task<AccessToken> Create(int installationId);
}
}
9 changes: 9 additions & 0 deletions Octokit/Clients/IApplicationClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace Octokit
{
public interface IApplicationClient
{
Task<Application> Create();
}
}
7 changes: 7 additions & 0 deletions Octokit/Clients/IInstallationsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Octokit
{
public interface IInstallationsClient
{
IAccessTokensClient AccessTokens { get; }
}
}
12 changes: 12 additions & 0 deletions Octokit/Clients/InstallationsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Octokit
{
class InstallationsClient : ApiClient, IInstallationsClient
{
public InstallationsClient(IApiConnection apiConnection) : base(apiConnection)
{
AccessTokens = new AccessTokensClient(apiConnection);
}

public IAccessTokensClient AccessTokens { get; private set; }
}
}
13 changes: 13 additions & 0 deletions Octokit/GitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public GitHubClient(IConnection connection)
Enterprise = new EnterpriseClient(apiConnection);
Gist = new GistsClient(apiConnection);
Git = new GitDatabaseClient(apiConnection);
Application = new ApplicationClient(apiConnection);
Installations = new InstallationsClient(apiConnection);
Issue = new IssuesClient(apiConnection);
Migration = new MigrationClient(apiConnection);
Miscellaneous = new MiscellaneousClient(connection);
Expand Down Expand Up @@ -171,6 +173,9 @@ public Uri BaseAddress
/// </remarks>
public IActivitiesClient Activity { get; private set; }

public IInstallationsClient Installations { get; private set; }


/// <summary>
/// Access GitHub's Issue API.
/// </summary>
Expand Down Expand Up @@ -251,6 +256,14 @@ public Uri BaseAddress
/// </remarks>
public IGitDatabaseClient Git { get; private set; }

/// <summary>
/// Access GitHub's Apps API.
/// </summary>
/// <remarks>
/// Refer to the API documentation for more information: https://developer.github.com/v3/git/
/// </remarks>
public IApplicationClient Application { get; private set; }

/// <summary>
/// Access GitHub's Search API.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Octokit/Helpers/AcceptHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ public static class AcceptHeaders
public const string OrganizationMembershipPreview = "application/vnd.github.korra-preview+json";

public const string NestedTeamsPreview = "application/vnd.github.hellcat-preview+json";

public const string MachineManPreview = "application/vnd.github.machine-man-preview+json";
}
}
31 changes: 31 additions & 0 deletions Octokit/Helpers/ApiUrls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,37 @@ public static Uri NotificationSubscription(int id)
return "notifications/threads/{0}/subscription".FormatUri(id);
}

public static Uri AccessTokens(int installationId)
{
return "installations/{0}/access_tokens".FormatUri(installationId);
}

/// <summary>
/// Returns the <see cref="Uri"/> that creates a github app.
/// </summary>
public static Uri App()
{
return "app".FormatUri();
}

/// <summary>
/// Returns the <see cref="Uri"/> that returns all the installations of the authenticated application.
/// </summary>
/// <returns></returns>
public static Uri Installations()
{
return "app/installations".FormatUri();
}

/// <summary>
/// Returns the <see cref="Uri"/> that returns a single installation of the authenticated application.
/// </summary>
/// <returns></returns>
public static Uri Installation(int installationId)
{
return "app/installations/{0}".FormatUri(installationId);
}

/// <summary>
/// Returns the <see cref="Uri"/> that returns all of the issues across all the authenticated user’s visible
/// repositories including owned repositories, member repositories, and organization repositories:
Expand Down
16 changes: 10 additions & 6 deletions Octokit/Http/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@ private Credentials()
AuthenticationType = AuthenticationType.Anonymous;
}

public Credentials(string token)
public Credentials(string token) : this(token, AuthenticationType.Oauth)
{
Ensure.ArgumentNotNullOrEmptyString(token, "token");

Login = null;
Password = token;
AuthenticationType = AuthenticationType.Oauth;
}

public Credentials(string login, string password)
Expand All @@ -32,6 +27,15 @@ public Credentials(string login, string password)
AuthenticationType = AuthenticationType.Basic;
}

public Credentials(string token, AuthenticationType authenticationType)
{
Ensure.ArgumentNotNullOrEmptyString(token, "token");

Login = null;
Password = token;
AuthenticationType = authenticationType;
}

public string Login
{
get;
Expand Down
2 changes: 2 additions & 0 deletions Octokit/Http/SimpleJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ private static Type GetPayloadType(string activityType)
return typeof(PullRequestCommentPayload);
case "PushEvent":
return typeof(PushEventPayload);
case "StatusEvent":
return typeof(StatusEventPayload);
case "WatchEvent":
return typeof(StarredEventPayload);
}
Expand Down
Loading