@@ -4,30 +4,34 @@ const asana = require('asana');
4
4
5
5
async function asanaOperations (
6
6
asanaPAT ,
7
- projectId ,
7
+ targets ,
8
8
taskId ,
9
- sectionName ,
10
9
taskComment
11
10
) {
12
11
try {
13
12
const client = asana . Client . create ( {
14
13
defaultHeaders : { 'asana-enable' : 'new-sections,string_ids' } ,
15
14
logAsanaChangeWarnings : false
16
15
} ) . useAccessToken ( asanaPAT ) ;
17
- if ( sectionName ) {
18
- let project = await client . sections . findByProject ( projectId ) ;
19
- if ( project ) {
20
- let requiredSection = project . find ( data => data . name === sectionName ) ;
21
- if ( requiredSection ) {
22
- await client . sections . addTask ( requiredSection . gid , { task : taskId } ) ;
23
- core . info ( 'Moved to: ' + requiredSection . name ) ;
16
+
17
+ const task = await client . tasks . findById ( taskId ) ;
18
+
19
+ targets . forEach ( async target => {
20
+ let targetProject = task . projects . find ( project => project . name === target . project ) ;
21
+ if ( targetProject ) {
22
+ let targetSection = await client . sections . findByProject ( targetProject . gid )
23
+ . then ( sections => sections . find ( section => section . name === target . section ) ) ;
24
+ if ( targetSection ) {
25
+ await client . sections . addTask ( targetSection . gid , { task : taskId } ) ;
26
+ core . info ( `Moved to: ${ target . project } /${ target . section } ` ) ;
24
27
} else {
25
- core . error ( ' Asana section ' + sectionName + ' not found.' ) ;
28
+ core . error ( ` Asana section ${ target . section } not found.` ) ;
26
29
}
27
30
} else {
28
- core . error ( 'Asana project with id ' + projectId + ' not found.' ) ;
31
+ core . info ( `This task does not exist in " ${ target . project } " project` ) ;
29
32
}
30
- }
33
+ } ) ;
34
+
31
35
if ( taskComment ) {
32
36
await client . tasks . addComment ( taskId , {
33
37
text : taskComment
@@ -41,7 +45,7 @@ async function asanaOperations(
41
45
42
46
try {
43
47
const ASANA_PAT = core . getInput ( 'asana-pat' ) ,
44
- SECTION_NAME = core . getInput ( 'target-section ' ) ,
48
+ TARGETS = core . getInput ( 'targets ' ) ,
45
49
TRIGGER_PHRASE = core . getInput ( 'trigger-phrase' ) ,
46
50
TASK_COMMENT = core . getInput ( 'task-comment' ) ,
47
51
PULL_REQUEST = github . context . payload . pull_request ,
@@ -50,21 +54,21 @@ try {
50
54
'g'
51
55
) ;
52
56
let taskComment = null ,
57
+ targets = TARGETS ? JSON . parse ( TARGETS ) : [ ] ,
53
58
parseAsanaURL = null ;
54
59
55
60
if ( ! ASANA_PAT ) {
56
- throw ( { message : " ASANA PAT Not Found!" } ) ;
61
+ throw ( { message : ' ASANA PAT Not Found!' } ) ;
57
62
}
58
63
if ( TASK_COMMENT ) {
59
64
taskComment = `${ TASK_COMMENT } ${ PULL_REQUEST . html_url } ` ;
60
65
}
61
66
while ( ( parseAsanaURL = REGEX . exec ( PULL_REQUEST . body ) ) !== null ) {
62
- let projectId = parseAsanaURL . groups . project ,
63
- taskId = parseAsanaURL . groups . task ;
64
- if ( projectId && taskId ) {
65
- asanaOperations ( ASANA_PAT , projectId , taskId , SECTION_NAME , taskComment ) ;
67
+ let taskId = parseAsanaURL . groups . task ;
68
+ if ( taskId ) {
69
+ asanaOperations ( ASANA_PAT , targets , taskId , taskComment ) ;
66
70
} else {
67
- core . info ( ' Invalid Asana task URL after the trigger phrase' + TRIGGER_PHRASE ) ;
71
+ core . info ( ` Invalid Asana task URL after the trigger phrase ${ TRIGGER_PHRASE } ` ) ;
68
72
}
69
73
}
70
74
} catch ( error ) {
0 commit comments