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:

 [string]$userName = $null,
 [string]$password = $null

function FindMatches($comments){
 $ret = @()
 $comments | ? {$_.body -Like $searchText } | % {
 $ret += @{
 Url = $_.url
 Body = $_.body
#Authenticated queries get a higher rate limit in the event you're searching a lot of comments
#more here
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 -Headers $headers | Out-Null

#sort and filtering options
$prs = Invoke-RestMethod "$($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) }