1- const https = require ( ' https' ) ;
1+ const https = require ( " https" ) ;
22
33async function sendDiscordNotification ( webhookUrl , commits , fileCount ) {
44 if ( ! webhookUrl ) {
5- throw new Error ( ' DISCORD_WEBHOOK_URL secret is missing' ) ;
5+ throw new Error ( " DISCORD_WEBHOOK_URL secret is missing" ) ;
66 }
77
88 if ( ! commits || commits . length === 0 ) {
9- throw new Error ( ' No commits found in this push event' ) ;
9+ throw new Error ( " No commits found in this push event" ) ;
1010 }
1111
12- const fields = commits . map ( ( commit ) => {
13- const commitFields = [
14- { name : `Commit ${ commit . sha } ` , value : commit . message , inline : false }
15- ] ;
12+ const fields = commits
13+ . map ( ( commit ) => {
14+ const commitFields = [
15+ { name : `Commit ${ commit . sha } ` , value : commit . message , inline : false } ,
16+ ] ;
1617
17- const body = commit . full_message . split ( '\n' ) . slice ( 1 ) . join ( '\n' ) . trim ( ) ;
18- if ( body !== '' ) {
19- commitFields . push ( { name : 'Details' , value : body , inline : false } ) ;
20- }
18+ const body = commit . full_message . split ( "\n" ) . slice ( 1 ) . join ( "\n" ) . trim ( ) ;
19+ if ( body !== "" ) {
20+ commitFields . push ( { name : "Details" , value : body , inline : false } ) ;
21+ }
22+
23+ commitFields . push ( { name : "By" , value : commit . author , inline : true } ) ;
2124
22- commitFields . push ( { name : 'By' , value : commit . author , inline : true } ) ;
23-
24- return commitFields ;
25- } ) . flat ( ) ;
25+ return commitFields ;
26+ } )
27+ . flat ( ) ;
2628
27- fields . push ( { name : 'Files Updated' , value : `${ fileCount } file(s)` , inline : true } ) ;
29+ fields . push ( {
30+ name : "Files Updated" ,
31+ value : `${ fileCount } file(s)` ,
32+ inline : true ,
33+ } ) ;
2834
2935 const embed = {
30- title : ' TDS Statistics Editor Update' ,
36+ title : " TDS Statistics Editor Update" ,
3137 description : `Push contains ${ commits . length } commit(s)` ,
3238 color : 5763719 ,
3339 fields,
34- footer : { text : ' Thank you for using the Statistics Editor!' } ,
35- timestamp : new Date ( ) . toISOString ( )
40+ footer : { text : " Thank you for using the Statistics Editor!" } ,
41+ timestamp : new Date ( ) . toISOString ( ) ,
3642 } ;
3743
3844 const payload = { embeds : [ embed ] } ;
@@ -43,30 +49,36 @@ async function sendDiscordNotification(webhookUrl, commits, fileCount) {
4349 const options = {
4450 hostname : url . hostname ,
4551 path : url . pathname + url . search ,
46- method : ' POST' ,
52+ method : " POST" ,
4753 headers : {
48- ' Content-Type' : ' application/json' ,
49- ' Content-Length' : Buffer . byteLength ( data ) ,
50- ' User-Agent' : ' github-actions/discord-notify'
51- }
54+ " Content-Type" : " application/json" ,
55+ " Content-Length" : Buffer . byteLength ( data ) ,
56+ " User-Agent" : " github-actions/discord-notify" ,
57+ } ,
5258 } ;
5359
5460 const req = https . request ( options , ( res ) => {
55- let body = '' ;
56- res . on ( ' data' , ( d ) => body += d ) ;
57- res . on ( ' end' , ( ) => {
61+ let body = "" ;
62+ res . on ( " data" , ( d ) => ( body += d ) ) ;
63+ res . on ( " end" , ( ) => {
5864 if ( res . statusCode < 200 || res . statusCode >= 300 ) {
59- reject ( new Error ( `Failed to post to Discord: HTTP ${ res . statusCode } ${ body } ` ) ) ;
65+ reject (
66+ new Error (
67+ `Failed to post to Discord: HTTP ${ res . statusCode } ${ body } ` ,
68+ ) ,
69+ ) ;
6070 } else {
61- resolve ( ' Discord webhook posted successfully' ) ;
71+ resolve ( " Discord webhook posted successfully" ) ;
6272 }
6373 } ) ;
6474 } ) ;
6575
66- req . on ( 'error' , ( err ) => reject ( new Error ( `Failed to post to Discord: ${ err . message } ` ) ) ) ;
76+ req . on ( "error" , ( err ) =>
77+ reject ( new Error ( `Failed to post to Discord: ${ err . message } ` ) ) ,
78+ ) ;
6779 req . write ( data ) ;
6880 req . end ( ) ;
6981 } ) ;
7082}
7183
72- module . exports = { sendDiscordNotification } ;
84+ module . exports = { sendDiscordNotification } ;
0 commit comments