Skip to content

Commit

Permalink
Plugin to create tasks in Asana
Browse files Browse the repository at this point in the history
  • Loading branch information
AlAdler committed Jan 22, 2015
1 parent a6a90fd commit 045d71f
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions PluginDirectories/1/asana.bundle/examples.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asana ~task(do the dishes)
11 changes: 11 additions & 0 deletions PluginDirectories/1/asana.bundle/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"categories" : [
"Utilities"
],
"displayName" : "Asana - Create a New Task",
"name" : "asana",
"description" : "Create a new task in Asana",
"examples" : [
"asana do the dishes"
]
}
31 changes: 31 additions & 0 deletions PluginDirectories/1/asana.bundle/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"options" : [
{
"text": "Workspace:",
"type": "dropdown",
"key": "workspace",
"options": [
{"text": "Personal Projects", "value": "personal"},
{"text": "Organization", "value": "organization"}
]
},
{
"type":"hint",
"text":"If you chose 'Organization', enter its name"
},
{
"text": "Organization Name",
"type": "text",
"key": "orgainzation_name"
},
{
"type":"hint",
"text":"Your API key is in Asana => Account Settings => Apps"
},
{
"text":"API Key",
"type":"text",
"key":"api_key"
}
]
}
87 changes: 87 additions & 0 deletions PluginDirectories/1/asana.bundle/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import json, urllib2, base64

def results(fields, original_query):
task = fields['~task']
settings = json.load(open("preferences.json"))
api_key = settings.get('api_key')
if api_key is None or api_key == '':
return {
"title": "asana '{0}'".format(task),
"run_args": ['NO_CREDENTIALS', ''],
"html": "<h2 style='font-family: sans-serif; padding: 2em'>Enter your Asana API key in the plugin settings</h2>"
}
else:
return {
"title": "asana '{0}'".format(task),
"run_args": [task, api_key],
"html": "<h1 style='font-family: sans-serif; padding: 2em'>Create task '{0}'</h1>".format(task)
}

def run(task, api_key):
if task == 'NO_CREDENTIALS':
return
workspaces = get_all_workspaces(api_key)
if workspaces is None:
post_notification('Please check your Asana API key')
return
chosen_space = get_chosen_workspace()
space_id = get_workspace_id(chosen_space, workspaces)
if space_id is None:
post_notification('Failed to find your Asana workspace')
return
user_id = get_user_id(api_key)
create_task(user_id, space_id, task, api_key)
post_notification('Asana task has been created!')

def get_workspace_id(chosen_space, workspaces):
space_id = None
for x in xrange(0,len(workspaces)):
if workspaces[x].get('name') == chosen_space:
space_id = workspaces[x].get('id')
break
return space_id

def get_all_workspaces(api_key):
request = urllib2.Request("https://app.asana.com/api/1.0/workspaces")
base64string = base64.encodestring('%s:%s' % (api_key, '')).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
try:
result = urllib2.urlopen(request)
workspaces = json.load(result).get('data')
except:
workspaces = None
return workspaces

def create_task(user_id, space_id, task, api_key):
task_data = {"data" : {"workspace" : space_id, "name" : task, "assignee": {"id": user_id}}}
request = urllib2.Request(url="https://app.asana.com/api/1.0/tasks", data=json.dumps(task_data))
base64string = base64.encodestring('%s:%s' % (api_key, '')).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
request.add_header('Content-Type', 'application/json')
result = urllib2.urlopen(request)

def get_user_id(api_key):
request = urllib2.Request("https://app.asana.com/api/1.0/users/me")
base64string = base64.encodestring('%s:%s' % (api_key, '')).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request)
user_id = json.load(result).get('data').get('id')
return user_id

def get_chosen_workspace():
settings = json.load(open("preferences.json"))
workspace = settings.get('workspace')
chosen_space = 'Personal Projects'
if workspace != 'personal':
chosen_space = settings.get('orgainzation_name')
if chosen_space is None or chosen_space == '':
chosen_space = 'Personal Projects'
return chosen_space

def post_notification(message, title="Flashlight"):
import os, json, pipes
# do string escaping:
message = json.dumps(message)
title = json.dumps(title)
script = 'display notification {0} with title {1}'.format(message, title)
os.system("osascript -e {0}".format(pipes.quote(script)))
5 changes: 5 additions & 0 deletions PluginDirectories/1/asana.bundle/preferences.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"workspace" : "personal",
"orgainzation_name": "",
"api_key":""
}

0 comments on commit 045d71f

Please sign in to comment.