Browse Source

User PRIVATE-TOKEN Header instead of query parameter

pull/76/head
Uldis Ansmits 5 years ago
parent
commit
fd55574e54
  1. 20
      src/main/java/com/englishtown/bitbucket/hook/MirrorRemoteAdmin.java
  2. 4
      src/main/java/com/englishtown/bitbucket/hook/MirrorRepositoryServlet.java
  3. 25
      src/test/java/com/englishtown/bitbucket/hook/MirrorRemoteAdminTest.java

20
src/main/java/com/englishtown/bitbucket/hook/MirrorRemoteAdmin.java

@ -69,14 +69,13 @@ public class MirrorRemoteAdmin {
WebResource webResource = client
.resource(settings.restApiURL + "/api/v4/projects")
.queryParam("search", repository.getName());
if (!settings.privateToken.isEmpty()) {
webResource = webResource.queryParam("private_token", plainPrivateToken);
WebResource.Builder webResourceBuilder= webResource.getRequestBuilder().accept(MediaType.APPLICATION_JSON);
if (!settings.privateToken.isEmpty()) {
webResourceBuilder = webResourceBuilder.header("PRIVATE-TOKEN",plainPrivateToken);
}
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
.get(ClientResponse.class);
ClientResponse response = webResourceBuilder.get(ClientResponse.class);
if (response.getStatus() != 200) {
addToStream(passwordHandler, response.toString());
throw new RuntimeException("Failed : HTTP error code : "
@ -91,7 +90,10 @@ public class MirrorRemoteAdmin {
}
Integer repoId = null;
for (JsonNode project : output) {
if (project.get("path_with_namespace").asText().equals(repository.getProject().getKey() + "/" + repository.getName())) {
if (!project.has("name_with_namespace")) {
continue;
}
if (project.get("name_with_namespace").asText().equals(repository.getProject().getName() + " / " + repository.getName())) {
repoId = project.get("id").asInt();
break;
}
@ -103,11 +105,11 @@ public class MirrorRemoteAdmin {
webResource = client
.resource(settings.restApiURL + "/api/v4/projects/" + repoId);
webResourceBuilder= webResource.getRequestBuilder().accept(MediaType.APPLICATION_JSON);
if (!settings.privateToken.isEmpty()) {
webResource = webResource.queryParam("private_token", plainPrivateToken);
webResourceBuilder = webResourceBuilder.header("PRIVATE-TOKEN",plainPrivateToken);
}
response = webResource.accept(MediaType.APPLICATION_JSON)
.delete(ClientResponse.class);
response = webResourceBuilder.delete(ClientResponse.class);
if (response.getStatus() != 202) {
addToStream(passwordHandler, response.toString());
throw new RuntimeException("Failed : HTTP error code : "

4
src/main/java/com/englishtown/bitbucket/hook/MirrorRepositoryServlet.java

@ -88,8 +88,8 @@ public class MirrorRepositoryServlet extends HttpServlet {
mirrorRemoteAdmin.delete(mirrorSettings, repository, outputHandler);
allSettings.put("stdout" + mirrorSettings.suffix, outputHandler.getOutput());
} catch (Exception e) {
log.debug("Deleting Mirroring failed with " + e);
allSettings.put("stderr" + mirrorSettings.suffix, e + "\n" + outputHandler.getOutput());
log.debug("Deleting Mirroring failed with " + e.getMessage());
allSettings.put("stderr" + mirrorSettings.suffix, e.getMessage() + "\n" + outputHandler.getOutput());
}
}
if (req.getParameter("trigger" + mirrorSettings.suffix) != null) {

25
src/test/java/com/englishtown/bitbucket/hook/MirrorRemoteAdminTest.java

@ -58,6 +58,7 @@ public class MirrorRemoteAdminTest {
when(repo.getName()).thenReturn("test");
project = mock(Project.class);
when(project.getKey()).thenReturn("PROJECT");
when(project.getName()).thenReturn("PROJECT");
when(repo.getProject()).thenReturn(project);
when(passwordEncryptor.decrypt(anyString())).then(AdditionalAnswers.returnsFirstArg());
}
@ -112,8 +113,9 @@ public class MirrorRemoteAdminTest {
mirrorSettings.restApiURL = "http://localhost:" + wireMockRule.port();
mirrorSettings.privateToken = "PRIVATETOKEN";
stubFor(get(urlEqualTo("/api/v4/projects?search=test&private_token=PRIVATETOKEN"))
stubFor(get(urlEqualTo("/api/v4/projects?search=test"))
.withHeader("Accept", equalTo(MediaType.APPLICATION_JSON))
.withHeader("PRIVATE-TOKEN", equalTo(mirrorSettings.privateToken))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", MediaType.APPLICATION_JSON)
@ -131,12 +133,13 @@ public class MirrorRemoteAdminTest {
mirrorSettings.restApiURL = "http://localhost:" + wireMockRule.port();
mirrorSettings.privateToken = "PRIVATETOKEN";
stubFor(get(urlEqualTo("/api/v4/projects?search=test&private_token=PRIVATETOKEN"))
stubFor(get(urlEqualTo("/api/v4/projects?search=test"))
.withHeader("Accept", equalTo(MediaType.APPLICATION_JSON))
.withHeader("PRIVATE-TOKEN", equalTo(mirrorSettings.privateToken))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", MediaType.APPLICATION_JSON)
.withBody("[{\"id\":1,\"path_with_namespace\":\"PROJECT/testing\"},{\"id\":3,\"path_with_namespace\":\"OTHER/test\"}]")));
.withBody("[{\"id\":1,\"name_with_namespace\":\"PROJECT / testing\"},{\"id\":3,\"name_with_namespace\":\"OTHER / test\"}]")));
try {
mirrorRemoteAdmin.delete(mirrorSettings, repo, handler);
Assert.fail("Exception not thrown");
@ -150,15 +153,17 @@ public class MirrorRemoteAdminTest {
mirrorSettings.restApiURL = "http://localhost:" + wireMockRule.port();
mirrorSettings.privateToken = "PRIVATETOKEN";
stubFor(get(urlEqualTo("/api/v4/projects?search=test&private_token=PRIVATETOKEN"))
stubFor(get(urlEqualTo("/api/v4/projects?search=test"))
.withHeader("Accept", equalTo(MediaType.APPLICATION_JSON))
.withHeader("PRIVATE-TOKEN", equalTo(mirrorSettings.privateToken))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", MediaType.APPLICATION_JSON)
.withBody("[{\"id\":1,\"path_with_namespace\":\"PROJECT/testing\"},{\"id\":2,\"path_with_namespace\":\"PROJECT/test\"},{\"id\":3,\"path_with_namespace\":\"OTHER/test\"}]")));
.withBody("[{\"id\":1,\"path_with_namespace\":\"PROJECT / testing\"},{\"id\":2,\"name_with_namespace\":\"PROJECT / test\"},{\"id\":3,\"name_with_namespace\":\"OTHER/test\"}]")));
stubFor(delete(urlEqualTo("/api/v4/projects/2?private_token=PRIVATETOKEN"))
stubFor(delete(urlEqualTo("/api/v4/projects/2"))
.withHeader("Accept", equalTo(MediaType.APPLICATION_JSON))
.withHeader("PRIVATE-TOKEN", equalTo(mirrorSettings.privateToken))
.willReturn(aResponse()
.withStatus(403)
.withHeader("Content-Type", MediaType.TEXT_PLAIN)
@ -177,15 +182,17 @@ public class MirrorRemoteAdminTest {
mirrorSettings.restApiURL = "http://localhost:" + wireMockRule.port();
mirrorSettings.privateToken = "PRIVATETOKEN";
stubFor(get(urlEqualTo("/api/v4/projects?search=test&private_token=PRIVATETOKEN"))
stubFor(get(urlEqualTo("/api/v4/projects?search=test"))
.withHeader("Accept", equalTo(MediaType.APPLICATION_JSON))
.withHeader("PRIVATE-TOKEN", equalTo(mirrorSettings.privateToken))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", MediaType.APPLICATION_JSON)
.withBody("[{\"id\":1,\"path_with_namespace\":\"PROJECT/testing\"},{\"id\":2,\"path_with_namespace\":\"PROJECT/test\"},{\"id\":3,\"path_with_namespace\":\"OTHER/test\"}]")));
.withBody("[{\"id\":1,\"name_with_namespace\":\"PROJECT / testing\"},{\"id\":2,\"name_with_namespace\":\"PROJECT / test\"},{\"id\":3,\"name_with_namespace\":\"OTHER / test\"}]")));
stubFor(delete(urlEqualTo("/api/v4/projects/2?private_token=PRIVATETOKEN"))
stubFor(delete(urlEqualTo("/api/v4/projects/2"))
.withHeader("Accept", equalTo(MediaType.APPLICATION_JSON))
.withHeader("PRIVATE-TOKEN", equalTo(mirrorSettings.privateToken))
.willReturn(aResponse()
.withStatus(202)));

Loading…
Cancel
Save