Last active
December 17, 2015 20:29
-
-
Save xt0rted/5667721 to your computer and use it in GitHub Desktop.
kudu service hook handler for springloops.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SpringloopsHandler : ServiceHookHandlerBase | |
{ | |
public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo) | |
{ | |
string deployer = payload.Value<string>("domain"); | |
if (!"springloops.io".Equals(deployer, StringComparison.OrdinalIgnoreCase)) | |
{ | |
deploymentInfo = null; | |
return DeployAction.UnknownPayload; | |
} | |
deploymentInfo = GetDeploymentInfo(request, payload, targetBranch); | |
return deploymentInfo == null ? DeployAction.NoOp : DeployAction.ProcessDeployment; | |
} | |
protected virtual GitDeploymentInfo GetDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch) | |
{ | |
string branch = payload.Value<string>("branch"); | |
if (!branch.Equals(targetBranch, StringComparison.OrdinalIgnoreCase)) | |
{ | |
return null; | |
} | |
var info = new GitDeploymentInfo { RepositoryType = RepositoryType.Git }; | |
info.RepositoryUrl = payload.Value<string>("repositoryURL"); | |
info.Deployer = "Springloops"; | |
info.NewRef = payload.Value<string>("afterPushRevision"); | |
var commits = payload.Value<JArray>("commits"); | |
info.TargetChangeset = ParseChangeSet(info.NewRef, commits); | |
return info; | |
} | |
protected static ChangeSet ParseChangeSet(string id, JArray commits) | |
{ | |
if (commits == null || !commits.HasValues) | |
{ | |
return new ChangeSet(id, authorName: null, authorEmail: null, message: null, timestamp: DateTimeOffset.UtcNow); | |
} | |
JToken latestCommit = commits.Last; | |
JObject commitAuthor = latestCommit.Value<JObject>("author"); | |
return new ChangeSet( | |
id, | |
commitAuthor.Value<string>("committerName"), | |
commitAuthor.Value<string>("committerEmail"), | |
latestCommit.Value<string>("commitMessage"), | |
DateTimeOffset.Parse(latestCommit.Value<string>("commitDate")) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment