Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
michelle0927 committed Sep 16, 2020
1 parent 9bed893 commit 7f18600
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 91 deletions.
191 changes: 126 additions & 65 deletions components/asana/asana.app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const axios = require("axios");
const crypto = require("crypto");

module.exports = {
type: "app",
Expand All @@ -10,7 +11,7 @@ module.exports = {
optional: false,
async options(opts) {
const workspaces = await this.getWorkspaces();
return workspaces.data.data.map((workspace) => {
return workspaces.map((workspace) => {
return {
label: workspace.name,
value: workspace.gid,
Expand All @@ -24,7 +25,7 @@ module.exports = {
optional: false,
async options(opts) {
const projects = await this.getProjects(opts.workspaceId);
return projects.data.data.map((project) => {
return projects.map((project) => {
return {
label: project.name,
value: project.gid,
Expand All @@ -37,13 +38,33 @@ module.exports = {
label: "Tasks",
async options(opts) {
const tasks = await this.getTasks(opts.projectId);
return tasks.data.data.map((task) => {
return tasks.map((task) => {
return { label: task.name, value: task.gid };
});
},
},
organizationId: {
type: "string",
label: "Organization",
optional: false,
async options(opts) {
const organizations = await this.getOrganizations();
return organizations.map((organization) => {
return {
label: organization.name,
value: organization.gid,
};
});
},
},
},
methods: {
async _getHeaders() {
return {
Accept: "application/json",
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
};
},
async _getAuthorizationHeader({ data, method, url, headers }) {
const token = {
key: this.$auth.oauth_access_token,
Expand Down Expand Up @@ -86,105 +107,145 @@ module.exports = {
const config = {
method: "delete",
url: `https://app.asana.com/api/1.0/webhooks/${hookId}`,
headers: {
Accept: "application/json",
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
headers: await this._getHeaders(),
};
try {
await axios(config);
} catch (err) {
console.log(err);
}
},
async verifyAsanaWebhookRequest(request) {
let secret = this.$auth.oauth_refresh_token;
var base64Digest = function (s) {
return crypto.createHmac("sha1", secret).update(s).digest("base64");
};
var content = JSON.stringify(request.body);
var doubleHash = base64Digest(content);
var headerHash = request.headers["x-hook-secret"];
return doubleHash == headerHash;
},
async getWorkspace(workspaceId) {
return await axios.get(`https://app.asana.com/api/1.0/workspaces/${workspaceId}`, {
headers: {
Accept: "application/json",
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
});
const workspace = await axios.get(
`https://app.asana.com/api/1.0/workspaces/${workspaceId}`,
{
headers: await this._getHeaders(),
}
);
return workspace.data.data;
},
async getWorkspaces() {
return await axios.get(`https://app.asana.com/api/1.0/workspaces/`, {
headers: {
Accept: "application/json",
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
});
const workspaces = await axios.get(
`https://app.asana.com/api/1.0/workspaces/`,
{
headers: await this._getHeaders(),
}
);
return workspaces.data.data;
},
async getOrganizations() {
let organizations = [];
const workspaces = await this.getWorkspaces();
for (const workspace of workspaces) {
let w = await this.getWorkspace(workspace.gid);
if (w.is_organization) organizations.push(w);
}
return organizations;
},
async getProject(projectId) {
return await axios.get(
const project = await axios.get(
`https://app.asana.com/api/1.0/projects/${projectId}`,
{
headers: {
Accept: "application/json",
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
headers: await this._getHeaders(),
}
);
return project.data.data;
},
async getProjects(workspaceId) {
return await axios.get(
const projects = await axios.get(
`https://app.asana.com/api/1.0/projects?workspace=${workspaceId}`,
{
headers: {
Accept: "application/json",
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
headers: await this._getHeaders(),
}
);
return projects.data.data;
},
async getStory(storyId) {
return await axios.get(`https://app.asana.com/api/1.0/stories/${storyId}`, {
headers: {
Accept: "application/json",
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
});
const story = await axios.get(
`https://app.asana.com/api/1.0/stories/${storyId}`,
{
headers: await this._getHeaders(),
}
);
return story.data.data;
},
async getTask(taskId) {
return await axios.get(`https://app.asana.com/api/1.0/tasks/${taskId}`, {
headers: {
Accept: "application/json",
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
});
const task = await axios.get(
`https://app.asana.com/api/1.0/tasks/${taskId}`,
{
headers: await this._getHeaders(),
}
);
return task.data.data;
},
async getTasks(projectId) {
return await axios.get(
let incompleteTasks = [];
const tasks = await axios.get(
`https://app.asana.com/api/1.0/projects/${projectId}/tasks`,
{
headers: {
Accept: "application/json",
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
headers: await this._getHeaders(),
}
);
for (const task of tasks.data.data) {
let t = await this.getTask(task.gid);
if (t.completed == false) incompleteTasks.push(task);
}
return incompleteTasks;
},
async getTag(tagId) {
return await axios.get(`https://app.asana.com/api/1.0/tags/${tagId}`, {
headers: {
Accept: "application/json",
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
});
const tag = await axios.get(
`https://app.asana.com/api/1.0/tags/${tagId}`,
{
headers: await this._getHeaders(),
}
);
return tag.data.data;
},
async getTeam(teamId) {
const team = await axios.get(
`https://app.asana.com/api/1.0/teams/${teamId}`,
{
headers: await this._getHeaders(),
}
);
return team.data.data;
},
async getTeams(organizationId) {
const teams = await axios.get(
`https://app.asana.com/api/1.0/organizations/${organizationId}/teams`,
{
headers: await this._getHeaders(),
}
);
return teams.data.data;
},
async getUser(userId) {
return await axios.get(`https://app.asana.com/api/1.0/users/${userId}`, {
headers: {
Accept: "application/json",
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
});
const user = await axios.get(
`https://app.asana.com/api/1.0/users/${userId}`,
{
headers: await this._getHeaders(),
}
);
return user.data.data;
},
async getUsers(workspaceId) {
return await axios.get(`https://app.asana.com/api/1.0/workspaces/${workspaceId}/users`, {
headers: {
Accept: "application/json",
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
});
const users = await axios.get(
`https://app.asana.com/api/1.0/workspaces/${workspaceId}/users`,
{
headers: await this._getHeaders(),
}
);
return users.data.data;
},
},
};
};
8 changes: 5 additions & 3 deletions components/asana/completed-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ module.exports = {
};
const resp = await this.asana.createHook(body);
this.db.set("hookId", resp.data.gid);
this.db.set("projectId", this.projectId);
},
async deactivate() {
console.log(this.db.get("hookId"));
Expand All @@ -46,6 +45,10 @@ module.exports = {
},

async run(event) {
// validate signature
if (!this.asana.verifyAsanaWebhookRequest(event))
return;

this.http.respond({
status: 200,
headers: {
Expand All @@ -61,8 +64,7 @@ module.exports = {
let tasks = [];

for (const e of body.events) {
let task = await this.asana.getTask(e.resource.gid);
tasks.push(task.data.data);
tasks.push(await this.asana.getTask(e.resource.gid));
}

for (const task of tasks) {
Expand Down
8 changes: 5 additions & 3 deletions components/asana/new-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ module.exports = {
};
const resp = await this.asana.createHook(body);
this.db.set("hookId", resp.data.gid);
this.db.set("workspaceId", this.workspaceId);
},
async deactivate() {
console.log(this.db.get("hookId"));
Expand All @@ -38,6 +37,10 @@ module.exports = {
},

async run(event) {
// validate signature
if (!this.asana.verifyAsanaWebhookRequest(event))
return;

this.http.respond({
status: 200,
headers: {
Expand All @@ -53,8 +56,7 @@ module.exports = {
let projects = [];

for (const e of body.events) {
let project = await this.asana.getProject(e.resource.gid);
projects.push(project.data.data);
projects.push(await this.asana.getProject(e.resource.gid));
}

for (const project of projects) {
Expand Down
8 changes: 5 additions & 3 deletions components/asana/new-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ module.exports = {
};
const resp = await this.asana.createHook(body);
this.db.set("hookId", resp.data.gid);
this.db.set("projectId", this.projectId);
},
async deactivate() {
console.log(this.db.get("hookId"));
Expand All @@ -45,6 +44,10 @@ module.exports = {
},

async run(event) {
// validate signature
if (!this.asana.verifyAsanaWebhookRequest(event))
return;

this.http.respond({
status: 200,
headers: {
Expand All @@ -60,8 +63,7 @@ module.exports = {
let stories = [];

for (const e of body.events) {
let story = await this.asana.getStory(e.resource.gid);
stories.push(story.data.data);
stories.push(await this.asana.getStory(e.resource.gid));;
}

for (const story of stories) {
Expand Down
7 changes: 5 additions & 2 deletions components/asana/new-subtask.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ module.exports = {
},

async run(event) {
// validate signature
if (!this.asana.verifyAsanaWebhookRequest(event))
return;

this.http.respond({
status: 200,
headers: {
Expand All @@ -66,8 +70,7 @@ module.exports = {

for (const e of body.events) {
if (e.parent.resource_type == "task" && (!taskIds || (taskIds.length < 0) || (Object.keys(taskIds).length === 0) || (taskIds && taskIds.includes(e.parent.gid)))) {
let task = await this.asana.getTask(e.resource.gid);
tasks.push(task.data.data);
tasks.push(await this.asana.getTask(e.resource.gid));
}
}

Expand Down
Loading

0 comments on commit 7f18600

Please sign in to comment.