Skip to content

Commit 9941731

Browse files
majaberejberejmaj
andauthored
github webhooks fix (#86)
Co-authored-by: berejmaj <maja.berej@orange.com>
1 parent f5f9118 commit 9941731

4 files changed

Lines changed: 62 additions & 37 deletions

File tree

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
11
package io.mixeway.mixewayflowapi.api.webhook.controller;
22

33
import ch.qos.logback.core.spi.ScanException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
45
import io.mixeway.mixewayflowapi.api.webhook.dto.GHMergeEventDTO;
56
import io.mixeway.mixewayflowapi.api.webhook.dto.GHPushEventDTO;
6-
import io.mixeway.mixewayflowapi.api.webhook.dto.GLMergeEventDTO;
7-
import io.mixeway.mixewayflowapi.api.webhook.dto.GLPushEventDTO;
87
import io.mixeway.mixewayflowapi.api.webhook.service.GitHubWebhookService;
98
import lombok.RequiredArgsConstructor;
109
import lombok.extern.log4j.Log4j2;
11-
import org.springframework.http.HttpStatus;
10+
import org.springframework.http.MediaType;
1211
import org.springframework.http.ResponseEntity;
1312
import org.springframework.validation.annotation.Validated;
14-
import org.springframework.web.bind.annotation.PostMapping;
15-
import org.springframework.web.bind.annotation.RequestBody;
16-
import org.springframework.web.bind.annotation.RestController;
13+
import org.springframework.web.bind.annotation.*;
1714

1815
import java.io.IOException;
19-
import java.util.List;
2016

2117
@RestController
2218
@RequiredArgsConstructor
2319
@Validated
2420
@Log4j2
2521
public class GitHubWebhookController {
22+
2623
private final GitHubWebhookService gitHubWebhookService;
24+
private final ObjectMapper objectMapper;
25+
26+
@PostMapping(value = "/api/v1/webhook/github/push", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
27+
public ResponseEntity<?> pushEventGH(@RequestParam("payload") String payload)
28+
throws ScanException, IOException, InterruptedException {
2729

28-
@PostMapping("/api/v1/webhook/github/push")
29-
public ResponseEntity<?> pushEventGH(@RequestBody List<GHPushEventDTO> ghPushEventDTOS) throws ScanException, IOException, InterruptedException {
30-
gitHubWebhookService.processPush(ghPushEventDTOS);
31-
return new ResponseEntity<>(HttpStatus.OK);
30+
GHPushEventDTO ghPushEventDTO = objectMapper.readValue(payload, GHPushEventDTO.class);
31+
gitHubWebhookService.processPush(ghPushEventDTO);
32+
return ResponseEntity.ok().build();
3233
}
3334

34-
@PostMapping("/api/v1/webhook/github/merge")
35-
public ResponseEntity<?> mergeEventGH( @RequestBody GHMergeEventDTO ghMergeEventDTO) throws ScanException, IOException, InterruptedException {
35+
@PostMapping(value = "/api/v1/webhook/github/merge", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
36+
public ResponseEntity<?> mergeEventGH(@RequestParam("payload") String payload)
37+
throws ScanException, IOException, InterruptedException {
38+
39+
GHMergeEventDTO ghMergeEventDTO = objectMapper.readValue(payload, GHMergeEventDTO.class);
3640
log.info("[GitHub Merge] Merge event for {}", ghMergeEventDTO.getRepository().getId());
3741
gitHubWebhookService.processMerge(ghMergeEventDTO);
38-
return new ResponseEntity<>(HttpStatus.OK);
42+
return ResponseEntity.ok().build();
3943
}
4044
}
Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,51 @@
11
package io.mixeway.mixewayflowapi.api.webhook.dto;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
35
import lombok.Data;
46

57
@Data
8+
@JsonIgnoreProperties(ignoreUnknown = true)
69
public class GHMergeEventDTO {
10+
11+
private String action;
12+
private Long number;
13+
14+
@JsonProperty("pull_request")
715
private PullRequestDTO pullRequest;
16+
817
private RepositoryDTO repository;
918

1019
@Data
20+
@JsonIgnoreProperties(ignoreUnknown = true)
1121
public static class PullRequestDTO {
1222
private Long id;
13-
private HeadDTO head;
14-
private BaseDTO base;
1523
private String state;
1624

17-
@Data
18-
public static class HeadDTO {
19-
private String ref;
20-
private String sha;
21-
}
22-
23-
@Data
24-
public static class BaseDTO {
25-
private String ref;
26-
private String sha;
27-
}
25+
@JsonProperty("head")
26+
private HeadBaseDTO head;
27+
28+
@JsonProperty("base")
29+
private HeadBaseDTO base;
2830
}
2931

3032
@Data
33+
@JsonIgnoreProperties(ignoreUnknown = true)
34+
public static class HeadBaseDTO {
35+
private String ref;
36+
private String sha;
37+
}
38+
39+
@Data
40+
@JsonIgnoreProperties(ignoreUnknown = true)
3141
public static class RepositoryDTO {
3242
private Long id;
43+
private String name;
44+
45+
@JsonProperty("full_name")
46+
private String fullName;
47+
48+
@JsonProperty("default_branch")
49+
private String defaultBranch;
3350
}
3451
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package io.mixeway.mixewayflowapi.api.webhook.dto;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
34
import lombok.Data;
45

56
@Data
7+
@JsonIgnoreProperties(ignoreUnknown = true)
68
public class GHPushEventDTO {
79
private String ref;
810
private String after;
911
private RepositoryDTO repository;
1012

11-
1213
@Data
14+
@JsonIgnoreProperties(ignoreUnknown = true)
1315
public static class RepositoryDTO {
1416
private Long id;
15-
1617
}
1718
}

backend/src/main/java/io/mixeway/mixewayflowapi/api/webhook/service/GitHubWebhookService.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ public class GitHubWebhookService {
2323
private final FindCodeRepoService findCodeRepoService;
2424
private final GetOrCreateCodeRepoBranchService getOrCreateCodeRepoBranchService;
2525

26-
public void processPush(List<GHPushEventDTO> ghPushEventDTOs) throws ScanException, IOException, InterruptedException {
27-
GHPushEventDTO ghPushEventDTO;
28-
if (ghPushEventDTOs != null && !ghPushEventDTOs.isEmpty()){
29-
ghPushEventDTO = ghPushEventDTOs.get(0);
30-
log.info("[GitHub Push] Push event for {} - {}", ghPushEventDTO.getRepository().getId(), ghPushEventDTO.getRef().replace("refs/heads/",""));
31-
CodeRepo codeRepo = findCodeRepoService.findByRemoteId(ghPushEventDTO.getRepository().getId());
32-
CodeRepoBranch codeRepoBranch = getOrCreateCodeRepoBranchService.getOrCreateCodeRepoBranch(ghPushEventDTO.getRef().replace("refs/heads/",""), codeRepo);
33-
scanManagerService.scanRepository(codeRepo,codeRepoBranch, ghPushEventDTO.getAfter(),null);
26+
public void processPush(GHPushEventDTO ghPushEventDTO) throws ScanException, IOException, InterruptedException {
27+
if (ghPushEventDTO != null) {
28+
log.info("[GitHub Push] Push event for {} - {}",
29+
ghPushEventDTO.getRepository().getId(),
30+
ghPushEventDTO.getRef().replace("refs/heads/",""));
3431

32+
CodeRepo codeRepo = findCodeRepoService.findByRemoteId(ghPushEventDTO.getRepository().getId());
33+
CodeRepoBranch codeRepoBranch = getOrCreateCodeRepoBranchService.getOrCreateCodeRepoBranch(
34+
ghPushEventDTO.getRef().replace("refs/heads/",""),
35+
codeRepo
36+
);
37+
scanManagerService.scanRepository(codeRepo, codeRepoBranch, ghPushEventDTO.getAfter(), null);
3538
}
3639
}
3740

0 commit comments

Comments
 (0)