2019-06-23 03:35:23 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2020-09-08 05:00:54 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2020-09-08 05:00:54 -07:00
|
|
|
import {
|
|
|
|
IDataObject,
|
2020-09-16 09:20:27 -07:00
|
|
|
INodePropertyOptions,
|
2020-09-08 05:00:54 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
get,
|
|
|
|
} from 'lodash';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to Asana
|
|
|
|
*
|
|
|
|
* @param {IHookFunctions} this
|
|
|
|
* @param {string} method
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} body
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
2020-09-08 05:00:54 -07:00
|
|
|
export async function asanaApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: object, query?: object, uri?: string | undefined): Promise<any> { // tslint:disable-line:no-any
|
2020-09-16 09:20:27 -07:00
|
|
|
const authenticationMethod = this.getNodeParameter('authentication', 0);
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
2020-09-16 09:20:27 -07:00
|
|
|
headers: {},
|
2019-06-23 03:35:23 -07:00
|
|
|
method,
|
|
|
|
body: { data: body },
|
|
|
|
qs: query,
|
2020-09-08 05:00:54 -07:00
|
|
|
uri: uri || `https://app.asana.com/api/1.0${endpoint}`,
|
2019-06-23 03:35:23 -07:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2020-09-16 09:20:27 -07:00
|
|
|
if (authenticationMethod === 'accessToken') {
|
|
|
|
const credentials = this.getCredentials('asanaApi');
|
|
|
|
|
|
|
|
if (credentials === undefined) {
|
|
|
|
throw new Error('No credentials got returned!');
|
|
|
|
}
|
|
|
|
|
|
|
|
options.headers!['Authorization'] = `Bearer ${credentials.accessToken}`;
|
|
|
|
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} else {
|
|
|
|
//@ts-ignore
|
|
|
|
return await this.helpers.requestOAuth2.call(this, 'asanaOAuth2Api', options);
|
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (error.statusCode === 401) {
|
|
|
|
// Return a clear error
|
|
|
|
throw new Error('The Asana credentials are not valid!');
|
|
|
|
}
|
|
|
|
|
2020-09-16 09:20:27 -07:00
|
|
|
if (error.statusCode === 403) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
if (error.response && error.response.body && error.response.body.errors) {
|
|
|
|
// Try to return the error prettier
|
|
|
|
const errorMessages = error.response.body.errors.map((errorData: { message: string }) => {
|
|
|
|
return errorData.message;
|
|
|
|
});
|
|
|
|
throw new Error(`Asana error response [${error.statusCode}]: ${errorMessages.join(' | ')}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If that data does not exist for some reason return the actual error
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2020-09-08 05:00:54 -07:00
|
|
|
|
2021-01-30 09:34:49 -08:00
|
|
|
export async function asanaApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
2020-09-08 05:00:54 -07:00
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
let uri: string | undefined;
|
|
|
|
query.limit = 100;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await asanaApiRequest.call(this, method, endpoint, body, query, uri);
|
|
|
|
uri = get(responseData, 'next_page.uri');
|
|
|
|
returnData.push.apply(returnData, responseData['data']);
|
|
|
|
} while (
|
|
|
|
responseData['next_page'] !== null
|
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
2020-09-16 09:20:27 -07:00
|
|
|
|
2021-01-30 09:34:49 -08:00
|
|
|
export async function getWorkspaces(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2020-09-16 09:20:27 -07:00
|
|
|
const endpoint = '/workspaces';
|
|
|
|
const responseData = await asanaApiRequestAllItems.call(this, 'GET', endpoint, {});
|
|
|
|
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2021-01-30 09:34:49 -08:00
|
|
|
for (const workspaceData of responseData) {
|
2020-09-16 09:20:27 -07:00
|
|
|
if (workspaceData.resource_type !== 'workspace') {
|
|
|
|
// Not sure if for some reason also ever other resources
|
|
|
|
// get returned but just in case filter them out
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
returnData.push({
|
|
|
|
name: workspaceData.name,
|
|
|
|
value: workspaceData.gid,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-30 09:34:49 -08:00
|
|
|
returnData.sort((a, b) => {
|
2020-09-16 09:20:27 -07:00
|
|
|
if (a.name < b.name) { return -1; }
|
|
|
|
if (a.name > b.name) { return 1; }
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
2020-12-25 11:41:48 -08:00
|
|
|
|
|
|
|
export function getTaskFields() {
|
|
|
|
return [
|
|
|
|
'*',
|
|
|
|
'GID',
|
|
|
|
'Resource Type',
|
|
|
|
'name',
|
|
|
|
'Approval Status',
|
|
|
|
'Assignee Status',
|
|
|
|
'Completed',
|
|
|
|
'Completed At',
|
|
|
|
'Completed By',
|
|
|
|
'Created At',
|
|
|
|
'Dependencies',
|
|
|
|
'Dependents',
|
|
|
|
'Due At',
|
|
|
|
'Due On',
|
|
|
|
'External',
|
|
|
|
'HTML Notes',
|
|
|
|
'Liked',
|
|
|
|
'Likes',
|
|
|
|
'Memberships',
|
|
|
|
'Modified At',
|
|
|
|
'Notes',
|
|
|
|
'Num Likes',
|
|
|
|
'Resource Subtype',
|
|
|
|
'Start On',
|
|
|
|
'Assignee',
|
|
|
|
'Custom Fields',
|
|
|
|
'Followers',
|
|
|
|
'Parent',
|
|
|
|
'Permalink URL',
|
|
|
|
'Projects',
|
|
|
|
'Tags',
|
|
|
|
'Workspace',
|
|
|
|
];
|
|
|
|
}
|