Skip to content

Instantly share code, notes, and snippets.

@samjoch
Created November 11, 2014 02:35
Show Gist options
  • Save samjoch/7c3310ba4be2c5c7cfcf to your computer and use it in GitHub Desktop.
Save samjoch/7c3310ba4be2c5c7cfcf to your computer and use it in GitHub Desktop.
Import github opened issues in Things.app area tasks
###
# Need a ./.config.json file containing token, repo, area infos
# ie:
# {
# "token": "...",
# "repo": "username/repo",
# "area": "areaname"
# }
###
fs = require 'fs'
colors = require 'colors'
request = require 'request'
_ = require 'underscore'
script = require 'applescript'
conf = require './.config.json'
conf.issues ||= {}
es = (text) -> text.replace(/"/gi, "\\\"")
sync = -> fs.writeFileSync('./.config.json', JSON.stringify(conf, null, 4))
console.log "> Sync", conf.repo.bold, 'and', 'Things.app'.blue.bold
things = """
tell application "Things"
"""
page = 1
host = "https://api.github.com"
url = -> "#{host}/repos/#{conf.repo}/issues?state=open&page=#{page}"
addIssues = 0
options = ->
url: url()
method: 'GET'
headers:
'User-Agent': 'Sync Issues / Things App'
'Authorization': "token #{conf.token}"
addIssue = (issue) ->
return if _.keys(conf.issues).indexOf(issue.number) >= 0
addIssues++
conf.issues[issue.number] = _.pick(issue, 'title', 'number', 'body', 'labels')
cmds = [
"set issue#{issue.number} to make new to do"
"with properties { name: \"##{issue.number} - #{es(issue.title)}\","
"notes: \"#{es(issue.body)}\" } at beginning of area \"#{conf.area}\""
].join(' ')
things += """
#{cmds}
"""
labels = _.pluck(issue.labels, 'name').join(', ')
things += """
set tag names of issue#{issue.number} to "#{labels}"
""" if labels.length > 0
start = ->
console.log '> Processing page', page
request options(), (err, res, _body) ->
issues = JSON.parse(_body)
after = _.after issues.length, ->
if res.headers['link'].match('next')
page++
req()
else
things += """
end tell
"""
sync()
script.execString things, (err) ->
return console.log '! Error'.red, err if err
console.log '>', (addIssues + '').green, 'issue(s) added'.green
_.each issues, (issue) ->
addIssue(issue)
after()
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment