citizen428.blog()

Try to learn something about everything

Ruby and HTTParty: GitHub Issues to CSV

To fill our backlog at work, our project manager needed a list of our current GitHub issues, ideally in a spreadsheet format. A couple lines of Ruby produce CSV output consisting of issue title, creation date, reporter and labels, which then can be redirected to a file and opened with any spreadsheet program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<span class='line'><span class="nb">require</span> <span class="s1">&#39;httparty&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">GitHubIssues</span>
</span><span class='line'>  <span class="kp">include</span> <span class="no">HTTParty</span>
</span><span class='line'>  <span class="n">base_uri</span> <span class="s1">&#39;http://github.com/api/v2/yaml&#39;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">show</span>
</span><span class='line'>    <span class="c1"># username must be of the form &#39;&lt;username&gt;/token:&lt;token&gt;&#39;</span>
</span><span class='line'>    <span class="n">opts</span> <span class="o">=</span> <span class="p">{</span><span class="ss">:basic_auth</span> <span class="o">=&gt;</span> <span class="p">{</span><span class="ss">:username</span> <span class="o">=&gt;</span> <span class="s1">&#39;&#39;</span><span class="p">}}</span>
</span><span class='line'>    <span class="nb">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;/issues/list/&lt;user&gt;/&lt;projects&gt;/open&#39;</span><span class="p">,</span> <span class="n">opts</span><span class="p">)</span><span class="o">[</span><span class="s2">&quot;issues&quot;</span><span class="o">].</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">issue</span><span class="o">|</span>
</span><span class='line'>      <span class="nb">puts</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">issue</span><span class="o">[</span><span class="s2">&quot;title&quot;</span><span class="o">]</span><span class="si">}</span><span class="s2">;</span><span class="si">#{</span><span class="n">issue</span><span class="o">[</span><span class="s2">&quot;created_at&quot;</span><span class="o">]</span><span class="si">}</span><span class="s2">;</span><span class="si">#{</span><span class="n">issue</span><span class="o">[</span><span class="s2">&quot;user&quot;</span><span class="o">]</span><span class="si">}</span><span class="s2">;</span><span class="si">#{</span><span class="n">issue</span><span class="o">[</span><span class="s2">&quot;labels&quot;</span><span class="o">].</span><span class="n">join</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="no">GitHubIssues</span><span class="o">.</span><span class="n">show</span>
</span>

It’s quick and dirty (e.g. will die if there are no issues), but does exactly what we needed in just a couple of lines thanks to the awesome HTTParty.

This also has been published in slightly different form on our company blog.

Comments

Copyright © 2016 - Michael Kohl - Powered by Octopress