Skip to content

Commit

Permalink
Merge #218 + #219 (#220)
Browse files Browse the repository at this point in the history
* fixes #219 Add Hidden Field Parameter
* fixes #218 "Show log" on Job without Build leads to error
  • Loading branch information
MCMicS authored Jun 3, 2020
1 parent cf57b7e commit 871ebe2
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 99 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Version 0.13.4
* #219 How to run a jenkins job with hidden fields?
* #218 "Show log" on Job without Build leads to error

Version 0.13.3
* add GoTo Allure report
* #213 NodeParameterDefinition is unsupported
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Jenkins Plugin 0.13.3 for Jetbrains products
# Jenkins Plugin 0.13.4 for Jetbrains products
[![Build Status](https://travis-ci.com/MCMicS/jenkins-control-plugin.svg?branch=master)](https://travis-ci.com/MCMicS/jenkins-control-plugin)
[![Gitter](https://badges.gitter.im/jenkins-control-plugin/community.svg)](https://gitter.im/jenkins-control-plugin/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=MCMicS_jenkins-control-plugin&metric=alert_status)](https://sonarcloud.io/dashboard?id=MCMicS_jenkins-control-plugin)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'org.codinjutsu'
version '0.13.3-2020.1'
version '0.13.4-2020.1'
apply plugin: 'org.jetbrains.intellij'
apply plugin: 'java'
apply plugin: 'idea'
Expand Down
9 changes: 7 additions & 2 deletions includes/pluginChanges.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<h3>0.13.4</h3>
<ul>
<li>#219 How to run a jenkins job with hidden fields?</li>
<li>#218 "Show log" on Job without Build leads to error</li>
</ul>
<h3>0.13.3</h3>
<ul>
<li>add GoTo Allure report</li>
Expand Down Expand Up @@ -52,5 +57,5 @@ <h3>0.11.0</h3>
<li>Announcement: Deprecate Jenkins 1 Support</li>
</ul>

<a href="https://github.com/MCMicS/jenkins-control-plugin/blob/v0.13.3/README.md">Read Me</a><br/>
<a href="https://github.com/MCMicS/jenkins-control-plugin/blob/v0.13.3/CHANGELOG.txt">Changelog</a>
<a href="https://github.com/MCMicS/jenkins-control-plugin/blob/v0.13.4/README.md">Read Me</a><br/>
<a href="https://github.com/MCMicS/jenkins-control-plugin/blob/v0.13.4/CHANGELOG.txt">Changelog</a>
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ public Build loadBuild(Build build) {
@Override
public String loadConsoleTextFor(Job job) {
try {
return getJob(job).getLastCompletedBuild().details().getConsoleOutputText();
final com.offbytwo.jenkins.model.Build lastCompletedBuild = getJob(job).getLastCompletedBuild();
return lastCompletedBuild.equals(com.offbytwo.jenkins.model.Build.BUILD_HAS_NEVER_RUN) ? null :
lastCompletedBuild.details().getConsoleOutputText();
} catch (IOException e) {
logger.warn("cannot load log for " + job.getName());
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,16 @@

public class BuildParamDialog extends JDialog {
private static final Logger logger = Logger.getLogger(BuildParamDialog.class);
/**
* com.intellij.icons.AllIcons.General#NotificationError
* com.intellij.icons.AllIcons.RunConfigurations#ToolbarError
* com.intellij.icons.AllIcons.General#BalloonError
* AllIcons.RunConfigurations.ToolbarError
*/
private static final Icon WARNING_ICON = AllIcons.General.BalloonWarning;
private final Job job;
private final JenkinsAppSettings configuration;
private final RequestManager requestManager;
private final RunBuildCallback runBuildCallback;
private final Collection<JLabel> labels = new LinkedHashSet<>();
private final Collection<JobParameterComponent> inputFields = new LinkedHashSet<>();
private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;
private JPanel contentPanel;

// UNSUPPORTED PARAMETERS
// FileParameterDefinition
// TextParameterDefinition
// RunParameterDefinition
// public static final JobParameterType ListSubversionTagsParameterDefinition = new JobParameterType("ListSubversionTagsParameterDefinition",
// "hudson.scm.listtagsparameter.ListSubversionTagsParameterDefinition");

BuildParamDialog(Job job, JenkinsAppSettings configuration, RequestManager requestManager, RunBuildCallback runBuildCallback) {
this.job = job;
this.configuration = configuration;
Expand All @@ -88,15 +73,14 @@ public class BuildParamDialog extends JDialog {

public static void showDialog(final Job job, final JenkinsAppSettings configuration, final RequestManager requestManager,
final RunBuildCallback runBuildCallback) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
BuildParamDialog dialog = new BuildParamDialog(job, configuration, requestManager, runBuildCallback);
dialog.setLocationRelativeTo(null);
dialog.setSize(dialog.getPreferredSize());
dialog.setMaximumSize(new Dimension(300, 200));
dialog.pack();
dialog.setVisible(true);
}
SwingUtilities.invokeLater(() -> {
BuildParamDialog dialog = new BuildParamDialog(job, configuration, requestManager, runBuildCallback);
dialog.setLocationRelativeTo(null);
dialog.setSize(dialog.getPreferredSize());
//dialog.setMinimumSize(new Dimension(300, 200));
dialog.setMaximumSize(new Dimension(800, 600));
dialog.pack();
dialog.setVisible(true);
});
}

Expand Down Expand Up @@ -128,31 +112,32 @@ private void addParameterInputs() {
contentPanel.setLayout(new SpringLayout());
List<JobParameter> parameters = job.getParameters();

final AtomicInteger rows = new AtomicInteger(parameters.size());
final AtomicInteger rows = new AtomicInteger(0);
for (JobParameter jobParameter : parameters) {
final JobParameterRenderer jobParameterRenderer = JobParameterRenderer.findRenderer(jobParameter)
.orElseGet(ErrorRenderer::new);
//final JobParameterComponent jobParameterComponent = createInputField(jobParameter);
final JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter);
jobParameterComponent.getViewElement().setName(jobParameter.getName());

final JLabel label = jobParameterRenderer.createLabel(jobParameter)
.map(setJLabelStyles(jobParameterComponent))
.map(BuildParamDialog::appendColonIfMissing)
.orElseGet(JLabel::new);
contentPanel.add(label);
contentPanel.add(jobParameterComponent.getViewElement());

final String description = jobParameter.getDescription();
if (StringUtils.isNotEmpty(description)) {
JLabel placeHolder = new JLabel("", SwingConstants.CENTER);
contentPanel.add(placeHolder);
contentPanel.add(new JLabel(description));
if (jobParameterComponent.isVisible()) {
rows.incrementAndGet();
}
jobParameterComponent.getViewElement().setName(jobParameter.getName());

final JLabel label = jobParameterRenderer.createLabel(jobParameter)
.map(setJLabelStyles(jobParameterComponent))
.map(BuildParamDialog::appendColonIfMissing)
.orElseGet(JLabel::new);
contentPanel.add(label);
contentPanel.add(jobParameterComponent.getViewElement());

final String description = jobParameter.getDescription();
if (StringUtils.isNotEmpty(description)) {
JLabel placeHolder = new JLabel("", SwingConstants.CENTER);
contentPanel.add(placeHolder);
contentPanel.add(new JLabel(description));
rows.incrementAndGet();
}

labels.add(label);
inputFields.add(jobParameterComponent);
inputFields.add(jobParameterComponent);
}
}

final int columns = 2;
Expand All @@ -169,34 +154,24 @@ private void addParameterInputs() {
}

private boolean hasError() {
return inputFields.stream().map(JobParameterComponent::hasError).anyMatch(Boolean.TRUE::equals);
return inputFields.stream().anyMatch(JobParameterComponent::hasError);
}

private void registerListeners() {
buttonOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onOK();
}
});
buttonOK.addActionListener(e -> onOK());

buttonCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onCancel();
}
});
buttonCancel.addActionListener(e -> onCancel());

setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
onCancel();
}
});

