Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for Asana 1.1 and allow add use of body for new tasks #256

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 57 additions & 21 deletions Source/Asana.spoon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@
-- Setup Environment --
-----------------------
-- Create locals for all needed globals so we have access to them
local pairs,ipairs,type,require = pairs,ipairs,type,require

local stringFormat = string.format
local hs = {
fnutils = hs.fnutils,
http = hs.http,
json = hs.json,
notify = hs.notify
notify = hs.notify,
logger = hs.logger,
inspect = hs.inspect
}

-- Empty environment in this scope, this prevents module from polluting global scope
Expand All @@ -67,24 +70,23 @@

local _, res = hs.http.get(baseUrl .. "/users/me", reqHeader)
res = hs.json.decode(res)
userId = res.data.id
userId = res.data.gid
hs.fnutils.each(
res.data.workspaces,
function(x)
workspaceIds[x.name] = x.id
workspaceIds[x.name] = x.gid
end
)
end


------------
-- Public --
------------
-- luacheck: no global

-- Spoon metadata
name = "Asana"
version = "0.1" -- obj.version = "0.1"
version = "0.2" -- obj.version = "0.1"
author = "Malo Bourgon"
license = "MIT - https://opensource.org/licenses/MIT"
homepage = "https://github.com/malob/Asana.spoon"
Expand All @@ -95,13 +97,40 @@
--- A "personal access token" for Asana. You can create one here: https://app.asana.com/0/developer-console
apiKey = ""


-- Thanks to mnz
function tableCopy(tabl)
local t = {}
for k,v in pairs(tabl) do
t[k] = type(v)=="table" and tableCopy(v) or v
end
return t
end

-- Thanks to mnz
function tableMerge(tbl1,tbl2)
local t = {}
for k,v in pairs(tbl1) do t[k] = type(v)=="table" and tableCopy(v) or v end
for k,v in pairs(tbl2) do t[k] = type(v)=="table" and tableCopy(v) or v end
return t
end

function getTaskBody(tbl1,tbl2)
s = stringFormat('{"data": %s}', hs.json.encode(tableMerge(tbl1,tbl2), false))
return s
end

--- Asana:createTask(taskName, workspaceName) -> Self

Check failure on line 123 in Source/Asana.spoon/init.lua

View workflow job for this annotation

GitHub Actions / build

Docstring signature/parameter mismatch

SIGNATURE/PARAMETER COUNT MISMATCH: 'Asana:createTask(taskName, workspaceName) ' says 2 parameters ('taskName, workspaceName'), but Parameters section has 3 entries:
--- Method
--- Creates a new task named `taskName` in the workspace `workspaceName`.
---

Check failure on line 126 in Source/Asana.spoon/init.lua

View workflow job for this annotation

GitHub Actions / build

Blank lines should not occur within sections

Asana:createTask(taskName, workspaceName) -> Self has a blank line in Parameters:
--- Parameters:
--- * taskName (String) - The title of the Asana task.
--- * WorkspaceName (String) - The name of the workspace in which to create the task.
--- * taskParameters (Table) - This is what will be sent as the `data` field of the POST body.
---
--- taskParmeters can be defined in the `configConsts.lua` or be rendered direcly in the
--- hammerspoon `init.lua` file where the task is registered.
---
--- Returns:
--- * Self
Expand All @@ -110,26 +139,33 @@
--- ```
--- spoon.Asana.createTask("Do that thing I forgot about", "My Company Workspace")
--- ```
function createTask(self, taskName, workspaceName)
function createTask(self, taskName, workspaceName, taskParameters)
if workspaceIds == {} or userId == "" then fetchRequiredIds() end

log = hs.logger.new('Asana', 'info')

local endPoint = baseUrl .. "/tasks"
local body = getTaskBody({
workspace = workspaceIds[workspaceName], name = taskName
}, taskParameters)

-- Add content type so the server will know how to read the body
reqHeader = tableMerge(reqHeader, {['Content-Type'] = "application/json; charset=UTF8"})

hs.http.asyncPost(
stringFormat(
"%s/tasks?assignee=%i&workspace=%i&name=%s",
baseUrl,
userId,
workspaceIds[workspaceName],
hs.http.encodeForQuery(taskName)
),
"", -- requires empty body
reqHeader,
function(code)
if code == 201 then
endPoint,
body,
reqHeader,
function(code, responseBody)
if code == 201 then
hs.notify.show("Asana", "", "New task added to workspace: " .. workspaceName)
else
hs.notify.show("Asana", "", "Error adding task")
end
end
else
local errorStr = stringFormat("%i: Error adding task.", code)
hs.notify.show("Asana", "", errorStr)
log.e(errorStr)
log.e("responseBody = " .. responseBody)
end
end
)
return self
end
Expand Down
Loading