Skip to content

Commit

Permalink
Clearer error message when diffRefs null
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Oct 8, 2019
1 parent 15f4303 commit a288423
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 25 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@

Changelog of Violation comments to gitlab lib.

## Unreleased
### No issue

**Create FUNDING.yml**


[15f4303a5db8fab](https://github.com/tomasbjerre/violation-comments-to-gitlab-lib/commit/15f4303a5db8fab) Tomas Bjerre *2019-09-28 07:04:09*


## 1.78
### No issue

Expand Down
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {
maven { url "https://jitpack.io" }
}
dependencies {
classpath 'com.github.tomasbjerre:gradle-scripts:gradle5-SNAPSHOT'
classpath 'com.github.tomasbjerre:gradle-scripts:master-SNAPSHOT'
}
}
apply from: project.buildscript.classLoader.getResource('java.gradle').toURI()
Expand All @@ -21,6 +21,9 @@ 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'

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

testCompile 'junit:junit:4.12'
testCompile 'org.assertj:assertj-core:2.3.0'
}
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
./gradlew clean gitChangelogTask eclipse build install -i
./gradlew clean googleJavaFormat gitChangelogTask eclipse build install -i
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import static java.util.logging.Level.SEVERE;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.LogRecord;
Expand All @@ -16,6 +18,7 @@
import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.ProxyClientConfig;
import org.gitlab4j.api.models.Diff;
import org.gitlab4j.api.models.DiffRef;
import org.gitlab4j.api.models.MergeRequest;
import org.gitlab4j.api.models.Note;
import org.gitlab4j.api.models.Position;
Expand All @@ -34,6 +37,7 @@ public class GitLabCommentsProvider implements CommentsProvider {
private final Project project;
private final MergeRequest mergeRequest;

@SuppressFBWarnings({"NP_LOAD_OF_KNOWN_NULL_VALUE", "SIC_INNER_SHOULD_BE_STATIC_ANON"})
public GitLabCommentsProvider(
final ViolationsLogger violationsLogger, final ViolationCommentsToGitLabApi api) {
this.violationsLogger = violationsLogger;
Expand Down Expand Up @@ -106,6 +110,7 @@ public void createComment(final String comment) {
/**
* Set the merge request as "Work in Progress" if configured to do so by the shouldSetWIP flag.
*/
@SuppressFBWarnings("NP_LOAD_OF_KNOWN_NULL_VALUE")
private void markMergeRequestAsWIP() {
if (!this.api.getShouldSetWIP()) {
return;
Expand Down Expand Up @@ -154,21 +159,34 @@ private void markMergeRequestAsWIP() {
}

@Override
@SuppressFBWarnings("NP_LOAD_OF_KNOWN_NULL_VALUE")
public void createSingleFileComment(
final ChangedFile file, final Integer newLine, final String content) {
markMergeRequestAsWIP();
final Integer projectId = project.getId();
final String baseSha = mergeRequest.getDiffRefs().getBaseSha();
final String startSha = mergeRequest.getDiffRefs().getStartSha();
final String headSha = mergeRequest.getDiffRefs().getHeadSha();
final String patchString = file.getSpecifics().get(0);
final String oldPath = file.getSpecifics().get(1);
final String newPath = file.getSpecifics().get(2);
final Integer oldLine =
new PatchParser(patchString) //
.findOldLine(newLine) //
.orElse(null);
Integer projectId = null;
String baseSha = null;
String startSha = null;
String headSha = null;
String newPath = null;
final DiffRef diffRefs = mergeRequest.getDiffRefs();
Objects.requireNonNull(
diffRefs,
"diffRefs is null for MR with Iid "
+ mergeRequest.getIid()
+ " in projectId "
+ mergeRequest.getProjectId());
try {
projectId = project.getId();
baseSha = diffRefs.getBaseSha();
startSha = diffRefs.getStartSha();
headSha = diffRefs.getHeadSha();
final String patchString = file.getSpecifics().get(0);
final String oldPath = file.getSpecifics().get(1);
newPath = file.getSpecifics().get(2);
final Integer oldLine =
new PatchParser(patchString) //
.findOldLine(newLine) //
.orElse(null);
final Date date = null;
final String positionHash = null;
final Position position = new Position();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
import se.bjurr.violations.lib.util.Utils;

public class ViolationCommentsToGitLabApi {
private static class ViolationsLoggerJavaLogger implements ViolationsLogger {
@Override
public void log(final Level level, final String string) {
Logger.getLogger(ViolationsLogger.class.getSimpleName()).log(level, string);
}

@Override
public void log(final Level level, final String string, final Throwable t) {
Logger.getLogger(ViolationsLogger.class.getSimpleName()).log(level, string, t);
}
}

private static final String DEFAULT_VIOLATION_TEMPLATE_MUSTACH =
"default-violation-template-gitlab.mustach";

Expand All @@ -37,18 +49,7 @@ public static ViolationCommentsToGitLabApi violationCommentsToGitLabApi() {
private boolean shouldKeepOldComments;
private boolean shouldSetWIP;
private String commentTemplate;
private ViolationsLogger violationsLogger =
new ViolationsLogger() {
@Override
public void log(final Level level, final String string) {
Logger.getLogger(ViolationsLogger.class.getSimpleName()).log(level, string);
}

@Override
public void log(final Level level, final String string, final Throwable t) {
Logger.getLogger(ViolationsLogger.class.getSimpleName()).log(level, string, t);
}
};
private ViolationsLogger violationsLogger = new ViolationsLoggerJavaLogger();
private String proxyServer;
private String proxyUser;
private String proxyPassword;
Expand Down

0 comments on commit a288423

Please sign in to comment.