1
0

ProjectServiceImpl.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package com.nju.edu.gitlab.service.impl;
  2. import com.nju.edu.gitlab.service.api.ProjectService;
  3. import com.nju.edu.gitlab.util.ApplicationProperties;
  4. import com.nju.edu.gitlab.web.dto.*;
  5. import org.gitlab4j.api.GitLabApi;
  6. import org.gitlab4j.api.GitLabApiException;
  7. import org.gitlab4j.api.models.*;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.stereotype.Component;
  11. import java.io.*;
  12. import java.util.Base64;
  13. import java.util.HashSet;
  14. import java.util.List;
  15. import java.util.Set;
  16. import java.util.stream.Collectors;
  17. @Component
  18. public class ProjectServiceImpl implements ProjectService {
  19. private final GitLabApi gitlabApi;
  20. private final String gitlabHost;
  21. private final String token;
  22. @Autowired
  23. public ProjectServiceImpl(ApplicationProperties properties) {
  24. this.gitlabHost=properties.getHost();
  25. this.token= properties.getToken();
  26. this.gitlabApi = new GitLabApi(this.gitlabHost, this.token);
  27. }
  28. @Override
  29. public Integer createProject(ProjectDTO projectDTO) throws GitLabApiException {
  30. String projectName=projectDTO.getProjectName();
  31. Visibility visibility=projectDTO.getVisibility();
  32. Project project=new Project();
  33. project.setName(projectName);
  34. project.setVisibility(visibility);
  35. project=gitlabApi.getProjectApi().createProject(project);
  36. return project.getId();
  37. }
  38. @Override
  39. public Integer forkProject(UserForkDTO userForkDTO) throws GitLabApiException {
  40. int userId=userForkDTO.getUserId();
  41. int projectId=userForkDTO.getProjectId();
  42. Integer namespaceId=fetchNameSpaceId(gitlabApi.getUserApi().getUser(userId).getUsername());
  43. if (namespaceId == null)
  44. throw new NullPointerException();
  45. Project project;
  46. try{
  47. gitlabApi.setSudoAsId(userId);
  48. project=gitlabApi.getProjectApi().forkProject(projectId,namespaceId);
  49. project.setVisibility(Visibility.PRIVATE);
  50. project=gitlabApi.getProjectApi().updateProject(project);
  51. }finally {
  52. gitlabApi.unsudo();
  53. }
  54. return project!=null?project.getId():null;
  55. }
  56. @Override
  57. public Integer forkGroupProject(GroupForkDTO groupForkDTO) throws GitLabApiException {
  58. int groupId=groupForkDTO.getGroupId();
  59. int projectId=groupForkDTO.getProjectId();
  60. Integer namespaceId=fetchGroupNameSpaceId(gitlabApi.getGroupApi().getGroup(groupId).getName());
  61. if (namespaceId == null)
  62. throw new NullPointerException();
  63. Project project;
  64. try{
  65. gitlabApi.sudo("root");
  66. project=gitlabApi.getProjectApi().forkProject(projectId,namespaceId);
  67. project.setVisibility(Visibility.PRIVATE);
  68. project=gitlabApi.getProjectApi().updateProject(project);
  69. }finally {
  70. gitlabApi.unsudo();
  71. }
  72. return project!=null?project.getId():null;
  73. }
  74. private Integer fetchGroupNameSpaceId(String name) throws GitLabApiException {
  75. Namespace namespace;
  76. namespace = gitlabApi.getNamespaceApi().findNamespaces(name)
  77. .stream()
  78. .filter(ns -> "group".equals(ns.getKind()))
  79. .reduce((ns1, ns2) -> ns1.getId() > ns2.getId() ? ns1 : ns2)
  80. .orElse(null);
  81. return namespace != null ? namespace.getId() : null;
  82. }
  83. @Override
  84. public Set<Diff> compare(int projectId, String fromBash, String toBash) throws GitLabApiException {
  85. CompareResults compareResults = gitlabApi.getRepositoryApi().compare(projectId, fromBash, toBash);
  86. return new HashSet<>(compareResults.getDiffs());
  87. }
  88. @Override
  89. public boolean addWebHook(WebHookDTO webHookDTO) throws GitLabApiException {
  90. int projectId=webHookDTO.getProjectId();
  91. String url= webHookDTO.getUrl();
  92. boolean doPushEvents=webHookDTO.isDoPushEvents();
  93. boolean doIssueEvents=webHookDTO.isDoIssueEvents();
  94. boolean doMergeRequestsEvents=webHookDTO.isDoMergeRequestsEvents();
  95. gitlabApi.getProjectApi().addHook(projectId, url, doPushEvents, doIssueEvents, doMergeRequestsEvents);
  96. return true;
  97. }
  98. @Override
  99. public boolean archiveProject(int projectId) throws GitLabApiException {
  100. gitlabApi.getProjectApi().deleteProject(projectId);
  101. return true;
  102. }
  103. @Override
  104. public String getProjectFile(int projectId,ProjectFileDTO projectFileDTO) throws UnsupportedEncodingException, GitLabApiException {
  105. String branch=projectFileDTO.getBranch();
  106. String path=projectFileDTO.getPath();
  107. RepositoryFile repositoryFile = gitlabApi.getRepositoryFileApi().getFile(projectId, path,branch);
  108. if(repositoryFile!=null){
  109. return new String(Base64.getDecoder().decode(repositoryFile.getContent()), "UTF-8");
  110. }
  111. return null;
  112. }
  113. @Override
  114. public String getProjectRawFile(int projectId, ProjectRawFileDTO projectRawFileDTO) throws GitLabApiException, IOException {
  115. String commitOrBranchName = projectRawFileDTO.getCommitOrBranchName();
  116. String path=projectRawFileDTO.getPath();
  117. InputStream inputStream=gitlabApi.getRepositoryFileApi().getRawFile(projectId,commitOrBranchName,path);
  118. if (inputStream==null){
  119. return null;
  120. }
  121. ByteArrayOutputStream bos=new ByteArrayOutputStream();
  122. byte[] buf=new byte[inputStream.available()];
  123. int len=-1;
  124. while ((len=inputStream.read(buf))!=-1){
  125. bos.write(buf,0,len);
  126. }
  127. String content=bos.toString();
  128. inputStream.close();
  129. bos.close();
  130. return content;
  131. }
  132. @Override
  133. public Commit createCommit(int projectId, CommitDTO commitDTO) throws GitLabApiException {
  134. String branch=commitDTO.getBranch();
  135. String commitMsg=commitDTO.getCommitMessage();
  136. String username=commitDTO.getUsername();
  137. String email=commitDTO.getEmail();
  138. List<CommitAction> actions=commitDTO.getActions();
  139. Commit commit=gitlabApi.getCommitsApi().createCommit(projectId,branch,commitMsg,null,email,username,actions);
  140. return commit;
  141. }
  142. @Override
  143. public List<Commit> getAllCommitMessage(int projectId, String commitHash) throws GitLabApiException {
  144. return gitlabApi.getCommitsApi().getCommits(projectId,commitHash,null,null);
  145. }
  146. @Override
  147. public List<Branch> getProjectBranches(int projectId) throws GitLabApiException {
  148. return gitlabApi.getRepositoryApi().getBranches(projectId);
  149. }
  150. private Integer fetchNameSpaceId(String username) throws GitLabApiException {
  151. Namespace namespace;
  152. namespace = gitlabApi.getNamespaceApi().findNamespaces(username)
  153. .stream()
  154. .filter(ns -> "user".equals(ns.getKind()))
  155. .reduce((ns1, ns2) -> ns1.getId() > ns2.getId() ? ns1 : ns2)
  156. .orElse(null);
  157. return namespace != null ? namespace.getId() : null;
  158. }
  159. }