Przeglądaj źródła

feat: add the feature of checking and editing visibility of projects

刘一也 3 lat temu
rodzic
commit
5e812f19dc

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

@@ -75,4 +75,8 @@ public interface SeecoderGitlabApi{
     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;
+
+    VisibilityVO getProjectVisibility(int projectId) throws SeecoderGitlabException;
+
+    VisibilityVO setProjectVisibility(VisibilityVO visibilityVO) throws SeecoderGitlabException;
 }

+ 45 - 0
seecoder-gitlab-client/src/main/java/com.nju.edu.gitlab/SeecoderGitlabClient.java

@@ -868,4 +868,49 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi {
             throw SeecoderGitlabException.IO_ERROR;
         }
     }
+
+    @Override
+    public VisibilityVO getProjectVisibility(int projectId) throws SeecoderGitlabException {
+        try {
+            ObjectMapper objectMapper = new ObjectMapper();
+            Request.Builder builder = new Request.Builder()
+                    .addHeader("Authorization", token);
+            builder = builder
+                    .url(host + String.format("/api/project/visibility?projectId=%d", projectId));
+            Request request = builder.build();
+            final Call call = client.newCall(request);
+            Response response = call.execute();
+            return processResponse(objectMapper, response, new TypeReference<VisibilityVO>() {
+            });
+        } 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 VisibilityVO setProjectVisibility(VisibilityVO visibilityVO) throws SeecoderGitlabException {
+        try {
+            ObjectMapper objectMapper = new ObjectMapper();
+            Request.Builder builder = new Request.Builder()
+                    .addHeader("Authorization", token);
+            builder = builder
+                    .url(host + String.format("/api/project/visibility"))
+                    .put(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(visibilityVO)));
+            Request request = builder.build();
+            final Call call = client.newCall(request);
+            Response response = call.execute();
+            return processResponse(objectMapper, response, new TypeReference<VisibilityVO>() {
+            });
+        } catch (JsonProcessingException e) {
+            logger.error("JSON writeValueAsTreeItem error", e);
+            throw SeecoderGitlabException.JSON_ERROR;
+        } catch (IOException e) {
+            logger.error("IOException", e);
+            throw SeecoderGitlabException.IO_ERROR;
+        }
+    }
 }

+ 15 - 0
seecoder-gitlab-common/src/main/java/com/nju/edu/gitlab/vo/VisibilityVO.java

@@ -0,0 +1,15 @@
+package com.nju.edu.gitlab.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+
+@AllArgsConstructor
+@NoArgsConstructor
+@Getter
+@Setter
+public class VisibilityVO {
+    private Integer projectId;
+    private String visibility;
+}

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

@@ -4,6 +4,7 @@ import com.nju.edu.gitlab.dto.commit.CommitDTO;
 import com.nju.edu.gitlab.dto.project.ProjectDTO;
 import com.nju.edu.gitlab.service.api.ProjectService;
 import com.nju.edu.gitlab.dto.*;
+import com.nju.edu.gitlab.vo.VisibilityVO;
 import com.nju.edu.gitlab.web.response.EmptyResponse;
 import com.nju.edu.gitlab.web.response.ResourceResponse;
 import com.nju.edu.gitlab.web.response.Response;
@@ -150,4 +151,14 @@ public class GitlabProjectController {
     ) throws GitLabApiException {
         return new ResourceResponse(projectService.mergeBranch(projectId, sourceBranch, targetBranch, title, description, reviewId));
     }
+
+    @GetMapping("/visibility")
+    public Response getProjectVisibility(@RequestParam Integer projectId) throws GitLabApiException {
+        return new ResourceResponse(projectService.getProjectVisibility(projectId));
+    }
+
+    @PutMapping("/visibility")
+    public Response setProjectVisibility(@RequestBody VisibilityVO visibilityVO) throws GitLabApiException {
+        return new ResourceResponse(projectService.setProjectVisibility(visibilityVO));
+    }
 }

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

@@ -6,6 +6,7 @@ import com.nju.edu.gitlab.dto.project.ProjectDTO;
 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.CommitVO;
 import org.gitlab4j.api.GitLabApiException;
 import org.gitlab4j.api.models.MergeRequest;
@@ -65,4 +66,8 @@ public interface ProjectService {
     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;
+
+    VisibilityVO getProjectVisibility(int projectId) throws GitLabApiException;
+
+    VisibilityVO setProjectVisibility(VisibilityVO visibilityVO) throws GitLabApiException;
 }

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

@@ -9,6 +9,7 @@ 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.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;
@@ -349,6 +350,28 @@ public class ProjectServiceImpl implements ProjectService {
         return mergeRequestApi.acceptMergeRequest(projectId, mergeRequest.getIid());
     }
 
+    @Override
+    public VisibilityVO getProjectVisibility(int projectId) throws GitLabApiException {
+        ProjectApi projectApi = gitlabApi.getProjectApi();
+        Project project = projectApi.getProject(projectId);
+        return new VisibilityVO(projectId, project.getVisibility().toString());
+    }
+
+    @Override
+    public VisibilityVO setProjectVisibility(VisibilityVO visibilityVO) throws GitLabApiException {
+        if (visibilityVO == null || visibilityVO.getVisibility() == null ||
+                !Objects.equals(visibilityVO.getVisibility(), "PRIVATE") &&
+                !Objects.equals(visibilityVO.getVisibility(), "PUBLIC")) {
+            throw new GitLabApiException("Invalid visibility found.");
+        }
+        Visibility visibility = Visibility.forValue(visibilityVO.getVisibility());
+        ProjectApi projectApi = gitlabApi.getProjectApi();
+        Project project = projectApi.getProject(visibilityVO.getProjectId());
+        project.setVisibility(visibility);
+        projectApi.updateProject(project);
+        return visibilityVO;
+    }
+
     private BranchVO castBranchToVO(Branch branch) {
         return BranchVO.builder()
                 .commit(castCommitToVO(branch.getCommit(),branch.getName()))