Skip to content

Commit

Permalink
Switching to Gitlab4j-api
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Feb 11, 2019
1 parent ff506a3 commit 27e3f97
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 124 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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.15'
testCompile 'junit:junit:4.12'
testCompile 'org.assertj:assertj-core:2.3.0'
}
Original file line number Diff line number Diff line change
@@ -1,59 +1,60 @@
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.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.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,
final ViolationCommentsToGitLabApi violationCommentsToGitLabApi) {
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);
}
Expand All @@ -65,7 +66,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);
}
Expand All @@ -79,29 +80,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);
}
Expand All @@ -111,74 +114,74 @@ 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 String patchString = file.getSpecifics().get(0);
final Integer projectId = project.getId();
final String baseSha = mergeRequest.getBaseSha();
final String startSha = mergeRequest.getStartSha();
final String headSha = mergeRequest.getHeadSha();
final String patchString = file.getSpecifics().get(0);
final String oldPath = file.getSpecifics().get(1);
final String newPath = file.getSpecifics().get(2);
Integer oldLine =
new PatchParser(patchString) //
.findOldLine(newLine) //
.orElse(null);
// Integer oldLine =
// new PatchParser(patchString) //
// .findOldLine(newLine) //
// .orElse(null);
try {
gitlabApi.createTextDiscussion(
mergeRequest,
content,
null,
baseSha,
startSha,
headSha,
newPath,
newLine,
oldPath,
oldLine);
// gitlabApi.createTextDiscussion(
// mergeRequest,
// content,
// null,
// baseSha,
// startSha,
// headSha,
// newPath,
// newLine,
// oldPath,
// oldLine);
} 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);
}
}

@Override
public List<Comment> getComments() {
final List<Comment> found = new ArrayList<>();
try {
final List<GitlabNote> 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<String> specifics = new ArrayList<>();
final Comment comment = new Comment(identifier, content, type, specifics);
found.add(comment);
}
// final List<GitlabNote> 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<String> 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);
}
Expand All @@ -188,16 +191,16 @@ public List<Comment> getComments() {
@Override
public List<ChangedFile> getFiles() {
final List<ChangedFile> changedFiles = new ArrayList<>();
for (final GitlabCommitDiff change : mergeRequest.getChanges()) {
final String filename = change.getNewPath();
final List<String> 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<String> 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;
}
Expand All @@ -206,9 +209,9 @@ public List<ChangedFile> getFiles() {
public void removeComments(final List<Comment> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 27e3f97

Please sign in to comment.