Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ChangeBuildDescriptionActionDescriptor #79

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}