11const fs = require ( 'fs' ) ;
22const path = require ( 'path' ) ;
3- const { Octokit } = require ( '@octokit/rest' ) ;
4- const octokit = new Octokit ( { auth : process . env . GH_PR_TOKEN } ) ;
53const surge = require ( 'surge' ) ;
64const publishFn = surge ( ) . publish ( ) ;
75
8- // From github actions
9- const ghrepo = process . env . GITHUB_REPOSITORY || '' ;
6+ ( async ( ) => {
7+ const { Octokit } = await import ( '@octokit/rest' ) ;
8+ const octokit = new Octokit ( { auth : process . env . GH_PR_TOKEN } ) ;
109
11- const owner = process . env . CIRCLE_PROJECT_USERNAME || ghrepo . split ( '/' ) [ 0 ] ; // patternfly
12- const repo = process . env . CIRCLE_PROJECT_REPONAME || ghrepo . split ( '/' ) [ 1 ] ;
13- const prnum = process . env . CIRCLE_PR_NUMBER || process . env . GH_PR_NUM ;
14- const prbranch = process . env . CIRCLE_BRANCH || process . env . GITHUB_REF . split ( '/' ) . pop ( ) ;
10+ // From github actions
11+ const ghrepo = process . env . GITHUB_REPOSITORY || '' ;
1512
16- const uploadFolder = process . argv [ 2 ] ;
17- if ( ! uploadFolder ) {
18- console . log ( 'Usage: upload-preview uploadFolder' ) ;
19- process . exit ( 1 ) ;
20- }
13+ const owner = process . env . CIRCLE_PROJECT_USERNAME || ghrepo . split ( '/' ) [ 0 ] ; // patternfly
14+ const repo = process . env . CIRCLE_PROJECT_REPONAME || ghrepo . split ( '/' ) [ 1 ] ;
15+ const prnum = process . env . CIRCLE_PR_NUMBER || process . env . GH_PR_NUM ;
16+ const prbranch = process . env . CIRCLE_BRANCH || process . env . GITHUB_REF . split ( '/' ) . pop ( ) ;
2117
22- const uploadFolderName = path . basename ( uploadFolder ) ;
23- let uploadURL = `${ repo } -${ prnum ? `pr-component-groups-${ prnum } ` : prbranch } ` . replace ( / [ \/ | \. ] / g, '-' ) ;
18+ const uploadFolder = process . argv [ 2 ] ;
19+ if ( ! uploadFolder ) {
20+ console . log ( 'Usage: upload-preview uploadFolder' ) ;
21+ process . exit ( 1 ) ;
22+ }
2423
25- switch ( uploadFolderName ) {
26- case 'coverage' :
27- uploadURL += '-a11y.surge.sh' ;
28- break ;
29- case 'public' :
30- if ( ! prnum && prbranch === 'main' ) {
31- uploadURL = 'https://pf-extensions.surge.sh/' ;
32- }
33- else {
24+ const uploadFolderName = path . basename ( uploadFolder ) ;
25+ let uploadURL = `${ repo } -${ prnum ? `pr-component-groups-${ prnum } ` : prbranch } ` . replace ( / [ \/ | \. ] / g, '-' ) ;
26+
27+ switch ( uploadFolderName ) {
28+ case 'coverage' :
29+ uploadURL += '-a11y.surge.sh' ;
30+ break ;
31+ case 'public' :
32+ if ( ! prnum && prbranch === 'main' ) {
33+ uploadURL = 'https://pf-extensions.surge.sh/' ;
34+ } else {
35+ uploadURL += '.surge.sh' ;
36+ }
37+ break ;
38+ default :
39+ uploadURL += `-${ uploadFolderName } ` ;
3440 uploadURL += '.surge.sh' ;
35- }
36- break ;
37- default :
38- uploadURL += `-${ uploadFolderName } ` ;
39- uploadURL += '.surge.sh' ;
40- break ;
41- }
41+ break ;
42+ }
4243
43- publishFn ( {
44- project : uploadFolder ,
45- p : uploadFolder ,
46- domain : uploadURL ,
47- d : uploadURL ,
48- e : 'https://surge.surge.sh' ,
49- endpoint : 'https://surge.surge.sh'
50- } ) ;
44+ publishFn ( {
45+ project : uploadFolder ,
46+ p : uploadFolder ,
47+ domain : uploadURL ,
48+ d : uploadURL ,
49+ e : 'https://surge.surge.sh' ,
50+ endpoint : 'https://surge.surge.sh'
51+ } ) ;
5152
52- function tryAddComment ( comment , commentBody ) {
53- if ( ! commentBody . includes ( comment ) ) {
54- return comment ;
53+ function tryAddComment ( comment , commentBody ) {
54+ if ( ! commentBody . includes ( comment ) ) {
55+ return comment ;
56+ }
57+ return '' ;
5558 }
56- return '' ;
57- }
5859
59- if ( prnum ) {
60- octokit . issues . listComments ( {
61- owner,
62- repo,
63- issue_number : prnum
64- } )
65- . then ( res => res . data )
66- . then ( comments => {
67- let commentBody = '' ;
68- const existingComment = comments . find ( comment => comment . user . login === 'patternfly-build' ) ;
69- if ( existingComment ) {
70- commentBody += existingComment . body . trim ( ) ;
71- commentBody += '\n\n' ;
72- }
60+ if ( prnum ) {
61+ const comments = await octokit . issues . listComments ( {
62+ owner,
63+ repo,
64+ issue_number : prnum
65+ } ) . then ( res => res . data ) ;
7366
74- if ( uploadFolderName === 'public' ) {
75- commentBody += tryAddComment ( `Preview: https:// ${ uploadURL } ` , commentBody ) ;
76- }
77- else if ( uploadFolderName === 'coverage' ) {
78- commentBody += tryAddComment ( `A11y report: https:// ${ uploadURL } ` , commentBody ) ;
79- }
67+ let commentBody = '' ;
68+ const existingComment = comments . find ( comment => comment . user . login === 'patternfly-build' ) ;
69+ if ( existingComment ) {
70+ commentBody += existingComment . body . trim ( ) ;
71+ commentBody += '\n\n' ;
72+ }
8073
81- if ( existingComment ) {
82- octokit . issues . updateComment ( {
83- owner,
84- repo,
85- comment_id : existingComment . id ,
86- body : commentBody
87- } ) . then ( ( ) => console . log ( 'Updated comment!' ) ) ;
88- } else {
89- octokit . issues . createComment ( {
90- owner,
91- repo,
92- issue_number : prnum ,
93- body : commentBody
94- } ) . then ( ( ) => console . log ( 'Created comment!' ) ) ;
95- }
96- } ) ;
97- }
74+ if ( uploadFolderName === 'public' ) {
75+ commentBody += tryAddComment ( `Preview: https://${ uploadURL } ` , commentBody ) ;
76+ } else if ( uploadFolderName === 'coverage' ) {
77+ commentBody += tryAddComment ( `A11y report: https://${ uploadURL } ` , commentBody ) ;
78+ }
79+
80+ if ( existingComment ) {
81+ await octokit . issues . updateComment ( {
82+ owner,
83+ repo,
84+ comment_id : existingComment . id ,
85+ body : commentBody
86+ } ) ;
87+ console . log ( 'Updated comment!' ) ;
88+ } else {
89+ await octokit . issues . createComment ( {
90+ owner,
91+ repo,
92+ issue_number : prnum ,
93+ body : commentBody
94+ } ) ;
95+ console . log ( 'Created comment!' ) ;
96+ }
97+ }
98+ } ) ( ) ;
0 commit comments