Просмотр исходного кода

add: getRepositoryTree & mergeBranch

shanshan 4 лет назад
Родитель
Сommit
fd730deb1d

+ 6 - 0
seecoder-gitlab-client/src/main/java/com.nju.edu.gitlab/SeecoderGitlabApi.java

@@ -6,6 +6,8 @@ import com.nju.edu.gitlab.dto.project.VisibilityForm;
 import com.nju.edu.gitlab.vo.*;
 import com.nju.edu.gitlab.vo.commit.CommitVO;
 import org.gitlab4j.api.GitLabApiException;
+import org.gitlab4j.api.models.MergeRequest;
+import org.gitlab4j.api.models.TreeItem;
 import org.gitlab4j.api.models.User;
 
 
@@ -69,4 +71,8 @@ public interface SeecoderGitlabApi{
     public List<Integer> getJoinedUserByProject(int projectId) throws SeecoderGitlabException;
 
     public List<String> getFilePaths(int projectId, String basePath, String branch) throws SeecoderGitlabException;
+
+    public List<TreeItem> getRepositoryTree(int projectId, String filePath, String branch) throws SeecoderGitlabException;
+
+    public MergeRequest mergeBranch(int projectId, String sourceBranch, String targetBranch, String title, String description, int reviewId) throws SeecoderGitlabException;
 }

+ 46 - 4
seecoder-gitlab-client/src/main/java/com.nju.edu.gitlab/SeecoderGitlabClient.java

@@ -14,13 +14,10 @@ import com.nju.edu.gitlab.dto.project.VisibilityForm;
 import com.nju.edu.gitlab.vo.*;
 import com.nju.edu.gitlab.vo.commit.CommitVO;
 import org.gitlab4j.api.GitLabApiException;
-import org.gitlab4j.api.models.User;
+import org.gitlab4j.api.models.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import okhttp3.*;
-import org.gitlab4j.api.models.AccessLevel;
-import org.gitlab4j.api.models.CommitAction;
-import org.gitlab4j.api.models.Visibility;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -824,4 +821,49 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi {
             throw SeecoderGitlabException.IO_ERROR;
         }
     }
+
+    @Override
+    public List<TreeItem> getRepositoryTree(int projectId, String filePath, String branch) throws SeecoderGitlabException {
+        try {
+            ObjectMapper objectMapper = new ObjectMapper();
+            Request.Builder builder = new Request.Builder()
+                    .addHeader("Authorization", token);
+            builder = builder
+                    .url(host + String.format("/api/project/repositoryTree?projectId=%d&&filePath=%s&&branch=%s", projectId, filePath, branch));
+            Request request = builder.build();
+            final Call call = client.newCall(request);
+            Response response = call.execute();
+            return processResponse(objectMapper, response, new TypeReference<List<TreeItem>>() {
+            });
+        } catch (JsonProcessingException e) {
+            logger.error("JSON writeValueAsTreeItem error", e);
+            throw SeecoderGitlabException.JSON_ERROR;
+        } catch (IOException e) {
+            logger.error("IOException", e);
+            throw SeecoderGitlabException.IO_ERROR;
+        }
+    }
+
+    @Override
+    public MergeRequest mergeBranch(int projectId, String sourceBranch, String targetBranch, String title, String description, int reviewId) throws SeecoderGitlabException {
+        try {
+            ObjectMapper objectMapper = new ObjectMapper();
+            Request.Builder builder = new Request.Builder()
+                    .addHeader("Authorization", token);
+            builder = builder
+                    .url(host + String.format("/api/project/mergeBranch?projectId=%d&&sourceBranch=%s&&targetBranch=%s&&title=%s&&description=%s&&reviewId=%d",
+                            projectId, sourceBranch, targetBranch, title, description, reviewId));
+            Request request = builder.build();
+            final Call call = client.newCall(request);
+            Response response = call.execute();
+            return processResponse(objectMapper, response, new TypeReference<MergeRequest>() {
+            });
+        } catch (JsonProcessingException e) {
+            logger.error("JSON writeValueAsTreeItem error", e);
+            throw SeecoderGitlabException.JSON_ERROR;
+        } catch (IOException e) {
+            logger.error("IOException", e);
+            throw SeecoderGitlabException.IO_ERROR;
+        }
+    }
 }

+ 25 - 0
seecoder-gitlab-client/src/test/java/GitlabClientTest.java

@@ -1,6 +1,8 @@
 import com.nju.edu.gitlab.SeecoderGitlabClient;
 import com.nju.edu.gitlab.dto.commit.CommitActionForm;
 import okhttp3.OkHttpClient;
+import org.gitlab4j.api.models.MergeRequest;
+import org.gitlab4j.api.models.TreeItem;
 import org.junit.jupiter.api.Test;
 
 import java.util.ArrayList;
