#89 -- Divide git api services

This commit is contained in:
Anton Romanov 2023-02-16 00:39:13 +04:00
parent aeab50cfb8
commit 3de664abb1
9 changed files with 170 additions and 21 deletions

View File

@ -0,0 +1,61 @@
package ru.ulstu.extractor.gitrepository.service;
import org.springframework.stereotype.Service;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.http.HttpService;
@Service
public class GithubApi implements GitApi {
private final HttpService httpService;
private static final String BRANCHES_COUNT_URL = "%s/api/v1/repos/%s/%s/branches";
private static final String STARS_COUNT_URL = "%s/api/v1/repos/%s/%s";
private static final String OPEN_ISSUES_URL = "%s/api/v1/repos/%s/%s/issues?state=open";
private static final String AUTHOR_COMPLETED_ISSUES_URL = "%s/api/v1/repos/%s/%s/issues?state=open";
private static final String AUTHORS_COUNT_URL = "%s/api/v1/repos/%s/%s/branches";
public GithubApi(HttpService httpService) {
this.httpService = httpService;
}
@Override
public Integer getBranchesCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), BRANCHES_COUNT_URL))
.length();
}
@Override
public Integer getStarsCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), STARS_COUNT_URL))
.getJSONObject(0)
.getInt("stars_count");
}
@Override
public Integer getOpenIssuesCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), OPEN_ISSUES_URL))
.length();
}
@Override
public String getFormattedUrl(String gitRepositoryUrl, String template) {
String[] urlParts = gitRepositoryUrl.split("/");
return String.format(template, urlParts[0] + "/" + urlParts[1] + "/" + urlParts[2], urlParts[3], urlParts[4]);
}
@Override
public Integer getAuthorsCompletedIssues(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), AUTHOR_COMPLETED_ISSUES_URL))
.length();
}
@Override
public Integer getAuthorsCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), AUTHORS_COUNT_URL))
.length();
}
}

View File

@ -0,0 +1,61 @@
package ru.ulstu.extractor.gitrepository.service;
import org.springframework.stereotype.Service;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.http.HttpService;
@Service
public class GitlabApi implements GitApi {
private final HttpService httpService;
private static final String BRANCHES_COUNT_URL = "%s/api/v1/repos/%s/%s/branches";
private static final String STARS_COUNT_URL = "%s/api/v1/repos/%s/%s";
private static final String OPEN_ISSUES_URL = "%s/api/v1/repos/%s/%s/issues?state=open";
private static final String AUTHOR_COMPLETED_ISSUES_URL = "%s/api/v1/repos/%s/%s/issues?state=open";
private static final String AUTHORS_COUNT_URL = "%s/api/v1/repos/%s/%s/branches";
public GitlabApi(HttpService httpService) {
this.httpService = httpService;
}
@Override
public Integer getBranchesCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), BRANCHES_COUNT_URL))
.length();
}
@Override
public Integer getStarsCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), STARS_COUNT_URL))
.getJSONObject(0)
.getInt("stars_count");
}
@Override
public Integer getOpenIssuesCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), OPEN_ISSUES_URL))
.length();
}
@Override
public String getFormattedUrl(String gitRepositoryUrl, String template) {
String[] urlParts = gitRepositoryUrl.split("/");
return String.format(template, urlParts[0] + "/" + urlParts[1] + "/" + urlParts[2], urlParts[3], urlParts[4]);
}
@Override
public Integer getAuthorsCompletedIssues(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), AUTHOR_COMPLETED_ISSUES_URL))
.length();
}
@Override
public Integer getAuthorsCount(Branch branch) {
return httpService
.get(getFormattedUrl(branch.getGitRepository().getUrl(), AUTHORS_COUNT_URL))
.length();
}
}

View File

@ -58,7 +58,7 @@ public class HttpService {
return jsonArray;
}
} catch (Exception e) {
return new JSONArray("[{response:\"empty\"}]");
throw new RuntimeException(e);
}
}
}

View File

