Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-30888] Added Job DSL support
- Loading branch information
1 parent
f8d7db8
commit 4da4c1a
Showing
3 changed files
with
135 additions
and
0 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
78 changes: 78 additions & 0 deletions
78
src/main/java/org/jenkinsci/plugins/codesonar/CodeSonarJobDslContext.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,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); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/org/jenkinsci/plugins/codesonar/CodeSonarJobDslExtension.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,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); | ||
} | ||
} |