diff --git a/src/main/java/ch/usi/si/seart/config/RestTemplateConfig.java b/src/main/java/ch/usi/si/seart/config/RestTemplateConfig.java deleted file mode 100644 index 06c7ff3d..00000000 --- a/src/main/java/ch/usi/si/seart/config/RestTemplateConfig.java +++ /dev/null @@ -1,20 +0,0 @@ -package ch.usi.si.seart.config; - -import org.springframework.boot.web.client.RestTemplateBuilder; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.client.RestTemplate; - -import java.time.Duration; - -@Configuration -public class RestTemplateConfig { - - @Bean - public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) { - return restTemplateBuilder - .setConnectTimeout(Duration.ofMinutes(1)) - .setReadTimeout(Duration.ofMinutes(1)) - .build(); - } -} diff --git a/src/main/java/ch/usi/si/seart/job/CleanUpProjectsJob.java b/src/main/java/ch/usi/si/seart/job/CleanUpProjectsJob.java index 9a669040..9c289307 100644 --- a/src/main/java/ch/usi/si/seart/job/CleanUpProjectsJob.java +++ b/src/main/java/ch/usi/si/seart/job/CleanUpProjectsJob.java @@ -1,6 +1,8 @@ package ch.usi.si.seart.job; import ch.usi.si.seart.config.properties.CleanUpProperties; +import ch.usi.si.seart.exception.github.GitHubRestException; +import ch.usi.si.seart.github.GitHubRestConnector; import ch.usi.si.seart.service.GitRepoService; import ch.usi.si.seart.stereotype.Job; import lombok.AccessLevel; @@ -13,15 +15,13 @@ import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.http.HttpStatus; import org.springframework.scheduling.TriggerContext; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.support.CronTrigger; import org.springframework.scheduling.support.SimpleTriggerContext; import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpStatusCodeException; -import org.springframework.web.client.RestClientException; -import org.springframework.web.client.RestTemplate; import java.util.Date; @@ -36,7 +36,7 @@ public class CleanUpProjectsJob implements Runnable { ObjectFactory lsRemoteCommandFactory; - RestTemplate restTemplate; + GitHubRestConnector gitHubRestConnector; GitRepoService gitRepoService; @@ -67,16 +67,15 @@ public void run() { private boolean checkIfExists(String name) { try { - String url = String.format("https://github.com/%s.git", name); - return checkWithGit(url) || checkWithHTTP(url); + return checkWithGit(name) || checkWithHTTP(name); } catch (HttpStatusCodeException ex) { log.error("An exception has occurred during cleanup! [{}]", ex.getStatusCode()); return true; - } catch (GitAPIException | RestClientException ex) { + } catch (GitAPIException | GitHubRestException ex) { /* - * It's safer to keep projects which we fail to check, + * It is safer to keep projects which we fail to check, * rather than removing them from the database. - * Let's say there is a bug with our implementation, + * Let us say there is a bug with our implementation, * do we prefer to lose stored entries one by one? */ log.error("An exception has occurred during cleanup!", ex); @@ -84,8 +83,9 @@ private boolean checkIfExists(String name) { } } - private boolean checkWithGit(String url) throws GitAPIException { + private boolean checkWithGit(String name) throws GitAPIException { try { + String url = String.format("https://github.com/%s.git", name); lsRemoteCommandFactory.getObject().setRemote(url).call(); return true; } catch (TransportException ex) { @@ -93,12 +93,8 @@ private boolean checkWithGit(String url) throws GitAPIException { } } - private boolean checkWithHTTP(String url) { - try { - restTemplate.getForEntity(url, String.class); - return true; - } catch (HttpClientErrorException.NotFound ex) { - return false; - } + private boolean checkWithHTTP(String name) { + HttpStatus status = gitHubRestConnector.pingRepository(name); + return status != HttpStatus.NOT_FOUND; } }