Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1 from andnyb/additionalChannel
Additional channel configuration
  • Loading branch information
andnyb committed Jun 3, 2015
2 parents 76c10b0 + ca86709 commit eac758e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -27,6 +27,7 @@ For a failed code coverage check it looks like this:
Parameter | Usage | Examples
--------------- | -------------------------- | --------
Slack hook|This is the hook into a given channel and it is generated by the Slack incoming Webhook extension. Just paste the full URL here.|https://hooks.slack.com/services/12/34/56
Additional channel|The default channel can be overridden by entering a given channel name.|'team-spawn'
Sonar root URL|This is the root of the remote Sonar installation. The URL is the base for the metrics query and linkage to jobs.|sonar.mycompany.com:9000
Sonar job name|The name the project has in Sonar. Case sensitive||'super-awesome-service'
Branch name|The name the project has in Sonar is usually post-pended with a branch name e.g. '<job name> <branch'. Set the branch name to find the correct job. Case sensitive.|'super-awesome-service bugfixBranch'
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -11,7 +11,7 @@
</parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>sonar-slack-pusher</artifactId>
<version>1.3-SNAPSHOT</version>
<version>1.3</version>
<packaging>hpi</packaging>

<name>Sonar Slack Pusher</name>
Expand Down
Expand Up @@ -47,6 +47,7 @@ public class SonarSlackPusher extends Notifier {
private String sonarUrl;
private String jobName;
private String branchName;
private String additionalChannel;

private PrintStream logger = null;

Expand All @@ -56,12 +57,13 @@ public class SonarSlackPusher extends Notifier {
private List<Attachment> attachments = new ArrayList<Attachment>();

@DataBoundConstructor
public SonarSlackPusher(String hook, String sonarUrl, String jobName, String branchName) {
public SonarSlackPusher(String hook, String sonarUrl, String jobName, String branchName, String additionalChannel) {
this.hook = hook.trim();
String url = sonarUrl.trim();
this.sonarUrl = url.endsWith("/") ? url.substring(0, url.length()-1) : url;
this.jobName = jobName.trim();
this.branchName = branchName.trim();
this.additionalChannel = additionalChannel.trim();
}

public String getHook() {
Expand All @@ -80,6 +82,10 @@ public String getBranchName() {
return branchName;
}

public String getAdditionalChannel() {
return additionalChannel;
}

@Override
public boolean perform(AbstractBuild<?, ?> build,Launcher launcher,BuildListener listener) {
logger = listener.getLogger();
Expand Down Expand Up @@ -255,8 +261,12 @@ private void pushNotification() {
} catch (URISyntaxException use) {
logger.println("[ssp] could not create link to Sonar job with the following content'"+sonarUrl + "/dashboard/index/" + id+"'");
}
String message =
"{\"text\":\"<"+linkUrl+"|*Sonar job*>\\n"+
String message = "{";
if (additionalChannel != null) {
message += "\"channel\":\""+additionalChannel+"\",";
}
message += "\"username\":\"Sonar Slack Pusher\",";
message += "\"text\":\"<"+linkUrl+"|*Sonar job*>\\n"+
"*Job:* "+jobName;
if (branch!=null) {
message += "\\n*Branch:* "+branch;
Expand All @@ -279,7 +289,7 @@ private void pushNotification() {
try {
HttpResponse res = client.execute(post);
if (res.getStatusLine().getStatusCode() != 200) {
logger.println("[ssp] could not push to Slack... got a non 200 response.");
logger.println("[ssp] could not push to Slack... got a non 200 response. Post body: '"+message+"'");
}
} catch (IOException ioe) {
logger.println("[ssp] could not push to slack... got an exception: '"+ioe.getMessage()+"'");
Expand Down
Expand Up @@ -4,6 +4,9 @@
<f:entry title="Slack hook" field="hook">
<f:textbox />
</f:entry>
<f:entry title="Additional channel" field="additionalChannel">
<f:textbox />
</f:entry>
<f:entry title="Sonar root URL" field="sonarUrl">
<f:textbox />
</f:entry>
Expand Down
@@ -0,0 +1,4 @@
<div>
Define the channel to send the notification to if other than the default channel. Note that the channel to send to has to be configured to accept
incoming notifications.</div>
</div>
Expand Up @@ -33,7 +33,7 @@ public void testParseData() throws Exception {

@Test
public void testReplaceWithParameters() throws Exception {
SonarSlackPusher ssp = new SonarSlackPusher("", "", "http://sonar.company.org:9000", "");
SonarSlackPusher ssp = new SonarSlackPusher("", "", "http://sonar.company.org:9000", "", "");
Method replace = ssp.getClass().getDeclaredMethod("getParams", String.class);
replace.setAccessible(true);

Expand Down

0 comments on commit eac758e

Please sign in to comment.