Skip to content

Commit

Permalink
Refactor CleanUpProjectsJob to use GitHubRestConnector
Browse files Browse the repository at this point in the history
  • Loading branch information
dabico committed Sep 19, 2024
1 parent ab6b0c8 commit 5ce34b7
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/main/java/ch/usi/si/seart/job/CleanUpProjectsJob.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -36,7 +36,7 @@ public class CleanUpProjectsJob implements Runnable {

ObjectFactory<LsRemoteCommand> lsRemoteCommandFactory;

RestTemplate restTemplate;
GitHubRestConnector gitHubRestConnector;

GitRepoService gitRepoService;

Expand Down Expand Up @@ -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.
Expand All @@ -84,21 +83,18 @@ 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) {
return false;
}
}

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;
}
}

0 comments on commit 5ce34b7

Please sign in to comment.