public virtual GithubEvent Validate(ApplicationLoadBalancerRequest request) { GithubEvent payload = null !; string eventType; ValidateMethod(request); ValidateEvent(request, out eventType); switch (eventType) { case "push": { ValidatePushBodyFormat(request, out var pushPayload); payload = pushPayload; break; } case "pull_request": { ValidatePullRequestBodyFormat(request, out var pullPayload); payload = pullPayload; break; } } ValidateContentsUrlPresent(payload); ValidateOwner(payload); ValidateSignature(request); return(payload); }
private static void ValidateContentsUrlPresent(GithubEvent payload) { if (payload.Repository?.ContentsUrl == null) { throw new NoContentsUrlException(); } }
private static string GetOwner(GithubEvent githubEvent) { switch (githubEvent) { case PushEvent pushEvent: return(pushEvent.Repository?.Owner?.Name); case PullRequestEvent pullRequestEvent: return(pullRequestEvent.Repository?.Owner?.Login); } return(""); }
public virtual async Task <string> FetchCommitMessage(GithubEvent @event) { switch (@event) { case PushEvent pushEvent: return(FetchPushEventCommitMessage(pushEvent)); case PullRequestEvent pullRequestEvent: return(await FetchPullRequestEventCommitMessage(pullRequestEvent)); } return(""); }
private void ValidateOwner(GithubEvent payload) { var expectedOwner = config.GithubOwner; if (expectedOwner == null) { return; } var matcher = new Regex("https:\\/\\/api\\.github\\.com\\/repos\\/([a-zA-Z0-9\\-\\._]+)\\/([a-zA-Z0-9\\-\\._]+)\\/contents\\/{\\+path}"); var repositoryOwner = GetOwner(payload); var contentsUrlMatches = matcher.Match(payload.Repository?.ContentsUrl).Groups[1]?.Captures; var contentsUrlOwner = contentsUrlMatches.Count == 1 ? contentsUrlMatches[0].Value : null; if (repositoryOwner != expectedOwner) { throw new UnexpectedOwnerException($"Unexpected repository owner '{repositoryOwner}'"); } if (contentsUrlOwner != expectedOwner) { throw new UnexpectedOwnerException($"Unexpected contents url owner: '{contentsUrlOwner}'"); } }