Skip to content

Instantly share code, notes, and snippets.

@sagarchauhan005
Created March 23, 2024 07:04
Show Gist options
  • Save sagarchauhan005/92d158c2009e1c87f74fa03455bf7af2 to your computer and use it in GitHub Desktop.
Save sagarchauhan005/92d158c2009e1c87f74fa03455bf7af2 to your computer and use it in GitHub Desktop.
Asana Ticketing Bot
const ASANA_ACCESS_TOKEN = ''; // Asana Access Token
const ASSIGN_TO_USER_ID = ''; // Default User ID to assign unassigned tasks to
const SECTION_ID=''; // Section where Tasks/Tickets land initially
const TARGET_SECTION_GID = ''; // Section where Tasks/Tickets shall be moved to
function updateUnassignedTasksWithUniqueId() {
// Step 1: Fetch unassigned tasks in this section
const sectionOptions = {
method: 'get',
headers: {
'Authorization': `Bearer ${ASANA_ACCESS_TOKEN}`
},
'muteHttpExceptions': true
};
const tasksUrl = `https://app.asana.com/api/1.0/sections/${SECTION_ID}/tasks?opt_expand=assignee,name`;
let tasksResponse = UrlFetchApp.fetch(tasksUrl, sectionOptions);
let tasksResult = JSON.parse(tasksResponse.getContentText());
let tasks = tasksResult.data;
// Step 2 : Loop through all the tasks
tasks.forEach(function(task) {
const hasTicketIdInTitle = task.name.startsWith('#');
const hasAssignee = task.assignee !== null;
const uniqueId = generateShortUUID();
let updateRequired = false;
let updatedTitle = task.name;
let payload = {
"data": {
}
};
if (!hasTicketIdInTitle) {
updatedTitle = `#${uniqueId} : ${task.name}`;
payload.data.name = updatedTitle;
updateRequired = true;
}
if (!hasAssignee) {
payload.data.assignee = ASSIGN_TO_USER_ID;
updateRequired = true;
}
if (updateRequired) {
console.log("Update is required");
console.log("Updated Title", updatedTitle);
// Step 3 : Update the task with a ticket and default user
let updateUrl = `https://app.asana.com/api/1.0/tasks/${task.gid}`;
let updateOptions = {
method: 'put',
headers: {
'Authorization': `Bearer ${ASANA_ACCESS_TOKEN}`,
'Content-Type': 'application/json'
},
payload: JSON.stringify(payload),
'muteHttpExceptions': true
};
UrlFetchApp.fetch(updateUrl, updateOptions);
// Move the task to the target section
moveTaskToSection(task.gid);
}else{
console.log("No update was made");
}
});
}
// Function to move a task to a specified section
function moveTaskToSection(taskGid) {
const moveUrl = `https://app.asana.com/api/1.0/sections/${TARGET_SECTION_GID}/addTask`;
const moveOptions = {
method: 'post',
headers: {
'Authorization': `Bearer ${ASANA_ACCESS_TOKEN}`,
'Content-Type': 'application/json'
},
payload: JSON.stringify({
"data": {
"task": taskGid
}
}),
'muteHttpExceptions': true
};
UrlFetchApp.fetch(moveUrl, moveOptions);
}
// Function to generate a short UUID
function generateShortUUID() {
// Use the current time to ensure uniqueness across different script executions
let now = new Date().getTime();
// Generate a random number based on the current time and take the last 6 digits
let uniqueId = ('000000' + (now % 1000000 + Math.floor(Math.random() * 1000)).toString()).slice(-7);
return uniqueId;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment