Skip to content

Commit

Permalink
fixes #4 , fixes #5. clarify serviceKey
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderlz committed Jan 3, 2016
1 parent fa9b09a commit d771849
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 26 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
<artifactId>pagerduty-incidents</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava</artifactId>
<version>1.0.12</version>
</dependency>
</dependencies>

</project>
75 changes: 55 additions & 20 deletions src/main/java/org/jenkinsci/plugins/pagerduty/PagerDutyTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,72 @@
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PagerDutyTrigger extends Notifier {

public final String apiKey;
public final boolean triggerOnSuccess;
public final String incidentKey;
public final String description;
public final Integer numPreviousBuildsToProbe;
private static PagerDuty pagerDuty;
public String serviceKey;
public boolean triggerOnSuccess;
public boolean triggerOnFailure;
public boolean triggerOnUnstable;
public boolean triggerOnAborted;
public boolean triggerOnNotBuilt;
public String incidentKey;
public String description;
public Integer numPreviousBuildsToProbe;
private PagerDuty pagerDuty = null;
private LinkedList<Result> resultProbe;
public static final String DEFAULT_DESCRIPTION_STRING = "I was too lazy to create a description, but trust me it's important!";

@DataBoundConstructor
public PagerDutyTrigger(String apiKey, boolean triggerOnSuccess, String incidentKey, String description,
public PagerDutyTrigger(String serviceKey, boolean triggerOnSuccess, boolean triggerOnFailure, boolean triggerOnAborted,
boolean triggerOnUnstable, boolean triggerOnNotBuilt, String incidentKey, String description,
Integer numPreviousBuildsToProbe) {
this.apiKey = apiKey;
this.serviceKey = serviceKey;
this.triggerOnSuccess = triggerOnSuccess;
this.triggerOnFailure = triggerOnFailure;
this.triggerOnUnstable = triggerOnUnstable;
this.triggerOnAborted = triggerOnAborted;
this.triggerOnNotBuilt = triggerOnNotBuilt;
this.incidentKey = incidentKey;
this.description = description;
this.numPreviousBuildsToProbe = (numPreviousBuildsToProbe != null && numPreviousBuildsToProbe > 0) ? numPreviousBuildsToProbe : 1;
pagerDuty = PagerDuty.create(apiKey);
if(this.serviceKey != null && this.serviceKey.trim().length() > 0)
this.pagerDuty = PagerDuty.create(serviceKey);
this.resultProbe = generateResultProbe();

}

private LinkedList<Result> generateResultProbe() {
LinkedList<Result> res = new LinkedList<Result>();
if(triggerOnSuccess)
res.add(Result.SUCCESS);
if(triggerOnFailure)
res.add(Result.FAILURE);
if(triggerOnUnstable)
res.add(Result.UNSTABLE);
if(triggerOnAborted)
res.add(Result.ABORTED);
if(triggerOnNotBuilt)
res.add(Result.NOT_BUILT);
return res;
}

/*
* method to fetch and replace possible Environment Variables from job parameteres
*/
private String replaceEnvVars(String str, EnvVars envv) {
StringBuffer sb = new StringBuffer();
if (str == null || str.trim().length() < 1)
str = DEFAULT_DESCRIPTION_STRING;
Matcher m = Pattern.compile("\\$\\{.*\\}|\\$[^\\-\\*\\.#!, ]*")
.matcher(str);
while (m.find()) {
String v = m.group();
v = v.replaceAll("\\$","").replaceAll("\\{","").replaceAll("\\}","");
v = v.replaceAll("\\$", "").replaceAll("\\{", "").replaceAll("\\}", "");
m.appendReplacement(sb, envv.get(v, ""));
}
m.appendTail(sb);
Expand All @@ -64,16 +96,16 @@ private String replaceEnvVars(String str, EnvVars envv) {
/*
* method to verify X previous builds finished with the desired result
*/
private boolean validWithPreviousResults(AbstractBuild<?, ?> build, Result desiredResult, int depth) {
int i=0;
while (i<depth && build != null) {
if (build.getResult() != desiredResult){
private boolean validWithPreviousResults(AbstractBuild<?, ?> build, List<Result> desiredResultList, int depth) {
int i = 0;
while (i < depth && build != null) {
if (!desiredResultList.contains(build.getResult())) {
break;
}
i++;
build = build.getPreviousBuild();
}
if (i==depth) {
if (i == depth) {
return true;
}
return false;
Expand All @@ -98,9 +130,12 @@ public BuildStepMonitor getRequiredMonitorService() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {
if (pagerDuty == null) {
listener.getLogger().println("Unbale to activate pagerduty module, check configuration!");
return false;
}
EnvVars env = build.getEnvironment(listener);
if ((validWithPreviousResults(build, Result.SUCCESS, numPreviousBuildsToProbe) && triggerOnSuccess) ||
(validWithPreviousResults(build, Result.FAILURE, numPreviousBuildsToProbe) && !triggerOnSuccess)) {
if (validWithPreviousResults(build, resultProbe, numPreviousBuildsToProbe)) {
listener.getLogger().println("Triggering PagerDuty Notification");
triggerPagerDuty(listener, env);
}
Expand All @@ -109,9 +144,9 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,

void triggerPagerDuty(BuildListener listener, EnvVars env) {
String descr = replaceEnvVars(this.description, env);
String apiK = replaceEnvVars(this.apiKey, env);
String serviceK = replaceEnvVars(this.serviceKey, env);
String incK = replaceEnvVars(this.incidentKey, env);
listener.getLogger().printf("Triggering pagerDuty with apiKey %s%n", apiK);
listener.getLogger().printf("Triggering pagerDuty with serviceKey %s%n", serviceK);

try {
Trigger trigger;
Expand All @@ -127,7 +162,7 @@ void triggerPagerDuty(BuildListener listener, EnvVars env) {
listener.getLogger().printf("PagerDuty Notification Result: %s%n", result.status());
} catch (Exception e) {
e.printStackTrace(listener.error("Tried to trigger PD with apiKey = [%s]",
apiK));
serviceK));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="Api Key" field="apiKey">
<f:password field="apiKey" />
<f:entry title="Service Integration Key" field="serviceKey">
<f:password field="serviceKey" />
</f:entry>
<f:entry title="incidentKey" field="incidentKey">
<f:textbox field="incidentKey"/>
Expand All @@ -15,4 +15,16 @@
<f:entry title="Trigger Incident on SUCCESS job result" field="triggerOnSuccess">
<f:checkbox name="triggerOnSuccess"/>
</f:entry>
<f:entry title="Trigger Incident on FAILURE job result" field="triggerOnFailure">
<f:checkbox name="triggerOnFailure"/>
</f:entry>
<f:entry title="Trigger Incident on UNSTABLE job result" field="triggerOnUnstable">
<f:checkbox name="triggerOnUnstable"/>
</f:entry>
<f:entry title="Trigger Incident on ABORTED job result" field="triggerOnAborted">
<f:checkbox name="triggerOnAborted"/>
</f:entry>
<f:entry title="Trigger Incident on NOT_BUILT job result" field="triggerOnNotBuilt">
<f:checkbox name="triggerOnNotBuilt"/>
</f:entry>
</j:jelly>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
The service integration Key of your PagerDuty account.
The service key will allow you to trigger incidents on a specific service.
(Don't mix with the API key, it serves different purposes)
</div>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div>
Trigger the PagerDuty Incident if job ends SUCCESSFULY. (default - false)
Trigger the PagerDuty Incident if job ends SUCCESSFULY.
</div>

0 comments on commit d771849

Please sign in to comment.