11module . exports = async ( { github, context, environment, project, infra} ) => {
2- const localRun = context . payload . act ;
3- const isLocalRun = ( localRun !== undefined ) ? localRun : false ;
2+ const isLocalRun = ciLocalRun ( context ) ;
43
5- const actor = context . actor ;
6- const username = ( actor !== undefined && actor !== "" ) ? actor : 'Unknown' ;
4+ const username = getUsername ( context ) ;
5+ const prNumber = context . payload . issue . number ;
6+ const message = generatePrCommentMsg ( username , environment , project , infra ) ;
77
8- const prNumber = context . payload . issue . number ;
9-
10- let message = `🚀 Deployment action request received from user: ${ username } \n` ;
11- if ( project ) {
12- message += `- Project: \`${ project } \`\n` ;
13- }
14- if ( environment ) {
15- message += `- Environment: \`${ environment } \`\n` ;
16- }
17- if ( infra ) {
18- message += `- Infrastructure: \`${ infra } \`\n` ;
8+ if ( isLocalRun ) {
9+ logMessageOnLocalEnv ( message ) ;
10+ return { comment_id : 10 , message : message } // arbitraty mocked comment number;
11+ } else {
12+ try {
13+ const comment = createPrComment ( github , context , prNumber , message ) ;
14+ return { comment_id : comment . data . id , message : message } ;
15+ } catch ( ex ) {
16+ console . log ( "Failed to POST the comment on the PR to notify the user due to =[> " + ex + "]" ) ;
17+ return { comment_id : null } ;
1918 }
19+ }
20+ }
2021
21- if ( isLocalRun ) {
22- console . log ( `Action is being runned locally by 'ACT'.
23- Skipping the REST request to post a message for notify the user on PR, but output would have been:
24- ${ message } ` ) ;
25- return { comment_id : 10 , message : message } // arbitraty mocked comment number;
26- } else {
27- let comment = { } ;
28- try {
29- comment = await github . rest . issues . createComment ( {
30- owner : context . repo . owner ,
31- repo : context . repo . repo ,
32- issue_number : prNumber ,
33- body : message ,
34- } ) ;
35- return { comment_id : comment . data . id , message : message } ;
36- } catch ( ex ) {
37- console . log ( "Failed to POST the comment on the PR to notify the user due to =[> " + ex + "]" ) ;
38- return { comment_id : null } ;
39- }
40- }
22+ function ciLocalRun ( context ) {
23+ const localRun = context . payload . act ;
24+ return ( localRun !== undefined ) ? localRun : false ;
25+ }
26+
27+ function getUsername ( context ) {
28+ const actor = context . actor ;
29+ return ( actor !== undefined && actor !== "" ) ? actor : 'Unknown' ;
30+ // TODO: throw ex when undefined or non valid actor
31+ }
32+
33+ function generatePrCommentMsg ( username , environment , project , infra ) {
34+ let message = `🚀 Deployment action request received from user: ${ username } \n` ;
35+ if ( project ) {
36+ message += `- Project: \`${ project } \`\n` ;
37+ }
38+ if ( environment ) {
39+ message += `- Environment: \`${ environment } \`\n` ;
40+ }
41+ if ( infra ) {
42+ message += `- Infrastructure: \`${ infra } \`\n` ;
43+ }
44+
45+ return message ;
46+ }
47+
48+ async function createPrComment ( github , context , prNumber , message ) {
49+ return await github . rest . issues . createComment ( {
50+ owner : context . repo . owner ,
51+ repo : context . repo . repo ,
52+ issue_number : prNumber ,
53+ body : message ,
54+ } ) ;
55+ }
56+
57+ function logMessageOnLocalEnv ( message ) {
58+ console . log ( `Action is being runned locally by 'ACT'.
59+ Skipping the REST request to post a message for notify the user on PR, but output would have been:
60+ ${ message } ` ) ;
4161}
0 commit comments