Skip to content

Commit

Permalink
Merge with JENKINS-31013
Browse files Browse the repository at this point in the history
  • Loading branch information
aquarellian committed Oct 21, 2015
2 parents 4099185 + f9ad3a1 commit 40a6624
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 35 deletions.
Expand Up @@ -63,6 +63,8 @@ public class SonarToGerritBuilder extends Builder {
private static final String DEFAULT_SONAR_URL = "http://localhost:9000";
private static final String DEFAULT_CATEGORY = "Code-Review";
private static final int DEFAULT_SCORE = 0;
private static final ReviewInput.NotifyHandling DEFAULT_NOTIFICATION_NO_ISSUES = ReviewInput.NotifyHandling.NONE;
private static final ReviewInput.NotifyHandling DEFAULT_NOTIFICATION_ISSUES = ReviewInput.NotifyHandling.OWNER;

public static final String GERRIT_FILE_DELIMITER = "/";
public static final String EMPTY_STR = "";
Expand All @@ -86,12 +88,16 @@ public class SonarToGerritBuilder extends Builder {
private final String noIssuesScore;
private final String issuesScore;

private final String noIssuesNotification;
private final String issuesNotification;


@DataBoundConstructor
public SonarToGerritBuilder(String projectPath, String sonarURL, String path,
String severity, boolean changedLinesOnly, boolean newIssuesOnly,
String noIssuesToPostText, String someIssuesToPostText, String issueComment,
boolean postScore, String category, String noIssuesScore, String issuesScore) {
boolean postScore, String category, String noIssuesScore, String issuesScore,
String noIssuesNotification, String issuesNotification) {
this.projectPath = MoreObjects.firstNonNull(projectPath, EMPTY_STR);
this.sonarURL = MoreObjects.firstNonNull(sonarURL, DEFAULT_SONAR_URL);
this.path = MoreObjects.firstNonNull(path, DEFAULT_PATH);
Expand All @@ -105,6 +111,8 @@ public SonarToGerritBuilder(String projectPath, String sonarURL, String path,
this.category = MoreObjects.firstNonNull(category, DEFAULT_CATEGORY);
this.noIssuesScore = noIssuesScore;
this.issuesScore = issuesScore;
this.noIssuesNotification = noIssuesNotification;
this.issuesNotification = issuesNotification;
}

public String getPath() {
Expand Down Expand Up @@ -155,6 +163,14 @@ public String getNoIssuesScore() {
return noIssuesScore;
}

public String getNoIssuesNotification() {
return noIssuesNotification;
}

public String getIssuesNotification() {
return issuesNotification;
}

public String getIssuesScore() {
return issuesScore;
}
Expand Down Expand Up @@ -197,7 +213,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
return false;
}

if (!gerritConfig.isUseRestApi()){
if (!gerritConfig.isUseRestApi()) {
logError(listener, "jenkins.plugin.error.gerrit.restapi.off", Level.SEVERE);
return false;
}
Expand Down Expand Up @@ -262,11 +278,21 @@ private String getReviewMessage(Multimap<String, Issue> finalIssues) {
return new CustomReportFormatter(finalIssues.values(), someIssuesToPostText, noIssuesToPostText).getMessage();
}

private int getReviewMark(Multimap<String, Issue> finalIssues) {
String mark = finalIssues.size() > 0 ? issuesScore : noIssuesScore;
private int getReviewMark(int finalIssuesCount) {
String mark = finalIssuesCount > 0 ? issuesScore : noIssuesScore;
return parseNumber(mark, DEFAULT_SCORE);
}

private ReviewInput.NotifyHandling getNotificationSettings(int finalIssuesCount) {
if (finalIssuesCount > 0) {
ReviewInput.NotifyHandling value = (issuesNotification == null ? null : ReviewInput.NotifyHandling.valueOf(issuesNotification));
return MoreObjects.firstNonNull(value, DEFAULT_NOTIFICATION_ISSUES);
} else {
ReviewInput.NotifyHandling value = (noIssuesNotification == null ? null : ReviewInput.NotifyHandling.valueOf(noIssuesNotification));
return MoreObjects.firstNonNull(value, DEFAULT_NOTIFICATION_NO_ISSUES);
}
}

private int parseNumber(String number, int deflt) {
try {
return Integer.parseInt(number);
Expand All @@ -281,6 +307,15 @@ ReviewInput getReviewResult(Multimap<String, Issue> finalIssues, Collection<Stri
String reviewMessage = getReviewMessage(finalIssues);
ReviewInput reviewInput = new ReviewInput().message(reviewMessage);

int finalIssuesCount = finalIssues.size();

reviewInput.notify = getNotificationSettings(finalIssuesCount);

if (postScore) {
String realCategory = getRealCategory(existingCategories);
reviewInput.label(realCategory, getReviewMark(finalIssuesCount));
}

reviewInput.comments = new HashMap<String, List<ReviewInput.CommentInput>>();
for (String file : finalIssues.keySet()) {
reviewInput.comments.put(file, Lists.newArrayList(
Expand All @@ -304,14 +339,10 @@ public ReviewInput.CommentInput apply(@Nullable Issue input) {
)
);
}
if (postScore) {
String realCategory = getRealCategory(existingCategories);
reviewInput.label(realCategory, getReviewMark(finalIssues));
}
return reviewInput;
}

private Collection<String> getCategoryNames(List<VerdictCategory> categories){
private Collection<String> getCategoryNames(List<VerdictCategory> categories) {
Set<String> availableCategories = new HashSet<String>();
for (VerdictCategory verdictCategory : categories) {
availableCategories.add(verdictCategory.getVerdictDescription());
Expand Down Expand Up @@ -586,18 +617,54 @@ public FormValidation doCheckIssuesScore(@QueryParameter String value) {
}

private FormValidation checkScore(@QueryParameter String value) {
try{
try {
Integer.parseInt(value);
} catch (NumberFormatException e){
} catch (NumberFormatException e) {
return FormValidation.error(getLocalized("jenkins.plugin.validation.review.score.not.numeric"));
}
return FormValidation.ok();
}

/**
* Performs on-the-fly validation of the form field 'noIssuesNotification'.
*
* @param value This parameter receives the value that the user has typed.
* @return Indicates the outcome of the validation. This is sent to the browser.
* <p/>
* Note that returning {@link FormValidation#error(String)} does not
* prevent the form from being saved. It just means that a message
* will be displayed to the user.
*/
@SuppressWarnings(value = "unused")
public FormValidation doCheckNoIssuesNotification(@QueryParameter String value) {
return checkNotificationType(value);
}

/**
* Performs on-the-fly validation of the form field 'issuesNotification'.
*
* @param value This parameter receives the value that the user has typed.
* @return Indicates the outcome of the validation. This is sent to the browser.
* <p/>
* Note that returning {@link FormValidation#error(String)} does not
* prevent the form from being saved. It just means that a message
* will be displayed to the user.
*/
@SuppressWarnings(value = "unused")
public FormValidation doCheckIssuesNotification(@QueryParameter String value) {
return checkNotificationType(value);
}

private FormValidation checkNotificationType(@QueryParameter String value) {
if (value == null || ReviewInput.NotifyHandling.valueOf(value) == null) {
return FormValidation.error(getLocalized("jenkins.plugin.validation.review.notification.recipient.unknown"));
}
return FormValidation.ok();
}

@Override
public boolean isApplicable(Class<? extends AbstractProject> aClass) {
// Indicates that this builder can be used with all kinds of project types
//todo check if gerrit trigger installed
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/messages.properties
Expand Up @@ -18,4 +18,5 @@ jenkins.plugin.error.sonar.report.not.exists=Sonar report '%s' does not exist. P
jenkins.plugin.validation.review.severity.unknown=Unknown severity
jenkins.plugin.validation.review.category.unknown=Unknown category
jenkins.plugin.validation.review.score.not.numeric=Score should be numeric
jenkins.plugin.error.gerrit.restapi.off=RestAPI in Gerrit-Trigger settings must be enabled
jenkins.plugin.error.gerrit.restapi.off=RestAPI in Gerrit-Trigger settings must be enabled
jenkins.plugin.validation.review.notification.recipient.unknown=Notification recipient is unknown
Expand Up @@ -87,6 +87,35 @@
<!--</table>-->
<!--</f:entry>-->
</f:section>
</f:advanced>

<f:section title="${%jenkins.plugin.settings.section.notification.settings.name}">
<f:entry title="${%jenkins.plugin.settings.notification.no.issues}" field="noIssuesNotification">
<select name="noIssuesNotification" field="severity">
<f:option value="NONE"
selected="${instance.noIssuesNotification =='NONE' || instance.noIssuesNotification == null}">
${%NONE}
</f:option>
<f:option value="OWNER" selected="${instance.noIssuesNotification =='OWNER'}">${%OWNER}</f:option>
<f:option value="OWNER_REVIEWERS"
selected="${instance.noIssuesNotification =='OWNER_REVIEWERS'}">${%OWNER_REVIEWERS}
</f:option>
<f:option value="ALL" selected="${instance.noIssuesNotification =='ALL'}">${%ALL}</f:option>

</select>
</f:entry>
<f:entry title="${%jenkins.plugin.settings.notification.issues}" field="issuesNotification">
<select name="issuesNotification" field="severity">
<f:option value="NONE" selected="${instance.issuesNotification =='NONE'}">${%NONE}</f:option>
<f:option value="OWNER"
selected="${instance.issuesNotification =='OWNER' || instance.issuesNotification == null}">
${%OWNER}
</f:option>
<f:option value="OWNER_REVIEWERS"
selected="${instance.issuesNotification =='OWNER_REVIEWERS'}">${%OWNER_REVIEWERS}
</f:option>
<f:option value="ALL" selected="${instance.issuesNotification =='ALL'}">${%ALL}</f:option>
</select>
</f:entry>
</f:section>
</f:advanced>
</j:jelly>
Expand Up @@ -31,5 +31,14 @@ jenkins.plugin.settings.gerrit.category=Category
jenkins.plugin.settings.gerrit.score.no.issues=Score for no Sonar violations found case
jenkins.plugin.settings.gerrit.score.issues=Score for Sonar violations found case

jenkins.plugin.settings.section.notification.settings.name=Notification Settings
jenkins.plugin.settings.notification.no.issues=To be notified if no Sonar violations found
jenkins.plugin.settings.notification.issues=To be notified if several Sonar violations found

NONE=None
OWNER=Owner
OWNER_REVIEWERS=Owner and Reviewers
ALL=All



@@ -0,0 +1,11 @@
<div>
This setting allow to determine who will be notified in case there are <b>several Sonar issues matching filter settings</b> found in particular commit. <br/>
An e-mails sent to recipients will contain review information: title of review and list of comments (issues).
Recipient settings: <br/>
<br/>
* None - No one;<br/>
* Owner - Owner (i.e. author of the commit);<br/>
* Owner and Reviewers - Owner (i.e. author of the commit) and all Reviewers (people who was added to reviewer list of the commit);<br/>
* All - everyone in the project list. <br/>
<br/>
</div>
@@ -0,0 +1,11 @@
<div>
This setting allow to determine who will be notified in case there are <b>no Sonar issues matching filter settings</b> found in particular commit. <br/>
An e-mails sent to recipients will contain review information: title of review.
Recipient settings: <br/>
<br/>
* None - No one;<br/>
* Owner - Owner (i.e. author of the commit);<br/>
* Owner and Reviewers - Owner (i.e. author of the commit) and all Reviewers (people who was added to reviewer list of the commit);<br/>
* All - everyone in the project list. <br/>
<br/>
</div>

0 comments on commit 40a6624

Please sign in to comment.