@ -1,8 +1,8 @@
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.gitrepository.service.GitApi;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.ts.service.TimeSeriesService;
@ -10,11 +10,12 @@ import ru.ulstu.extractor.ts.service.TimeSeriesService;
@Component
public class AuthorTS extends ScheduledTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final GitApi gitApi;
private final ApplicationContext applicationContext;
public AuthorTS(TimeSeriesService timeSeriesService, GitApi gitApi) {
public AuthorTS(TimeSeriesService timeSeriesService,
ApplicationContext applicationContext) {
this.timeSeriesService = timeSeriesService;
this.gitApi = gitApi;
this.applicationContext = applicationContext;
}
@Override
@ -24,7 +25,8 @@ public class AuthorTS extends ScheduledTimeSeriesCreator {
@Override
public TimeSeriesValue getNewTimeSeriesValue(Branch branch) {
return new TimeSeriesValue(gitApi.getAuthorsCount(branch));
return new TimeSeriesValue(applicationContext.getBean(getGitApiServiceClass(branch))
.getAuthorsCount(branch));
}
@Override

View File

@ -1,8 +1,8 @@
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.gitrepository.service.GitApi;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.ts.service.TimeSeriesService;
@ -10,12 +10,12 @@ import ru.ulstu.extractor.ts.service.TimeSeriesService;
@Component
public class BranchTS extends ScheduledTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final GitApi gitApi;
private final ApplicationContext applicationContext;
public BranchTS(TimeSeriesService timeSeriesService,
GitApi gitApi) {
ApplicationContext applicationContext) {
this.timeSeriesService = timeSeriesService;
this.gitApi = gitApi;
this.applicationContext = applicationContext;
}
@Override
@ -30,6 +30,7 @@ public class BranchTS extends ScheduledTimeSeriesCreator {
@Override
public TimeSeriesValue getNewTimeSeriesValue(Branch branch) {
return new TimeSeriesValue(gitApi.getBranchesCount(branch));
return new TimeSeriesValue(applicationContext.getBean(getGitApiServiceClass(branch))
.getBranchesCount(branch));
}
}

View File

@ -1,8 +1,8 @@
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.gitrepository.service.GitApi;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.ts.service.TimeSeriesService;
@ -10,17 +10,18 @@ import ru.ulstu.extractor.ts.service.TimeSeriesService;
@Component
public class IssuesTS extends ScheduledTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final GitApi gitApi;
private final ApplicationContext applicationContext;
public IssuesTS(TimeSeriesService timeSeriesService,
GitApi gitApi) {
ApplicationContext applicationContext) {
this.timeSeriesService = timeSeriesService;
this.gitApi = gitApi;
this.applicationContext = applicationContext;
}
@Override
public TimeSeriesValue getNewTimeSeriesValue(Branch branch) {
return new TimeSeriesValue(gitApi.getOpenIssuesCount(branch));
return new TimeSeriesValue(applicationContext.getBean(getGitApiServiceClass(branch))
.getOpenIssuesCount(branch));
}
@Override

View File

@ -2,13 +2,24 @@ package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.gitrepository.service.GitApi;
import ru.ulstu.extractor.gitrepository.service.GitAtheneApi;
import ru.ulstu.extractor.gitrepository.service.GithubApi;
import ru.ulstu.extractor.gitrepository.service.GitlabApi;
import ru.ulstu.extractor.ts.creator.AbstractTimeSeriesCreator;
import ru.ulstu.extractor.ts.model.TimeSeries;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.util.StringUtils;
import java.util.Collections;
import java.util.Map;
public abstract class ScheduledTimeSeriesCreator extends AbstractTimeSeriesCreator {
private final Map<String, Class<? extends GitApi>> gitApiServiceHolder = Map.of(
"git.athene.tech", GitAtheneApi.class,
"github.com", GithubApi.class,
"gitlab.com", GitlabApi.class);
@Transactional
public void addTimeSeriesValue(Branch branch) {
TimeSeries timeSeries = getTimeSeriesService().findByBranchAndTypeOrCreate(branch, getTimeSeriesType());
@ -17,4 +28,8 @@ public abstract class ScheduledTimeSeriesCreator extends AbstractTimeSeriesCreat
}
public abstract TimeSeriesValue getNewTimeSeriesValue(Branch branch);
public Class<? extends GitApi> getGitApiServiceClass(Branch branch) {
return gitApiServiceHolder.get(StringUtils.getServerDomain(branch.getGitRepository().getUrl()));
}
}

View File

@ -1,8 +1,8 @@
package ru.ulstu.extractor.ts.creator.scheduled;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import ru.ulstu.extractor.branch.model.Branch;
import ru.ulstu.extractor.gitrepository.service.GitApi;
import ru.ulstu.extractor.ts.model.TimeSeriesType;
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
import ru.ulstu.extractor.ts.service.TimeSeriesService;
@ -10,11 +10,12 @@ import ru.ulstu.extractor.ts.service.TimeSeriesService;
@Component
public class StarTS extends ScheduledTimeSeriesCreator {
private final TimeSeriesService timeSeriesService;
private final GitApi gitApi;
private final ApplicationContext applicationContext;
public StarTS(TimeSeriesService timeSeriesService, GitApi gitApi) {
public StarTS(TimeSeriesService timeSeriesService,
ApplicationContext applicationContext) {
this.timeSeriesService = timeSeriesService;
this.gitApi = gitApi;
this.applicationContext = applicationContext;
}
@Override
@ -29,6 +30,7 @@ public class StarTS extends ScheduledTimeSeriesCreator {
@Override
public TimeSeriesValue getNewTimeSeriesValue(Branch branch) {
return new TimeSeriesValue(gitApi.getStarsCount(branch));
return new TimeSeriesValue(applicationContext.getBean(getGitApiServiceClass(branch))
.getStarsCount(branch));
}
}

View File

@ -9,6 +9,7 @@ import java.nio.file.FileSystems;
public class StringUtils {
public final static String EMPTY_STRING = "";
public final static String SERVER_DOMAIN_PATTERN = "%s";
private final static String PATH_SEPARATOR = FileSystems.getDefault().getSeparator();
public static String addPathSeparator(String path) {
@ -25,4 +26,9 @@ public class StringUtils {
public static boolean fileInSubdirectory(String filePath, String projectPath, String subDirectory) {
return filePath.startsWith(projectPath + PATH_SEPARATOR + subDirectory);
}
public static String getServerDomain(String url) {
String[] urlParts = url.split("/");
return urlParts[2];
}
}