From fd55574e542f04d37eb9d3db09f7ceba9986ec33 Mon Sep 17 00:00:00 2001 From: Uldis Ansmits Date: Mon, 11 Nov 2019 14:18:54 +0200 Subject: [PATCH] User PRIVATE-TOKEN Header instead of query parameter --- .../bitbucket/hook/MirrorRemoteAdmin.java | 20 ++++++++------- .../hook/MirrorRepositoryServlet.java | 4 +-- .../bitbucket/hook/MirrorRemoteAdminTest.java | 25 ++++++++++++------- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/englishtown/bitbucket/hook/MirrorRemoteAdmin.java b/src/main/java/com/englishtown/bitbucket/hook/MirrorRemoteAdmin.java index 5ef3629..579b43f 100644 --- a/src/main/java/com/englishtown/bitbucket/hook/MirrorRemoteAdmin.java +++ b/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 : " diff --git a/src/main/java/com/englishtown/bitbucket/hook/MirrorRepositoryServlet.java b/src/main/java/com/englishtown/bitbucket/hook/MirrorRepositoryServlet.java index beeb132..69aa283 100644 --- a/src/main/java/com/englishtown/bitbucket/hook/MirrorRepositoryServlet.java +++ b/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) { diff --git a/src/test/java/com/englishtown/bitbucket/hook/MirrorRemoteAdminTest.java b/src/test/java/com/englishtown/bitbucket/hook/MirrorRemoteAdminTest.java index 5ad3a5c..f25216c 100644 --- a/src/test/java/com/englishtown/bitbucket/hook/MirrorRemoteAdminTest.java +++ b/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)));