package com.nju.edu.gitlab; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.nju.edu.gitlab.dto.*; import com.nju.edu.gitlab.dto.commit.CommitActionForm; import com.nju.edu.gitlab.dto.commit.CommitDTO; import com.nju.edu.gitlab.dto.member.AccessLevelForm; import com.nju.edu.gitlab.dto.member.GroupMemberDTO; import com.nju.edu.gitlab.dto.project.ProjectDTO; 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.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import okhttp3.*; import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import static com.nju.edu.gitlab.SeecoderGitlabException.BIZ_ERROR_CODE; public class SeecoderGitlabClient implements SeecoderGitlabApi { private final Logger logger = LoggerFactory.getLogger(getClass()); private OkHttpClient client; private String host; private String token; public SeecoderGitlabClient(String host, String token) { this.token = token; this.host = host; this.client = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .retryOnConnectionFailure(true) .build(); } private T processResponse(ObjectMapper objectMapper, Response response, TypeReference typeReference) throws SeecoderGitlabException { try { validateResponseStatus(response); String body = response.body().string(); JsonNode node = objectMapper.readTree(body); //todo 注释的代码和ResourceResponse字段对应不上,我先改成能用的形式,后续在修改 // author: wph Integer err = Integer.parseInt(node.get("err").asText()); if (err != 0) { String msg = node.get("msg").asText(); throw new SeecoderGitlabException(BIZ_ERROR_CODE, msg); } if (typeReference == null) { return null; } if (node.get("res") != null) return objectMapper.readValue(node.get("res").traverse(), typeReference); else throw SeecoderGitlabException.NOT_FOUND_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } private void validateResponseStatus(Response response) throws SeecoderGitlabException { if (response.code() == 403) { throw SeecoderGitlabException.UNAHTORIZATION_ERROR; } else if (response.code() == 500) { throw SeecoderGitlabException.INTERNAL_SERVER_ERROR; } else if (response.code() != 200) { throw SeecoderGitlabException.UNKNOWN_ERROR; } } @Override public GitlabUserVO createGitlabUser(int userId, String username, String password, String email) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); GitlabUserDTO gitlabUserDTO = GitlabUserDTO.builder() .userId(userId) .email(email) .password(password) .username(username) .build(); builder = builder .url(host + "/api/user/") .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(gitlabUserDTO))); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); GitlabUserVO gitlabUserVO = processResponse(objectMapper, response, new TypeReference() { }); return gitlabUserVO; } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public boolean changePassword(int userId, String password) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); ChangePasswordDTO changePasswordDTO = ChangePasswordDTO.builder() .password(password) .userId(userId) .build(); builder = builder .url(host + "/api/user/changePassword") .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(changePasswordDTO))); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); return processResponse(objectMapper, response, new TypeReference() { }); } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public boolean changeUserName(int userId, String password, String username) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); ChangeUserNameDTO changeUserNameDTO = ChangeUserNameDTO.builder() .userId(userId) .username(username) .password(password) .build(); builder = builder .url(host + "/api/user/changeUserName") .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(changeUserNameDTO))); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); return processResponse(objectMapper, response, new TypeReference() { }); } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public boolean deleteUser(int userId) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); builder = builder .url(host + "/api/user/" + userId) .delete(); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); boolean isDeleted = processResponse(objectMapper, response, new TypeReference() { }); return isDeleted; } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public String getUserEmail(int userId) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); builder = builder .url(host + "/api/user/" + userId + "/email") .get(); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); return processResponse(objectMapper, response, new TypeReference() { }); } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public User getUserByUserName(String userName) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); builder = builder .url(host + "/api/user/getByUserName?userName=" + userName) .get(); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); return processResponse(objectMapper, response, new TypeReference() { }); } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public GitlabUserVO getUserByUserNameNew(String userName) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); builder = builder .url(host + "/api/user/getByUserNameNew?userName=" + userName) .get(); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); return processResponse(objectMapper, response, new TypeReference() { }); } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public GitlabGroup createGroup(String groupName) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); builder = builder .url(host + "/api/user") .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), groupName)); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); GitlabGroup gitlabGroup = processResponse(objectMapper, response, new TypeReference() { }); return gitlabGroup; } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public boolean addMember(int groupId, int userId, AccessLevelForm accessLevelForm) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); AccessLevel accessLevel = AccessLevel.forValue(accessLevelForm.value); GroupMemberDTO groupMemberDTO = GroupMemberDTO.builder() .accessLevel(accessLevel) .userId(userId) .groupId(groupId) .build(); builder = builder .url(host + "/api/group/addMember") .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(groupMemberDTO))); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); boolean isAdded = processResponse(objectMapper, response, new TypeReference() { }); return isAdded; } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public ProjectVO createProject(String projectName, VisibilityForm visibilityForm) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); Visibility visibility = Visibility.valueOf(visibilityForm.toString()); ProjectDTO projectDTO = ProjectDTO.builder() .projectName(projectName) .visibility(visibility) .build(); builder = builder .url(host + "/api/project/") .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(projectDTO))); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); return processResponse(objectMapper, response, new TypeReference() { }); } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public ProjectVO createPrivateProject(String projectName, VisibilityForm visibilityForm, Integer userId) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); Visibility visibility = Visibility.valueOf(visibilityForm.toString()); ProjectDTO projectDTO = ProjectDTO.builder() .projectName(projectName) .visibility(visibility) .userId(userId) .build(); builder = builder .url(host + "/api/project/private") .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(projectDTO))); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); return processResponse(objectMapper, response, new TypeReference() { }); } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public boolean deleteProject(int projectId) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); builder = builder .url(host + "/api/project/" + projectId) .delete(); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); boolean isDeleted = processResponse(objectMapper, response, new TypeReference() { }); return isDeleted; } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public Integer forkProject(int projectId, int userId) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); UserForkDTO userForkDTO = UserForkDTO.builder() .projectId(projectId) .userId(userId) .build(); builder = builder .url(host + "/api/project/userFork") .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(userForkDTO))); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); projectId = processResponse(objectMapper, response, new TypeReference() { }); return projectId; } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public Integer forkGroupProject(int projectId, int groupId) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); GroupForkDTO groupForkDTO = GroupForkDTO.builder() .projectId(projectId) .groupId(groupId) .build(); builder = builder .url(host + "/api/project/groupFork") .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(groupForkDTO))); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); projectId = processResponse(objectMapper, response, new TypeReference() { }); return projectId; } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public Set compare(int projectId, String fromBash, String toBash) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); builder = builder .url(host + "/api/project/compare?projectId=" + projectId + "&fromBash=" + fromBash + "&toBash=" + toBash) .get(); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); Set diffVOSet = new HashSet<>(); diffVOSet = processResponse(objectMapper, response, new TypeReference>() { }); return diffVOSet; } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public boolean addWebHook(int projectId, String url, boolean doPushEvents, boolean doIssueEvents, boolean doMergeRequestsEvents) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); WebHookDTO webHookDTO = WebHookDTO.builder() .projectId(projectId) .url(url) .doIssueEvents(doIssueEvents) .doPushEvents(doPushEvents) .doMergeRequestsEvents(doMergeRequestsEvents) .build(); builder = builder .url(host + "/api/project/webhook") .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(webHookDTO))); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); return processResponse(objectMapper, response, new TypeReference() { }); } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public boolean archiveProject(int projectId) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); builder = builder .url(host + "/api/project/archive") .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(projectId))); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); return processResponse(objectMapper, response, new TypeReference() { }); } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public String getProjectFile(int projectId, String branch, String path) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); ProjectFileDTO projectFileDTO = ProjectFileDTO.builder() .path(path) .branch(branch) .build(); builder = builder .url(host + "/api/project/" + projectId + "/getFile") .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(projectFileDTO))); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); return processResponse(objectMapper, response, new TypeReference() { }); } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public String getProjectRawFile(int projectId, String commitOrBranchName, String path) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); ProjectRawFileDTO projectFileDTO = ProjectRawFileDTO.builder() .commitOrBranchName(commitOrBranchName) .path(path) .build(); builder = builder .url(host + "/api/project/" + projectId + "/getRawFile") .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(projectFileDTO))); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); return processResponse(objectMapper, response, new TypeReference() { }); } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public CommitVO createCommit(int projectId, String branch, String commitMessage, String email, String username, List actions) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); List actionList = actions.stream().map(action -> new CommitAction() // .withAction(CommitAction.Action.forValue(action.getAction().toString())) // .withEncoding(CommitAction.Encoding.forValue(action.getEncoding().toString())) .withAction(Enum.valueOf(CommitAction.Action.class, action.getAction().toString())) .withEncoding(Enum.valueOf(CommitAction.Encoding.class, action.getEncoding().toString())) .withFilePath(action.getFilepath()) .withContent(action.getContent())).collect(Collectors.toList()); CommitDTO commitDTO = CommitDTO.builder() .branch(branch) .commitMessage(commitMessage) .email(email) .username(username) .actions(actionList) .build(); builder = builder .url(host + "/api/project/" + projectId + "/commit") .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(commitDTO))); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); return processResponse(objectMapper, response, new TypeReference() { }); } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public List getAllCommitMessage(int projectId, String commitHash) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); builder = builder .url(host + "/api/project/" + projectId + "/commit/" + commitHash) .get(); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); List commitVOList = new ArrayList<>(); commitVOList = processResponse(objectMapper, response, new TypeReference>() { }); return commitVOList; } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public List getCommitsByBranch(int projectId, String branchName) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); builder = builder .url(host + "/api/project/" + projectId + "/branch-commit/" + branchName) .get(); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); List commitVOList = new ArrayList<>(); commitVOList = processResponse(objectMapper, response, new TypeReference>() { }); return commitVOList; } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public CommitVO getCommitByHash(int projectId, String hash) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); builder = builder .url(host + "/api/project/" + projectId + "/commitInfo/" + hash) .get(); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); return processResponse(objectMapper, response, new TypeReference() {}); } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public List getProjectBranches(int projectId) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); builder = builder .url(host + "/api/project/" + projectId + "/branches") .get(); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); List branchVOList = new ArrayList<>(); branchVOList = processResponse(objectMapper, response, new TypeReference>() { }); return branchVOList; } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public boolean addAuthorization(int projectId, int userId, AccessLevelForm accessLevelForm) throws GitLabApiException, SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); AccessLevel accessLevel = AccessLevel.forValue(accessLevelForm.value); UpdateMemberDTO updateMemberDTO = UpdateMemberDTO.builder() .accessLevel(accessLevel) .userId(userId) .projectId(projectId) .build(); builder = builder .url(host + "/api/project/updateMember") .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(updateMemberDTO))); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); boolean isAdded = processResponse(objectMapper, response, new TypeReference() { }); return isAdded; } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public List getAllProjectsByUserId(int userId) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); builder = builder .url(host + "/api/project/allProjects/" + userId) .get(); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); List projectVOList = new ArrayList<>(); projectVOList = processResponse(objectMapper, response, new TypeReference>() { }); return projectVOList; } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public List getJoinedUserByProject(int projectId) throws SeecoderGitlabException { try { ObjectMapper objectMapper = new ObjectMapper(); Request.Builder builder = new Request.Builder() .addHeader("Authorization", token); builder = builder .url(host + "/api/project/projectMembers/" + projectId) .get(); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); List userList = new ArrayList<>(); userList = processResponse(objectMapper, response, new TypeReference>() { }); return userList; } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public List getFilePaths(int projectId, String basePath, 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/filePaths?projectId=%d&&basePath=%s&&branch=%s", projectId, basePath, branch)) .get(); Request request = builder.build(); final Call call = client.newCall(request); Response response = call.execute(); return processResponse(objectMapper, response, new TypeReference>() { }); } catch (JsonProcessingException e) { logger.error("JSON writeValueAsString error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } @Override public List 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>() { }); } 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() { }); } catch (JsonProcessingException e) { logger.error("JSON writeValueAsTreeItem error", e); throw SeecoderGitlabException.JSON_ERROR; } catch (IOException e) { logger.error("IOException", e); throw SeecoderGitlabException.IO_ERROR; } } }