2021-10-31 16:59:25 +01:00
|
|
|
package io.gitlab.jfronny.inceptum.util.api;
|
|
|
|
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
2022-01-18 18:43:39 +01:00
|
|
|
import io.gitlab.jfronny.inceptum.model.gitlab.GitlabJob;
|
|
|
|
import io.gitlab.jfronny.inceptum.model.gitlab.GitlabPackage;
|
|
|
|
import io.gitlab.jfronny.inceptum.model.gitlab.GitlabPackageFile;
|
|
|
|
import io.gitlab.jfronny.inceptum.model.gitlab.GitlabProject;
|
|
|
|
import io.gitlab.jfronny.inceptum.util.HttpUtils;
|
2021-10-31 16:59:25 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.lang.reflect.Type;
|
2022-01-18 18:43:39 +01:00
|
|
|
import java.net.URISyntaxException;
|
2021-10-31 16:59:25 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.function.Predicate;
|
|
|
|
|
|
|
|
public class GitlabApi {
|
2022-01-18 18:43:39 +01:00
|
|
|
public static final String PROJECTS = "https://gitlab.com/api/v4/projects/";
|
2021-12-11 13:53:46 +01:00
|
|
|
private static final Type packageInfoListType = new TypeToken<List<GitlabPackage>>() {}.getType();
|
|
|
|
private static final Type jobListType = new TypeToken<List<GitlabJob>>() {}.getType();
|
|
|
|
private static final Type packageFileInfoListType = new TypeToken<List<GitlabPackageFile>>() {}.getType();
|
2021-10-31 16:59:25 +01:00
|
|
|
|
2022-01-18 18:43:39 +01:00
|
|
|
public static GitlabProject getProject(Long projectId) throws IOException, URISyntaxException {
|
|
|
|
return HttpUtils.get(PROJECTS + projectId).sendJson(GitlabProject.class);
|
2021-10-31 16:59:25 +01:00
|
|
|
}
|
|
|
|
|
2022-01-18 18:43:39 +01:00
|
|
|
public static List<GitlabPackage> getPackages(GitlabProject project) throws IOException, URISyntaxException {
|
|
|
|
List<GitlabPackage> list = HttpUtils.get(PROJECTS + project.id + "/packages").sendJson(packageInfoListType);
|
2021-12-11 14:21:58 +01:00
|
|
|
list.sort((left, right) -> right.created_at.compareTo(left.created_at));
|
|
|
|
return list;
|
2021-10-31 16:59:25 +01:00
|
|
|
}
|
|
|
|
|
2022-01-18 18:43:39 +01:00
|
|
|
public static List<GitlabJob> getJobs(GitlabProject project, Long pipelineId) throws IOException, URISyntaxException {
|
|
|
|
return HttpUtils.get(PROJECTS + project.id + "/pipelines/" + pipelineId + "/jobs").sendJson(jobListType);
|
2021-12-11 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
2022-01-18 18:43:39 +01:00
|
|
|
public static GitlabPackageFile getFile(GitlabProject project, GitlabPackage packageInfo, Predicate<GitlabPackageFile> isValid) throws IOException, URISyntaxException {
|
2021-10-31 16:59:25 +01:00
|
|
|
int page = 0;
|
|
|
|
while (true) {
|
2022-01-18 18:43:39 +01:00
|
|
|
List<GitlabPackageFile> files = HttpUtils.get(PROJECTS + project.id + "/packages/" + packageInfo.id + "/package_files?per_page=100&page=" + ++page).sendJson(packageFileInfoListType);
|
2021-10-31 16:59:25 +01:00
|
|
|
if (files.isEmpty()) return null;
|
2021-12-11 13:53:46 +01:00
|
|
|
for (GitlabPackageFile file : files) {
|
2021-10-31 16:59:25 +01:00
|
|
|
if (isValid.test(file))
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|