diff --git a/src/main/java/io/spring/projectapi/web/webhook/CacheController.java b/src/main/java/io/spring/projectapi/web/webhook/CacheController.java index b39a132..5538398 100644 --- a/src/main/java/io/spring/projectapi/web/webhook/CacheController.java +++ b/src/main/java/io/spring/projectapi/web/webhook/CacheController.java @@ -63,6 +63,8 @@ public class CacheController { private static final String PING_EVENT = "ping"; + private static final String MAIN_BRANCH = "refs/heads/main"; + private final ObjectMapper objectMapper; private final Mac hmac; @@ -100,6 +102,9 @@ public ResponseEntity refresh(@RequestBody String payload, } Map push = this.objectMapper.readValue(payload, Map.class); logPayload(push); + if (!push.get("ref").equals(MAIN_BRANCH)) { + return ResponseEntity.ok("{ \"message\": \"Push event not on main\" }"); + } List> commits = (List>) push.get("commits"); List changes = getChangedFiles(commits); this.repository.update(changes); diff --git a/src/test/java/io/spring/projectapi/web/webhook/CacheControllerTests.java b/src/test/java/io/spring/projectapi/web/webhook/CacheControllerTests.java index ea64533..9372bca 100644 --- a/src/test/java/io/spring/projectapi/web/webhook/CacheControllerTests.java +++ b/src/test/java/io/spring/projectapi/web/webhook/CacheControllerTests.java @@ -37,6 +37,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; /** * Tests for {@link CacheController}. @@ -103,18 +104,32 @@ void invalidJsonPushEventShouldBeRejected() throws Exception { } @Test - void shouldTriggerCacheRefresh() throws Exception { + void cacheRefreshNotTriggeredOnBranchOtherThanMain() throws Exception { this.mockMvc .perform(MockMvcRequestBuilders.post("/refresh_cache") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON) .header("X-Hub-Signature", "sha1=C8D5B1C972E8DCFB69AB7124678D4C91E11D6F23") .header("X-GitHub-Event", "push") + .content(getTestPayload("push_other_branch"))) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.content().string("{ \"message\": \"Push event not on main\" }")); + verifyNoInteractions(this.projectRepository); + } + + @Test + void shouldTriggerCacheRefresh() throws Exception { + this.mockMvc + .perform(MockMvcRequestBuilders.post("/refresh_cache") + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .header("X-Hub-Signature", "sha1=E1C54005D92AE14A76A7C0CD164AEAC6E6740C64") + .header("X-GitHub-Event", "push") .content(getTestPayload("push"))) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.content() .string("{ \"message\": \"Successfully processed cache refresh\" }")); - verify(this.projectRepository, times(1)).update(List.of("index-common.html")); + verify(this.projectRepository, times(1)).update(List.of("added.html", "index-common.html")); } private String getTestPayload(String fileName) throws Exception { diff --git a/src/test/resources/io/spring/projectapi/web/webhook/push.json b/src/test/resources/io/spring/projectapi/web/webhook/push.json index d9c8a86..dac9b2e 100644 --- a/src/test/resources/io/spring/projectapi/web/webhook/push.json +++ b/src/test/resources/io/spring/projectapi/web/webhook/push.json @@ -1,5 +1,5 @@ { - "ref": "refs/heads/master", + "ref": "refs/heads/main", "after": "a427f3f4db6c96ae5e86b610d0eef149393c9996", "before": "3193db21394092a84d973bef8c4a3741aec1b814", "created": false, @@ -24,6 +24,7 @@ "username": "cbeams" }, "added": [ + "added.html" ], "removed": [ ], diff --git a/src/test/resources/io/spring/projectapi/web/webhook/push_other_branch.json b/src/test/resources/io/spring/projectapi/web/webhook/push_other_branch.json new file mode 100644 index 0000000..d9c8a86 --- /dev/null +++ b/src/test/resources/io/spring/projectapi/web/webhook/push_other_branch.json @@ -0,0 +1,112 @@ +{ + "ref": "refs/heads/master", + "after": "a427f3f4db6c96ae5e86b610d0eef149393c9996", + "before": "3193db21394092a84d973bef8c4a3741aec1b814", + "created": false, + "deleted": false, + "forced": false, + "compare": "https://github.com/spring-guides/gs-test-guide/compare/3193db213940...a427f3f4db6c", + "commits": [ + { + "id": "6bdc0feaec5ccc85a9c4fe2a3e62bebe5a69e8fc", + "distinct": true, + "message": "Touch index-common to trigger gh-pages build", + "timestamp": "2013-08-10T15:56:23-07:00", + "url": "https://github.com/spring-projects/gs-test-guide/commit/6bdc0feaec5ccc85a9c4fe2a3e62bebe5a69e8fc", + "author": { + "name": "Chris Beams", + "email": "cbeams@gopivotal.com", + "username": "cbeams" + }, + "committer": { + "name": "Chris Beams", + "email": "cbeams@gopivotal.com", + "username": "cbeams" + }, + "added": [ + ], + "removed": [ + ], + "modified": [ + "index-common.html" + ] + }, + { + "id": "a427f3f4db6c96ae5e86b610d0eef149393c9996", + "distinct": true, + "message": "Remove empty newline at end of index-common", + "timestamp": "2013-08-10T16:12:39-07:00", + "url": "https://github.com/spring-guides/gs-test-guide/commit/a427f3f4db6c96ae5e86b610d0eef149393c9996", + "author": { + "name": "Chris Beams", + "email": "cbeams@gopivotal.com", + "username": "cbeams" + }, + "committer": { + "name": "Chris Beams", + "email": "cbeams@gopivotal.com", + "username": "cbeams" + }, + "added": [ + ], + "removed": [ + ], + "modified": [ + "index-common.html" + ] + } + ], + "head_commit": { + "id": "a427f3f4db6c96ae5e86b610d0eef149393c9996", + "distinct": true, + "message": "Remove empty newline at end of index-common", + "timestamp": "2013-08-10T16:12:39-07:00", + "url": "https://github.com/spring-guides/gs-test-guide/commit/a427f3f4db6c96ae5e86b610d0eef149393c9996", + "author": { + "name": "Chris Beams", + "email": "cbeams@gopivotal.com", + "username": "cbeams" + }, + "committer": { + "name": "Chris Beams", + "email": "cbeams@gopivotal.com", + "username": "cbeams" + }, + "added": [ + ], + "removed": [ + ], + "modified": [ + "index-common.html" + ] + }, + "repository": { + "id": 11634233, + "name": "gs-test-guide", + "url": "https://github.com/spring-guides/gs-test-guide", + "description": "A test guide", + "homepage": "", + "watchers": 0, + "stargazers": 0, + "forks": 0, + "fork": false, + "size": 452, + "owner": { + "name": "spring-guides", + "email": null + }, + "private": false, + "open_issues": 0, + "has_issues": true, + "has_downloads": true, + "has_wiki": true, + "language": "Java", + "created_at": 1374668910, + "pushed_at": 1376176368, + "master_branch": "master", + "organization": "spring-guides" + }, + "pusher": { + "name": "none" + } +} \ No newline at end of file