Skip to content

Commit

Permalink
Merge pull request #3 from duckduckgo/nastia/add_tags_to_tasks
Browse files Browse the repository at this point in the history
Add ability to add tags to Asana tasks
  • Loading branch information
malmstein authored Oct 25, 2024
2 parents 32dc041 + b637fbe commit 3bd2cfc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 46 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ The Asana section ID in the Asana Project
**Required** Name of the Asana task
### `asana-task-description`
**Required** Description of the Asana task
### `asana-tag`
ID of Asana tag to be added to the task i.e. https://app.asana.com/0/1208613272217946/

#### Example Usage

Expand All @@ -275,5 +277,6 @@ jobs:
asana-section: 'Asana Section Id'
asana-task-name: 'Asana Task Name'
asana-task-description: 'Asana Task Description'
asana-tag: 'Tag Id'
action: 'create-asana-task'
```
77 changes: 31 additions & 46 deletions action.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,72 +250,57 @@ async function findTaskInSection(client, sectionId, name) {
return existingTaskId
}

async function createTask(client, name, description, projectId) {
console.log('creating new task', name);
let createdTaskId = "0"
try {
await client.tasks.createTask({name: name,
notes: description,
projects: [projectId],
pretty: true})
.then((result) => {
createdTaskId = result.gid
console.log('task created', createdTaskId);
})
} catch (error) {
console.error('rejecting promise', error);
async function createTask(client, name, description, projectId, sectionId = '', tag = '') {
const taskOpts = {
name: name,
notes: description,
projects: [projectId],
pretty: true
};

if (sectionId != '') {
console.log('checking for duplicate task before creating a new one', name);
let existingTaskId = await findTaskInSection(client, sectionId, name)
if (existingTaskId != "0") {
console.log("task already exists, skipping")
core.setOutput('taskId', existingTaskId)
core.setOutput('duplicate', true)
return existingTaskId;
}

taskOpts.memberships = [{project: projectId, section: sectionId}];
}
return createdTaskId
}

async function createTaskInSection(client, name, description, projectId, sectionId) {
console.log('creating new task in section', sectionId);
if (tag != '')
taskOpts.tags = [tag];

console.log(`creating new task with section='${sectionId}' and tag='${tag}'`);
let createdTaskId = "0"
try {
await client.tasks.createTask({name: name,
notes: description,
projects: [projectId],
memberships: [{project: projectId, section: sectionId}],
pretty: true})
await client.tasks.createTask(taskOpts)
.then((result) => {
createdTaskId = result.gid
console.log('task created in section', createdTaskId);
core.setOutput('taskId', createdTaskId)
core.setOutput('duplicate', false)
console.log('task created', createdTaskId);
core.setOutput('taskId', createdTaskId);
core.setOutput('duplicate', false);
})
} catch (error) {
console.error('rejecting promise', error);
}
return createdTaskId
}

async function createTaskIfNotDuplicate(client, name, description, projectId, sectionId) {
console.log('checking for duplicate task before creating a new one', name);
let existingTaskId = await findTaskInSection(client, sectionId, name)
if (existingTaskId == "0") {
return createTaskInSection(client, name, description, projectId, sectionId)
} else {
console.log("task already exists, skipping")
core.setOutput('taskId', existingTaskId)
core.setOutput('duplicate', true)
}
return existingTaskId
}

async function createAsanaTask(){
const client = await buildAsanaClient();

const
projectId = core.getInput('asana-project', {required: true}),
sectionId = core.getInput('asana-section'),
taskName = core.getInput('asana-task-name', {required: true}),
taskDescription = core.getInput('asana-task-description', {required: true});
taskDescription = core.getInput('asana-task-description', {required: true}),
tag = core.getInput('asana-tag');

if (sectionId === "") {
return createTask(client, taskName, taskDescription, projectId)
} else {
return createTaskIfNotDuplicate(client, taskName, taskDescription, projectId, sectionId)
}
return createTask(client, taskName, taskDescription, projectId, sectionId, tag);
}

async function addTaskPRDescription(){
Expand Down Expand Up @@ -409,4 +394,4 @@ async function action() {
module.exports = {
action,
default: action,
};
};
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ inputs:
asana-task-description:
description: 'Description of the Asana task you want to create.'
required: false
asana-tag:
description: 'Tag to be added to the Asana task.'
required: false
trigger-phrase:
description: 'Prefix used to identify Asana tasks (URL).'
required: false
Expand Down

0 comments on commit 3bd2cfc

Please sign in to comment.