2022-09-04 21:21:24 +02:00
|
|
|
package io.gitlab.jfronny.inceptum.common.api;
|
2021-10-31 16:59:25 +01:00
|
|
|
|
2022-04-28 23:13:37 +02:00
|
|
|
import io.gitlab.jfronny.commons.HttpUtils;
|
|
|
|
import io.gitlab.jfronny.gson.reflect.TypeToken;
|
2022-09-04 21:21:24 +02:00
|
|
|
import io.gitlab.jfronny.inceptum.common.model.gitlab.*;
|
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/";
|
2022-09-18 19:28:26 +02:00
|
|
|
|
|
|
|
public static final long PROJECT_ID = 30862253L;
|
|
|
|
public static final String PROJECT_MAVEN = "https://gitlab.com/api/v4/projects/" + PROJECT_ID + "/packages/maven/";
|
|
|
|
|
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 {
|
2022-04-28 23:13:37 +02:00
|
|
|
return HttpUtils.get(PROJECTS + projectId).sendSerialized(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 {
|
2022-04-28 23:13:37 +02:00
|
|
|
return HttpUtils.get(PROJECTS + project.id + "/packages?order_by=created_at&sort=desc").sendSerialized(packageInfoListType);
|
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 {
|
2022-04-28 23:13:37 +02:00
|
|
|
List<GitlabJob> list = HttpUtils.get(PROJECTS + project.id + "/pipelines/" + pipelineId + "/jobs").sendSerialized(jobListType);
|
2022-01-19 22:43:50 +01:00
|
|
|
list.sort((left, right) -> right.created_at.compareTo(left.created_at));
|
|
|
|
return list;
|
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-04-28 23:13:37 +02:00
|
|
|
List<GitlabPackageFile> files = HttpUtils.get(PROJECTS + project.id + "/packages/" + packageInfo.id + "/package_files?per_page=100&page=" + ++page).sendSerialized(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|