Skip to content

Instantly share code, notes, and snippets.

@craigeley
Last active April 1, 2017 20:24
Show Gist options
  • Save craigeley/33ce47145fd254987748 to your computer and use it in GitHub Desktop.
Save craigeley/33ce47145fd254987748 to your computer and use it in GitHub Desktop.
If you have a server with [rem](https://github.com/kykim/rem) installed, this Ruby script can create and complete Apple Reminders when GitHub issues are opened or closed. It is currently configured to push issues from a single repo to multiple iCloud lists based on issue tags. These lists must already exist.
require 'sinatra'
require 'json'
require 'time'
require 'octokit'
require 'yaml'
set :bind, '0.0.0.0'
# Fix Encoding Errors on OSX
if RUBY_VERSION =~ /2.*.*/
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
end
# Load variables
token = "YOUR_GITHUB_ACCESS_TOKEN"
repo = "USERNAME/REPO"
# Parse the GitHub webhook
post '/payload' do
# Get GitHub Data
push = JSON.parse(request.body.read)
puts push["action"]
# Get Reminders
names = []
reminders = `/usr/local/bin/rem`
reminders.lines.each_with_index do |line, i|
/(\d+).\s(?<item>.*)/ =~ line
names.push(item)
end
# Create new reminder when issue is created if it doesn't already exist
if push["action"] == "opened" || push["action"] == "reopened"
issue = push["issue"]["title"]
num = push["issue"]["number"]
url = push["issue"]["html_url"]
if names.include?(issue)
puts "Already present!"
else
sleep 0.5
client = Octokit::Client.new(:access_token => "#{token}")
myList = ''
myList = client.issue("#{repo}", "#{num}").labels[0].name
puts myList
%x{ osascript <<APPLESCRIPT
tell application "Reminders"
tell list "#{myList}"
make new reminder with properties {name:"#{issue}", body: "#{url}", due date:(current date)}
end tell
end tell
APPLESCRIPT}
puts "#{issue} created"
end
end
# Close Reminder when an issue is closed
if push["action"] == "closed"
entry = push["issue"]["title"].strip
body = push["issue"]["body"].strip
url = push["issue"]["html_url"].strip
if push["repository"]["name"] == "fieldnoise"
tag = "Fieldnoise"
else
tag = push["issue"]["labels"][0]["name"]
end
data = `/usr/local/bin/rem`
data.lines.each_with_index do |line, i|
if line =~ /#{entry}/
/(?<num>\d+).\s(?<item>.*)/ =~ line
/(?<var>\w+)/ =~ data.lines[i - num.to_i]
`/usr/local/bin/rem done #{var} #{num}`
puts "Reminder closed"
end
end
end
# Delete completed Reminders
%x{ osascript <<APPLESCRIPT
tell application "Reminders"
delete (every reminder whose completed is true)
end tell
APPLESCRIPT}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment