Skip to content
This repository was archived by the owner on Aug 12, 2020. It is now read-only.

Commit d21fb67

Browse files
committed
Refactoring
1 parent 271cb5a commit d21fb67

File tree

10 files changed

+239
-138
lines changed

10 files changed

+239
-138
lines changed

src/main/java/com/github/goober/sonarqube/plugin/PullRequestDecoratorPlugin.java

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@
2222
import com.github.goober.sonarqube.plugin.decorator.bitbucket.BitbucketProperties;
2323
import com.github.goober.sonarqube.plugin.decorator.bitbucket.BitbucketPropertiesSensor;
2424
import com.github.goober.sonarqube.plugin.decorator.bitbucket.BitbucketPullRequestDecorator;
25+
import com.github.goober.sonarqube.plugin.decorator.sonarqube.PullRequestReportCollector;
2526
import com.github.goober.sonarqube.plugin.decorator.sonarqube.SonarQubeClient;
2627
import org.sonar.api.Plugin;
2728
import org.sonar.api.SonarQubeSide;
28-
import org.sonar.api.config.PropertyDefinition;
29-
import org.sonar.api.resources.Qualifiers;
3029

3130
public class PullRequestDecoratorPlugin implements Plugin {
3231

@@ -36,39 +35,11 @@ public void define(Context context) {
3635
context.addExtension(BitbucketPropertiesSensor.class);
3736
}
3837
if (SonarQubeSide.COMPUTE_ENGINE == context.getRuntime().getSonarQubeSide()) {
39-
context.addExtensions(SonarQubeClient.class, BitbucketClient.class, BitbucketPullRequestDecorator.class);
38+
context.addExtensions(PullRequestReportCollector.class, SonarQubeClient.class,
39+
BitbucketClient.class, BitbucketPullRequestDecorator.class);
4040
}
4141

42-
context.addExtensions(
43-
PropertyDefinition.builder(BitbucketProperties.ENDPOINT.getKey())
44-
.index(0)
45-
.name("The URL of the Bitbucket Server")
46-
.description("This is the base URL for your Bitbucket Server instance")
47-
.onQualifiers(Qualifiers.PROJECT)
48-
.category("pullrequest").subCategory("bitbucket")
49-
.build(),
50-
PropertyDefinition.builder(BitbucketProperties.TOKEN.getKey())
51-
.index(1)
52-
.name("Personal access token")
53-
.description("The personal access token of the user that will be used to decorate the pull requests")
54-
.onQualifiers(Qualifiers.PROJECT)
55-
.category("pullrequest").subCategory("bitbucket")
56-
.build(),
57-
PropertyDefinition.builder(BitbucketProperties.PROJECT.getKey())
58-
.index(2)
59-
.name("Bitbucket Server project key")
60-
.description("You can find it in the Bitbucket Server repository URL")
61-
.onlyOnQualifiers(Qualifiers.PROJECT)
62-
.category("pullrequest").subCategory("bitbucket")
63-
.build(),
64-
PropertyDefinition.builder(BitbucketProperties.REPOSITORY.getKey())
65-
.index(3)
66-
.name("Bitbucket Server repository slug")
67-
.description("You can find it in the Bitbucket Server repository URL")
68-
.onlyOnQualifiers(Qualifiers.PROJECT)
69-
.category("pullrequest").subCategory("bitbucket")
70-
.build());
71-
42+
context.addExtensions(BitbucketProperties.definitions());
7243
}
7344

7445
}

src/main/java/com/github/goober/sonarqube/plugin/decorator/bitbucket/BitbucketClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import com.fasterxml.jackson.annotation.JsonInclude.Include;
44
import com.fasterxml.jackson.databind.DeserializationFeature;
55
import com.fasterxml.jackson.databind.ObjectMapper;
6-
import com.github.goober.sonarqube.plugin.decorator.bitbucket.model.ServerProperties;
76
import com.github.goober.sonarqube.plugin.decorator.bitbucket.model.CreateAnnotationsRequest;
87
import com.github.goober.sonarqube.plugin.decorator.bitbucket.model.CreateReportRequest;
8+
import com.github.goober.sonarqube.plugin.decorator.bitbucket.model.ServerProperties;
99
import okhttp3.MediaType;
1010
import okhttp3.OkHttpClient;
1111
import okhttp3.Request;

src/main/java/com/github/goober/sonarqube/plugin/decorator/bitbucket/BitbucketProperties.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.github.goober.sonarqube.plugin.decorator.bitbucket;
22

