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 withChecks step #49

Merged
merged 11 commits into from
Dec 20, 2020
17 changes: 17 additions & 0 deletions src/main/java/io/jenkins/plugins/checks/steps/ChecksInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.jenkins.plugins.checks.steps;

import java.io.Serializable;

public class ChecksInfo implements Serializable {
private static final long serialVersionUID = 1L;

private String name;

public ChecksInfo(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
73 changes: 73 additions & 0 deletions src/main/java/io/jenkins/plugins/checks/steps/WithChecksStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.jenkins.plugins.checks.steps;

import hudson.Extension;
import hudson.model.Run;
import hudson.model.TaskListener;
import org.jenkinsci.plugins.workflow.steps.*;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.Serializable;
import java.util.*;

public class WithChecksStep extends Step implements Serializable {
private static final long serialVersionUID = 1L;

private String name;

/**
* Constructor used for pipeline by Stapler.
*/
@DataBoundConstructor
public WithChecksStep(final String name) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it can take more than one, normally you would use a databoundsetter for any optional parameters

Then the syntax would be:

withChecks('Tests') {
...
}
withChecks(name: 'Tests', myotherparam: 'hello') {
...
}

if there's only one mandatory field in constructor then you can omit the parameter name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should we make all params optional in case that in the future, some users may only want to inject a certain field like conclusion instead of name?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we do that then the user will have to specify the name in a more verbose way,

withChecks(name: 'hello') rather than withChecks('hello')

Copy link
Contributor Author

@XiongKezhi XiongKezhi Dec 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specify the name wouldn't be annoying for users IMO but make it mandatory will be an issue for future changes?

Also, it seems the snippet generator will always specify name

@mrginglymus What do you think? Do you think there will be situations where you do not want to specify the name but the status (e.g. publish several checks in the same status (say in progress) but with different names in the closure)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be fine to be mandatory

super();

this.name = name;
}

@Override
public StepExecution start(final StepContext stepContext) {
return new WithChecksStep.WithChecksStepExecution(stepContext, this);
}

@Extension
public static class WithChecksStepDescriptor extends StepDescriptor {
@Override
public String getFunctionName() {
return "withChecks";
}

@Override
public Set<? extends Class<?>> getRequiredContext() {
return Collections.unmodifiableSet(new HashSet<>(Arrays.asList(Run.class, TaskListener.class)));
}

@Override
public boolean takesImplicitBlockArgument() {
return true;
}
}

static class WithChecksStepExecution extends AbstractStepExecutionImpl {
private WithChecksStep step;

WithChecksStepExecution(final StepContext context, final WithChecksStep step) {
super(context);
this.step = step;
}

@Override
public boolean start() {
StepContext context = getContext();
context.newBodyInvoker()
.withContext(new ChecksInfo(step.name))
.withCallback(BodyExecutionCallback.wrap(context))
.start();
return false;
}

@Override
public void stop(Throwable cause) {
getContext().onFailure(cause);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Provides default Findbugs annotations.
*/
@DefaultAnnotation(NonNull.class)
package io.jenkins.plugins.checks.steps;

import edu.umd.cs.findbugs.annotations.DefaultAnnotation;
import edu.umd.cs.findbugs.annotations.NonNull;