GitLabUtil.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package api.common.util;
  2. import lombok.SneakyThrows;
  3. import org.gitlab4j.api.GitLabApi;
  4. import org.gitlab4j.api.models.Commit;
  5. import org.gitlab4j.api.models.Project;
  6. import java.util.Date;
  7. import java.util.List;
  8. /**
  9. * <!-- https://mvnrepository.com/artifact/org.gitlab4j/gitlab4j-api -->
  10. * <dependency>
  11. * <groupId>org.gitlab4j</groupId>
  12. * <artifactId>gitlab4j-api</artifactId>
  13. * <version>5.0.1</version>
  14. * </dependency>
  15. */
  16. public class GitLabUtil {
  17. /**
  18. * @param projectUrl git 仓库地址 http://gitlab-441-442-443/root/test1021.git
  19. * @param username
  20. * @param password
  21. * @return
  22. */
  23. @SneakyThrows
  24. public static long getLastCommitTimestamp(String projectUrl, String username, String password) {
  25. String[] split = projectUrl.split("/");
  26. String hostUrl = split[0] + "/" + split[1] + "/" + split[2];
  27. String projectName = split[4].split("\\.")[0];
  28. GitLabApi gitLabApi = GitLabApi.oauth2Login(hostUrl, username, password);
  29. Project project = gitLabApi.getProjectApi().getProject(username, projectName);
  30. Long projectId = project.getId();
  31. List<Commit> master = gitLabApi.getCommitsApi().getCommits(projectId, "master", new Date(1666331629),
  32. new Date(System.currentTimeMillis()));
  33. master.sort((commit1, commit2) -> commit2.getCommittedDate().compareTo(commit1.getCommittedDate())); // 降序
  34. Commit commit = master.get(0);
  35. Date committedDate = commit.getCommittedDate();
  36. return committedDate.getTime();
  37. }
  38. }