Skip to content

Instantly share code, notes, and snippets.

@ToniRib
Last active March 15, 2018 21:41
Show Gist options
  • Save ToniRib/ab52870a739903ef0901756f3367114c to your computer and use it in GitHub Desktop.
Save ToniRib/ab52870a739903ef0901756f3367114c to your computer and use it in GitHub Desktop.
A guide on how to party with rabbits

Party Rabbit!

So you've got the party rabbit for the day, huh? It's time to party!

Requirements

  • Requires you to have the ruby gem terminal-notifier installed
  • Requires you to have an environment variable GITHUB_API_TOKEN set in your .bash_profile
    • This is a crontab requirement as it does not have access to all of the environment variables that your normal shell session has.
    • If you are using fish and this is in your config.fish, you MUST copy it into your .bash_profile.
    • If you are using zsh and your environment variables are exported in a .zshrc file, that's fine, just change line 4 of the snippet below to be source $HOME/.zshrc.

Setup

Bash Script

Feel free to take these instructions less literally if you have a different place that you like to store your executable functions. The overall goal is to get an executable bash script that crontab will be able to pick up and run.

  1. Navigate to cd /usr/local/bin

  2. touch check_reviewable_pull_requests.sh

  3. Open that file

  4. Paste in the following code and save the file:

    #!/bin/bash
    
    PATH=/usr/bin:/bin:/usr/local/bin
    source $HOME/.bash_profile
    curl=`curl -H "Authorization: bearer $GITHUB_API_TOKEN" -X GET https://api.github.com/repos/gospotcheck/gospotcheck/pulls`
    needs_review_count=`echo $curl | grep -c "review requested"`
    
    if [ $needs_review_count != "0" ]; then
      pr_text="PRs"
    
      if [ $needs_review_count = "1" ]; then
        pr_text="PR"
      fi
    
      terminal-notifier -message "You have $needs_review_count new $pr_text to review. Click to go to GitHub." -title "Time to party!" -open https://github.com/gospotcheck/gospotcheck/pulls
    fi
    
  5. Make it executable with chmod u+x check_reviewable_pull_requests.sh

Trigger functions

Add the following fish function that will trigger crontab to run the script above every hour.

  • If you want to test this at first by running it every minute, change the first 0 to a *.
  • If you put your executable script at a different path, make sure you reflect that here
  • The >/dev/null 2>&1 ensure that you won't get any terminal mail created when this runs
function party_on
  echo "0 * * * * /usr/local/bin/check_reviewable_pull_requests.sh >/dev/null 2>&1" | crontab
end

This function will turn it off again:

function party_off
  crontab -r
end

If you use zsh or bash, the only difference is how you define the function, such as:

function party_on() {
  echo "0 * * * * /usr/local/bin/check_reviewable_pull_requests.sh" | crontab
}

Usage

If you're on duty with the Party Rabbit, run party_on at the beginning of the day. Every hour, on the hour, you will receive a Mac notification only if there are pull requests with the review requested label applied. When the notification pops you, you can click it to be taken to the pull requests page on GitHub. At the end of the day, run party_off to turn the notifications off!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment