Search GitHub stars with Octokit and fzf

- github ruby cli api

Search GitHub stars with Octokit and fzf

If you are anything like me, you’re using Github’s repository stars as bookmarks for interesting projects you want to check out at a later date. And if you’re really like me (generally not advised) you love the command line and think it would be awesome if you had an easy way to search your stars from there. Today I had a spare hour, so I finally got around to build a solution that really fits my workflow, maybe it will appeal to you too.

Fetching starred repositories with Octokit

Octokit is “the simple and official way to build amazing experiences using the GitHub API”, written by the GitHub team. It’s extremely convenient and I used it for writing myself a small utitlity called stars, which will fetch my starred repositories and output their full name, description and URL, separated by tabs. By default it will only return the latest 30 repositories (the first page of the API response), but can also retrieve all of them when called as stars all.

This is the self-explanotary script in all its non-glory:

#!/usr/bin/env ruby

require 'octokit'

Octokit.auto_paginate = true if ARGV[0] == 'all'
client = Octokit::Client.new(access_token: 'your_access_token')

starred = client.starred('your_username')
starred.each do |star|
  puts "#{star.full_name}\t#{star.description}\t#{star.html_url}"
end

(Note: the access token is optional, but allows for more API requests which was useful while developing)

This generates the following output (abbreviated):

→ stars
tagomoris/deferral	Golang-style defer in Ruby	https://github.com/tagomoris/deferral
iridakos/duckrails	A development tool to quickly & dynamically mock API endpoints :duck:	https://github.com/iridakos/duckrails
baweaver/qo	Qo - Query Object - Pattern matching and fluent querying in Ruby	https://github.com/baweaver/qo
trailofbits/ethersplay	EVM dissassembler	https://github.com/trailofbits/ethersplay
letsgetrandy/brototype	Bro, do you even?	https://github.com/letsgetrandy/brototype
devise-security/devise-security	A security extension for devise, meeting industrial standard security demands for web applications.	https://github.com/devise-security/devise-security

Finding repositories with fzf

If you don’t know about fzf, do yourself a favor and check it out immediately. It’s an awesome command-line fuzzy finder written in Go, which is not only fast but also offers a pretty comprehensive feature set.

To integrate my stars utility with fzf I added a function named sstars (for “search stars”, no awards for originality there) to my .zshrc:

function sstars() {
  local url
  url=$(stars "$1" | fzf -e | cut -f3)
  open $url
}

This expect stars to be available on your path. It will then pipe its output into fzf, assign the third field of the line (the URL) to a variable and let macOS’ open utility take care of the rest.

Here’s a screenshot of the tool in action:

Searching for Haskell repositories I starred when I still believed in a better world for software developers

That’s all folks!

Scratching a personal itch is always the best motivation to get started on a little weekend project and I did learn about Octokit in the process, so that’s a nice side-effect.

Feedback