-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from danshan/feature/sonarqube
Feature/sonarqube
- Loading branch information
Showing
17 changed files
with
299 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
#!/bin/bash | ||
|
||
mvn sonar:sonar \ | ||
-Dsonar.host.url=https://sonarqube.com \ | ||
-Dsonar.organization=$SONAR_ORG \ | ||
-Dsonar.login=$SONAR_TOKEN | ||
|
||
java -jar \ | ||
-Dslack.daocloud="$SLACK_DAOCLOUD" \ | ||
-Dslack.microbadger="$SLACK_MICROBADGER" \ | ||
-Dslack.docker="$SLACK_DOCKER" \ | ||
-Dslack.sonar="$SLACK_SONAR" \ | ||
$DATA_DIR/$APP_NAME/target/$APP_NAME.jar |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/main/java/com/shanhh/webhook/sonarqube/beans/SonarPayload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.shanhh.webhook.sonarqube.beans; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
/** | ||
* @author dan | ||
* @since 2017-03-21 17:30 | ||
*/ | ||
@Data | ||
@NoArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class SonarPayload implements Serializable { | ||
/* | ||
@formatter:on | ||
{ | ||
"analysedAt": "2016-11-18T10:46:28+0100", | ||
"project": { | ||
"key": "org.sonarqube:example", | ||
"name": "Example" | ||
}, | ||
"properties": { | ||
}, | ||
"qualityGate": { | ||
"conditions": [ | ||
{ | ||
"errorThreshold": "1", | ||
"metric": "new_security_rating", | ||
"onLeakPeriod": true, | ||
"operator": "GREATER_THAN", | ||
"status": "OK", | ||
"value": "1" | ||
}, | ||
{ | ||
"errorThreshold": "1", | ||
"metric": "new_reliability_rating", | ||
"onLeakPeriod": true, | ||
"operator": "GREATER_THAN", | ||
"status": "OK", | ||
"value": "1" | ||
}, | ||
{ | ||
"errorThreshold": "1", | ||
"metric": "new_maintainability_rating", | ||
"onLeakPeriod": true, | ||
"operator": "GREATER_THAN", | ||
"status": "OK", | ||
"value": "1" | ||
}, | ||
{ | ||
"errorThreshold": "80", | ||
"metric": "new_coverage", | ||
"onLeakPeriod": true, | ||
"operator": "LESS_THAN", | ||
"status": "NO_VALUE" | ||
} | ||
], | ||
"name": "SonarQube way", | ||
"status": "OK" | ||
}, | ||
"status": "SUCCESS", | ||
"taskId": "AVh21JS2JepAEhwQ-b3u" | ||
} | ||
@formatter:off | ||
*/ | ||
|
||
private String analysedAt; | ||
private Project project; | ||
private QualityGate qualityGate; | ||
private String status; | ||
private String taskId; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
public static class Project implements Serializable { | ||
private String key; | ||
private String name; | ||
} | ||
|
||
@Data | ||
@NoArgsConstructor | ||
public static class QualityGate implements Serializable { | ||
private List<Condition> conditions; | ||
private String name; | ||
private String status; | ||
} | ||
|
||
@Data | ||
@NoArgsConstructor | ||
public static class Condition implements Serializable { | ||
private String errorThreshold; | ||
private String metric; | ||
private boolean onLeakPeriod; | ||
private String operator; | ||
private String status; | ||
private String value; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/shanhh/webhook/sonarqube/service/SonarService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.shanhh.webhook.sonarqube.service; | ||
|
||
import com.shanhh.webhook.slack.beans.SlackPayload; | ||
import com.shanhh.webhook.sonarqube.beans.SonarPayload; | ||
|
||
/** | ||
* @author dan | ||
* @since 2017-03-21 17:37 | ||
*/ | ||
public interface SonarService { | ||
SlackPayload exec(SonarPayload payload); | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/shanhh/webhook/sonarqube/service/SonarServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.shanhh.webhook.sonarqube.service; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.shanhh.webhook.slack.beans.SlackAttaColor; | ||
import com.shanhh.webhook.slack.beans.SlackAttaPayload; | ||
import com.shanhh.webhook.slack.beans.SlackPayload; | ||
import com.shanhh.webhook.sonarqube.beans.SonarPayload; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* @author dan | ||
* @since 2017-03-21 17:38 | ||
*/ | ||
@Slf4j | ||
@Service | ||
public class SonarServiceImpl implements SonarService { | ||
|
||
@Override | ||
public SlackPayload exec(SonarPayload payload) { | ||
if (checkSkip(payload)) { | ||
return null; | ||
} | ||
|
||
SlackAttaPayload slack = new SlackAttaPayload(); | ||
|
||
List<SlackAttaPayload.Attachment> attachments = Lists.newLinkedList(); | ||
if (payload.getQualityGate() != null && !CollectionUtils.isEmpty(payload.getQualityGate().getConditions())) { | ||
payload.getQualityGate().getConditions().stream() | ||
// .filter(condition -> !"OK".equals(condition.getStatus())) | ||
.forEach(condition -> { | ||
SlackAttaPayload.Attachment attachment = SlackAttaPayload.Attachment.builder() | ||
.color(SlackAttaColor.danger.name()) | ||
.title(condition.getMetric()) | ||
.text(String.format("*%s*: `%s` %s `%s`", condition.getStatus(), condition.getValue(), condition.getOperator(), condition.getErrorThreshold())) | ||
.mrkdwnIn(Arrays.asList("text")) | ||
.build(); | ||
attachments.add(attachment); | ||
}); | ||
} | ||
while (attachments.size() > 30) { | ||
attachments.remove(attachments.size() - 1); | ||
} | ||
|
||
slack.setText(String.format("%s: `%s`", payload.getProject().getName(), payload.getStatus())); | ||
slack.setAttachments(attachments); | ||
slack.setMrkdwnIn(Arrays.asList("text")); | ||
return slack; | ||
} | ||
|
||
private boolean checkSkip(SonarPayload payload) { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ slack: | |
daocloud: | ||
microbadger: | ||
docker: | ||
sonar: |
Oops, something went wrong.