Use PowerShell to create a GitHub Issue(s)

I was a bit surprised that I could not find some existing code that to do this (Google Fail?). Luckily I was able to find enough resources to solve this fairly quickly. If you know of another resource please let me know and I will add a link.

Here is my scenario for this code:

I’m working on an existing project with a few other guys, and we’ve just identified a large list of items that need to have issues created. I find it easier to break my work down in a bulleted list of tasks/features, then create issues. The problem is that using GitHub to create a list of issues(say 30-40) is a major time hog! So I wrote some PowerShell code to do the work for me! The goal is to create an Issue for a list of Tasks that each have a Title, Description(Body), and a label.

The most painful portion of this was the Authentication to GitHub, I wish more Enterprise vendors would support PowerShell, and provide examples! Be sure to update the Security Protocol:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Next, Create an API token from the GitHub API (see screen capture below).
  • Log into GitHub
  • Click on the drop down of your avatar in the Top right
    • Select ‘Settings’
    • Select ‘Developer settings’
    • Select ‘Personal Access Tokens’
    • Click ‘Generate New token’
      • Confirm your password
      • Type a Name
      • Add the Appropriate Permissions
      • Click the ‘Generate token’ button
      • Finally you can copy the new token (Save it in a secure location)
Capture

Great job so far!! Now lets setup some headers for the REST call.

$UserToken = "a60b16b5c8b3b268abd8b29fc774be1aacf67b91"
$Headers = @{
Authorization='token '+$UserToken
}

 

First, here is the function to actually create the data in GitHub:

New-GitHubIssue – Located under the Invoke-Automation repo tied to my GitHub account. Click the link to view the Function code:

Invoke-Automation/Powershell/GitHub/New-GitHubIssue.ps1

Next, a few lines to read a CSV and call the ‘New-GitHubIssue‘ function to create all the Issues.

Here is an example of the CSV(Fields: Title,Description,Label):

NewIssue01,FixThisBug,FancyLabelName
NewIssue02,FixThisBug,Component01
NewIssue03,FixThisBug,Web

Here is the loop to process the CSV:

[string]$Owner = "jpsider"
[string]$Repository = "Invoke-Automation"
$csvFile = "C:\Temp\NewIssues.csv"
$Content = Get-Content -Path $csvFile
foreach($line in $Content) {
$Title,$Description,$Label=$line.split(",")
Write-Output"Entering Issue - Title:$Title,Description:$description,Label:$Label"
New-GithubIssue-Title $Title-Description $Description-Label $Label-owner $Owner-    Repository $Repository-Headers $Headers
}

Boom, Done. 30-40 Issues created in less than 10 seconds. Give it a Shot! Add different fields that suit your needs.

One thought on “Use PowerShell to create a GitHub Issue(s)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s