From 8a57ca893baa680233c3b565cf29177895d91b66 Mon Sep 17 00:00:00 2001 From: dabico Date: Thu, 19 Sep 2024 11:15:10 +0200 Subject: [PATCH 1/3] Update comment --- src/main/java/ch/usi/si/seart/job/CleanUpProjectsJob.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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..a9ce6023 100644 --- a/src/main/java/ch/usi/si/seart/job/CleanUpProjectsJob.java +++ b/src/main/java/ch/usi/si/seart/job/CleanUpProjectsJob.java @@ -74,9 +74,9 @@ private boolean checkIfExists(String name) { return true; } catch (GitAPIException | RestClientException 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); From 78ec42d8efb1ad86211353bfcd5cc6a5d4aa8634 Mon Sep 17 00:00:00 2001 From: dabico Date: Thu, 19 Sep 2024 11:32:05 +0200 Subject: [PATCH 2/3] Refactor `CleanUpProjectsJob` to use `GitHubRestConnector` --- .../usi/si/seart/job/CleanUpProjectsJob.java | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) 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 a9ce6023..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,12 +67,11 @@ 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 is safer to keep projects which we fail to check, * rather than removing them from the database. @@ -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; } } From 14e49530dc8abb4e08463dfae3078c0cb681941d Mon Sep 17 00:00:00 2001 From: dabico Date: Thu, 19 Sep 2024 11:35:44 +0200 Subject: [PATCH 3/3] Remove redundant `RestTemplateConfig` --- .../si/seart/config/RestTemplateConfig.java | 20 ------------------- 1 file changed, 20 deletions(-) delete mode 100644 src/main/java/ch/usi/si/seart/config/RestTemplateConfig.java 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(); - } -}