Explorar o código

fix: 修改gitlab api版本。本版本仅作为测试版本,正式生产版本需要重新考虑Long和Integer类型的修改

fanyanpeng %!s(int64=2) %!d(string=hai) anos
pai
achega
2a159d18d5

+ 1 - 1
seecoder-gitlab-server/pom.xml

@@ -71,7 +71,7 @@
         <dependency>
             <groupId>org.gitlab4j</groupId>
             <artifactId>gitlab4j-api</artifactId>
-            <version>4.8.7</version>
+            <version>5.3.0</version>
         </dependency>
         <dependency>
             <groupId>mysql</groupId>

+ 5 - 5
seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/service/impl/GroupServiceImpl.java

@@ -1,11 +1,11 @@
 package com.nju.edu.gitlab.service.impl;
 
+import com.nju.edu.gitlab.dto.member.GroupMemberDTO;
 import com.nju.edu.gitlab.entity.GitlabUser;
 import com.nju.edu.gitlab.repository.GitlabUserRepository;
 import com.nju.edu.gitlab.service.api.GroupService;
-import com.nju.edu.gitlab.vo.GitlabGroup;
 import com.nju.edu.gitlab.util.ApplicationProperties;
-import com.nju.edu.gitlab.dto.member.GroupMemberDTO;
+import com.nju.edu.gitlab.vo.GitlabGroup;
 import org.gitlab4j.api.GitLabApi;
 import org.gitlab4j.api.GitLabApiException;
 import org.gitlab4j.api.models.AccessLevel;
