Permalink
Browse files

restructure servicehook hanlder

  • Loading branch information...
suwatch committed Nov 29, 2012
1 parent 513bed0 commit cb7ccf5d4529495696126ab67072a18dfc53f3bc
@@ -188,10 +188,10 @@ private static void RegisterServices(IKernel kernel)
kernel.Bind<IServiceHookHandler>().To<GitHubHandler>();
kernel.Bind<IServiceHookHandler>().To<BitbucketHandler>();
kernel.Bind<IServiceHookHandler>().To<DropboxHandler>();
+ kernel.Bind<IServiceHookHandler>().To<CodePlexHandler>();
kernel.Bind<IServiceHookHandler>().To<CodebaseHqHandler>();
kernel.Bind<IServiceHookHandler>().To<GitlabHqHandler>();
- kernel.Bind<IServiceHookHandler>().To<JsonServiceHookHandler>();
- kernel.Bind<IServiceHookHandler>().To<CodePlexHandler>();
+ kernel.Bind<IServiceHookHandler>().To<GitHubCompatHandler>();
// Editor
kernel.Bind<IProjectSystem>().ToMethod(context => GetEditorProjectSystem(environment, context))
@@ -12,6 +12,8 @@
using Kudu.Core.Infrastructure;
using Kudu.Core.SourceControl.Git;
using Kudu.Services.GitServer.ServiceHookHandlers;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
namespace Kudu.Services.GitServer
{
@@ -55,7 +57,8 @@ public override void ProcessRequest(HttpContext context)
RepositoryInfo repositoryInfo = null;
try
{
- repositoryInfo = GetRepositoryInfo(context.Request);
+ JObject payload = GetPayload(context.Request);
+ repositoryInfo = GetRepositoryInfo(context.Request, payload);
}
catch (FormatException ex)
{
@@ -151,11 +154,11 @@ private void PerformDeployment(RepositoryInfo repositoryInfo, string targetBranc
} while (hasPendingDeployment);
}
- private void TracePayload(string json)
+ private void TracePayload(JObject json)
{
var attribs = new Dictionary<string, string>
{
- { "json", json }
+ { "json", json.ToString() }
};
_tracer.Trace("payload", attribs);
@@ -171,18 +174,13 @@ private void TraceHandler(IServiceHookHandler handler)
_tracer.Trace("handler", attribs);
}
- private RepositoryInfo GetRepositoryInfo(HttpRequest request)
+ private RepositoryInfo GetRepositoryInfo(HttpRequest request, JObject payload)
{
RepositoryInfo info = null;
foreach (var handler in _serviceHookHandlers)
{
- if (handler.TryGetRepositoryInfo(request, out info))
+ if (handler.TryGetRepositoryInfo(request, payload, out info))
{
- Debug.Assert(info != null, "info must not be null");
- Debug.Assert(!String.IsNullOrEmpty(info.OldRef), "OldRef must not be null");
- Debug.Assert(!String.IsNullOrEmpty(info.NewRef), "NewRef must not be null");
- Debug.Assert(!String.IsNullOrEmpty(info.Deployer), "Deployer must not be null");
-
if (_tracer.TraceLevel >= TraceLevel.Verbose)
{
TraceHandler(handler);
@@ -195,5 +193,41 @@ private RepositoryInfo GetRepositoryInfo(HttpRequest request)
throw new FormatException(Resources.Error_UnsupportedFormat);
}
+
+ private JObject GetPayload(HttpRequest request)
+ {
+ JObject payload;
+
+ // we don't care about content type, just let it choked
+ if (request.Form.Count > 0)
+ {
+ string json = request.Form["payload"];
+ if (String.IsNullOrEmpty(json))
+ {
+ json = request.Form[0];
+ }
+
+ payload = JsonConvert.DeserializeObject<JObject>(json);
+ }
+ else
+ {
+ using (JsonTextReader reader = new JsonTextReader(new StreamReader(request.GetInputStream())))
+ {
+ payload = JObject.Load(reader);
+ }
+ }
+
+ if (payload == null)
+ {
+ throw new FormatException(Resources.Error_EmptyPayload);
+ }
+
+ if (_tracer.TraceLevel >= TraceLevel.Verbose)
+ {
+ TracePayload(payload);
+ }
+
+ return payload;
+ }
}
}
@@ -5,34 +5,38 @@
namespace Kudu.Services.GitServer.ServiceHookHandlers
{
- public class BitbucketHandler : JsonServiceHookHandler
+ public class BitbucketHandler : IServiceHookHandler
{
+ private readonly IGitServer _gitServer;
+
public BitbucketHandler(IGitServer gitServer)
- : base(gitServer)
{
+ _gitServer = gitServer;
}
- public override bool TryGetRepositoryInfo(HttpRequest request, out RepositoryInfo repositoryInfo)
+ public bool TryGetRepositoryInfo(HttpRequest request, JObject payload, out RepositoryInfo repositoryInfo)
{
repositoryInfo = null;
if (request.UserAgent != null &&
request.UserAgent.StartsWith("Bitbucket", StringComparison.OrdinalIgnoreCase))
{
- return base.TryGetRepositoryInfo(request, out repositoryInfo);
+ repositoryInfo = GetRepositoryInfo(request, payload);
}
- return false;
+
+ return repositoryInfo != null;
+ }
+
+ public void Fetch(RepositoryInfo repositoryInfo, string targetBranch)
+ {
+ // Fetch from url
+ _gitServer.FetchWithoutConflict(repositoryInfo.RepositoryUrl, "external", targetBranch);
}
- protected override RepositoryInfo GetRepositoryInfo(HttpRequest request, JObject payload)
+ private RepositoryInfo GetRepositoryInfo(HttpRequest request, JObject payload)
{
- // TODO, suwatch: this needs to throw, not returning null.
// bitbucket format
// { repository: { absolute_url: "/a/b", is_private: true }, canon_url: "https//..." }
var repository = payload.Value<JObject>("repository");
- if (repository == null)
- {
- return null;
- }
var info = new RepositoryInfo();
string server = payload.Value<string>("canon_url"); // e.g. https://bitbucket.org
@@ -52,6 +56,18 @@ protected override RepositoryInfo GetRepositoryInfo(HttpRequest request, JObject
info.Deployer = request.UserAgent;
+ // private repo, use SSH
+ if (info.IsPrivate)
+ {
+ Uri uri = new Uri(info.RepositoryUrl);
+ if (uri.Scheme.StartsWith("http", StringComparison.OrdinalIgnoreCase))
+ {
+ info.Host = "git@" + uri.Host;
+ info.RepositoryUrl = info.Host + ":" + uri.AbsolutePath.TrimStart('/');
+ info.UseSSH = true;
+ }
+ }
+
return info;
}
}
@@ -14,18 +14,8 @@ public CodePlexHandler(IGitServer gitServer)
_gitServer = gitServer;
}
- public bool TryGetRepositoryInfo(HttpRequest request, out RepositoryInfo repositoryInfo)
+ public bool TryGetRepositoryInfo(HttpRequest request, JObject payload, out RepositoryInfo repositoryInfo)
{
- repositoryInfo = null;
-
- string json = request.Form["payload"];
- if (String.IsNullOrEmpty(json))
- {
- return false;
- }
-
- JObject payload = JObject.Parse(json);
-
// Look for the generic format
// { url: "", branch: "", deployer: "", oldRef: "", newRef: "" }
repositoryInfo = new RepositoryInfo
@@ -36,10 +26,7 @@ public bool TryGetRepositoryInfo(HttpRequest request, out RepositoryInfo reposit
NewRef = payload.Value<string>("newRef")
};
- return !String.IsNullOrEmpty(repositoryInfo.RepositoryUrl) &&
- !String.IsNullOrEmpty(repositoryInfo.Deployer) &&
- !String.IsNullOrEmpty(repositoryInfo.OldRef) &&
- !String.IsNullOrEmpty(repositoryInfo.NewRef);
+ return repositoryInfo.IsValid();
}
public virtual void Fetch(RepositoryInfo repositoryInfo, string targetBranch)
@@ -5,22 +5,23 @@
namespace Kudu.Services.GitServer.ServiceHookHandlers
{
- public class CodebaseHqHandler : JsonServiceHookHandler
+ public class CodebaseHqHandler : GitHubCompatHandler
{
public CodebaseHqHandler(IGitServer gitServer)
: base(gitServer)
{
}
- public override bool TryGetRepositoryInfo(HttpRequest request, out RepositoryInfo repositoryInfo)
+ public override bool TryGetRepositoryInfo(HttpRequest request, JObject payload, out RepositoryInfo repositoryInfo)
{
repositoryInfo = null;
if (request.UserAgent != null &&
request.UserAgent.StartsWith("Codebasehq", StringComparison.OrdinalIgnoreCase))
{
- return base.TryGetRepositoryInfo(request, out repositoryInfo);
+ repositoryInfo = GetRepositoryInfo(request, payload);
}
- return false;
+
+ return repositoryInfo != null;
}
protected override RepositoryInfo GetRepositoryInfo(HttpRequest request, JObject payload)
@@ -42,8 +43,12 @@ protected override RepositoryInfo GetRepositoryInfo(HttpRequest request, JObject
info.RepositoryUrl = urls.Value<string>("http");
}
- info.Deployer = "CodebaseHQ";
return info;
}
+
+ protected override string GetDeployer(HttpRequest request)
+ {
+ return "CodebaseHQ";
+ }
}
}
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
-using System.IO;
using System.Linq;
using System.Web;
using Kudu.Contracts.Dropbox;
@@ -9,7 +8,7 @@
using Kudu.Core;
using Kudu.Core.SourceControl;
using Kudu.Services.Dropbox;
-using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
namespace Kudu.Services.GitServer.ServiceHookHandlers
{
@@ -27,22 +26,12 @@ public class DropboxHandler : IServiceHookHandler
_helper = new DropboxHelper(tracer, repository, settings, environment);
}
- public bool TryGetRepositoryInfo(HttpRequest request, out RepositoryInfo repositoryInfo)
+ public bool TryGetRepositoryInfo(HttpRequest request, JObject payload, out RepositoryInfo repositoryInfo)
{
repositoryInfo = null;
- if (request.UserAgent != null &&
- request.UserAgent.StartsWith(DropboxHelper.Dropbox , StringComparison.OrdinalIgnoreCase))
+ if (!String.IsNullOrEmpty(payload.Value<string>("NewCursor")))
{
- DropboxInfo info = DropboxInfo.Parse(request.GetInputStream());
-
- _tracer.Trace("Dropbox payload", new Dictionary<string, string>
- {
- { "changes", info.DeployInfo.Deltas.Count().ToString() },
- { "oldcursor", info.DeployInfo.OldCursor },
- { "newcursor", info.DeployInfo.NewCursor }
- });
-
- repositoryInfo = info;
+ repositoryInfo = new DropboxInfo(payload);
}
return repositoryInfo != null;
@@ -52,42 +41,21 @@ public virtual void Fetch(RepositoryInfo repositoryInfo, string targetBranch)
{
// Sync with dropbox
DropboxInfo info = (DropboxInfo)repositoryInfo;
+
_helper.Sync(info.DeployInfo, targetBranch);
}
private class DropboxInfo : RepositoryInfo
{
- public DropboxDeployInfo DeployInfo { get; set; }
-
- public static DropboxInfo Parse(Stream stream)
+ public DropboxInfo(JObject payload)
{
- DropboxDeployInfo deployInfo = null;
- using (JsonTextReader reader = new JsonTextReader(new StreamReader(stream)))
- {
- JsonSerializer serializer = new JsonSerializer();
- try
- {
- deployInfo = serializer.Deserialize<DropboxDeployInfo>(reader);
- }
- catch (Exception ex)
- {
- throw new FormatException(Resources.Error_UnsupportedFormat, ex);
- }
-
- if (deployInfo == null)
- {
- throw new FormatException(Resources.Error_EmptyPayload);
- }
- }
-
- return new DropboxInfo
- {
- Deployer = DropboxHelper.Dropbox,
- NewRef = "dummy",
- OldRef = "dummy",
- DeployInfo = deployInfo
- };
+ Deployer = DropboxHelper.Dropbox;
+ NewRef = "dummy";
+ OldRef = "dummy";
+ DeployInfo = payload.ToObject<DropboxDeployInfo>();
}
+
+ public DropboxDeployInfo DeployInfo { get; set; }
}
}
}
Oops, something went wrong.

0 comments on commit cb7ccf5

Please sign in to comment.