contentPane.registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onCancel();
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
contentPane.registerKeyboardAction(e -> onCancel(),
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
}

private void onOK() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ public void run(@NotNull ProgressIndicator progressIndicator) {

@Override
public void update(AnActionEvent event) {
Job selectedJob = browserPanel.getSelectedJob();
event.getPresentation().setVisible(selectedJob != null && selectedJob.isBuildable());
final Job selectedJob = browserPanel.getSelectedJob();
final boolean canShowLogForLastBuild = selectedJob != null
&& selectedJob.isBuildable()
&& selectedJob.getLastBuild() != null
&& !selectedJob.isInQueue();
event.getPresentation().setVisible(canShowLogForLastBuild);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.codinjutsu.tools.jenkins.view.parameter;

import org.codinjutsu.tools.jenkins.model.JobParameter;
import org.codinjutsu.tools.jenkins.model.JobParameterType;
import org.codinjutsu.tools.jenkins.view.extension.JobParameterRenderer;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class HiddenParameterRenderer implements JobParameterRenderer {

static final JobParameterType TYPE = new JobParameterType("WHideParameterDefinition",
"com.wangyin.parameter.WHideParameterDefinition");

@NotNull
@Override
public JobParameterComponent render(@NotNull JobParameter jobParameter) {
return new JobParameterComponent(jobParameter, new JLabel(), false);
}

@Override
public boolean isForJobParameter(@NotNull JobParameter jobParameter) {
return TYPE.equals(jobParameter.getJobParameterType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

@Getter
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
Expand All @@ -34,30 +33,52 @@ public class JobParameterComponent {
@NotNull
private BooleanSupplier validator;

private boolean isVisible;

public <C extends JComponent> JobParameterComponent(@NotNull JobParameter jobParameter, @NotNull C viewElement) {
this(jobParameter, viewElement, () -> false);
}

public <C extends JComponent> JobParameterComponent(@NotNull JobParameter jobParameter, @NotNull C viewElement,
boolean isVisible) {
this(jobParameter, viewElement, () -> false, isVisible);
}

public <C extends JComponent> JobParameterComponent(@NotNull JobParameter jobParameter, @NotNull C viewElement,
@NotNull BooleanSupplier validator) {
this(jobParameter, viewElement, component -> null, validator);
}

public <C extends JComponent> JobParameterComponent(@NotNull JobParameter jobParameter, @NotNull C viewElement,
@NotNull BooleanSupplier validator,
boolean isVisible) {
this(jobParameter, viewElement, component -> null, validator, isVisible);
}

public <C extends JComponent> JobParameterComponent(@NotNull JobParameter jobParameter,
@NotNull C viewElement,
@NotNull Function<C, String> valueProvider) {
this(jobParameter, viewElement, valueProvider, () -> false);
}

@SuppressWarnings("unchecked")
public <C extends JComponent> JobParameterComponent(@NotNull JobParameter jobParameter,
@NotNull C viewElement,
@NotNull Function<C, String> valueProvider,
@NotNull BooleanSupplier validator) {
this(jobParameter, viewElement, valueProvider, validator, true);
}

@SuppressWarnings("unchecked")
public <C extends JComponent> JobParameterComponent(@NotNull JobParameter jobParameter,
@NotNull C viewElement,
@NotNull Function<C, String> valueProvider,
@NotNull BooleanSupplier validator,
boolean isVisible) {
this.jobParameter = jobParameter;
this.viewElement = viewElement;
this.valueProvider = (Function<JComponent, String>) valueProvider;
this.validator = validator;
this.isVisible = isVisible;
}

public void ifHasValue(Consumer<String> valueConsumer) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/JobParameterRenderer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
id="BUILT_IN_RENDERER" />
<buildParameterRenderer implementation="org.codinjutsu.tools.jenkins.view.parameter.SeparatorRenderer" />
<buildParameterRenderer implementation="org.codinjutsu.tools.jenkins.view.parameter.NodeParameterRenderer" />
<buildParameterRenderer implementation="org.codinjutsu.tools.jenkins.view.parameter.HiddenParameterRenderer" />
</extensions>
</idea-plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
import org.codinjutsu.tools.jenkins.model.JobParameter;
import org.codinjutsu.tools.jenkins.model.JobParameterType;
import org.codinjutsu.tools.jenkins.view.extension.JobParameterRenderers;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import org.mockito.Mockito;

import javax.swing.*;

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

public class BuiltInJobParameterRendererTest {
public class BuiltInJobParameterRendererTest implements JobParameterTest {

private final BuiltInJobParameterRenderer jobParameterRenderer = new BuiltInJobParameterRenderer();

Expand Down Expand Up @@ -69,10 +67,4 @@ public void render() {
jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.TextParameterDefinition));
assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JTextArea.class);
}

@NotNull
private JobParameter createJobParameter(JobParameterType jobParameterType) {
return JobParameter.builder().jobParameterType(jobParameterType)
.name("Test").defaultValue("default").description("Test Parameter").build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.codinjutsu.tools.jenkins.view.parameter;

import org.codinjutsu.tools.jenkins.model.BuildInJobParameter;
import org.codinjutsu.tools.jenkins.model.JobParameter;
import org.codinjutsu.tools.jenkins.model.JobParameterType;
import org.junit.Test;

import javax.swing.*;

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

public class HiddenParameterRendererTest implements JobParameterTest {

private final HiddenParameterRenderer jobParameterRenderer = new HiddenParameterRenderer();
private final JobParameterType hiddenParameter = new JobParameterType("WHideParameterDefinition",
"com.wangyin.parameter.WHideParameterDefinition");

@Test
public void render() {
final JobParameter jobParameter = createJobParameter(hiddenParameter);
JobParameterComponent jobParameterComponent = jobParameterRenderer.render(jobParameter);
assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JLabel.class);
assertThat(jobParameterComponent.isVisible()).isFalse();

jobParameterComponent = jobParameterRenderer.render(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition));
assertThat(jobParameterComponent.getViewElement()).isInstanceOf(JLabel.class);
assertThat(jobParameterComponent.isVisible()).isFalse();
}

@Test
public void isForJobParameter() {
assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(hiddenParameter)))
.isTrue();
assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(new JobParameterType("WHideParameterDefinition",
"com.wangyin.parameter.invalid.WHideParameterDefinition"))))
.isFalse();
assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(BuildInJobParameter.ChoiceParameterDefinition)))
.isFalse();
}

}
Loading

0 comments on commit 871ebe2

Please sign in to comment.