Skip to content

Commit

Permalink
Refresh cache only if push event is for the main branch
Browse files Browse the repository at this point in the history
See gh-23
  • Loading branch information
mbhave committed Nov 9, 2024
1 parent 4e49abe commit 8eb1c70
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -100,6 +102,9 @@ public ResponseEntity<String> 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<Map<String, ?>> commits = (List<Map<String, ?>>) push.get("commits");
List<String> changes = getChangedFiles(commits);
this.repository.update(changes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"ref": "refs/heads/master",
"ref": "refs/heads/main",
"after": "a427f3f4db6c96ae5e86b610d0eef149393c9996",
"before": "3193db21394092a84d973bef8c4a3741aec1b814",
"created": false,
Expand All @@ -24,6 +24,7 @@
"username": "cbeams"
},
"added": [
"added.html"
],
"removed": [
],
Expand Down
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"username": "cbeams"
},
"committer": {
"name": "Chris Beams",
"email": "[email protected]",
"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": "[email protected]",
"username": "cbeams"
},
"committer": {
"name": "Chris Beams",
"email": "[email protected]",
"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": "[email protected]",
"username": "cbeams"
},
"committer": {
"name": "Chris Beams",
"email": "[email protected]",
"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"
}
}

0 comments on commit 8eb1c70

Please sign in to comment.