3+
import org.sonar.api.config.PropertyDefinition;
4+
import org.sonar.api.resources.Qualifiers;
5+
6+
import java.util.Arrays;
7+
import java.util.Collection;
8+
39
public enum BitbucketProperties {
410
ENDPOINT("sonar.pullrequest.bitbucket.endpoint"),
511
TOKEN("sonar.pullrequest.bitbucket.token.secured"),
@@ -16,4 +22,35 @@ public enum BitbucketProperties {
1622
public String getKey() {
1723
return key;
1824
}
25+
26+
public static Collection<PropertyDefinition> definitions() {
27+
return Arrays.asList(PropertyDefinition.builder(ENDPOINT.getKey())
28+
.index(0)
29+
.name("The URL of the Bitbucket Server")
30+
.description("This is the base URL for your Bitbucket Server instance")
31+
.onQualifiers(Qualifiers.PROJECT)
32+
.category("pullrequest").subCategory("bitbucket")
33+
.build(),
34+
PropertyDefinition.builder(TOKEN.getKey())
35+
.index(1)
36+
.name("Personal access token")
37+
.description("The personal access token of the user that will be used to decorate the pull requests")
38+
.onQualifiers(Qualifiers.PROJECT)
39+
.category("pullrequest").subCategory("bitbucket")
40+
.build(),
41+
PropertyDefinition.builder(PROJECT.getKey())
42+
.index(2)
43+
.name("Bitbucket Server project key")
44+
.description("You can find it in the Bitbucket Server repository URL")
45+
.onlyOnQualifiers(Qualifiers.PROJECT)
46+
.category("pullrequest").subCategory("bitbucket")
47+
.build(),
48+
PropertyDefinition.builder(REPOSITORY.getKey())
49+
.index(3)
50+
.name("Bitbucket Server repository slug")
51+
.description("You can find it in the Bitbucket Server repository URL")
52+
.onlyOnQualifiers(Qualifiers.PROJECT)
53+
.category("pullrequest").subCategory("bitbucket")
54+
.build());
55+
}
1956
}

src/main/java/com/github/goober/sonarqube/plugin/decorator/bitbucket/BitbucketPullRequestDecorator.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,33 @@
22

33
import com.github.goober.sonarqube.plugin.decorator.PullRequestDecorator;
44
import com.github.goober.sonarqube.plugin.decorator.bitbucket.model.CreateAnnotationsRequest;
5-
import com.github.goober.sonarqube.plugin.decorator.sonarqube.SonarQubeClient;
65
import com.github.goober.sonarqube.plugin.decorator.bitbucket.model.ServerProperties;
6+
import com.github.goober.sonarqube.plugin.decorator.sonarqube.PullRequestReportCollector;
7+
import lombok.RequiredArgsConstructor;
78
import org.sonar.api.utils.log.Logger;
89
import org.sonar.api.utils.log.Loggers;
910

1011
import java.io.IOException;
1112

1213
import static java.lang.String.format;
14+
15+
@RequiredArgsConstructor
1316
public class BitbucketPullRequestDecorator implements PullRequestDecorator {
1417

1518
private static final Logger LOGGER = Loggers.get(BitbucketPullRequestDecorator.class);
1619

17-
private final SonarQubeClient sonarqubeClient;
20+
private final PullRequestReportCollector reportCollector;
1821
private final BitbucketClient bitbucketClient;
1922

20-
public BitbucketPullRequestDecorator(SonarQubeClient sonarqubeClient, BitbucketClient bitbucketClient) {
21-
this.sonarqubeClient = sonarqubeClient;
22-
this.bitbucketClient = bitbucketClient;
23-
}
24-
2523
@Override
2624
public boolean isActivated() {
2725
return bitbucketClient.isConfigured() && hasApiSupport();
2826
}
2927

3028
@Override
3129
public void decorate(ProjectAnalysis analysis) {
32-
BitbucketPullRequestReport report = new BitbucketPullRequestReport(analysis);
3330
try {
34-
35-
report.setReportDetails(sonarqubeClient.listMetrics().getMetrics())
36-
.setReportData(sonarqubeClient.listMeasures(analysis.getProject().getKey(), report.getPullRequestId(),
37-
"new_code_smells",
38-
"new_bugs",
39-
"new_vulnerabilities",
40-
"new_coverage",
41-
"new_duplicated_lines_density"))
42-
.setAnnotations(sonarqubeClient.listOpenIssues(analysis.getProject().getKey(), report.getPullRequestId()).getIssues());
31+
BitbucketPullRequestReport report = new BitbucketPullRequestReport(reportCollector.collect(analysis));
4332

4433
bitbucketClient.createReport(report.getProject(),
4534
report.getRepository(),
@@ -57,7 +46,10 @@ public void decorate(ProjectAnalysis analysis) {
5746
.build());
5847

5948
} catch (IOException e) {
60-
LOGGER.error("Could not decorate pull request {}, in project {}", report.getPullRequestId(), analysis.getProject().getKey(), e);
49+
LOGGER.error("Could not decorate pull request for project {}", analysis.getProject().getKey(), e);
50+
}
51+
}
52+
6153
private boolean hasApiSupport() {
6254
try {
6355
ServerProperties server = bitbucketClient.getServerProperties();

0 commit comments

Comments
 (0)