@@ -38,39 +38,63 @@ async function asanaOperations(
38
38
} ) ;
39
39
core . info ( 'Added the pull request link to the Asana task.' ) ;
40
40
}
41
- } catch ( ex ) {
42
- console . error ( ex . value ) ;
41
+ } catch ( error ) {
42
+ core . error ( error . value ) ;
43
43
}
44
44
}
45
45
46
- try {
47
- const ASANA_PAT = core . getInput ( 'asana-pat' ) ,
48
- TARGETS = core . getInput ( 'targets' ) ,
49
- TRIGGER_PHRASE = core . getInput ( 'trigger-phrase' ) ,
50
- TASK_COMMENT = core . getInput ( 'task-comment ' ) ,
51
- PULL_REQUEST = github . context . payload . pull_request ,
52
- REGEX_STRING = `\\*\\* ${ TRIGGER_PHRASE } \\*\\* \\[(.*?)\\]\\(https:\\/\\/app.asana.com\\/(\\d+)\\/(?<project>\\d+)\\/(?< task>\\d+).*?\\)` ,
53
- REGEX = new RegExp ( REGEX_STRING , 'g' ) ;
54
- core . info ( `Regex: ${ REGEX_STRING } ` ) ;
55
- core . info ( `pr body: ${ PULL_REQUEST . body } ` ) ;
56
- let taskComment = null ,
57
- targets = TARGETS ? JSON . parse ( TARGETS ) : [ ] ,
58
- parseAsanaURL = null ;
46
+ async function run ( ) {
47
+ try {
48
+ const GITHUB_TOKEN = core . getInput ( 'github-token' , { required : true } ) ;
49
+ ASANA_PAT = core . getInput ( 'asana-pat' , { required : true } ) ,
50
+ TARGETS = core . getInput ( 'targets ' ) ,
51
+ TRIGGER_PHRASE = core . getInput ( 'trigger-phrase' ) ,
52
+ TASK_COMMENT = core . getInput ( ' task-comment' ) ,
53
+ PULL_REQUEST = github . context . payload . pull_request ,
54
+ REGEX_STRING = ` ${ TRIGGER_PHRASE } (?:\s*)https:\\/\\/app.asana.com\\/(\\d+)\\/(?<project>\\d+)\\/(?<task>\\d+)` ,
55
+ REGEX = new RegExp ( REGEX_STRING , 'g' ) ,
56
+ // all inputs are strings
57
+ LINK_REQUIRED = core . getInput ( 'link-required' ) === 'true'
58
+ ;
59
59
60
- if ( ! ASANA_PAT ) {
61
- throw ( { message : 'ASANA PAT Not Found!' } ) ;
62
- }
63
- if ( TASK_COMMENT ) {
64
- taskComment = ` ${ TASK_COMMENT } ${ PULL_REQUEST . html_url } ` ;
65
- }
66
- while ( ( parseAsanaURL = REGEX . exec ( PULL_REQUEST . body ) ) !== null ) {
67
- let taskId = parseAsanaURL . groups . task ;
68
- if ( taskId ) {
69
- asanaOperations ( ASANA_PAT , targets , taskId , taskComment ) ;
70
- } else {
71
- core . info ( `Invalid Asana task URL after the trigger phrase ${ TRIGGER_PHRASE } ` ) ;
60
+ const octokit = new github . GitHub ( GITHUB_TOKEN ) ;
61
+
62
+ core . debug ( `Regex: ${ REGEX_STRING } ` ) ;
63
+ // contains the markdown version of the PR
64
+ core . debug ( `pr body: ${ PULL_REQUEST . body } ` ) ;
65
+
66
+ let taskComment = null ,
67
+ targets = TARGETS ? JSON . parse ( TARGETS ) : [ ] ,
68
+ parseAsanaURL = null ;
69
+
70
+ if ( TASK_COMMENT ) {
71
+ taskComment = ` ${ TASK_COMMENT } ${ PULL_REQUEST . html_url } ` ;
72
72
}
73
+
74
+ let foundAsanaTasks = [ ] ;
75
+ while ( ( parseAsanaURL = REGEX . exec ( PULL_REQUEST . body ) ) !== null ) {
76
+ let taskId = parseAsanaURL . groups . task ;
77
+ if ( taskId ) {
78
+ foundAsanaLink = true ;
79
+ asanaOperations ( ASANA_PAT , targets , taskId , taskComment ) ;
80
+ foundAsanaTasks . push ( taskId ) ;
81
+ } else {
82
+ core . error ( `Invalid Asana task URL after the trigger phrase ${ TRIGGER_PHRASE } ` ) ;
83
+ }
84
+ }
85
+
86
+ const statusState = ( ! LINK_REQUIRED || foundAsanaTasks . length > 0 ) ? 'success' : 'error' ;
87
+ core . info ( `setting ${ statusState } for ${ github . context . payload . pull_request . head . sha } ` ) ;
88
+ octokit . repos . createStatus ( {
89
+ ...github . context . repo ,
90
+ 'context' : 'asana-link-presence' ,
91
+ 'state' : statusState ,
92
+ 'description' : 'asana link not found' ,
93
+ 'sha' : github . context . payload . pull_request . head . sha ,
94
+ } ) ;
95
+ } catch ( error ) {
96
+ core . setFailed ( error . message ) ;
73
97
}
74
- } catch ( error ) {
75
- core . error ( error . message ) ;
76
98
}
99
+
100
+ run ( )
0 commit comments