Skip to content

Commit

Permalink
add link to SonarQube
Browse files Browse the repository at this point in the history
fixes a part of #67
  • Loading branch information
tisoft authored and mc1arke committed Jan 13, 2020
1 parent ec0e2fc commit 1e28de6
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.markup.FormatterFactory;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.markup.Heading;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.markup.Image;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.markup.Link;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.markup.ListItem;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.markup.Node;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.markup.Paragraph;
Expand All @@ -47,6 +48,7 @@
import org.sonar.server.measure.Rating;

import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
Expand All @@ -73,6 +75,7 @@ public class AnalysisDetails {

public static final String IMAGE_URL_BASE = "com.github.mc1arke.sonarqube.plugin.branch.image-url-base";

private final String publicRootURL;
private final BranchDetails branchDetails;
private final MeasuresHolder measuresHolder;
private final PostAnalysisIssueVisitor postAnalysisIssueVisitor;
Expand All @@ -83,8 +86,9 @@ public class AnalysisDetails {

AnalysisDetails(BranchDetails branchDetails, PostAnalysisIssueVisitor postAnalysisIssueVisitor,
QualityGate qualityGate, MeasuresHolder measuresHolder, Analysis analysis, Project project,
Configuration configuration) {
Configuration configuration, String publicRootURL) {
super();
this.publicRootURL = publicRootURL;
this.branchDetails = branchDetails;
this.measuresHolder = measuresHolder;
this.postAnalysisIssueVisitor = postAnalysisIssueVisitor;
Expand Down Expand Up @@ -180,7 +184,8 @@ public String createAnalysisSummary(FormatterFactory formatterFactory) {
.map(i -> i + "% Duplicated Code")
.orElse("No duplication information") + " (" +
decimalFormat.format(duplications) +
"% Estimated after merge)"))));
"% Estimated after merge)"))),
new Link(publicRootURL + "/dashboard?id=" + URLEncoder.encode(project.getKey()) + "&pullRequest=" + branchDetails.getBranchName(), new Text("View in SonarQube")));

return formatterFactory.documentFormatter().format(document, formatterFactory);
}
Expand All @@ -202,7 +207,8 @@ public String createAnalysisIssueSummary(PostAnalysisIssueVisitor.ComponentIssue
new Paragraph(new Text(String.format("**Severity:** %s ", issue.severity())), new Image(issue.severity(), String.format("%s/checks/Severity/%s.svg?sanitize=true", baseImageUrl, issue.severity().toLowerCase()))),
new Paragraph(new Text(String.format("**Message:** %s", issue.getMessage()))),
effortNode,
resolutionNode
resolutionNode,
new Link(publicRootURL + "/project/issues?id=" + URLEncoder.encode(project.getKey()) + "&pullRequest=" + branchDetails.getBranchName() + "&issues=" + issue.key() + "&open=" + issue.key(), new Text("View in SonarQube"))
);
return formatterFactory.documentFormatter().format(document, formatterFactory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.sonar.api.ce.posttask.PostProjectAnalysisTask;
import org.sonar.api.ce.posttask.QualityGate;
import org.sonar.api.config.Configuration;
import org.sonar.api.platform.Server;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.ce.task.projectanalysis.component.ConfigurationRepository;
Expand All @@ -39,18 +40,21 @@ public class PullRequestPostAnalysisTask implements PostProjectAnalysisTask,
private static final Logger LOGGER = Loggers.get(PullRequestPostAnalysisTask.class);

private final List<PullRequestBuildStatusDecorator> pullRequestDecorators;
private final Server server;
private final ConfigurationRepository configurationRepository;
private final PostAnalysisIssueVisitor postAnalysisIssueVisitor;
private final MetricRepository metricRepository;
private final MeasureRepository measureRepository;
private final TreeRootHolder treeRootHolder;

public PullRequestPostAnalysisTask(ConfigurationRepository configurationRepository,
public PullRequestPostAnalysisTask(Server server,
ConfigurationRepository configurationRepository,
List<PullRequestBuildStatusDecorator> pullRequestDecorators,
PostAnalysisIssueVisitor postAnalysisIssueVisitor,
MetricRepository metricRepository, MeasureRepository measureRepository,
TreeRootHolder treeRootHolder) {
super();
this.server = server;
this.configurationRepository = configurationRepository;
this.pullRequestDecorators = pullRequestDecorators;
this.postAnalysisIssueVisitor = postAnalysisIssueVisitor;
Expand Down Expand Up @@ -119,7 +123,7 @@ public void finished(PostProjectAnalysisTask.ProjectAnalysis projectAnalysis) {
postAnalysisIssueVisitor, qualityGate,
new AnalysisDetails.MeasuresHolder(metricRepository, measureRepository,
treeRootHolder), analysis,
projectAnalysis.getProject(), configuration);
projectAnalysis.getProject(), configuration, server.getPublicRootUrl());

PullRequestBuildStatusDecorator pullRequestDecorator = optionalPullRequestDecorator.get();
LOGGER.info("using pull request decorator" + pullRequestDecorator.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ private static <N extends Node> Formatter<N> formatterFor(FormatterFactory forma
return (Formatter<N>) formatterFactory.paragraphFormatter();
} else if (node instanceof Text) {
return (Formatter<N>) formatterFactory.textFormatter();
} else if (node instanceof Link) {
return (Formatter<N>) formatterFactory.linkFormatter();
} else {
throw new IllegalArgumentException("Unknown node type: " + node.getClass().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public interface FormatterFactory {

Formatter<Image> imageFormatter();

Formatter<Link> linkFormatter();

Formatter<List> listFormatter();

Formatter<ListItem> listItemFormatter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ int getLevel() {

@Override
boolean isValidChild(Node child) {
return child instanceof Text || child instanceof Image;
return child instanceof Text || child instanceof Image || child instanceof Link;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2020 Markus Heberling
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.markup;

public final class Link extends Node {

private final String url;

public Link(String url, Node... children) {
super(children);
this.url=url;
}

public String getUrl() {
return url;
}

@Override
boolean isValidChild(Node child) {
return child instanceof Text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public ListItem(Node... children) {

@Override
boolean isValidChild(Node child) {
return child instanceof Text || child instanceof Image;
return child instanceof Text || child instanceof Image|| child instanceof Link;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public String format(Image node, FormatterFactory formatterFactory) {
};
}

@Override
public Formatter<Link> linkFormatter() {
return new BaseFormatter<Link>() {
@Override
public String format(Link node, FormatterFactory formatterFactory) {
return String.format("[%s](%s)", node.getChildren().isEmpty() ? node.getUrl() : childContents(node, formatterFactory), node.getUrl());
}
};
}

@Override
public Formatter<List> listFormatter() {
return new BaseFormatter<List>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public Paragraph(Node... children) {

@Override
boolean isValidChild(Node child) {
return child instanceof Image || child instanceof Text;
return child instanceof Image || child instanceof Text || child instanceof Link;
}
}
Loading

0 comments on commit 1e28de6

Please sign in to comment.