Skip to content

Commit

Permalink
Improve log by adding the jobname as context (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
theKBro authored Jan 31, 2021
1 parent 71e42eb commit 251ca7b
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,26 @@ public List<Rule> getRules() {
*/
@Override
public synchronized void perform(Job<?, ?> job) throws IOException, InterruptedException {
String uniquePerformName = job.getFullName();
LOG.info(uniquePerformName + ": start evaluating build history");

// reset counters of matched builds
for (Rule rule : rules) {
rule.initialize();
rule.initialize(uniquePerformName);
}

Run<?, ?> run = job.getLastCompletedBuild();
// for each completed build...
while (run != null) {
LOG.info("Processing build #" + run.getNumber());
LOG.info(uniquePerformName + ": Processing build #" + run.getNumber());
if (run.isKeepLog()) {
LOG.info("build #" + run.getNumber() + " is marked as keep forever -> skipping");
LOG.info(uniquePerformName + ": build #" + run.getNumber() + " is marked as keep forever -> skipping");
} else {
for (int i = 0; i < rules.size(); i++) {
Rule rule = rules.get(i);
LOG.info("Processing rule no " + (i + 1));
LOG.info(uniquePerformName + ": Processing rule no " + (i + 1));
if (rule.validateConditions(run)) {
LOG.info("Processing actions for rule no " + (i + 1));
LOG.info(uniquePerformName + ": Processing actions for rule no " + (i + 1));
rule.performActions(run);

// if other rules should not be proceed, shift to next build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class Rule extends AbstractDescribableImpl<Rule> {

private final RuleConfiguration configuration = new RuleConfiguration();

private String uniquePerformName;

private transient int matchedTimes;

@DataBoundConstructor
Expand Down Expand Up @@ -62,7 +64,8 @@ public boolean getContinueAfterMatch() {
/**
* Resets local counters and variables before processing conditions and actions.
*/
public void initialize() {
public void initialize(String uniquePerformName) {
this.uniquePerformName = uniquePerformName;
matchedTimes = 0;
}

Expand All @@ -75,17 +78,17 @@ public void initialize() {
public boolean validateConditions(Run<?, ?> run) {
// stop checking if max number of processed builds is reached
if (matchedTimes == getMatchAtMost()) {
LOG.info(String.format("Skipping rule because matched %d times", matchedTimes));
LOG.info(uniquePerformName + ": " + String.format("Skipping rule because matched %d times", matchedTimes));
return false;
}

// validateConditions condition one by one...
for (Condition condition : conditions) {
LOG.info(String.format("Processing condition '%s'", condition.getDescriptor().getDisplayName()));
LOG.info(uniquePerformName + ": " + String.format("Processing condition '%s'", condition.getDescriptor().getDisplayName()));
boolean conditionMatched = condition.matches(run, configuration);
// stop checking rest conditions when at least condition does not match
if (!conditionMatched) {
LOG.info(String.format("Condition '%s' does not match", condition.getDescriptor().getDisplayName()));
LOG.info(uniquePerformName + ": " + String.format("Condition '%s' does not match", condition.getDescriptor().getDisplayName()));
return false;
}
}
Expand All @@ -96,7 +99,7 @@ public boolean validateConditions(Run<?, ?> run) {

public void performActions(Run<?, ?> run) throws IOException, InterruptedException {
for (Action action : actions) {
LOG.info(String.format("Processing action '%s' for build #%d",
LOG.info(uniquePerformName + ": " + String.format("Processing action '%s' for build #%d",
action.getDescriptor().getDisplayName(), run.getNumber()));
action.perform(run);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
@RunWith(PowerMockRunner.class)
@PrepareForTest({
Run.class, // isKeepLog()
AbstractItem.class // getFullName()
})
public class BuildHistoryManagerTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Collections;
import java.util.List;

import hudson.model.AbstractItem;
import hudson.model.Job;
import jenkins.model.Jenkins;
import org.junit.Before;
Expand All @@ -24,7 +25,10 @@
* @author Damian Szczepanik (damianszczepanik@github)
* @see <a href="https://github.com/jenkinsci/build-history-manager-plugin/wiki/Delete-artifacts-action">documentation</a>
*/
@PrepareForTest(Jenkins.class)
@PrepareForTest({
Jenkins.class,
AbstractItem.class // getFullName()
})
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.xml.*")
public class DeleteBuildActionIT {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Calendar;
import java.util.List;

import hudson.model.AbstractItem;
import hudson.model.Job;
import jenkins.model.Jenkins;
import org.junit.Before;
Expand All @@ -25,7 +26,10 @@
* @author Damian Szczepanik (damianszczepanik@github)
* @see <a href="https://github.com/jenkinsci/build-history-manager-plugin/wiki/Build-age-range-condition">documentation</a>
*/
@PrepareForTest(Jenkins.class)
@PrepareForTest({
Jenkins.class,
AbstractItem.class // getFullName()
})
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.xml.*")
public class BuildAgeRangeConditionIT {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Arrays;
import java.util.List;

import hudson.model.AbstractItem;
import hudson.model.Job;
import jenkins.model.Jenkins;
import org.junit.Before;
Expand All @@ -24,7 +25,10 @@
* @author Damian Szczepanik (damianszczepanik@github)
* @see <a href="https://github.com/jenkinsci/build-history-manager-plugin/wiki/Build-number-range-condition">documentation</a>
*/
@PrepareForTest(Jenkins.class)
@PrepareForTest({
Jenkins.class,
AbstractItem.class // getFullName()
})
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.xml.*")
public class BuildNumberRangeConditionIT {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Collections;
import java.util.List;

import hudson.model.AbstractItem;
import hudson.model.Job;
import hudson.model.Result;
import jenkins.model.Jenkins;
Expand All @@ -26,7 +27,10 @@
* @author Damian Szczepanik (damianszczepanik@github)
* @see <a href="https://github.com/jenkinsci/build-history-manager-plugin/wiki/Build-result-condition">documentation</a>
*/
@PrepareForTest(Jenkins.class)
@PrepareForTest({
Jenkins.class,
AbstractItem.class // getFullName()
})
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.xml.*")
public class BuildResultConditionIT {
Expand Down Expand Up @@ -94,3 +98,4 @@ public void testBuildResultCondition() throws IOException, InterruptedException
run30.assertBuildWasDeleted();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static Job buildSampleJob() {

public static Job buildSampleJob(Run lastBuild) {
Job job = mock(Job.class);

when(job.getFullName()).thenReturn("sampleJob");
when(job.getLastCompletedBuild()).thenReturn(lastBuild);

return job;
Expand Down

0 comments on commit 251ca7b

Please sign in to comment.