Search Github Repo Comments

We’ve all found ourselves there.  I have the perfect explaination for the current situation, in fact, I know I’ve documented it beautifully in the finest Github review comment you’ve ever seen, I just can’t find it.

A while back I had posted a Github comment that one of my teammates thought would be good blog material.  Problem was, I couldn’t find it.  Solution?  Write script using Github’s freaking awesomely straightforward api and net two blog posts!  Winner, winner, chicken dinner.  Here it is in powershell:

param
(
 [Parameter(Mandatory=$true)]
 [string]$repo,
 [Parameter(Mandatory=$true)]
 [string]$searchText,
 [string]$userName = $null,
 [string]$password = $null
)

function FindMatches($comments){
 $ret = @()
 $comments | ? {$_.body -Like $searchText } | % {
 $ret += @{
 Url = $_.url
 Body = $_.body
 }
 $ret
 }
}
#Authenticated queries get a higher rate limit in the event you're searching a lot of comments
#more here https://developer.github.com/v3/#rate-limiting
if(-not [string]::IsNullOrEmpty($userName))
{
 $authInfo = ("{0}:{1}" -f $userName,$password)
 $authInfo = [System.Text.Encoding]::UTF8.GetBytes($authInfo)
 $authInfo = [System.Convert]::ToBase64String($authInfo)

 $headers = @{Authorization=("Basic {0}" -f $authInfo)}

 Invoke-RestMethod -Uri https://api.github.com/user -Headers $headers | Out-Null
}

#sort and filtering options https://developer.github.com/v3/pulls/#list-pull-requests
$prs = Invoke-RestMethod "https://api.github.com/repos/$($repo)/pulls?state=all" -Headers $headers

$matches = @()

$prs | % {
 $pr = $_
 #Discussion Tab comments
 $matches += FindMatches(Invoke-RestMethod -Uri $pr.comments_url -Headers $headers)

 #Review comments on File Changes tab
 $matches += FindMatches(Invoke-RestMethod -Uri $pr.review_comments_url -Headers $headers)
}

Write-Host 'Matches:'
$matches | % { Write-Host ("`r`n{0}`r`n{1}`r`n" -f $_.Url, $_.Body) }
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s