Created
February 22, 2019 09:08
-
-
Save CraigLager/f8882c9dcc71d4b85aeb074f36733b08 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.Diagnostics; | |
using System.Linq; | |
using Newtonsoft.Json.Linq; | |
using Octokit.Helpers; | |
using Utility; | |
namespace GitHub.Models | |
{ | |
/// <summary> | |
/// Issue hook processing | |
/// </summary> | |
public class Issue : GitHubWebhookBase | |
{ | |
/// <summary> | |
/// Creates a new issue hook processor | |
/// </summary> | |
/// <param name="issue">Webhook payload</param> | |
public Issue(JObject issue) : base(issue) | |
{ | |
} | |
/// <summary> | |
/// Api url for the issue | |
/// </summary> | |
public string Url => Payload["issue"]["url"].ToString(); | |
/// <summary> | |
/// Issue number | |
/// </summary> | |
public int Number => int.Parse(Payload["issue"]["number"].ToString()); | |
/// <summary> | |
/// Issue title | |
/// </summary> | |
public string Title => Payload["issue"]["title"].ToString(); | |
/// <summary> | |
/// Unique issue ID | |
/// </summary> | |
public long Id => long.Parse(Payload["issue"]["id"].ToString()); | |
/// <summary> | |
/// Processes the message | |
/// </summary> | |
public override void Process() | |
{ | |
switch (Action) | |
{ | |
case "opened": | |
IssueOpened(); | |
break; | |
case "labeled": | |
IssueLabelled(); | |
break; | |
default: | |
Debug.WriteLine("unhandled action " + Action); | |
break; | |
} | |
} | |
/// <summary> | |
/// Handles issues being opened | |
/// </summary> | |
private void IssueOpened() | |
{ | |
} | |
/// <summary> | |
/// Handle labels being applied | |
/// </summary> | |
public void IssueLabelled() | |
{ | |
if (Payload["issue"]["labels"] != null && Payload["issue"]["labels"].HasValues) | |
{ | |
if (Payload["issue"]["labels"].Any(l => l["name"].ToString() == "code change")) | |
{ | |
CreateBranchForIssue(); | |
} | |
} | |
} | |
/// <summary> | |
/// Cretes a branch for the current issue | |
/// </summary> | |
private void CreateBranchForIssue() | |
{ | |
// calculate a branch name from the issue | |
var branchName = Number + "-" + | |
Title.ToLower().Replace(" ", "-") | |
.ReplaceAll("`¬!\"£$%^&*()_+=/*|\\,./;'#[]<>?:@~{}".ToCharArray().Select(c => c.ToString()).ToList(), ""); | |
try | |
{ | |
// create a branch at github | |
var createResult = GitClient.Git.Reference.CreateBranch(RepositoryOwnerLogin, RepositoryName, branchName) | |
.Result; | |
// link the branch and the issue | |
var result = GitClient.Issue.Comment.Create(RepositoryId, Number, | |
"Branch [" + branchName + "](https://www.github.com/" + RepositoryFullName + "/tree/" + branchName + | |
") created").Result; | |
} | |
catch (System.AggregateException ex) | |
{ | |
// validation exceptions happen if the branch exists | |
if(ex.InnerException.GetType() != typeof(Octokit.ApiValidationException)) | |
{ | |
throw ex.InnerException; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment