Skip to content

Commit

Permalink
Correcting integration to get diff_refs
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Oct 9, 2019
1 parent e896d92 commit 655f185
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ apply from: project.buildscript.classLoader.getResource('release.gradle').toURI(

dependencies {
compile 'se.bjurr.violations:violation-comments-lib:1.98'
compile 'org.gitlab4j:gitlab4j-api:4.9.18'
compile 'org.gitlab4j:gitlab4j-api:4.12.3'

compileOnly 'com.github.spotbugs:spotbugs-annotations:3.1.12'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public class GitLabCommentsProvider implements CommentsProvider {
private final ViolationsLogger violationsLogger;
private final GitLabApi gitLabApi;
private final Project project;
private final MergeRequest mergeRequest;
private final MergeRequest mergeRequestChanges;
private MergeRequest mergeRequest;

@SuppressFBWarnings({"NP_LOAD_OF_KNOWN_NULL_VALUE", "SIC_INNER_SHOULD_BE_STATIC_ANON"})
public GitLabCommentsProvider(
Expand Down Expand Up @@ -73,8 +74,12 @@ public void log(final LogRecord record) {

final Integer mergeRequestId = api.getMergeRequestIid();
try {
mergeRequest =
mergeRequestChanges =
gitLabApi.getMergeRequestApi().getMergeRequestChanges(project.getId(), mergeRequestId);
// This will populate diff_refs,
// https://docs.gitlab.com/ee/api/merge_requests.html#get-single-mr
mergeRequest =
gitLabApi.getMergeRequestApi().getMergeRequest(project.getId(), mergeRequestId);
} catch (final Throwable e) {
throw new RuntimeException("Could not get MR " + projectId + " " + mergeRequestId, e);
}
Expand All @@ -101,7 +106,7 @@ public void createComment(final String comment) {
try {
this.gitLabApi
.getNotesApi()
.createMergeRequestNote(project.getId(), this.mergeRequest.getIid(), comment);
.createMergeRequestNote(project.getId(), this.mergeRequestChanges.getIid(), comment);
} catch (final Throwable e) {
violationsLogger.log(SEVERE, "Could create comment " + comment, e);
}
Expand All @@ -116,14 +121,14 @@ private void markMergeRequestAsWIP() {
return;
}

final String currentTitle = mergeRequest.getTitle();
final String currentTitle = mergeRequestChanges.getTitle();
final String startTitle = "WIP: (VIOLATIONS) ";
if (currentTitle.startsWith(startTitle)) {
// To avoid setting WIP again on new comments
return;
}
final Integer projectId = this.project.getId();
final Integer mergeRequestIid = this.mergeRequest.getIid();
final Integer mergeRequestIid = this.mergeRequestChanges.getIid();
final String targetBranch = null;
final Integer assigneeId = null;
final String title = startTitle + currentTitle;
Expand All @@ -136,7 +141,7 @@ private void markMergeRequestAsWIP() {
final Boolean discussionLocked = null;
final Boolean allowCollaboration = null;
try {
mergeRequest.setTitle(title);
mergeRequestChanges.setTitle(title);
gitLabApi
.getMergeRequestApi()
.updateMergeRequest(
Expand Down Expand Up @@ -201,7 +206,7 @@ public void createSingleFileComment(
gitLabApi
.getDiscussionsApi()
.createMergeRequestDiscussion(
projectId, mergeRequest.getIid(), content, date, positionHash, position);
projectId, mergeRequestChanges.getIid(), content, date, positionHash, position);
} catch (final Throwable e) {
final String lineSeparator = System.lineSeparator();
violationsLogger.log(
Expand Down Expand Up @@ -240,7 +245,9 @@ public List<Comment> getComments() {
try {

final List<Note> notes =
gitLabApi.getNotesApi().getMergeRequestNotes(project.getId(), mergeRequest.getIid());
gitLabApi
.getNotesApi()
.getMergeRequestNotes(project.getId(), mergeRequestChanges.getIid());

for (final Note note : notes) {
final String identifier = note.getId() + "";
Expand All @@ -259,7 +266,7 @@ public List<Comment> getComments() {
@Override
public List<ChangedFile> getFiles() {
final List<ChangedFile> changedFiles = new ArrayList<>();
for (final Diff change : mergeRequest.getChanges()) {
for (final Diff change : mergeRequestChanges.getChanges()) {
final String filename = change.getNewPath();
final List<String> specifics = new ArrayList<>();
final String patchString = change.getDiff();
Expand All @@ -280,7 +287,7 @@ public void removeComments(final List<Comment> comments) {
final int noteId = Integer.parseInt(comment.getIdentifier());
this.gitLabApi
.getNotesApi()
.deleteMergeRequestNote(project.getId(), mergeRequest.getIid(), noteId);
.deleteMergeRequestNote(project.getId(), mergeRequestChanges.getIid(), noteId);
} catch (final Throwable e) {
violationsLogger.log(SEVERE, "Could not delete note " + comment, e);
}
Expand Down

0 comments on commit 655f185

Please sign in to comment.