1
- import * as core from " @actions/core" ;
2
- import * as github from " @actions/github" ;
3
- import * as axios from " axios"
1
+ import * as core from ' @actions/core'
2
+ import * as github from ' @actions/github'
3
+ import * as axios from ' axios'
4
4
5
- function sleep ( ms : number ) {
6
- return new Promise ( ( resolve ) => {
7
- setTimeout ( resolve , ms ) ;
8
- } ) ;
9
- }
5
+ async function sleep ( ms : number ) : Promise < unknown > {
6
+ return new Promise ( resolve => {
7
+ setTimeout ( resolve , ms )
8
+ } )
9
+ }
10
10
11
- async function run ( ) {
11
+ async function run ( ) : Promise < unknown > {
12
12
try {
13
- const token = core . getInput ( " github-token" ) ;
13
+ const token = core . getInput ( ' github-token' )
14
14
if ( ! token ) {
15
15
core . setFailed ( "'github-token' input can't be empty" )
16
16
return
17
17
}
18
18
19
- const webhookUri = core . getInput ( " webhook-uri" )
19
+ const webhookUri = core . getInput ( ' webhook-uri' )
20
20
if ( ! webhookUri ) {
21
21
core . setFailed ( "'webhook-uri' input can't be empty" )
22
22
return
23
23
}
24
- const ctx = github . context ;
25
- const o = github . getOctokit ( token ) ;
24
+ const ctx = github . context
25
+ const o = github . getOctokit ( token )
26
26
27
27
core . debug ( JSON . stringify ( ctx ) )
28
28
@@ -31,16 +31,24 @@ async function run() {
31
31
const jobList = await o . actions . listJobsForWorkflowRun ( {
32
32
repo : ctx . repo . repo ,
33
33
owner : ctx . repo . owner ,
34
- run_id : ctx . runId ,
35
- } ) ;
34
+ run_id : ctx . runId
35
+ } )
36
36
37
37
const jobs = jobList . data . jobs
38
38
core . debug ( JSON . stringify ( jobs ) )
39
-
40
- const job = jobs . find ( job => job . name === ctx . job ) ;
41
39
42
- const stoppedStep = job ?. steps . find ( s => s . conclusion === "failure" || s . conclusion === "timed_out" || s . conclusion === "cancelled" || s . conclusion === "action_required" )
43
- const lastStep = stoppedStep ? stoppedStep : job ?. steps . reverse ( ) . find ( s => s . status === "completed" )
40
+ const job = jobs . find ( j => j . name === ctx . job )
41
+
42
+ const stoppedStep = job ?. steps . find (
43
+ s =>
44
+ s . conclusion === 'failure' ||
45
+ s . conclusion === 'timed_out' ||
46
+ s . conclusion === 'cancelled' ||
47
+ s . conclusion === 'action_required'
48
+ )
49
+ const lastStep = stoppedStep
50
+ ? stoppedStep
51
+ : job ?. steps . reverse ( ) . find ( s => s . status === 'completed' )
44
52
45
53
const wr = await o . actions . getWorkflowRun ( {
46
54
owner : ctx . repo . owner ,
@@ -53,42 +61,56 @@ async function run() {
53
61
const repository_url = ctx . payload . repository ?. html_url
54
62
const commit_author = ctx . actor
55
63
56
- var themeColor = lastStep ?. conclusion === "success" ? "90C978" : lastStep ?. conclusion === "cancelled" ? "FFF175" : "C23B23"
57
- const conclusion = lastStep ?. conclusion === "success" ? "SUCCEEDED" : lastStep ?. conclusion === "cancelled" ? "CANCELLED" : "FAILED"
58
-
64
+ const themeColor =
65
+ lastStep ?. conclusion === 'success'
66
+ ? '90C978'
67
+ : lastStep ?. conclusion === 'cancelled'
68
+ ? 'FFF175'
69
+ : 'C23B23'
70
+ const conclusion =
71
+ lastStep ?. conclusion === 'success'
72
+ ? 'SUCCEEDED'
73
+ : lastStep ?. conclusion === 'cancelled'
74
+ ? 'CANCELLED'
75
+ : 'FAILED'
76
+
59
77
const webhookBody = {
60
- " @type" : " MessageCard" ,
61
- " @context" : " http://schema.org/extensions" ,
62
- " themeColor" : `${ themeColor } ` ,
63
- " summary" : `${ commit_author } commited new changes` ,
64
- " sections" : [
78
+ ' @type' : ' MessageCard' ,
79
+ ' @context' : ' http://schema.org/extensions' ,
80
+ themeColor : `${ themeColor } ` ,
81
+ summary : `${ commit_author } commited new changes` ,
82
+ sections : [
65
83
{
66
- " activityTitle" : `Workflow '${ ctx . workflow } ' #${ ctx . runNumber } ${ conclusion } ` ,
67
- " activitySubtitle" : `on [${ ctx . payload . repository ?. full_name } ](${ repository_url } )` ,
68
- " facts" : [
84
+ activityTitle : `Workflow '${ ctx . workflow } ' #${ ctx . runNumber } ${ conclusion } ` ,
85
+ activitySubtitle : `on [${ ctx . payload . repository ?. full_name } ](${ repository_url } )` ,
86
+ facts : [
69
87
{
70
- name : " Commit" ,
88
+ name : ' Commit' ,
71
89
value : `[${ wr . data . head_commit . message } ](${ wr . data . repository . html_url } /commit/${ wr . data . head_sha } ) by [${ ctx . payload . sender ?. login } ](${ ctx . payload . sender ?. html_url } )`
72
90
} ,
73
91
{
74
- name : ctx . eventName === "pull_request" ? "Pull request" : "Branch" ,
75
- value : ctx . eventName === "pull_request" ? `[${ ctx . payload . pull_request ?. html_url } ](${ ctx . payload . pull_request ?. html_url } )` : `[${ ctx . payload . repository ?. html_url } /tree/${ ctx . ref } ](${ ctx . payload . repository ?. html_url } /tree/${ ctx . ref } )`
92
+ name :
93
+ ctx . eventName === 'pull_request' ? 'Pull request' : 'Branch' ,
94
+ value :
95
+ ctx . eventName === 'pull_request'
96
+ ? `[${ ctx . payload . pull_request ?. html_url } ](${ ctx . payload . pull_request ?. html_url } )`
97
+ : `[${ ctx . payload . repository ?. html_url } /tree/${ ctx . ref } ](${ ctx . payload . repository ?. html_url } /tree/${ ctx . ref } )`
76
98
} ,
77
99
{
78
- name : " Workflow run details" ,
100
+ name : ' Workflow run details' ,
79
101
value : `[${ wr . data . html_url } ](${ wr . data . html_url } )`
80
102
}
81
103
] ,
82
- " markdown" : true
104
+ markdown : true
83
105
}
84
106
]
85
107
}
86
108
const response = await axios . default . post ( webhookUri , webhookBody )
87
109
core . debug ( JSON . stringify ( response . data ) )
88
110
// TODO: check response status, if not succesful, mark workflow as failed
89
111
} catch ( error ) {
90
- core . setFailed ( error . message ) ;
112
+ core . setFailed ( error . message )
91
113
}
92
114
}
93
115
94
- run ( ) ;
116
+ run ( )
0 commit comments