Skip to content

Commit

Permalink
always skip builds which are marked as keep forever (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
theKBro authored Jan 30, 2021
1 parent 27f2a5c commit 71e42eb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,20 @@ public synchronized void perform(Job<?, ?> job) throws IOException, InterruptedE
// for each completed build...
while (run != null) {
LOG.info("Processing build #" + run.getNumber());
for (int i = 0; i < rules.size(); i++) {
Rule rule = rules.get(i);
LOG.info("Processing rule no " + (i + 1));
if (rule.validateConditions(run)) {
LOG.info("Processing actions for rule no " + (i + 1));
rule.performActions(run);
if (run.isKeepLog()) {
LOG.info("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));
if (rule.validateConditions(run)) {
LOG.info("Processing actions for rule no " + (i + 1));
rule.performActions(run);

// if other rules should not be proceed, shift to next build
if (!rule.getContinueAfterMatch()) {
break;
// if other rules should not be proceed, shift to next build
if (!rule.getContinueAfterMatch()) {
break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package pl.damianszczepanik.jenkins.buildhistorymanager;

import static org.assertj.core.api.Assertions.assertThat;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;

import hudson.model.AbstractItem;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import hudson.model.Job;
import hudson.model.Run;
import mockit.Deencapsulation;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import pl.damianszczepanik.jenkins.buildhistorymanager.model.ConditionBuilder;
import pl.damianszczepanik.jenkins.buildhistorymanager.model.Rule;
import pl.damianszczepanik.jenkins.buildhistorymanager.model.RuleBuilder;
Expand All @@ -19,6 +27,10 @@
/**
* @author Damian Szczepanik (damianszczepanik@github)
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({
Run.class, // isKeepLog()
})
public class BuildHistoryManagerTest {

private List<Rule> sampleRules;
Expand Down Expand Up @@ -87,6 +99,24 @@ public void perform_validatesEachRules() throws IOException, InterruptedExceptio
}
}

@Test
public void perform_OnKeptBuild_SkipsValidate() throws IOException, InterruptedException {

// given
BuildHistoryManager discarder = new BuildHistoryManager(sampleRules);
Run keptRun = mock(Run.class);
when(keptRun.isKeepLog()).thenReturn(true);
Job<?, ?> job = JobBuilder.buildSampleJob(keptRun);

// when
discarder.perform(job);

// then
for (Rule rule : sampleRules) {
assertThat(((RuleBuilder.TestRule) rule).validateConditionsTimes).isZero();
}
}

@Test
public void perform_OnNegativeCondition_ValidatesEachRules() throws IOException, InterruptedException {

Expand Down

0 comments on commit 71e42eb

Please sign in to comment.