Skip to content

Commit

Permalink
Adjustments on diff comments from PR #7
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Oct 5, 2018
2 parents 2c1e73d + 2e19300 commit 7e19c6c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 47 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.70'
compile 'org.gitlab:java-gitlab-api:4.0.0'
compile 'org.gitlab:java-gitlab-api:4.1.0'
testCompile 'junit:junit:4.12'
testCompile 'org.assertj:assertj-core:2.3.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,69 +62,101 @@ public GitLabCommentsProvider(

@Override
public void createCommentWithAllSingleFileComments(final String comment) {
addingComment();
markMergeRequestAsWIP();
try {
gitlabApi.createNote(mergeRequest, comment);
} catch (final Throwable e) {
violationsLogger.log(SEVERE, "Could create comment " + comment, e);
}
}

private void addingComment() {
if (this.violationCommentsToGitLabApi.getShouldSetWIP()) {
final String currentTitle = mergeRequest.getTitle();
if (currentTitle.startsWith("WIP:")) {
return;
}
final Serializable projectId = violationCommentsToGitLabApi.getProjectId();
final Integer mergeRequestId = violationCommentsToGitLabApi.getMergeRequestIid();
final String targetBranch = null;
final Integer assigneeId = null;
final String title = "WIP: >>> CONTAINS VIOLATIONS! <<< " + currentTitle;
final String description = null;
final String stateEvent = null;
final String labels = null;
try {
mergeRequest.setTitle(title); // To avoid setting WIP again on new comments
gitlabApi.updateMergeRequest(
projectId,
mergeRequestId,
targetBranch,
assigneeId,
title,
description,
stateEvent,
labels);
} catch (final Throwable e) {
violationsLogger.log(SEVERE, e.getMessage(), e);
}
/**
* Set the {@link GitlabMergeRequest} as "Work in Progress" if configured to do so by the
* shouldSetWIP flag.
*/
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 targetBranch = null;
final Integer assigneeId = null;
final String title = "WIP: >>> CONTAINS VIOLATIONS! <<< " + currentTitle;
final String description = null;
final String stateEvent = null;
final String labels = null;
try {
mergeRequest.setTitle(title); // To avoid setting WIP again on new comments
gitlabApi.updateMergeRequest(
projectId,
mergeRequestIid,
targetBranch,
assigneeId,
title,
description,
stateEvent,
labels);
} catch (final Throwable e) {
violationsLogger.log(SEVERE, e.getMessage(), e);
}
}

@Override
public void createSingleFileComment(
final ChangedFile file, final Integer line, final String comment) {
addingComment();
final ChangedFile file, final Integer newLine, final String content) {
markMergeRequestAsWIP();
final Integer projectId = project.getId();
final String sha = mergeRequest.getSourceBranch();
final String note = comment;
final String path = file.getFilename();
final String line_type = "new";
final String baseSha = mergeRequest.getBaseSha();
final String startSha = mergeRequest.getStartSha();
final String headSha = mergeRequest.getHeadSha();
final String newPath = file.getFilename();
final String oldPath = null;
final Integer oldLine = null;
try {
gitlabApi.createCommitComment(projectId, sha, note, path, line + "", line_type);
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 commit comment"
"Could not create diff discussion!"
+ lineSeparator
+ "ProjectID: "
+ projectId
+ " "
+ sha
+ " "
+ note
+ " "
+ path
+ " "
+ line_type,
+ lineSeparator
+ "SourceSha: "
+ baseSha
+ lineSeparator
+ "HeadSha: "
+ headSha
+ lineSeparator
+ "TargetSha: "
+ startSha
+ lineSeparator
+ "Path "
+ newPath
+ lineSeparator
+ "Violation: "
+ content
+ lineSeparator
+ "NewLine "
+ newLine
+ ", OldLine"
+ newLine,
e);
}
}
Expand Down Expand Up @@ -192,7 +224,7 @@ public boolean shouldCreateCommentWithAllSingleFileComments() {

@Override
public boolean shouldCreateSingleFileComment() {
return false;
return violationCommentsToGitLabApi.getCreateCommentPerViolation();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static ViolationCommentsToGitLabApi violationCommentsToGitLabApi() {

private List<Violation> violations;
private boolean createCommentWithAllSingleFileComments = false;
private boolean createCommentPerViolation = false;
private boolean commentOnlyChangedContent = false;
private String hostUrl;
private String apiToken;
Expand Down Expand Up @@ -137,6 +138,12 @@ public ViolationCommentsToGitLabApi setCreateCommentWithAllSingleFileComments(
return this;
}

public ViolationCommentsToGitLabApi setCreateCommentPerViolation(
final boolean createCommentPerViolation) {
this.createCommentPerViolation = createCommentPerViolation;
return this;
}

public ViolationCommentsToGitLabApi setCommentOnlyChangedContent(
final boolean commentOnlyChangedContent) {
this.commentOnlyChangedContent = commentOnlyChangedContent;
Expand All @@ -153,6 +160,16 @@ public boolean getCreateCommentWithAllSingleFileComments() {
return createCommentWithAllSingleFileComments;
}

/**
* Gets if a comment per violation should be created.
*
* @return <code>true</code> if a comment should be created for each violation, <code>false</code>
* otherwise.
*/
public boolean getCreateCommentPerViolation() {
return createCommentPerViolation;
}

public void toPullRequest() throws Exception {
if (Utils.isNullOrEmpty(commentTemplate)) {
commentTemplate = getDefaultTemplate();
Expand Down

0 comments on commit 7e19c6c

Please sign in to comment.