@@ -34,7 +34,7 @@ public class GroupServiceImpl implements GroupService {
     public synchronized GitlabGroup createGroup(String groupName) throws GitLabApiException {
         Group group=gitlabApi.getGroupApi().addGroup(groupName,groupName);
         GitlabGroup gitlabGroup=new GitlabGroup();
-        gitlabGroup.setGroupId(group.getId());
+        gitlabGroup.setGroupId(Math.toIntExact(group.getId()));
         gitlabGroup.setNamespaceId(fetchGroupNameSpace(groupName));
         return gitlabGroup;
     }
@@ -45,7 +45,7 @@ public class GroupServiceImpl implements GroupService {
         int userId=groupMemberDTO.getUserId();
         GitlabUser gitlabUser = getGitlabUser(userId);
         AccessLevel accessLevel=groupMemberDTO.getAccessLevel();
-        gitlabApi.getGroupApi().addMember(groupId,gitlabUser.getGitlabUserId(),accessLevel);
+        gitlabApi.getGroupApi().addMember(groupId, Long.valueOf(gitlabUser.getGitlabUserId()),accessLevel);
         return true;
     }
 
@@ -57,7 +57,7 @@ public class GroupServiceImpl implements GroupService {
                 .filter(ns -> "group".equals(ns.getKind()))
                 .reduce((ns1, ns2) -> ns1.getId() > ns2.getId() ? ns1 : ns2)
                 .orElse(null);
-        return namespace != null ? namespace.getId() : null;
+        return Math.toIntExact(namespace != null ? namespace.getId() : null);
     }
 
     private GitlabUser getGitlabUser(int userId) throws GitLabApiException {

+ 21 - 19
seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/service/impl/ProjectServiceImpl.java

@@ -1,25 +1,27 @@
 package com.nju.edu.gitlab.service.impl;
 
+import com.nju.edu.gitlab.dto.*;
 import com.nju.edu.gitlab.dto.commit.CommitDTO;
 import com.nju.edu.gitlab.dto.project.ProjectDTO;
 import com.nju.edu.gitlab.entity.GitlabUser;
 import com.nju.edu.gitlab.repository.GitlabUserRepository;
 import com.nju.edu.gitlab.service.api.ProjectService;
 import com.nju.edu.gitlab.util.ApplicationProperties;
-import com.nju.edu.gitlab.dto.*;
 import com.nju.edu.gitlab.vo.BranchVO;
+import com.nju.edu.gitlab.vo.DiffVO;
 import com.nju.edu.gitlab.vo.ProjectVO;
 import com.nju.edu.gitlab.vo.VisibilityVO;
-import com.nju.edu.gitlab.vo.commit.CommitStatsVO;
 import com.nju.edu.gitlab.vo.commit.CommitVO;
-import com.nju.edu.gitlab.vo.DiffVO;
 import lombok.extern.slf4j.Slf4j;
 import org.gitlab4j.api.*;
 import org.gitlab4j.api.models.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
-import java.io.*;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -49,7 +51,7 @@ public class ProjectServiceImpl implements ProjectService {
         project.setVisibility(visibility);
         project=gitlabApi.getProjectApi().createProject(project);
         return ProjectVO.builder()
-                .projectId(project.getId())
+                .projectId(Math.toIntExact(project.getId()))
                 .webUrl(project.getWebUrl())
                 .projectName(projectName)
                 .build();
@@ -62,11 +64,11 @@ public class ProjectServiceImpl implements ProjectService {
         Project project=new Project();
         project.setName(projectDTO.getProjectName());
         project.setVisibility(projectDTO.getVisibility());
-        gitlabApi.setSudoAsId(gitlabUser.getGitlabUserId());
+        gitlabApi.setSudoAsId((long) gitlabUser.getGitlabUserId());
         project=gitlabApi.getProjectApi().createProject(project);
         gitlabApi.unsudo();
         return ProjectVO.builder()
-                .projectId(project.getId())
+                .projectId(Math.toIntExact(project.getId()))
                 .webUrl(project.getWebUrl())
                 .projectName(project.getName())
                 .build();
@@ -87,15 +89,15 @@ public class ProjectServiceImpl implements ProjectService {
         Project project;
         log.info("trying to fork project {} to user {} (whose gitlab id is {})", projectId, userId, gitlabUser.getGitlabUserId());
         try{
-            gitlabApi.setSudoAsId(gitlabUser.getGitlabUserId());
-            project=gitlabApi.getProjectApi().forkProject(projectId,namespaceId);
+            gitlabApi.setSudoAsId((long) gitlabUser.getGitlabUserId());
+            project=gitlabApi.getProjectApi().forkProject(projectId, String.valueOf(namespaceId));
             project.setVisibility(Visibility.PRIVATE);
             project=gitlabApi.getProjectApi().updateProject(project);
         }finally {
             gitlabApi.unsudo();
         }
 
-        return project!=null?project.getId():null;
+        return Math.toIntExact(project != null ? project.getId() : null);
     }
 
     @Override
@@ -109,14 +111,14 @@ public class ProjectServiceImpl implements ProjectService {
         Project project;
         try{
             gitlabApi.sudo("root");
-            project=gitlabApi.getProjectApi().forkProject(projectId,namespaceId);
+            project=gitlabApi.getProjectApi().forkProject(projectId, String.valueOf(namespaceId));
             project.setVisibility(Visibility.PRIVATE);
             project=gitlabApi.getProjectApi().updateProject(project);
         }finally {
             gitlabApi.unsudo();
         }
 
-        return project!=null?project.getId():null;
+        return Math.toIntExact(project != null ? project.getId() : null);
     }
 
     private Integer fetchGroupNameSpaceId(String name) throws GitLabApiException {
@@ -126,7 +128,7 @@ public class ProjectServiceImpl implements ProjectService {
                 .filter(ns -> "group".equals(ns.getKind()))
                 .reduce((ns1, ns2) -> ns1.getId() > ns2.getId() ? ns1 : ns2)
                 .orElse(null);
-        return namespace != null ? namespace.getId() : null;
+        return Math.toIntExact(namespace != null ? namespace.getId() : null);
     }
 
     @Override
@@ -167,7 +169,7 @@ public class ProjectServiceImpl implements ProjectService {
     public String getProjectFile(int projectId,ProjectFileDTO projectFileDTO) throws UnsupportedEncodingException, GitLabApiException {
         String branch=projectFileDTO.getBranch();
         String path=projectFileDTO.getPath();
-        RepositoryFile repositoryFile = gitlabApi.getRepositoryFileApi().getFile(path,projectId,branch);
+        RepositoryFile repositoryFile = gitlabApi.getRepositoryFileApi().getFile(path, (long) projectId,branch);
         if(repositoryFile!=null){
             return new String(Base64.getDecoder().decode(repositoryFile.getContent()), "UTF-8");
         }
@@ -302,22 +304,22 @@ public class ProjectServiceImpl implements ProjectService {
         int userId = updateMemberDTO.getUserId();
         GitlabUser gitlabUser = getGitlabUser(userId);
         AccessLevel accessLevel = updateMemberDTO.getAccessLevel();
-        gitlabApi.getProjectApi().addMember(projectId,gitlabUser.getGitlabUserId(),accessLevel);
+        gitlabApi.getProjectApi().addMember(projectId, Long.valueOf(gitlabUser.getGitlabUserId()),accessLevel);
         return true;
     }
 
     @Override
     public List<ProjectVO> getAllProjectByUserId(int userId) throws GitLabApiException {
         GitlabUser gitlabUser = getGitlabUser(userId);
-        gitlabApi.setSudoAsId(gitlabUser.getGitlabUserId());
-        List<ProjectVO> projectVOS = gitlabApi.getProjectApi().getMemberProjects().stream().map(project->ProjectVO.builder().projectId(project.getId()).webUrl(project.getWebUrl()).projectName(project.getName()).build()).collect(Collectors.toList());
+        gitlabApi.setSudoAsId((long) gitlabUser.getGitlabUserId());
+        List<ProjectVO> projectVOS = gitlabApi.getProjectApi().getMemberProjects().stream().map(project->ProjectVO.builder().projectId(Math.toIntExact(project.getId())).webUrl(project.getWebUrl()).projectName(project.getName()).build()).collect(Collectors.toList());
         gitlabApi.unsudo();
         return projectVOS;
     }
 
     @Override
     public List<Integer> getProjectJoinedUserId(int projectId) throws GitLabApiException {
-        List<Integer> gitlabUserIds = gitlabApi.getProjectApi().getMembers(projectId).stream().map(Member::getId).collect(Collectors.toList());
+        List<Integer> gitlabUserIds = gitlabApi.getProjectApi().getMembers(projectId).stream().map(Member::getId).map(l -> Math.toIntExact(l)).collect(Collectors.toList());
         return gitlabUserRepository.findAllByGitlabUserIdIn(gitlabUserIds).stream()
                 .map(GitlabUser::getUserId)
                 .collect(Collectors.toList());
@@ -350,7 +352,7 @@ public class ProjectServiceImpl implements ProjectService {
     @Override
     public MergeRequest mergeBranch(int projectId, String sourceBranch, String targetBranch, String title, String description, int reviewerId) throws GitLabApiException {
         MergeRequestApi mergeRequestApi = gitlabApi.getMergeRequestApi();
-        MergeRequest mergeRequest = mergeRequestApi.createMergeRequest(projectId, sourceBranch, targetBranch, title, description, reviewerId);
+        MergeRequest mergeRequest = mergeRequestApi.createMergeRequest(projectId, sourceBranch, targetBranch, title, description, Long.valueOf(reviewerId));
         return mergeRequestApi.acceptMergeRequest(projectId, mergeRequest.getIid());
     }
 

+ 13 - 13
seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/service/impl/UserServiceImpl.java

@@ -1,23 +1,23 @@
 package com.nju.edu.gitlab.service.impl;
 
+import com.nju.edu.gitlab.dto.ChangePasswordDTO;
 import com.nju.edu.gitlab.dto.ChangeUserNameDTO;
+import com.nju.edu.gitlab.dto.GitlabUserDTO;
 import com.nju.edu.gitlab.entity.GitlabUser;
 import com.nju.edu.gitlab.repository.GitlabUserRepository;
 import com.nju.edu.gitlab.service.api.UserService;
-import com.nju.edu.gitlab.vo.GitlabUserVO;
 import com.nju.edu.gitlab.util.ApplicationProperties;
 import com.nju.edu.gitlab.util.Tools;
-import com.nju.edu.gitlab.dto.ChangePasswordDTO;
-import com.nju.edu.gitlab.dto.GitlabUserDTO;
+import com.nju.edu.gitlab.vo.GitlabUserVO;
 import org.gitlab4j.api.GitLabApi;
 import org.gitlab4j.api.GitLabApiException;
-import org.gitlab4j.api.models.*;
+import org.gitlab4j.api.models.ImpersonationToken;
 import org.gitlab4j.api.models.ImpersonationToken.Scope;
+import org.gitlab4j.api.models.Namespace;
+import org.gitlab4j.api.models.User;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
-import javax.persistence.EntityNotFoundException;
-
 @Component
 public class UserServiceImpl implements UserService {
     private final GitLabApi gitlabApi;
@@ -49,9 +49,9 @@ public class UserServiceImpl implements UserService {
         user = gitlabApi.getUserApi().createUser(user, password, 1000);
         GitlabUser gitlabUser = new GitlabUser();
         gitlabUser.setUserId(userId);
-        gitlabUser.setGitlabUserId(user.getId());
+        gitlabUser.setGitlabUserId(Math.toIntExact(user.getId()));
         gitlabUser.setNamespaceId(fetchNameSpaceId(username));
-        gitlabUser.setToken(createToken(user.getId()));
+        gitlabUser.setToken(createToken(Math.toIntExact(user.getId())));
 
         gitlabUser = gitlabUserRepository.save(gitlabUser);
 
@@ -77,14 +77,14 @@ public class UserServiceImpl implements UserService {
         String password = changePasswordDTO.getPassword();
         System.out.println("userId: "+ userId);
         int gitlabUserId = gitlabUserRepository.findByUserId(userId).getGitlabUserId();
-        User user = gitlabApi.getUserApi().getUser(gitlabUserId);
+        User user = gitlabApi.getUserApi().getUser(Long.valueOf(gitlabUserId));
         gitlabApi.getUserApi().modifyUser(user, password, 1000);
         return true;
     }
 
     @Override
     public boolean changeUserName(ChangeUserNameDTO changeUserNameDTO) throws GitLabApiException {
-        User user = gitlabApi.getUserApi().getUser(changeUserNameDTO.getUserId());
+        User user = gitlabApi.getUserApi().getUser((long) changeUserNameDTO.getUserId());
         user.setUsername(changeUserNameDTO.getUsername());
         user.setName(changeUserNameDTO.getUsername());
         gitlabApi.getUserApi().modifyUser(user, changeUserNameDTO.getPassword(), 1000);
@@ -107,7 +107,7 @@ public class UserServiceImpl implements UserService {
     public GitlabUserVO findByUsernameNew(String username) throws GitLabApiException {
         System.out.println(gitlabApi.getSudoAsId());
         User user = gitlabApi.getUserApi().getUser(username);
-        GitlabUser gitlabUser = gitlabUserRepository.findById(user.getId()).orElse(null);
+        GitlabUser gitlabUser = gitlabUserRepository.findById(Math.toIntExact(user.getId())).orElse(null);
 
         GitlabUserVO gitlabUserVO = new GitlabUserVO();
         if(gitlabUser!=null){
@@ -122,7 +122,7 @@ public class UserServiceImpl implements UserService {
     @Override
     public String getUserEmail(int userId) throws GitLabApiException {
         userId = gitlabUserRepository.findByUserId(userId).getGitlabUserId();
-        return gitlabApi.getUserApi().getUser(userId).getEmail();
+        return gitlabApi.getUserApi().getUser(Long.valueOf(userId)).getEmail();
     }
 
     private String createToken(int userId) throws GitLabApiException {
@@ -139,6 +139,6 @@ public class UserServiceImpl implements UserService {
                 .filter(ns -> "user".equals(ns.getKind()))
                 .reduce((ns1, ns2) -> ns1.getId() > ns2.getId() ? ns1 : ns2)
                 .orElse(null);
-        return namespace != null ? namespace.getId() : null;
+        return namespace != null ? Math.toIntExact(namespace.getId()) : null;
     }
 }

+ 4 - 4
seecoder-gitlab-server/src/main/resources/application.yml

@@ -3,9 +3,9 @@ server:
 
 spring:
   datasource:
-    url: jdbc:mysql://localhost:3306/gitlab?useUnicode=true&characterEncoding=UTF-8&useSSL=false
+    url: jdbc:mysql://localhost:3306/gitlab?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
     username: root
-    password: NJU67mysql
+    password: 123456
     driver-class-name: com.mysql.jdbc.Driver
   jpa:
     database-platform: org.hibernate.dialect.MySQL57InnoDBDialect
@@ -16,8 +16,8 @@ spring:
 
 
 gitlab:
-  host: http://gitlab.seecoder.cn
+  host: https://seecoder-git-gitlab.internal-paas.seec.seecoder.cn
   username: root
-  token: Y4LjwFez_VF4fHFbeRmK
+  token: glpat-MYtoe-Su9HWNLsm1K_Fu
   administrator: root
   email: noreply@mooc.com