@@ -42,4 +44,27 @@ public class GitlabClientTest {
             System.out.println(e.toString());
         }
     }
+
+    @Test
+    public void testGetRepositoryTree() {
+        SeecoderGitlabClient seecoderGitlabClient = new SeecoderGitlabClient("http://localhost:8080", "wXDeETv2Kmt-JhiApJ9H");
+        try {
+            List<TreeItem>  list = seecoderGitlabClient.getRepositoryTree(4302,"","master");
+            System.out.println(list);
+        } catch (Exception e) {
+            System.out.println(e.toString());
+        }
+    }
+
+    @Test
+    public void testMergeBranch() {
+        SeecoderGitlabClient seecoderGitlabClient = new SeecoderGitlabClient("http://localhost:8080", "wXDeETv2Kmt-JhiApJ9H");
+        try {
+            MergeRequest file = seecoderGitlabClient.mergeBranch(4302, "dev", "master", "统一代码服务合并测试", "", 1479);
+            System.out.println(file);
+        } catch (Exception e) {
+            System.out.println(e.toString());
+        }
+    }
+
 }

+ 21 - 0
seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/controller/GitlabProjectController.java

@@ -129,4 +129,25 @@ public class GitlabProjectController {
             @RequestParam(value = "branch") String branch) throws GitLabApiException {
         return new ResourceResponse(projectService.getFilePaths(projectId, basePath, branch));
     }
+
+    @GetMapping("/repositoryTree")
+    public Response getRepositoryTree(
+            @RequestParam(value = "projectId") Integer projectId,
+            @RequestParam(value = "filePath") String path,
+            @RequestParam(value = "branch") String branch
+    ) throws GitLabApiException {
+         return new ResourceResponse(projectService.getRepositoryTree(projectId, path, branch));
+    }
+
+    @GetMapping("/mergeBranch")
+    public Response mergeBranch(
+            @RequestParam(value = "projectId") Integer projectId,
+            @RequestParam(value = "sourceBranch") String sourceBranch,
+            @RequestParam(value = "targetBranch") String targetBranch,
+            @RequestParam(value = "title") String title,
+            @RequestParam(value = "description") String description,
+            @RequestParam(value = "reviewId") Integer reviewId
+    ) throws GitLabApiException {
+        return new ResourceResponse(projectService.mergeBranch(projectId, sourceBranch, targetBranch, title, description, reviewId));
+    }
 }

+ 6 - 0
seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/service/api/ProjectService.java

@@ -8,6 +8,8 @@ import com.nju.edu.gitlab.vo.DiffVO;
 import com.nju.edu.gitlab.vo.ProjectVO;
 import com.nju.edu.gitlab.vo.commit.CommitVO;
 import org.gitlab4j.api.GitLabApiException;
+import org.gitlab4j.api.models.MergeRequest;
+import org.gitlab4j.api.models.TreeItem;
 
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
@@ -59,4 +61,8 @@ public interface ProjectService {
     public List<Integer> getProjectJoinedUserId(int projectId) throws GitLabApiException;
 
     public List<String> getFilePaths(int projectId, String basePath, String branch) throws GitLabApiException;
+
+    public List<TreeItem> getRepositoryTree(int projectId, String filePath, String branch) throws GitLabApiException;
+
+    public MergeRequest mergeBranch(int projectId, String sourceBranch, String targetBranch, String title, String description, int reviewerId) throws GitLabApiException;
 }

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

@@ -12,10 +12,7 @@ import com.nju.edu.gitlab.vo.ProjectVO;
 import com.nju.edu.gitlab.vo.commit.CommitStatsVO;
 import com.nju.edu.gitlab.vo.commit.CommitVO;
 import com.nju.edu.gitlab.vo.DiffVO;
-import org.gitlab4j.api.GitLabApi;
-import org.gitlab4j.api.GitLabApiException;
-import org.gitlab4j.api.Pager;
-import org.gitlab4j.api.RepositoryApi;
+import org.gitlab4j.api.*;
 import org.gitlab4j.api.models.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
@@ -332,6 +329,20 @@ public class ProjectServiceImpl implements ProjectService {
         return filePaths;
     }
 
+    @Override
+    public List<TreeItem> getRepositoryTree(int projectId, String filePath, String branch) throws GitLabApiException {
+        RepositoryApi repositoryApi = gitlabApi.getRepositoryApi();
+        List<TreeItem> treeItems = repositoryApi.getTree(projectId, filePath, branch);
+        return treeItems;
+    }
+
+    @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);
+        return mergeRequestApi.acceptMergeRequest(projectId, mergeRequest.getIid());
+    }
+
     private BranchVO castBranchToVO(Branch branch) {
         return BranchVO.builder()
                 .commit(castCommitToVO(branch.getCommit(),branch.getName()))