Skip to content
Permalink
Browse files

[JENKINS-30888] Added Job DSL support

  • Loading branch information...
praqma-thi authored and MadsNielsen committed Sep 9, 2015
1 parent f8d7db8 commit 4da4c1a7fac958cfe55527633ed7593fd11a694c
@@ -453,6 +453,12 @@
</reporting>

<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>job-dsl</artifactId>
<version>1.37</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@@ -0,0 +1,78 @@
package org.jenkinsci.plugins.codesonar;

import hudson.model.Result;
import java.util.ArrayList;
import java.util.List;
import javaposse.jobdsl.dsl.Context;
import org.jenkinsci.plugins.codesonar.conditions.Condition;
import org.jenkinsci.plugins.codesonar.conditions.NewWarningsIncreasedByPercentageCondition;
import org.jenkinsci.plugins.codesonar.conditions.PercentageOfWariningsIncreasedInCasesBellowCertainRankCondition;
import org.jenkinsci.plugins.codesonar.conditions.ProcedureCyclomaticComplexityExceededCondition;
import org.jenkinsci.plugins.codesonar.conditions.RedAlertLimitCondition;
import org.jenkinsci.plugins.codesonar.conditions.WarningCountIncreasedByPercentageCondition;
import org.jenkinsci.plugins.codesonar.conditions.YellowAlertLimitCondition;

class CodeSonarJobDslContext implements Context {

List<Condition> conditions = new ArrayList<>();

public void cyclomaticComplexity(int max, boolean fail) {
ProcedureCyclomaticComplexityExceededCondition condition = new ProcedureCyclomaticComplexityExceededCondition(max);
if (fail) {
condition.setWarrantedResult(Result.FAILURE.toString());
} else {
condition.setWarrantedResult(Result.UNSTABLE.toString());
}
conditions.add(condition);
}

public void redAlert(int max, boolean fail) {
RedAlertLimitCondition condition = new RedAlertLimitCondition(max);
if (fail) {
condition.setWarrantedResult(Result.FAILURE.toString());
} else {
condition.setWarrantedResult(Result.UNSTABLE.toString());
}
conditions.add(condition);
}

public void yellowAlert(int max, boolean fail) {
YellowAlertLimitCondition condition = new YellowAlertLimitCondition(max);
if (fail) {
condition.setWarrantedResult(Result.FAILURE.toString());
} else {
condition.setWarrantedResult(Result.UNSTABLE.toString());
}
conditions.add(condition);
}

public void newWarningCountIncrease(float percentage, boolean fail) {
NewWarningsIncreasedByPercentageCondition condition = new NewWarningsIncreasedByPercentageCondition(percentage);
if (fail) {
condition.setWarrantedResult(Result.FAILURE.toString());
} else {
condition.setWarrantedResult(Result.UNSTABLE.toString());
}
conditions.add(condition);
}

public void overallWarningCountIncrease(float percentage, boolean fail) {
WarningCountIncreasedByPercentageCondition condition = new WarningCountIncreasedByPercentageCondition(percentage);
if (fail) {
condition.setWarrantedResult(Result.FAILURE.toString());
} else {
condition.setWarrantedResult(Result.UNSTABLE.toString());
}
conditions.add(condition);
}

public void rankedWarningCountIncrease(int rank, float percentage, boolean fail) {
PercentageOfWariningsIncreasedInCasesBellowCertainRankCondition condition = new PercentageOfWariningsIncreasedInCasesBellowCertainRankCondition(rank, percentage);
if (fail) {
condition.setWarrantedResult(Result.FAILURE.toString());
} else {
condition.setWarrantedResult(Result.UNSTABLE.toString());
}
conditions.add(condition);
}
}
@@ -0,0 +1,51 @@
package org.jenkinsci.plugins.codesonar;

import hudson.Extension;
import javaposse.jobdsl.dsl.RequiresPlugin;
import javaposse.jobdsl.dsl.helpers.publisher.PublisherContext;
import javaposse.jobdsl.plugin.ContextExtensionPoint;
import javaposse.jobdsl.plugin.DslExtensionMethod;

/*
```
job{
publishers{
codeSonar(String hubAddress, String projectName){
cyclomaticComplexity(int maxComplexity, boolean markAsFailed)
redAlert(int maxAlerts, boolean markAsFailed)
yellowAlert(int maxAlerts, boolean markAsFailed)
newWarningCountIncrease(float percentage, boolean markAsFailed)
overallWarningCountIncrease(float percentage, boolean markAsFailed)
rankedWarningCountIncrease(int minRank, float percentage, boolean markAsFailed)
}
}
}
```
For example:
```
job('myProject_GEN'){
publishers{
codeSonar('hub','proj'){
cyclomaticComplexity(20, false)
redAlert(3, true)
yellowAlert(10, false)
newWarningCountIncrease(5, true)
overallWarningCountIncrease(5, false)
rankedWarningCountIncrease(30, 5, true)
}
}
}
```
*/

@Extension(optional = true)
public class CodeSonarJobDslExtension extends ContextExtensionPoint {
@RequiresPlugin(id = "codesonar", minimumVersion = "1.0.0")
@DslExtensionMethod(context = PublisherContext.class)
public Object codeSonar(String hubAddress, String projectName, Runnable closure){
CodeSonarJobDslContext context = new CodeSonarJobDslContext();
executeInContext(closure, context);

return new CodeSonarPublisher(context.conditions, hubAddress, projectName);
}
}

0 comments on commit 4da4c1a

Please sign in to comment.
You can’t perform that action at this time.