How to automate add my c# application issues to Github programmatically

2.9k views Asked by At

How can I programmatically add issues on my Github repository using C#?

I have an Error Handler library (ErrorControlSystem) to attach that in a win application to raise that exceptions on a SQL table.

Now, I want to store ErrorControlSystem self exceptions without the target app exceptions on self Github repository issues.

How to can I do it?

3

There are 3 answers

3
Ole Albers On BEST ANSWER

You can use the GitHub API for that. Create a webhook and add an issue the following way:

POST /repos/:owner/:repo/issues

Example from https://developer.github.com/v3/issues/

{
  "title": "Found a bug",
  "body": "I'm having a problem with this.",
  "assignee": "octocat",
  "milestone": 1,
  "labels": [
    "Label1",
    "Label2"
  ]
}

So all you have to do is a HTTP - POST command to add an issue.

You can do a post request using a WebRequest.

Complete description for the api: https://api.github.com/repos/octocat/Hello-World/issues/1347

Complete C#-Example:

public void CreateBug(Exception ex) {
  WebRequest request = WebRequest.Create ("https://api.github.com/repos/yourUserName/YourRepo/issues ");
  request.Method = "POST";
  string postData = "{'title':'exception occured!', 'body':'{0}','assignee': 'yourUserName'}";
  byte[] byteArray = Encoding.UTF8.GetBytes (string.Format(postData,ex));
  request.ContentLength = byteArray.Length;
  Stream dataStream = request.GetRequestStream ();
  dataStream.Write (byteArray, 0, byteArray.Length);
  dataStream.Close ();
  WebResponse response = request.GetResponse ();
}

Now your issue has been created and response contains the response from GitHub

This is the "fast, easy" solution. If you want do do more with GitHub issues, @VonC's answer might be the better one as it offers a more object-related solution

0
VonC On

If you need to create issues on a GitHub repo programmatically with C#, you can refer to the C# project octokit/octokit.net which will use the GitHub API.

It can create issue:

var createIssue = new NewIssue("this thing doesn't work");
var issue = await _issuesClient.Create("octokit", "octokit.net", createIssue);

Create returns a Task<Issue> which represents the created issue.

0
Kirkaiya On

If you're looking for a more complete code solution and walk-thru of how the different parts work, feel free to look at the solution I created recently (in .NET 6) that creates an issue in a GitHub repo, as well as shows how to set it up as a GitHub app. In your case, you probably want to use the OAuth authentication mechanism rather than a separate GitHub app, but you could do it either way really.

My solution is here on my GitHub: https://github.com/Kirkaiya/Branch-Protector-GHApp-.NET-Lambda

The method in Function.cs that actually creates the issue (using the Octokit .NET client library) is here:

private void CreateIssue(GitHubClient ghc, long repoId)
{
    string issuebody = GetIssueBody();

     try
    {
        var newIssueRequest = new NewIssue("Branch protections enabled for this repo");
        newIssueRequest.Body = issuebody;
        newIssueRequest.Assignees.Add("Kirkaiya");
        var issue = ghc.Issue.Create(repoId, newIssueRequest).Result;

        Console.WriteLine($"Created new issue at {issue.HtmlUrl}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error creating issue: {ex.Message}");
    }
}

But look in src/Function.cs for the full code, and how the auth is done, etc.