Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated to sonar v 8.2.0.32929 API #280

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<license.mailto>gabriel.allaigre@gmail.com</license.mailto>
<license.owner>Talanlabs</license.owner>

<sonar.version>7.0</sonar.version>
<sonar.version>8.2.0.32929</sonar.version>
<sonar.pluginName>GitLab</sonar.pluginName>
<sonar.pluginClass>com.talanlabs.sonar.plugins.gitlab.GitLabPlugin</sonar.pluginClass>

Expand All @@ -32,7 +32,7 @@
<!-- Release: enable publication to Bintray -->
<artifactsToPublish>${project.groupId}:${project.artifactId}:jar</artifactsToPublish>

<jacoco-maven-plugin.version>0.7.5.201505241946</jacoco-maven-plugin.version>
<jacoco-maven-plugin.version>0.8.5</jacoco-maven-plugin.version>
<jacoco.ut.execution.data.file>${project.build.directory}/coverage-reports/jacoco-ut.exec
</jacoco.ut.execution.data.file>

Expand Down Expand Up @@ -83,6 +83,9 @@
<contributor>
<name>David Marin Vaquero</name>
</contributor>
<contributor>
<name>Jakub Vavrik</name>
</contributor>
</contributors>
<scm>
<url>https://github.com/gabrie-allaigre/sonar-gitlab-plugin</url>
Expand Down Expand Up @@ -140,6 +143,12 @@
<version>${sonar.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-plugin-api-impl</artifactId>
<version>${sonar.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-ws</artifactId>
Expand All @@ -166,6 +175,11 @@
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
</dependency>

<!-- unit tests -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.apache.commons.lang3.StringEscapeUtils;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.log.Logger;
Expand All @@ -53,19 +52,17 @@ public abstract class AbstractCommentBuilder {
protected final String revision;
protected final List<ReportIssue> reportIssues;
protected final MarkDownUtils markDownUtils;
protected final AnalysisMode analysisMode;
private final String templateName;
private final String template;

AbstractCommentBuilder(GitLabPluginConfiguration gitLabPluginConfiguration, String revision, List<ReportIssue> reportIssues, MarkDownUtils markDownUtils, AnalysisMode analysisMode,
AbstractCommentBuilder(GitLabPluginConfiguration gitLabPluginConfiguration, String revision, List<ReportIssue> reportIssues, MarkDownUtils markDownUtils,
String templateName, String template) {
super();

this.gitLabPluginConfiguration = gitLabPluginConfiguration;
this.revision = revision;
this.reportIssues = reportIssues;
this.markDownUtils = markDownUtils;
this.analysisMode = analysisMode;
this.templateName = templateName;
this.template = template;
}
Expand Down Expand Up @@ -110,7 +107,7 @@ protected Map<String, Object> createContext() {
root.put("onlyIssueFromCommitFile", gitLabPluginConfiguration.onlyIssueFromCommitFile());
root.put("commentNoIssue", gitLabPluginConfiguration.commentNoIssue());
root.put("sonarUrl", gitLabPluginConfiguration.baseUrl());
root.put("publishMode", analysisMode.isPublish());
root.put("publishMode", true);
// Report
root.put("revision", revision);
Arrays.stream(Severity.values()).forEach(severity -> root.put(severity.name(), severity));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@

import com.google.common.annotations.VisibleForTesting;
import com.talanlabs.sonar.plugins.gitlab.models.JsonMode;
import org.sonar.api.batch.InstantiationStrategy;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.scanner.ScannerSide;
import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
Expand All @@ -41,7 +40,6 @@
/**
* Facade for all WS interaction with GitLab.
*/
@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
@ScannerSide
public class CommitFacade {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ public void describe(PostJobDescriptor descriptor) {

@Override
public void execute(PostJobContext context) {
LOG.info("Will execute CommitPublishPostJob of GitlabPlugin.");
try {
if (!gitLabPluginConfiguration.isEnabled()) {
LOG.info("GitLab plugin is disabled");
return;
} else {
LOG.info("GitLab plugin is enabled");
}
File baseDir = fileFromProperty(context, SONAR_PROJECT_BASE_DIR);
if (baseDir == null) {
Expand All @@ -89,13 +92,8 @@ public void execute(PostJobContext context) {
}
QualityGate qualityGate;
List<Issue> issues;
if (context.analysisMode().isPublish()) {
qualityGate = sonarFacade.loadQualityGate();
issues = sonarFacade.getNewIssues();
} else {
qualityGate = null;
issues = toIssues(context.issues());
}
qualityGate = sonarFacade.loadQualityGate();
issues = sonarFacade.getNewIssues();

Reporter report = reporterBuilder.build(qualityGate, issues);
notification(report);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import com.talanlabs.sonar.plugins.gitlab.models.QualityGateFailMode;
import com.talanlabs.sonar.plugins.gitlab.models.StatusNotificationsMode;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.InstantiationStrategy;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.scanner.ScannerSide;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.config.Configuration;
import org.sonar.api.utils.System2;
Expand All @@ -36,7 +35,6 @@
import java.util.Arrays;
import java.util.List;

@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
@ScannerSide
public class GitLabPluginConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,23 @@
import com.talanlabs.sonar.plugins.gitlab.models.Issue;
import com.talanlabs.sonar.plugins.gitlab.models.QualityGate;
import com.talanlabs.sonar.plugins.gitlab.models.ReportIssue;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.rule.Severity;

import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class GlobalCommentBuilder extends AbstractCommentBuilder {

private final String author;
private final QualityGate qualityGate;
private final Reporter reporter;

public GlobalCommentBuilder(GitLabPluginConfiguration gitLabPluginConfiguration, String author, QualityGate qualityGate, Reporter reporter, MarkDownUtils markDownUtils,
AnalysisMode analysisMode) {
super(gitLabPluginConfiguration, gitLabPluginConfiguration.commitSHA().get(0), reporter.getReportIssues(), markDownUtils, analysisMode, "global", gitLabPluginConfiguration.globalTemplate());
public GlobalCommentBuilder(GitLabPluginConfiguration gitLabPluginConfiguration, String author, QualityGate qualityGate, Reporter reporter, MarkDownUtils markDownUtils) {
super(gitLabPluginConfiguration, gitLabPluginConfiguration.commitSHA().get(0), reporter.getReportIssues(), markDownUtils, "global", gitLabPluginConfiguration.globalTemplate());

this.author = author;
this.qualityGate = qualityGate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package com.talanlabs.sonar.plugins.gitlab;

import com.talanlabs.sonar.plugins.gitlab.models.ReportIssue;
import org.sonar.api.batch.AnalysisMode;

import java.util.List;
import java.util.Map;
Expand All @@ -32,8 +31,8 @@ public class InlineCommentBuilder extends AbstractCommentBuilder {
private final String author;

public InlineCommentBuilder(GitLabPluginConfiguration gitLabPluginConfiguration, String revision, String author, Integer lineNumber, List<ReportIssue> reportIssues,
MarkDownUtils markDownUtils, AnalysisMode analysisMode) {
super(gitLabPluginConfiguration, revision, reportIssues, markDownUtils, analysisMode, "inline", gitLabPluginConfiguration.inlineTemplate());
MarkDownUtils markDownUtils) {
super(gitLabPluginConfiguration, revision, reportIssues, markDownUtils, "inline", gitLabPluginConfiguration.inlineTemplate());

this.lineNumber = lineNumber;
this.author = author;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
*/
package com.talanlabs.sonar.plugins.gitlab;

import org.sonar.api.batch.InstantiationStrategy;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.scanner.ScannerSide;

import javax.annotation.Nullable;

@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
@ScannerSide
public class MarkDownUtils {

Expand Down
26 changes: 23 additions & 3 deletions src/main/java/com/talanlabs/sonar/plugins/gitlab/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@
*/
package com.talanlabs.sonar.plugins.gitlab;

import com.google.common.base.Charsets;
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.talanlabs.sonar.plugins.gitlab.models.*;
import org.apache.commons.lang3.StringEscapeUtils;
import org.sonar.api.batch.rule.Severity;

import javax.annotation.Nullable;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -231,10 +239,22 @@ public String buildJson() {
private String buildIssueCodeQualityJson(ReportIssue reportIssue) {
Issue issue = reportIssue.getIssue();

String description = prepareMessageJson(issue.getMessage());
String location = buildLocationCodeQualityJson(reportIssue);

HashFunction hf = Hashing.md5();
HashCode hc = hf.newHasher()
.putString(description, Charsets.UTF_8)
.putString(location, Charsets.UTF_8)
.hash();

String fingerprint = hc.toString();

StringJoiner sj = new StringJoiner(",", "{", "}");
sj.add("\"fingerprint\":\"" + issue.getKey() + "\"");
sj.add("\"description\":\"" + prepareMessageJson(issue.getMessage()) + "\"");
sj.add("\"location\":" + buildLocationCodeQualityJson(reportIssue));
sj.add("\"fingerprint\":\"" + fingerprint + "\"");
sj.add("\"description\":\"" + description + "\"");
sj.add("\"severity\":\"" + issue.getSeverity().name().toLowerCase() + "\"");
sj.add("\"location\":" + location);
return sj.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
*/
package com.talanlabs.sonar.plugins.gitlab;

import com.talanlabs.sonar.plugins.gitlab.models.*;
import com.talanlabs.sonar.plugins.gitlab.models.Issue;
import com.talanlabs.sonar.plugins.gitlab.models.JsonMode;
import com.talanlabs.sonar.plugins.gitlab.models.QualityGate;
import com.talanlabs.sonar.plugins.gitlab.models.ReportIssue;
import com.talanlabs.sonar.plugins.gitlab.models.Rule;
import org.sonar.api.ExtensionPoint;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.InstantiationStrategy;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.scanner.ScannerSide;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

Expand All @@ -35,7 +37,6 @@
import java.util.Map;
import java.util.stream.Stream;

@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
@ScannerSide
@ExtensionPoint
public class ReporterBuilder {
Expand All @@ -48,14 +49,12 @@ public class ReporterBuilder {
private final SonarFacade sonarFacade;
private final CommitFacade commitFacade;
private final MarkDownUtils markDownUtils;
private final AnalysisMode analysisMode;

public ReporterBuilder(GitLabPluginConfiguration gitLabPluginConfiguration, SonarFacade sonarFacade, CommitFacade commitFacade, MarkDownUtils markDownUtils, AnalysisMode analysisMode) {
public ReporterBuilder(GitLabPluginConfiguration gitLabPluginConfiguration, SonarFacade sonarFacade, CommitFacade commitFacade, MarkDownUtils markDownUtils) {
this.gitLabPluginConfiguration = gitLabPluginConfiguration;
this.sonarFacade = sonarFacade;
this.commitFacade = commitFacade;
this.markDownUtils = markDownUtils;
this.analysisMode = analysisMode;
}

/**
Expand Down Expand Up @@ -152,6 +151,7 @@ private int toSeverityNum(Severity severity) {
}

private void updateReviewComments(Reporter report) {
LOG.info("Will try to update review comments.");
for (Map.Entry<String, Map<File, Map<Integer, List<ReportIssue>>>> entry : report.getFileLineMap().entrySet()) {
String revision = entry.getKey();

Expand Down Expand Up @@ -181,7 +181,7 @@ private void updateReviewComments(String revision, String username, File file, I
}

private void updateReviewCommentsPerInline(String revision, String username, File file, Integer lineNumber, List<ReportIssue> reportIssues) {
String body = new InlineCommentBuilder(gitLabPluginConfiguration, revision, username, lineNumber, reportIssues, markDownUtils, analysisMode).buildForMarkdown();
String body = new InlineCommentBuilder(gitLabPluginConfiguration, revision, username, lineNumber, reportIssues, markDownUtils).buildForMarkdown();
if (body != null && !body.trim().isEmpty()) {
boolean exists = commitFacade.hasSameCommitCommentsForFile(revision, file, lineNumber, body);
if (!exists) {
Expand All @@ -192,7 +192,7 @@ private void updateReviewCommentsPerInline(String revision, String username, Fil

private void updateGlobalComments(QualityGate qualityGate, Reporter report) {
String username = commitFacade.getUsernameForRevision(gitLabPluginConfiguration.commitSHA().get(0));
String body = new GlobalCommentBuilder(gitLabPluginConfiguration, username, qualityGate, report, markDownUtils, analysisMode).buildForMarkdown();
String body = new GlobalCommentBuilder(gitLabPluginConfiguration, username, qualityGate, report, markDownUtils).buildForMarkdown();
if (body != null && !body.trim().isEmpty()) {
commitFacade.addGlobalComment(body);
}
Expand Down
Loading