Skip to content

Commit

Permalink
Add ChangeBuildDescriptionActionDescriptor (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
damianszczepanik authored Mar 14, 2023
1 parent e5d2286 commit dd70567
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 4 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/.idea
/docs
**/*.iml
target/
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package pl.damianszczepanik.jenkins.buildhistorymanager.descriptors.actions;

import hudson.Extension;
import hudson.model.Descriptor;
import org.jenkinsci.Symbol;
import pl.damianszczepanik.jenkins.buildhistorymanager.model.actions.Action;
import pl.damianszczepanik.jenkins.buildhistorymanager.model.actions.ChangeBuildDescriptionAction;

/**
* Descriptor implementation needed to render UI for {@link ChangeBuildDescriptionAction}.
*
* @author Damian Szczepanik (damianszczepanik@github)
*/
@Extension
@Symbol("ChangeBuildDescription")
public class ChangeBuildDescriptionActionDescriptor extends Descriptor<Action> {

public ChangeBuildDescriptionActionDescriptor() {
super(ChangeBuildDescriptionAction.class);
}

@Override
public String getDisplayName() {
return "Change build description";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public ChangeBuildDescriptionAction() {
@Override
public void perform(Run<?, ?> run) throws IOException, InterruptedException {
// null-safe concatenation
run.setDescription(PRE_DESCRIPTION + StringUtils.defaultString(run.getDescription()));
String description = StringUtils.defaultString(run.getDescription());

// do not append if already updated
if (!description.startsWith(PRE_DESCRIPTION)) {
run.setDescription(PRE_DESCRIPTION + description);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p><b>Description</b></p>
<p>Updates build description.</p>

<p><b>Use cases</b></p>
<p>This action is helpful for testing and debugging. It does not change or delete the build what might be hard to
rollback. Instead, it allows to test conditions before final action is used.</p>

<p>The result of this action is a string <code>[build-history-manager]</code> which is prepend to job description.</p>
<p>Use this action as long as the final result of condition is not examined.
This is helpful also for users who do not have access to Jenkins logs.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package pl.damianszczepanik.jenkins.buildhistorymanager.descriptors.actions;

import static org.assertj.core.api.Assertions.assertThat;

import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import org.jenkinsci.plugins.structs.SymbolLookup;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

/**
* @author Damian Szczepanik (damianszczepanik@github)
*/
public class ChangeBuildDescriptionActionDescriptorTest {

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void getDisplayName_ReturnsDescriptorName() {

// given
Descriptor descriptor = SymbolLookup.get().findDescriptor(AbstractDescribableImpl.class, "ChangeBuildDescription");

// when
String displayName = descriptor.getDisplayName();

// then
assertThat(displayName).isEqualTo("Change build description");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,39 @@
*/
public class ChangeBuildDescriptionActionTest {

private static final String PRE_DESCRIPTION = "[build-history-manager]\n";

@Test
public void perform_DescriptionUpdate() throws IOException, InterruptedException {
public void perform__UpdatedAgain() throws IOException, InterruptedException {

// given
ChangeBuildDescriptionAction action = new ChangeBuildDescriptionAction();
RunStub run = new RunStub();

final String oldDescription = "Yellow Duck";
run.setDescription(oldDescription);

// when
action.perform(run);

// then
assertThat(run.getDescription()).startsWith(PRE_DESCRIPTION + oldDescription);
}

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

// given
ChangeBuildDescriptionAction action = new ChangeBuildDescriptionAction();
RunStub run = new RunStub();

final String oldDescription = "Yellow Duck";
run.setDescription(PRE_DESCRIPTION + oldDescription);

// when
action.perform(run);

// then
assertThat(run.getDescription()).startsWith("[build-history-manager]");
assertThat(run.getDescription()).startsWith(PRE_DESCRIPTION + oldDescription);
}
}

0 comments on commit dd70567

Please sign in to comment.