From 6b1c5c08a2deda3cd0a63224b51256dacc063b3a Mon Sep 17 00:00:00 2001 From: Alexander Suter Date: Fri, 27 Dec 2024 11:08:12 +0100 Subject: [PATCH] Add more tests --- .../webhook/GitHubWebhookController.java | 25 ++++++------------- .../webhook/GitHubWebhookControllerIT.java | 25 +++++++++++++++++++ 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/ivyteam/devops/github/webhook/GitHubWebhookController.java b/src/main/java/io/ivyteam/devops/github/webhook/GitHubWebhookController.java index f771b21..7045ece 100644 --- a/src/main/java/io/ivyteam/devops/github/webhook/GitHubWebhookController.java +++ b/src/main/java/io/ivyteam/devops/github/webhook/GitHubWebhookController.java @@ -56,24 +56,24 @@ public ResponseEntity push(@RequestBody PushBean bean) { } @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE, headers = "X-GitHub-Event=delete") - public ResponseEntity deleteBranch(@RequestBody BranchBean bean) { + public ResponseEntity delete(@RequestBody BranchBean bean) { if ("branch".equals(bean.ref_type)) { - branches.delete(bean.repo(), bean.name()); - return ResponseEntity.ok().body(bean); + branches.delete(bean.repository().full_name(), bean.ref()); + return ResponseEntity.ok().body("DELETED"); } throw new RuntimeException("ref type not supported: " + bean.ref_type); } @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE, headers = "X-GitHub-Event=pull_request") - public ResponseEntity pr(@RequestBody PullRequestBean bean) { + public ResponseEntity pr(@RequestBody PullRequestBean bean) { var pr = bean.toPullRequest(); if ("opened".equals(bean.action)) { prs.create(pr); - return ResponseEntity.ok().body(pr); + return ResponseEntity.ok().body("CREATED"); } if ("closed".equals(bean.action)) { prs.delete(pr); - return ResponseEntity.ok().body(pr); + return ResponseEntity.ok().body("DELETED"); } return ResponseEntity.noContent().build(); } @@ -94,7 +94,6 @@ Branch toBranch() { .repository(this.repository.full_name) .name(ref.replace("refs/heads/", "")) .lastCommitAuthor(this.head_commit.author.username) - .protectedBranch(false) .authoredDate(tsToDate(this.head_commit.timestamp)) .build(); } @@ -103,17 +102,7 @@ Branch toBranch() { record BranchBean( String ref, String ref_type, - Repository repository, - User sender, - String updated_at) { - - String repo() { - return repository.full_name; - } - - String name() { - return ref; - } + Repository repository) { } record PullRequestBean( diff --git a/src/test/java/io/ivyteam/devops/github/webhook/GitHubWebhookControllerIT.java b/src/test/java/io/ivyteam/devops/github/webhook/GitHubWebhookControllerIT.java index bfc3923..81681ff 100644 --- a/src/test/java/io/ivyteam/devops/github/webhook/GitHubWebhookControllerIT.java +++ b/src/test/java/io/ivyteam/devops/github/webhook/GitHubWebhookControllerIT.java @@ -85,4 +85,29 @@ void push_delete() throws Exception { assertThat(response.body()).isEqualTo("DELETED"); } } + + @Test + void delete() throws Exception { + var push = """ + { + "ref": "market-install-result", + "repository": { + "full_name": "axonivy/core" + }, + "ref_type": "branch" + } + """; + + try (var client = HttpClient.newHttpClient()) { + var request = HttpRequest.newBuilder() + .uri(URI.create("http://localhost:8080/github-webhook/")) + .header("X-GitHub-Event", "delete") + .header("Content-Type", "application/json") + .POST(BodyPublishers.ofString(push)) + .build(); + var response = client.send(request, BodyHandlers.ofString()); + assertThat(response.statusCode()).isEqualTo(200); + assertThat(response.body()).isEqualTo("DELETED"); + } + } }