Skip to content

Commit 8eb1c70

Browse files
committed
Refresh cache only if push event is for the main branch
See gh-23
1 parent 4e49abe commit 8eb1c70

File tree

4 files changed

+136
-3
lines changed

4 files changed

+136
-3
lines changed

src/main/java/io/spring/projectapi/web/webhook/CacheController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public class CacheController {
6363

6464
private static final String PING_EVENT = "ping";
6565

66+
private static final String MAIN_BRANCH = "refs/heads/main";
67+
6668
private final ObjectMapper objectMapper;
6769

6870
private final Mac hmac;
@@ -100,6 +102,9 @@ public ResponseEntity<String> refresh(@RequestBody String payload,
100102
}
101103
Map<?, ?> push = this.objectMapper.readValue(payload, Map.class);
102104
logPayload(push);
105+
if (!push.get("ref").equals(MAIN_BRANCH)) {
106+
return ResponseEntity.ok("{ \"message\": \"Push event not on main\" }");
107+
}
103108
List<Map<String, ?>> commits = (List<Map<String, ?>>) push.get("commits");
104109
List<String> changes = getChangedFiles(commits);
105110
this.repository.update(changes);

src/test/java/io/spring/projectapi/web/webhook/CacheControllerTests.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
import static org.mockito.Mockito.times;
3939
import static org.mockito.Mockito.verify;
40+
import static org.mockito.Mockito.verifyNoInteractions;
4041

4142
/**
4243
* Tests for {@link CacheController}.
@@ -103,18 +104,32 @@ void invalidJsonPushEventShouldBeRejected() throws Exception {
103104
}
104105

105106
@Test
106-
void shouldTriggerCacheRefresh() throws Exception {
107+
void cacheRefreshNotTriggeredOnBranchOtherThanMain() throws Exception {
107108
this.mockMvc
108109
.perform(MockMvcRequestBuilders.post("/refresh_cache")
109110
.accept(MediaType.APPLICATION_JSON)
110111
.contentType(MediaType.APPLICATION_JSON)
111112
.header("X-Hub-Signature", "sha1=C8D5B1C972E8DCFB69AB7124678D4C91E11D6F23")
112113
.header("X-GitHub-Event", "push")
114+
.content(getTestPayload("push_other_branch")))
115+
.andExpect(MockMvcResultMatchers.status().isOk())
116+
.andExpect(MockMvcResultMatchers.content().string("{ \"message\": \"Push event not on main\" }"));
117+
verifyNoInteractions(this.projectRepository);
118+
}
119+
120+
@Test
121+
void shouldTriggerCacheRefresh() throws Exception {
122+
this.mockMvc
123+
.perform(MockMvcRequestBuilders.post("/refresh_cache")
124+
.accept(MediaType.APPLICATION_JSON)
125+
.contentType(MediaType.APPLICATION_JSON)
126+
.header("X-Hub-Signature", "sha1=E1C54005D92AE14A76A7C0CD164AEAC6E6740C64")
127+
.header("X-GitHub-Event", "push")
113128
.content(getTestPayload("push")))
114129
.andExpect(MockMvcResultMatchers.status().isOk())
115130
.andExpect(MockMvcResultMatchers.content()
116131
.string("{ \"message\": \"Successfully processed cache refresh\" }"));
117-
verify(this.projectRepository, times(1)).update(List.of("index-common.html"));
132+
verify(this.projectRepository, times(1)).update(List.of("added.html", "index-common.html"));
118133
}
119134

120135
private String getTestPayload(String fileName) throws Exception {

src/test/resources/io/spring/projectapi/web/webhook/push.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"ref": "refs/heads/master",
2+
"ref": "refs/heads/main",
33
"after": "a427f3f4db6c96ae5e86b610d0eef149393c9996",
44
"before": "3193db21394092a84d973bef8c4a3741aec1b814",
55
"created": false,
@@ -24,6 +24,7 @@
2424
"username": "cbeams"
2525
},
2626
"added": [
27+
"added.html"
2728
],
2829
"removed": [
2930
],
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"ref": "refs/heads/master",
3+
"after": "a427f3f4db6c96ae5e86b610d0eef149393c9996",
4+
"before": "3193db21394092a84d973bef8c4a3741aec1b814",
5+
"created": false,
6+
"deleted": false,
7+
"forced": false,
8+
"compare": "https://github.com/spring-guides/gs-test-guide/compare/3193db213940...a427f3f4db6c",
9+
"commits": [
10+
{
11+
"id": "6bdc0feaec5ccc85a9c4fe2a3e62bebe5a69e8fc",
12+
"distinct": true,
13+
"message": "Touch index-common to trigger gh-pages build",
14+
"timestamp": "2013-08-10T15:56:23-07:00",
15+
"url": "https://github.com/spring-projects/gs-test-guide/commit/6bdc0feaec5ccc85a9c4fe2a3e62bebe5a69e8fc",
16+
"author": {
17+
"name": "Chris Beams",
18+
"email": "[email protected]",
19+
"username": "cbeams"
20+
},
21+
"committer": {
22+
"name": "Chris Beams",
23+
"email": "[email protected]",
24+
"username": "cbeams"
25+
},
26+
"added": [
27+
],
28+
"removed": [
29+
],
30+
"modified": [
31+
"index-common.html"
32+
]
33+
},
34+
{
35+
"id": "a427f3f4db6c96ae5e86b610d0eef149393c9996",
36+
"distinct": true,
37+
"message": "Remove empty newline at end of index-common",
38+
"timestamp": "2013-08-10T16:12:39-07:00",
39+
"url": "https://github.com/spring-guides/gs-test-guide/commit/a427f3f4db6c96ae5e86b610d0eef149393c9996",
40+
"author": {
41+
"name": "Chris Beams",
42+
"email": "[email protected]",
43+
"username": "cbeams"
44+
},
45+
"committer": {
46+
"name": "Chris Beams",
47+
"email": "[email protected]",
48+
"username": "cbeams"
49+
},
50+
"added": [
51+
],
52+
"removed": [
53+
],
54+
"modified": [
55+
"index-common.html"
56+
]
57+
}
58+
],
59+
"head_commit": {
60+
"id": "a427f3f4db6c96ae5e86b610d0eef149393c9996",
61+
"distinct": true,
62+
"message": "Remove empty newline at end of index-common",
63+
"timestamp": "2013-08-10T16:12:39-07:00",
64+
"url": "https://github.com/spring-guides/gs-test-guide/commit/a427f3f4db6c96ae5e86b610d0eef149393c9996",
65+
"author": {
66+
"name": "Chris Beams",
67+
"email": "[email protected]",
68+
"username": "cbeams"
69+
},
70+
"committer": {
71+
"name": "Chris Beams",
72+
"email": "[email protected]",
73+
"username": "cbeams"
74+
},
75+
"added": [
76+
],
77+
"removed": [
78+
],
79+
"modified": [
80+
"index-common.html"
81+
]
82+
},
83+
"repository": {
84+
"id": 11634233,
85+
"name": "gs-test-guide",
86+
"url": "https://github.com/spring-guides/gs-test-guide",
87+
"description": "A test guide",
88+
"homepage": "",
89+
"watchers": 0,
90+
"stargazers": 0,
91+
"forks": 0,
92+
"fork": false,
93+
"size": 452,
94+
"owner": {
95+
"name": "spring-guides",
96+
"email": null
97+
},
98+
"private": false,
99+
"open_issues": 0,
100+
"has_issues": true,
101+
"has_downloads": true,
102+
"has_wiki": true,
103+
"language": "Java",
104+
"created_at": 1374668910,
105+
"pushed_at": 1376176368,
106+
"master_branch": "master",
107+
"organization": "spring-guides"
108+
},
109+
"pusher": {
110+
"name": "none"
111+
}
112+
}

0 commit comments

Comments
 (0)