@@ -12,128 +12,137 @@ dayjs.extend(customParseFormat);
12
12
dayjs . extend ( utc ) ;
13
13
dayjs . extend ( timezone ) ;
14
14
15
- // Create Octokit constructor with .paginate API and custom user agent
16
- const MyOctokit = Octokit . plugin ( paginateRest ) . defaults ( {
17
- userAgent : "gr2m-helpdesk" ,
18
- } ) ;
19
- const octokit = new MyOctokit ( {
20
- auth : process . env . GITHUB_TOKEN ,
21
- } ) ;
22
-
23
- // load open issues with the `show` label
24
- const showIssues = await octokit . paginate ( "GET /repos/{owner}/{repo}/issues" , {
25
- owner : "gr2m" ,
26
- repo : "helpdesk" ,
27
- labels : "show" ,
28
- state : "open" ,
29
- per_page : 100 ,
30
- } ) ;
31
-
32
- const currentShowIssue = showIssues . find ( ( issue ) => {
33
- const dayString = issue . body
34
- . match ( / 📅 .* / )
35
- . pop ( )
36
- . replace ( / 📅 \s * / , "" )
37
- . replace ( / ^ \w + , / , "" )
38
- . trim ( ) ;
39
- const timeString = issue . body
40
- . match ( / 🕐 [ ^ ( \r \n ] + / )
41
- . pop ( )
42
- . replace ( / 🕐 \s * / , "" )
43
- . replace ( "Pacific Time" , "" )
44
- . trim ( ) ;
45
-
46
- // workaround: cannot parse "June 3, 2021 1:00pm" but can parse "June 3, 2021 12:00pm"
47
- // workaround: cannot set default timezone, so parse the date/time string first, then use `.tz()` with the expected date/time format
48
- let timeStringWithoutAmPm = timeString . replace ( / ( a m | p m ) \b / , "" ) ;
49
-
50
- let hours = parseInt ( timeStringWithoutAmPm , 10 ) ;
51
-
52
- if ( hours < 9 ) {
53
- timeStringWithoutAmPm = timeStringWithoutAmPm . replace ( hours , hours + 12 ) ;
54
- }
15
+ if ( process . env . GITHUB_ACTIONS && process . env . NODE_ENV !== "test" ) {
16
+ // Create Octokit constructor with .paginate API and custom user agent
17
+ const MyOctokit = Octokit . plugin ( paginateRest ) . defaults ( {
18
+ userAgent : "gr2m-helpdesk" ,
19
+ } ) ;
20
+ const octokit = new MyOctokit ( {
21
+ auth : process . env . GITHUB_TOKEN ,
22
+ } ) ;
23
+ run ( process . env , core , octokit , twitterRequest ) ;
24
+ }
55
25
56
- const tmp = dayjs (
57
- [ dayString , timeStringWithoutAmPm ] . join ( " " ) ,
58
- // "MMMM D, YYYY H:mma", // see workaround
59
- "MMMM D, YYYY H:mm" ,
60
- true
26
+ export async function run ( env , core , octokit , twitterRequest ) {
27
+ // load open issues with the `show` label
28
+ const showIssues = await octokit . paginate (
29
+ "GET /repos/{owner}/{repo}/issues" ,
30
+ {
31
+ owner : "gr2m" ,
32
+ repo : "helpdesk" ,
33
+ labels : "show" ,
34
+ state : "open" ,
35
+ per_page : 100 ,
36
+ }
61
37
) ;
62
38
63
- let time = dayjs . tz ( tmp . format ( "YYYY-MM-DD HH:mm" ) , "America/Los_Angeles" ) ;
64
-
65
- const showIsWithinRange =
66
- time . subtract ( 30 , "minutes" ) > dayjs ( ) . subtract ( 25 , "minutes" ) &&
67
- time . subtract ( 30 , "minutes" ) < dayjs ( ) . add ( 25 , "minutes" ) ;
68
- return showIsWithinRange ;
69
- } ) ;
70
-
71
- if ( ! currentShowIssue ) {
72
- core . setFailed ( "No current issue found to comment on" ) ;
73
- process . exit ( 1 ) ;
74
- }
75
-
76
- const [ , , title , , guest ] = currentShowIssue . title . split ( / ( - | w i t h @ ) / g) ;
77
-
78
- const currentShow = {
79
- title,
80
- number : currentShowIssue . number ,
81
- issue : currentShowIssue ,
82
- guest,
83
- url : currentShowIssue . html_url ,
84
- } ;
85
-
86
- // add comment on issue
87
- const {
88
- data : { html_url : commentUrl } ,
89
- } = await octokit . request (
90
- "POST /repos/{owner}/{repo}/issues/{issue_number}/comments" ,
91
- {
92
- owner : "gr2m" ,
93
- repo : "helpdesk" ,
94
- issue_number : currentShow . number ,
95
- body : "Going live in 30 minutes at https://twitch.tv/gregorcodes" ,
39
+ const currentShowIssue = showIssues . find ( ( issue ) => {
40
+ const dayString = issue . body
41
+ . match ( / 📅 .* / )
42
+ . pop ( )
43
+ . replace ( / 📅 \s * / , "" )
44
+ . replace ( / ^ \w + , / , "" )
45
+ . trim ( ) ;
46
+ const timeString = issue . body
47
+ . match ( / 🕐 [ ^ ( \r \n ] + / )
48
+ . pop ( )
49
+ . replace ( / 🕐 \s * / , "" )
50
+ . replace ( "Pacific Time" , "" )
51
+ . trim ( ) ;
52
+
53
+ // workaround: cannot parse "June 3, 2021 1:00pm" but can parse "June 3, 2021 12:00pm"
54
+ // workaround: cannot set default timezone, so parse the date/time string first, then use `.tz()` with the expected date/time format
55
+ let timeStringWithoutAmPm = timeString . replace ( / ( a m | p m ) \b / , "" ) ;
56
+
57
+ let hours = parseInt ( timeStringWithoutAmPm , 10 ) ;
58
+
59
+ if ( hours < 9 ) {
60
+ timeStringWithoutAmPm = timeStringWithoutAmPm . replace ( hours , hours + 12 ) ;
61
+ }
62
+
63
+ const tmp = dayjs (
64
+ [ dayString , timeStringWithoutAmPm ] . join ( " " ) ,
65
+ // "MMMM D, YYYY H:mma", // see workaround
66
+ "MMMM D, YYYY H:mm" ,
67
+ true
68
+ ) ;
69
+
70
+ let time = dayjs . tz ( tmp . format ( "YYYY-MM-DD HH:mm" ) , "America/Los_Angeles" ) ;
71
+
72
+ const showIsWithinRange =
73
+ time . subtract ( 30 , "minutes" ) > dayjs ( ) . subtract ( 25 , "minutes" ) &&
74
+ time . subtract ( 30 , "minutes" ) < dayjs ( ) . add ( 25 , "minutes" ) ;
75
+
76
+ return showIsWithinRange ;
77
+ } ) ;
78
+
79
+ if ( ! currentShowIssue ) {
80
+ core . setFailed ( "No current issue found to comment on" ) ;
81
+ process . exit ( 1 ) ;
96
82
}
97
- ) ;
98
- console . log ( "Comment created at %s" , commentUrl ) ;
99
83
100
- // Tweet out that the show is live:
101
- const auth = {
102
- consumerKey : process . env . TWITTER_CONSUMER_KEY ,
103
- consumerSecret : process . env . TWITTER_CONSUMER_SECRET ,
104
- accessTokenKey : process . env . TWITTER_ACCESS_TOKEN_KEY ,
105
- accessTokenSecret : process . env . TWITTER_ACCESS_TOKEN_SECRET ,
106
- } ;
84
+ const [ , , title , , guest ] = currentShowIssue . title . split ( / ( - | w i t h @ ) / g) ;
85
+
86
+ const currentShow = {
87
+ title,
88
+ number : currentShowIssue . number ,
89
+ issue : currentShowIssue ,
90
+ guest,
91
+ url : currentShowIssue . html_url ,
92
+ } ;
93
+
94
+ // add comment on issue
95
+ const {
96
+ data : { html_url : commentUrl } ,
97
+ } = await octokit . request (
98
+ "POST /repos/{owner}/{repo}/issues/{issue_number}/comments" ,
99
+ {
100
+ owner : "gr2m" ,
101
+ repo : "helpdesk" ,
102
+ issue_number : currentShow . number ,
103
+ body : "Going live in 30 minutes at https://twitch.tv/gregorcodes" ,
104
+ }
105
+ ) ;
106
+ core . info ( `Comment created at ${ commentUrl } ` ) ;
107
+
108
+ // Tweet out that the show is live:
109
+ const auth = {
110
+ consumerKey : env . TWITTER_CONSUMER_KEY ,
111
+ consumerSecret : env . TWITTER_CONSUMER_SECRET ,
112
+ accessTokenKey : env . TWITTER_ACCESS_TOKEN_KEY ,
113
+ accessTokenSecret : env . TWITTER_ACCESS_TOKEN_SECRET ,
114
+ } ;
107
115
108
- const tweetText = `📯 Starting in 30 minutes
116
+ const tweetText = `📯 Starting in 30 minutes
109
117
110
118
💁🏻♂️ ${ currentShow . title }
111
119
🔴 Watch live at https://twitch.tv/gregorcodes
112
120
113
121
${ currentShow . url } `;
114
122
115
- const data = await twitterRequest ( `POST statuses/update.json` , {
116
- auth,
117
- status : tweetText ,
118
- } ) ;
119
- const tweetUrl = `https://twitter.com/gr2m/status/${ data . id_str } ` ;
120
-
121
- console . log ( "Tweeted at %s" , tweetUrl ) ;
122
-
123
- // update TODOs in issue
124
- await octokit . request ( "PATCH /repos/{owner}/{repo}/issues/{issue_number}" , {
125
- owner : "gr2m" ,
126
- repo : "helpdesk" ,
127
- issue_number : currentShow . number ,
128
- body : currentShow . issue . body
129
- . replace (
130
- / - \[ \] < ! - - t o d o : a n n o u n c e m e n t - t w e e t - - > ( [ ^ \n ] + ) / ,
131
- `- [x] <!-- todo:announcement-tweet --> $1 (${ tweetUrl } )`
132
- )
133
- . replace (
134
- / - \[ \] < ! - - t o d o : a n n o u n c e m e n t - i s s u e - c o m m e n t - - > ( [ ^ \n ] + ) / ,
135
- `- [x] <!-- todo:announcement-issue-comment --> $1 (${ commentUrl } )`
136
- ) ,
137
- } ) ;
138
-
139
- console . log ( "TODOs in issue updated: %s" , currentShow . url ) ;
123
+ const data = await twitterRequest ( `POST statuses/update.json` , {
124
+ auth,
125
+ status : tweetText ,
126
+ } ) ;
127
+ const tweetUrl = `https://twitter.com/gr2m/status/${ data . id_str } ` ;
128
+
129
+ core . info ( `Tweeted at ${ tweetUrl } ` ) ;
130
+
131
+ // update TODOs in issue
132
+ await octokit . request ( "PATCH /repos/{owner}/{repo}/issues/{issue_number}" , {
133
+ owner : "gr2m" ,
134
+ repo : "helpdesk" ,
135
+ issue_number : currentShow . number ,
136
+ body : currentShow . issue . body
137
+ . replace (
138
+ / - \[ \] < ! - - t o d o : a n n o u n c e m e n t - t w e e t - - > ( [ ^ \n ] + ) / ,
139
+ `- [x] <!-- todo:announcement-tweet --> $1 (${ tweetUrl } )`
140
+ )
141
+ . replace (
142
+ / - \[ \] < ! - - t o d o : a n n o u n c e m e n t - i s s u e - c o m m e n t - - > ( [ ^ \n ] + ) / ,
143
+ `- [x] <!-- todo:announcement-issue-comment --> $1 (${ commentUrl } )`
144
+ ) ,
145
+ } ) ;
146
+
147
+ core . info ( `TODOs in issue updated: ${ currentShow . url } ` ) ;
148
+ }
0 commit comments