|
|
@ -25,90 +25,94 @@ public class MirrorRemoteAdmin { |
|
|
|
private final I18nService i18nService; |
|
|
|
private final I18nService i18nService; |
|
|
|
|
|
|
|
|
|
|
|
Client client; |
|
|
|
Client client; |
|
|
|
public MirrorRemoteAdmin(PasswordEncryptor passwordEncryptor,I18nService i18nService) { |
|
|
|
|
|
|
|
this.passwordEncryptor=passwordEncryptor; |
|
|
|
public MirrorRemoteAdmin(PasswordEncryptor passwordEncryptor, I18nService i18nService) { |
|
|
|
this.i18nService=i18nService; |
|
|
|
this.passwordEncryptor = passwordEncryptor; |
|
|
|
ClientConfig config=new DefaultClientConfig(); |
|
|
|
this.i18nService = i18nService; |
|
|
|
|
|
|
|
ClientConfig config = new DefaultClientConfig(); |
|
|
|
this.client = Client.create(config); |
|
|
|
this.client = Client.create(config); |
|
|
|
} |
|
|
|
} |
|
|
|
private void addToStream(StringOutputHandler outputHandler,String text) { |
|
|
|
|
|
|
|
|
|
|
|
private void addToStream(StringOutputHandler outputHandler, String text) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
outputHandler.process(new ByteArrayInputStream(text.getBytes())); |
|
|
|
outputHandler.process(new ByteArrayInputStream(text.getBytes())); |
|
|
|
} catch (ProcessException e) { |
|
|
|
} catch (ProcessException e) { |
|
|
|
log.error("Failed to process response: "+e.getMessage()); |
|
|
|
log.error("Failed to process response: " + e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void delete(MirrorSettings settings, Repository repository, StringOutputHandler outputHandler) { |
|
|
|
public void delete(MirrorSettings settings, Repository repository, StringOutputHandler outputHandler) { |
|
|
|
String plainPassword = passwordEncryptor.decrypt(settings.password); |
|
|
|
String plainPassword = passwordEncryptor.decrypt(settings.password); |
|
|
|
String plainPrivateToken = passwordEncryptor.decrypt(settings.privateToken); |
|
|
|
String plainPrivateToken = passwordEncryptor.decrypt(settings.privateToken); |
|
|
|
PasswordHandler passwordHandler = new PasswordHandler(plainPassword,plainPrivateToken, |
|
|
|
PasswordHandler passwordHandler = new PasswordHandler(plainPassword, plainPrivateToken, |
|
|
|
new GitCommandExitHandler(i18nService, repository)); |
|
|
|
new GitCommandExitHandler(i18nService, repository)); |
|
|
|
RuntimeException e=null; |
|
|
|
RuntimeException e = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
delete(settings, repository, passwordHandler); |
|
|
|
delete(settings, repository, passwordHandler); |
|
|
|
} catch (RuntimeException deleteException) { |
|
|
|
} catch (RuntimeException deleteException) { |
|
|
|
e=deleteException; |
|
|
|
e = deleteException; |
|
|
|
} |
|
|
|
} |
|
|
|
addToStream(outputHandler,passwordHandler.getOutput()); |
|
|
|
addToStream(outputHandler, passwordHandler.getOutput()); |
|
|
|
if(e!=null) { |
|
|
|
if (e != null) { |
|
|
|
throw e; |
|
|
|
throw e; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void delete(MirrorSettings settings, Repository repository, PasswordHandler passwordHandler) { |
|
|
|
private void delete(MirrorSettings settings, Repository repository, PasswordHandler passwordHandler) { |
|
|
|
ObjectMapper mapper = new ObjectMapper(); |
|
|
|
ObjectMapper mapper = new ObjectMapper(); |
|
|
|
String plainPrivateToken = passwordEncryptor.decrypt(settings.privateToken); |
|
|
|
String plainPrivateToken = passwordEncryptor.decrypt(settings.privateToken); |
|
|
|
|
|
|
|
|
|
|
|
if(settings.restApiURL.isEmpty()) { |
|
|
|
if (settings.restApiURL.isEmpty()) { |
|
|
|
log.error("Remote REST Api URL not configured for "+ repository.getName()); |
|
|
|
log.error("Remote REST Api URL not configured for " + repository.getName()); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
WebResource webResource = client |
|
|
|
WebResource webResource = client |
|
|
|
.resource(settings.restApiURL+"/api/v4/projects") |
|
|
|
.resource(settings.restApiURL + "/api/v4/projects") |
|
|
|
.queryParam("search",repository.getName()); |
|
|
|
.queryParam("search", repository.getName()); |
|
|
|
if(!settings.privateToken.isEmpty()) { |
|
|
|
if (!settings.privateToken.isEmpty()) { |
|
|
|
|
|
|
|
|
|
|
|
webResource=webResource.queryParam("private_token", plainPrivateToken); |
|
|
|
webResource = webResource.queryParam("private_token", plainPrivateToken); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON) |
|
|
|
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON) |
|
|
|
.get(ClientResponse.class); |
|
|
|
.get(ClientResponse.class); |
|
|
|
|
|
|
|
|
|
|
|
if (response.getStatus() != 200) { |
|
|
|
if (response.getStatus() != 200) { |
|
|
|
addToStream(passwordHandler,response.toString()); |
|
|
|
addToStream(passwordHandler, response.toString()); |
|
|
|
throw new RuntimeException("Failed : HTTP error code : " |
|
|
|
throw new RuntimeException("Failed : HTTP error code : " |
|
|
|
+ response.getStatus()); |
|
|
|
+ response.getStatus()); |
|
|
|
} |
|
|
|
} |
|
|
|
JsonNode output; |
|
|
|
JsonNode output; |
|
|
|
try { |
|
|
|
try { |
|
|
|
output = mapper.readTree(response.getEntityInputStream()); |
|
|
|
output = mapper.readTree(response.getEntityInputStream()); |
|
|
|
} catch (IOException e){ |
|
|
|
} catch (IOException e) { |
|
|
|
addToStream(passwordHandler,e.toString()); |
|
|
|
addToStream(passwordHandler, e.toString()); |
|
|
|
throw new RuntimeException("Failed : Invalid response data from "+ settings.restApiURL + " : " + e.getMessage()); |
|
|
|
throw new RuntimeException("Failed : Invalid response data from " + settings.restApiURL + " : " + e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
Integer repoId=null; |
|
|
|
Integer repoId = null; |
|
|
|
for(JsonNode project: output) { |
|
|
|
for (JsonNode project : output) { |
|
|
|
if(project.get("path_with_namespace").asText().equals(repository.getProject().getKey() +"/"+repository.getName())) { |
|
|
|
if (project.get("path_with_namespace").asText().equals(repository.getProject().getKey() + "/" + repository.getName())) { |
|
|
|
repoId= project.get("id").asInt(); |
|
|
|
repoId = project.get("id").asInt(); |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if(repoId == null) { |
|
|
|
if (repoId == null) { |
|
|
|
addToStream(passwordHandler, response.toString()); |
|
|
|
addToStream(passwordHandler, response.toString()); |
|
|
|
throw new RuntimeException("Remote repository not found"); |
|
|
|
throw new RuntimeException("Remote repository not found"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
webResource = client |
|
|
|
webResource = client |
|
|
|
.resource(settings.restApiURL+"/api/v4/projects/"+repoId); |
|
|
|
.resource(settings.restApiURL + "/api/v4/projects/" + repoId); |
|
|
|
if(!settings.privateToken.isEmpty()) { |
|
|
|
if (!settings.privateToken.isEmpty()) { |
|
|
|
webResource=webResource.queryParam("private_token", plainPrivateToken); |
|
|
|
webResource = webResource.queryParam("private_token", plainPrivateToken); |
|
|
|
} |
|
|
|
} |
|
|
|
response = webResource.accept(MediaType.APPLICATION_JSON) |
|
|
|
response = webResource.accept(MediaType.APPLICATION_JSON) |
|
|
|
.delete(ClientResponse.class); |
|
|
|
.delete(ClientResponse.class); |
|
|
|
if (response.getStatus() != 202) { |
|
|
|
if (response.getStatus() != 202) { |
|
|
|
addToStream(passwordHandler,response.toString()); |
|
|
|
addToStream(passwordHandler, response.toString()); |
|
|
|
throw new RuntimeException("Failed : HTTP error code : " |
|
|
|
throw new RuntimeException("Failed : HTTP error code : " |
|
|
|
+ response.getStatus()); |
|
|
|
+ response.getStatus()); |
|
|
|
} |
|
|
|
} |
|
|
|
addToStream(passwordHandler,response.toString()); |
|
|
|
addToStream(passwordHandler, response.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|