From a2ee6f8a072d4eae7c2efc41b3e54d415139450c Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Tue, 22 Jan 2019 20:52:08 +0100 Subject: [PATCH] Switching to Gitlab4j-api Waiting for https://github.com/gmessner/gitlab4j-api/issues/291 --- build.gradle | 4 +- .../gitlab/lib/GitLabCommentsProvider.java | 239 +++++++++--------- .../lib/ViolationCommentsToGitLabApi.java | 9 +- 3 files changed, 130 insertions(+), 122 deletions(-) diff --git a/build.gradle b/build.gradle index d226277..a324b09 100644 --- a/build.gradle +++ b/build.gradle @@ -19,8 +19,8 @@ apply from: project.buildscript.classLoader.getResource('release.gradle').toURI( dependencies { - compile 'se.bjurr.violations:violation-comments-lib:1.83' - compile 'com.github.tomasbjerre:java-gitlab-api:comment-on-diff-2' + compile 'se.bjurr.violations:violation-comments-lib:1.85' + compile 'org.gitlab4j:gitlab4j-api:4.9.16' testCompile 'junit:junit:4.12' testCompile 'org.assertj:assertj-core:2.3.0' } diff --git a/src/main/java/se/bjurr/violations/comments/gitlab/lib/GitLabCommentsProvider.java b/src/main/java/se/bjurr/violations/comments/gitlab/lib/GitLabCommentsProvider.java index eb079ae..59d186c 100644 --- a/src/main/java/se/bjurr/violations/comments/gitlab/lib/GitLabCommentsProvider.java +++ b/src/main/java/se/bjurr/violations/comments/gitlab/lib/GitLabCommentsProvider.java @@ -1,34 +1,36 @@ package se.bjurr.violations.comments.gitlab.lib; -import static java.util.logging.Level.INFO; -import static java.util.logging.Level.SEVERE; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import org.gitlab.api.AuthMethod; -import org.gitlab.api.GitlabAPI; -import org.gitlab.api.TokenType; -import org.gitlab.api.models.GitlabCommitDiff; -import org.gitlab.api.models.GitlabMergeRequest; -import org.gitlab.api.models.GitlabNote; -import org.gitlab.api.models.GitlabProject; +import org.gitlab4j.api.Constants; +import org.gitlab4j.api.GitLabApi; +import org.gitlab4j.api.GitLabApiException; +import org.gitlab4j.api.models.MergeRequest; +import org.gitlab4j.api.models.Position; +import org.gitlab4j.api.models.Project; import se.bjurr.violations.comments.lib.CommentsProvider; import se.bjurr.violations.comments.lib.PatchParser; import se.bjurr.violations.comments.lib.ViolationsLogger; import se.bjurr.violations.comments.lib.model.ChangedFile; import se.bjurr.violations.comments.lib.model.Comment; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Optional; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +import static java.util.logging.Level.INFO; +import static java.util.logging.Level.SEVERE; + public class GitLabCommentsProvider implements CommentsProvider { private final ViolationCommentsToGitLabApi violationCommentsToGitLabApi; - - private final GitlabAPI gitlabApi; private final ViolationsLogger violationsLogger; + private final GitLabApi gitLabApi; + private final Project project; + private final MergeRequest mergeRequest; - private GitlabProject project; - - private GitlabMergeRequest mergeRequest; public GitLabCommentsProvider( ViolationsLogger violationsLogger, @@ -36,24 +38,25 @@ public GitLabCommentsProvider( this.violationsLogger = violationsLogger; final String hostUrl = violationCommentsToGitLabApi.getHostUrl(); final String apiToken = violationCommentsToGitLabApi.getApiToken(); - final TokenType tokenType = violationCommentsToGitLabApi.getTokenType(); - final AuthMethod method = violationCommentsToGitLabApi.getMethod(); - gitlabApi = GitlabAPI.connect(hostUrl, apiToken, tokenType, method); - - final boolean ignoreCertificateErrors = - violationCommentsToGitLabApi.isIgnoreCertificateErrors(); - gitlabApi.ignoreCertificateErrors(ignoreCertificateErrors); + this.gitLabApi = new GitLabApi(hostUrl,apiToken); + gitLabApi.setIgnoreCertificateErrors(violationCommentsToGitLabApi.isIgnoreCertificateErrors()); + gitLabApi.enableRequestResponseLogging(Level.INFO); + gitLabApi.withRequestResponseLogging(new Logger(GitLabCommentsProvider.class.getName(),null) { + public void log(LogRecord record) { + violationsLogger.log(record.getLevel(),record.getMessage()); + } + },Level.FINE); final String projectId = violationCommentsToGitLabApi.getProjectId(); try { - project = gitlabApi.getProject(projectId); - } catch (final Throwable e) { + this.project = gitLabApi.getProjectApi().getProject(projectId); + } catch (GitLabApiException e) { throw new RuntimeException("Could not get project " + projectId, e); } final Integer mergeRequestId = violationCommentsToGitLabApi.getMergeRequestIid(); try { - mergeRequest = gitlabApi.getMergeRequestChanges(project.getId(), mergeRequestId); + mergeRequest = gitLabApi.getMergeRequestApi().getMergeRequest(project.getId(), mergeRequestId); } catch (final Throwable e) { throw new RuntimeException("Could not get MR " + projectId + " " + mergeRequestId, e); } @@ -65,7 +68,7 @@ public GitLabCommentsProvider( public void createCommentWithAllSingleFileComments(final String comment) { markMergeRequestAsWIP(); try { - gitlabApi.createNote(mergeRequest, comment); + this.gitLabApi.getNotesApi().createMergeRequestNote(project.getId(),this.mergeRequest.getIid(),comment); } catch (final Throwable e) { violationsLogger.log(SEVERE, "Could create comment " + comment, e); } @@ -79,29 +82,31 @@ private void markMergeRequestAsWIP() { if (!this.violationCommentsToGitLabApi.getShouldSetWIP()) { return; } - final String currentTitle = mergeRequest.getTitle(); - if (currentTitle.startsWith("WIP:")) { - return; - } - final Serializable projectId = violationCommentsToGitLabApi.getProjectId(); - final Integer mergeRequestIid = violationCommentsToGitLabApi.getMergeRequestIid(); + + final String currentTitle = mergeRequest.getTitle(); + if (currentTitle.startsWith("WIP:")) { + // To avoid setting WIP again on new comments + return; + } + final Integer projectId = this.project.getId(); + final Integer mergeRequestIid = this.mergeRequest.getIid(); final String targetBranch = null; final Integer assigneeId = null; - final String title = "WIP: >>> CONTAINS VIOLATIONS! <<< " + currentTitle; + final String title = "WIP: >>> CONTAINS VIOLATIONS! <<< " + currentTitle; final String description = null; - final String stateEvent = null; + final Constants.StateEvent stateEvent = null; final String labels = null; + Integer milestoneId = null; + Boolean removeSourceBranch = null; + Boolean squash = null; + Boolean discussionLocked = null; + Boolean allowCollaboration = null; try { - mergeRequest.setTitle(title); // To avoid setting WIP again on new comments - gitlabApi.updateMergeRequest( - projectId, - mergeRequestIid, - targetBranch, - assigneeId, - title, - description, - stateEvent, - labels); + mergeRequest.setTitle(title); + gitLabApi.getMergeRequestApi().updateMergeRequest(projectId, mergeRequestIid, + targetBranch, title, assigneeId, description, + stateEvent, labels, milestoneId, removeSourceBranch, + squash, discussionLocked, allowCollaboration); } catch (final Throwable e) { violationsLogger.log(SEVERE, e.getMessage(), e); } @@ -111,58 +116,62 @@ private void markMergeRequestAsWIP() { public void createSingleFileComment( final ChangedFile file, final Integer newLine, final String content) { markMergeRequestAsWIP(); - final Integer projectId = project.getId(); - final String baseSha = mergeRequest.getBaseSha(); - final String startSha = mergeRequest.getStartSha(); - final String headSha = mergeRequest.getHeadSha(); + final Integer projectId = project.getId(); + final String baseSha = mergeRequest.getSha(); + final String startSha = mergeRequest.getMergeCommitSha(); + final String headSha = mergeRequest.getMergeCommitSha(); final String patchString = file.getSpecifics().get(0); - final String oldPath = file.getSpecifics().get(1); - final String newPath = file.getSpecifics().get(2); - Integer oldLine = + final String oldPath = file.getSpecifics().get(1); + final String newPath = file.getSpecifics().get(2); + Integer oldLine = new PatchParser(patchString) // .findOldLine(newLine) // .orElse(null); try { - gitlabApi.createTextDiscussion( - mergeRequest, - content, - null, - baseSha, - startSha, - headSha, - newPath, - newLine, - oldPath, - oldLine); + final Date date = null; + final String positionHash = null; + final Position position = new Position(); + position.setPositionType(Position.PositionType.TEXT); + position.setBaseSha(baseSha); + position.setStartSha(startSha); + position.setHeadSha(headSha); + position.setNewLine(newLine); + position.setNewPath(newPath); + position.setOldLine(oldLine); + position.setOldPath(oldPath); + gitLabApi + .getDiscussionsApi() + .createMergeRequestDiscussion(projectId,mergeRequest.getIid(), + content,date,positionHash,position); } catch (final Throwable e) { final String lineSeparator = System.lineSeparator(); - violationsLogger.log( - SEVERE, - "Could not create diff discussion!" - + lineSeparator - + "ProjectID: " - + projectId - + lineSeparator - + "SourceSha: " - + baseSha - + lineSeparator - + "HeadSha: " - + headSha - + lineSeparator - + "TargetSha: " - + startSha - + lineSeparator - + "Path " - + newPath - + lineSeparator - + "Violation: " - + content - + lineSeparator - + "NewLine " - + newLine - + ", OldLine" - + newLine, - e); + // violationsLogger.log( + // SEVERE, + // "Could not create diff discussion!" + // + lineSeparator + // + "ProjectID: " + // + projectId + // + lineSeparator + // + "SourceSha: " + // + baseSha + // + lineSeparator + // + "HeadSha: " + // + headSha + // + lineSeparator + // + "TargetSha: " + // + startSha + // + lineSeparator + // + "Path " + // + newPath + // + lineSeparator + // + "Violation: " + // + content + // + lineSeparator + // + "NewLine " + // + newLine + // + ", OldLine" + // + newLine, + // e); } } @@ -170,15 +179,15 @@ public void createSingleFileComment( public List getComments() { final List found = new ArrayList<>(); try { - final List notes = gitlabApi.getAllNotes(mergeRequest); - for (final GitlabNote note : notes) { - final String identifier = note.getId() + ""; - final String content = note.getBody(); - final String type = "PR"; - final List specifics = new ArrayList<>(); - final Comment comment = new Comment(identifier, content, type, specifics); - found.add(comment); - } + // final List notes = gitlabApi.getAllNotes(mergeRequest); + // for (final GitlabNote note : notes) { + // final String identifier = note.getId() + ""; + // final String content = note.getBody(); + // final String type = "PR"; + // final List specifics = new ArrayList<>(); + // final Comment comment = new Comment(identifier, content, type, specifics); + // found.add(comment); + // } } catch (final Throwable e) { violationsLogger.log(SEVERE, "Could not get comments", e); } @@ -188,16 +197,16 @@ public List getComments() { @Override public List getFiles() { final List changedFiles = new ArrayList<>(); - for (final GitlabCommitDiff change : mergeRequest.getChanges()) { - final String filename = change.getNewPath(); - final List specifics = new ArrayList<>(); - final String patchString = change.getDiff(); - specifics.add(patchString); - specifics.add(change.getOldPath()); - specifics.add(change.getNewPath()); - final ChangedFile changedFile = new ChangedFile(filename, specifics); - changedFiles.add(changedFile); - } + // for (final GitlabCommitDiff change : mergeRequest.getChanges()) { + // final String filename = change.getNewPath(); + // final List specifics = new ArrayList<>(); + // final String patchString = change.getDiff(); + // specifics.add(patchString); + // specifics.add(change.getOldPath()); + // specifics.add(change.getNewPath()); + // final ChangedFile changedFile = new ChangedFile(filename, specifics); + // changedFiles.add(changedFile); + // } return changedFiles; } @@ -206,9 +215,9 @@ public List getFiles() { public void removeComments(final List comments) { for (final Comment comment : comments) { try { - final GitlabNote noteToDelete = new GitlabNote(); - noteToDelete.setId(Integer.parseInt(comment.getIdentifier())); - gitlabApi.deleteNote(mergeRequest, noteToDelete); + // final GitlabNote noteToDelete = new GitlabNote(); + // noteToDelete.setId(Integer.parseInt(comment.getIdentifier())); + // gitlabApi.deleteNote(mergeRequest, noteToDelete); } catch (final Throwable e) { violationsLogger.log( INFO, diff --git a/src/main/java/se/bjurr/violations/comments/gitlab/lib/ViolationCommentsToGitLabApi.java b/src/main/java/se/bjurr/violations/comments/gitlab/lib/ViolationCommentsToGitLabApi.java index 92ca256..30259af 100644 --- a/src/main/java/se/bjurr/violations/comments/gitlab/lib/ViolationCommentsToGitLabApi.java +++ b/src/main/java/se/bjurr/violations/comments/gitlab/lib/ViolationCommentsToGitLabApi.java @@ -11,8 +11,7 @@ import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; -import org.gitlab.api.AuthMethod; -import org.gitlab.api.TokenType; +import org.gitlab4j.api.Constants.TokenType; import se.bjurr.violations.comments.lib.CommentsProvider; import se.bjurr.violations.comments.lib.ViolationsLogger; import se.bjurr.violations.lib.model.Violation; @@ -33,7 +32,7 @@ public static ViolationCommentsToGitLabApi violationCommentsToGitLabApi() { private String hostUrl; private String apiToken; private TokenType tokenType; - private AuthMethod method; + private String method; private boolean ignoreCertificateErrors; private String projectId; private Integer mergeRequestIid; @@ -95,11 +94,11 @@ public ViolationCommentsToGitLabApi setTokenType(final TokenType tokenType) { return this; } - public AuthMethod getMethod() { + public String getMethod() { return method; } - public ViolationCommentsToGitLabApi setMethod(final AuthMethod method) { + public ViolationCommentsToGitLabApi setMethod(final String method) { this.method = method; return this; }