@@ -3,9 +3,10 @@ import { Webhooks } from '@octokit/webhooks';
3
3
import { sendActionRequest } from '../sqs' ;
4
4
import { CheckRunEvent , WorkflowJobEvent } from '@octokit/webhooks-types' ;
5
5
import { getParameterValue } from '../ssm' ;
6
- import { logger as rootLogger } from './logger' ;
6
+ import { logger as rootLogger , LogFields } from './logger' ;
7
7
import { Response } from '../lambda' ;
8
8
9
+ const supportedEvents = [ 'check_run' , 'workflow_job' ] ;
9
10
const logger = rootLogger . getChildLogger ( ) ;
10
11
11
12
export async function handle ( headers : IncomingHttpHeaders , body : string ) : Promise < Response > {
@@ -24,33 +25,53 @@ export async function handle(headers: IncomingHttpHeaders, body: string): Promis
24
25
return response ;
25
26
}
26
27
const payload = JSON . parse ( body ) ;
27
- logger . info ( `Received Github event ${ githubEvent } from ${ payload . repository . full_name } ` ) ;
28
+ LogFields . fields . event = githubEvent ;
29
+ LogFields . fields . repository = payload . repository . full_name ;
30
+ LogFields . fields . action = payload . action ;
31
+
32
+ if ( ! supportedEvents . includes ( githubEvent ) ) {
33
+ logger . warn ( `Unsupported event type.` , LogFields . print ( ) ) ;
34
+ return {
35
+ statusCode : 202 ,
36
+ body : `Ignoring unsupported event ${ githubEvent } ` ,
37
+ } ;
38
+ }
39
+
40
+ LogFields . fields . name = payload [ githubEvent ] . name ;
41
+ LogFields . fields . status = payload [ githubEvent ] . status ;
42
+ LogFields . fields . started_at = payload [ githubEvent ] ?. started_at ;
43
+
44
+ /*
45
+ The app subscribes to all `check_run` and `workflow_job` events.
46
+ If the event status is `completed`, log the data for workflow metrics.
47
+ */
48
+ LogFields . fields . completed_at = payload [ githubEvent ] ?. completed_at ;
49
+ LogFields . fields . conclusion = payload [ githubEvent ] ?. conclusion ;
28
50
29
51
if ( isRepoNotAllowed ( payload . repository . full_name ) ) {
30
- console . warn ( `Received event from unauthorized repository ${ payload . repository . full_name } ` ) ;
52
+ logger . error ( `Received event from unauthorized repository ${ payload . repository . full_name } ` , LogFields . print ( ) ) ;
31
53
return {
32
54
statusCode : 403 ,
33
55
} ;
34
56
}
35
57
58
+ logger . info ( `Processing Github event` , LogFields . print ( ) ) ;
59
+
36
60
if ( githubEvent == 'workflow_job' ) {
37
61
response = await handleWorkflowJob ( payload as WorkflowJobEvent , githubEvent ) ;
38
62
} else if ( githubEvent == 'check_run' ) {
39
63
response = await handleCheckRun ( payload as CheckRunEvent , githubEvent ) ;
40
- } else {
41
- logger . warn ( `Ignoring unsupported event ${ githubEvent } ` ) ;
42
- response = {
43
- statusCode : 202 ,
44
- body : `Ignoring unsupported event ${ githubEvent } ` ,
45
- } ;
46
64
}
47
65
48
66
return response ;
49
67
}
50
68
51
69
async function verifySignature ( githubEvent : string , signature : string , body : string ) : Promise < number > {
52
70
if ( ! signature ) {
53
- logger . error ( "Github event doesn't have signature. This webhook requires a secret to be configured." ) ;
71
+ logger . error (
72
+ "Github event doesn't have signature. This webhook requires a secret to be configured." ,
73
+ LogFields . print ( ) ,
74
+ ) ;
54
75
return 500 ;
55
76
}
56
77
@@ -60,7 +81,7 @@ async function verifySignature(githubEvent: string, signature: string, body: str
60
81
secret : secret ,
61
82
} ) ;
62
83
if ( ! ( await webhooks . verify ( body , signature ) ) ) {
63
- logger . error ( 'Unable to verify signature!' ) ;
84
+ logger . error ( 'Unable to verify signature!' , LogFields . print ( ) ) ;
64
85
return 401 ;
65
86
}
66
87
return 200 ;
@@ -70,7 +91,10 @@ async function handleWorkflowJob(body: WorkflowJobEvent, githubEvent: string): P
70
91
const disableCheckWorkflowJobLabelsEnv = process . env . DISABLE_CHECK_WORKFLOW_JOB_LABELS || 'false' ;
71
92
const disableCheckWorkflowJobLabels = JSON . parse ( disableCheckWorkflowJobLabelsEnv ) as boolean ;
72
93
if ( ! disableCheckWorkflowJobLabels && ! canRunJob ( body ) ) {
73
- logger . warn ( `Received event contains runner labels '${ body . workflow_job . labels } ' that are not accepted.` ) ;
94
+ logger . warn (
95
+ `Received event contains runner labels '${ body . workflow_job . labels } ' that are not accepted.` ,
96
+ LogFields . print ( ) ,
97
+ ) ;
74
98
return {
75
99
statusCode : 202 ,
76
100
body : `Received event contains runner labels '${ body . workflow_job . labels } ' that are not accepted.` ,
@@ -90,7 +114,7 @@ async function handleWorkflowJob(body: WorkflowJobEvent, githubEvent: string): P
90
114
installationId : installationId ,
91
115
} ) ;
92
116
}
93
- console . info ( `Successfully queued job for ${ body . repository . full_name } ` ) ;
117
+ logger . info ( `Successfully queued job for ${ body . repository . full_name } ` , LogFields . print ( ) ) ;
94
118
return { statusCode : 201 } ;
95
119
}
96
120
@@ -108,7 +132,7 @@ async function handleCheckRun(body: CheckRunEvent, githubEvent: string): Promise
108
132
installationId : installationId ,
109
133
} ) ;
110
134
}
111
- console . info ( `Successfully queued job for ${ body . repository . full_name } ` ) ;
135
+ logger . info ( `Successfully queued job for ${ body . repository . full_name } ` , LogFields . print ( ) ) ;
112
136
return { statusCode : 201 } ;
113
137
}
114
138
@@ -139,6 +163,7 @@ function canRunJob(job: WorkflowJobEvent): boolean {
139
163
`Received workflow job event with labels: '${ JSON . stringify ( job . workflow_job . labels ) } '. The event does ${
140
164
runnerMatch ? '' : 'NOT '
141
165
} match the configured labels: '${ Array . from ( runnerLabels ) . join ( ',' ) } '`,
166
+ LogFields . print ( ) ,
142
167
) ;
143
168
return runnerMatch ;
144
169
}
0 commit comments