2
2
3
3
import java .util .Collection ;
4
4
import java .util .Collections ;
5
+ import java .util .LinkedHashMap ;
5
6
import java .util .List ;
6
7
import java .util .Map ;
7
8
import java .util .Optional ;
@@ -126,8 +127,30 @@ public IWebhookEvent wrapAsEvent(Map<String, ?> input) {
126
127
if (input .containsKey (KEY_BODY ) && input .containsKey ("headers" )) {
127
128
// see CheckWebhooksLambdaFunction.saveToDynamoDb(String, IWebhookEvent, AmazonDynamoDB)
128
129
// event = SaveToDynamoDb.NONE;
129
- event = new CleanThatWebhookEvent ((Map <String , ?>) input .get ("headers" ),
130
- (Map <String , ?>) input .get (KEY_BODY ));
130
+
131
+ Map <String , Object > rootBody = PepperMapHelper .getRequiredMap (input , KEY_BODY );
132
+
133
+ if (rootBody .containsKey ("github" )) {
134
+ Map <String , Object > github = PepperMapHelper .getRequiredMap (rootBody , "github" );
135
+
136
+ Map <String , Object > githubHeaders = PepperMapHelper .getRequiredMap (github , "headers" );
137
+
138
+ // Headers is typically empty as we fails fetching headers from API Gateway
139
+ if (!githubHeaders .containsKey ("X-GitHub-Delivery" )) {
140
+ githubHeaders = new LinkedHashMap <>(githubHeaders );
141
+
142
+ // TODO We should push this to the headers next to the actual github body, which may be deeper
143
+ // We should also push it when the initial event is received
144
+ String eventKey = PepperMapHelper .getRequiredString (input , "X-GitHub-Delivery" );
145
+ githubHeaders .put ("X-GitHub-Delivery" , eventKey );
146
+
147
+ // Install the updated headers
148
+ github .put ("headers" , githubHeaders );
149
+ }
150
+ }
151
+
152
+ Map <String , Object > headers = PepperMapHelper .getRequiredMap (input , "headers" );
153
+ event = new CleanThatWebhookEvent (headers , rootBody );
131
154
} else {
132
155
event = new GithubWebhookEvent (input );
133
156
}
@@ -144,12 +167,11 @@ public IWebhookEvent wrapAsEvent(Map<String, ?> input) {
144
167
145
168
String eventName = PepperMapHelper .getRequiredString (r , "eventName" );
146
169
147
- if (!"INSERT" .equals (eventName ) && !"MODIFY" .equals (eventName )) {
148
- // https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_Record.html
149
- // We are in a REMOVE event
150
- LOGGER .info ("We discard eventName={}" , eventName );
151
- asMap = Collections .emptyMap ();
152
- } else {
170
+ if (
171
+ // INSERT event: this is something new process
172
+ "INSERT" .equals (eventName )
173
+ // MODIFY event: this is typically an admin which modify an event manually
174
+ || "MODIFY" .equals (eventName )) {
153
175
Map <String , ?> dynamoDbMap = PepperMapHelper .getRequiredMap (r , "dynamodb" , "NewImage" );
154
176
155
177
// We receive from DynamoDb a json in a special format
@@ -164,6 +186,11 @@ public IWebhookEvent wrapAsEvent(Map<String, ?> input) {
164
186
165
187
LOGGER .info ("Processing X-GitHub-Delivery={}" ,
166
188
PepperMapHelper .getRequiredString (asMap , "X-GitHub-Delivery" ));
189
+ } else {
190
+ // https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_Record.html
191
+ // We are in a REMOVE event
192
+ LOGGER .info ("We discard eventName={}" , eventName );
193
+ asMap = Collections .emptyMap ();
167
194
}
168
195
return asMap ;
169
196
}
0 commit comments