|
|
@ -1,5 +1,6 @@
|
|
|
|
package ru.ulstu.extractor.gitrepository.service;
|
|
|
|
package ru.ulstu.extractor.gitrepository.service;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import ru.ulstu.extractor.branch.model.Branch;
|
|
|
|
import ru.ulstu.extractor.branch.model.Branch;
|
|
|
|
import ru.ulstu.extractor.http.HttpService;
|
|
|
|
import ru.ulstu.extractor.http.HttpService;
|
|
|
@ -7,11 +8,11 @@ import ru.ulstu.extractor.http.HttpService;
|
|
|
|
@Service
|
|
|
|
@Service
|
|
|
|
public class GitlabApi implements GitApi {
|
|
|
|
public class GitlabApi implements GitApi {
|
|
|
|
private final HttpService httpService;
|
|
|
|
private final HttpService httpService;
|
|
|
|
private static final String BRANCHES_COUNT_URL = "%s/api/v1/repos/%s/%s/branches";
|
|
|
|
private static final String BRANCHES_COUNT_URL = "%s/api/v4/projects/%s/repository/branches";
|
|
|
|
private static final String STARS_COUNT_URL = "%s/api/v1/repos/%s/%s";
|
|
|
|
private final static String PROJECT_ID_URL = "https://gitlab.com/api/v4/users/%s/projects?search=%s";
|
|
|
|
private static final String OPEN_ISSUES_URL = "%s/api/v1/repos/%s/%s/issues?state=open";
|
|
|
|
private static final String PROJECT_INFO_URL = "%s/api/v4/projects/%s";
|
|
|
|
private static final String AUTHOR_COMPLETED_ISSUES_URL = "%s/api/v1/repos/%s/%s/issues?state=open";
|
|
|
|
private static final String OPEN_ISSUES_URL = "%s/api/v4/projects/%s/issues?state=opened";
|
|
|
|
private static final String AUTHORS_COUNT_URL = "%s/api/v1/repos/%s/%s/branches";
|
|
|
|
private static final String COMPLETED_ISSUES_URL = "%s/api/v4/projects/%s/issues?state=closed";
|
|
|
|
|
|
|
|
|
|
|
|
public GitlabApi(HttpService httpService) {
|
|
|
|
public GitlabApi(HttpService httpService) {
|
|
|
|
this.httpService = httpService;
|
|
|
|
this.httpService = httpService;
|
|
|
@ -20,42 +21,51 @@ public class GitlabApi implements GitApi {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Integer getBranchesCount(Branch branch) {
|
|
|
|
public Integer getBranchesCount(Branch branch) {
|
|
|
|
return httpService
|
|
|
|
return httpService
|
|
|
|
.get(getFormattedUrl(branch.getGitRepository().getUrl(), BRANCHES_COUNT_URL))
|
|
|
|
.get(getFormattedUrl(branch, BRANCHES_COUNT_URL))
|
|
|
|
.length();
|
|
|
|
.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Integer getStarsCount(Branch branch) {
|
|
|
|
public Integer getStarsCount(Branch branch) {
|
|
|
|
return httpService
|
|
|
|
return httpService
|
|
|
|
.get(getFormattedUrl(branch.getGitRepository().getUrl(), STARS_COUNT_URL))
|
|
|
|
.get(getFormattedUrl(branch, PROJECT_INFO_URL))
|
|
|
|
.getJSONObject(0)
|
|
|
|
.getJSONObject(0)
|
|
|
|
.getInt("stars_count");
|
|
|
|
.getInt("star_count");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Integer getOpenIssuesCount(Branch branch) {
|
|
|
|
public Integer getOpenIssuesCount(Branch branch) {
|
|
|
|
return httpService
|
|
|
|
return httpService
|
|
|
|
.get(getFormattedUrl(branch.getGitRepository().getUrl(), OPEN_ISSUES_URL))
|
|
|
|
.get(getFormattedUrl(branch, OPEN_ISSUES_URL))
|
|
|
|
.length();
|
|
|
|
.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public String getFormattedUrl(String gitRepositoryUrl, String template) {
|
|
|
|
public String getFormattedUrl(Branch branch, String template) {
|
|
|
|
String[] urlParts = gitRepositoryUrl.split("/");
|
|
|
|
String[] urlParts = branch.getGitRepository().getUrl().split("/");
|
|
|
|
return String.format(template, urlParts[0] + "/" + urlParts[1] + "/" + urlParts[2], urlParts[3], urlParts[4]);
|
|
|
|
return String.format(template, urlParts[0] + "/" + urlParts[1] + "/" + urlParts[2], getProjectId(branch));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getFormattedUrlForProjectId(Branch branch, String template) {
|
|
|
|
|
|
|
|
String[] urlParts = branch.getGitRepository().getUrl().split("/");
|
|
|
|
|
|
|
|
// получаем корректное название репозитория, gitlab всегда добавляет .git в конец
|
|
|
|
|
|
|
|
return String.format(template, urlParts[3], branch.getGitRepository().getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Integer getAuthorsCompletedIssues(Branch branch) {
|
|
|
|
public Integer getAuthorsCompletedIssues(Branch branch) {
|
|
|
|
return httpService
|
|
|
|
return httpService
|
|
|
|
.get(getFormattedUrl(branch.getGitRepository().getUrl(), AUTHOR_COMPLETED_ISSUES_URL))
|
|
|
|
.get(getFormattedUrl(branch, COMPLETED_ISSUES_URL))
|
|
|
|
.length();
|
|
|
|
.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
private String getProjectId(Branch branch) {
|
|
|
|
public Integer getAuthorsCount(Branch branch) {
|
|
|
|
JSONArray projects = httpService.get(getFormattedUrlForProjectId(branch, PROJECT_ID_URL));
|
|
|
|
return httpService
|
|
|
|
for (int i = 0; i < projects.length(); i++) {
|
|
|
|
.get(getFormattedUrl(branch.getGitRepository().getUrl(), AUTHORS_COUNT_URL))
|
|
|
|
if (projects.getJSONObject(i).get("name").equals(branch.getGitRepository().getName())) {
|
|
|
|
.length();
|
|
|
|
return String.valueOf(projects.getJSONObject(i).getInt("id"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new RuntimeException("Id проекта не найден: " + branch.getGitRepository().getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|