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 GitHub scm source implementation for status checks properties #66

Merged
Merged
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<useBeta>true</useBeta>

<!-- Jenkins Plug-in Dependencies Versions -->
<checks-api.version>1.0.2</checks-api.version>
<checks-api.version>2.0.0-SNAPSHOT</checks-api.version>
Copy link
Member

Choose a reason for hiding this comment

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

It would make sense to use the associated incrementals build:
https://repo.jenkins-ci.org/incrementals/io/jenkins/plugins/checks-api/

<plugin-util-api.version>1.3.0</plugin-util-api.version>
<github-branch-source.version>2.9.1</github-branch-source.version>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.jenkins.plugins.checks.github;

import edu.hm.hafner.util.VisibleForTesting;
import hudson.Extension;
import hudson.model.Job;
import io.jenkins.plugins.checks.status.StatusChecksProperties;
import org.jenkinsci.plugins.github_branch_source.GitHubSCMSource;

import java.util.Optional;

/**
* Implementing {@link StatusChecksProperties} to retrieve properties from jobs with
* {@link GitHubSCMSourceStatusChecksTrait}.
*/
@Extension
public class GitHubSCMSourceStatusChecksProperties implements StatusChecksProperties {
private final SCMFacade scmFacade;

/**
* Default constructor.
*/
public GitHubSCMSourceStatusChecksProperties() {
this(new SCMFacade());
}

@VisibleForTesting
GitHubSCMSourceStatusChecksProperties(final SCMFacade scmFacade) {
this.scmFacade = scmFacade;
}

/**
* {@inheritDoc}
*/
Copy link
Member

Choose a reason for hiding this comment

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

Such comments can be removed since the IDE is smart enough

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added more meaningful docs.

@Override
public boolean isApplicable(final Job<?, ?> job) {
return scmFacade.findGitHubSCMSource(job).isPresent();
}

/**
* {@inheritDoc}
*/
@Override
public String getName(final Job<?, ?> job) {
Optional<GitHubSCMSource> source = scmFacade.findGitHubSCMSource(job);
if (!source.isPresent()) {
return "";
}

return getStatusChecksTrait(source.get()).getName();
}

/**
* {@inheritDoc}
*/
@Override
public boolean isSkipped(final Job<?, ?> job) {
Optional<GitHubSCMSource> source = scmFacade.findGitHubSCMSource(job);
return source.filter(s -> getStatusChecksTrait(s).isSkip()).isPresent();
}

private GitHubSCMSourceStatusChecksTrait getStatusChecksTrait(final GitHubSCMSource source) {
return source.getTraits()
.stream()
.filter(t -> t instanceof GitHubSCMSourceStatusChecksTrait)
.findFirst()
.map(t -> (GitHubSCMSourceStatusChecksTrait)t)
.orElseThrow(IllegalStateException::new);
Copy link
Member

Choose a reason for hiding this comment

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

Are you planning to add a test? It is hard to follow the setup here.
If there is no trait defined then an exception will be thrown, is this intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, just a problem in my logic. Fixed.

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package io.jenkins.plugins.checks.github;

import hudson.Extension;
import hudson.util.FormValidation;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.trait.SCMSourceContext;
import jenkins.scm.api.trait.SCMSourceTrait;
import jenkins.scm.api.trait.SCMSourceTraitDescriptor;
import org.apache.commons.lang3.StringUtils;
import org.jenkinsci.plugins.github_branch_source.GitHubSCMSource;
import org.jenkinsci.plugins.github_branch_source.GitHubSCMSourceContext;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;

/**
* Traits to control {@link io.jenkins.plugins.checks.status.StatusChecksProperties} for jobs using
* {@link GitHubSCMSource}.
*/
public class GitHubSCMSourceStatusChecksTrait extends SCMSourceTrait {
private boolean skip = false;
private String name = "Jenkins";

/**
* Constructor for stapler.
*/
@DataBoundConstructor
public GitHubSCMSourceStatusChecksTrait() {
super();
}

/**
* Defines the status checks name which is also used as identifier for GitHub checks.
*
* @return the name of status checks
*/
public String getName() {
return name;
}

/**
* Defines whether to skip publishing status checks.
*
* @return true to skip publishing checks
*/
public boolean isSkip() {
return skip;
}

/**
* Set the name of the status checks.
*
* @param name
* name of the checks
*/
@DataBoundSetter
public void setName(final String name) {
this.name = name;
}

/**
* Set if skip publishing status checks.
*
* @param skip
* true if skip
*/
@DataBoundSetter
public void setSkip(final boolean skip) {
this.skip = skip;
}

/**
* Descriptor implementation for {@link GitHubSCMSourceStatusChecksTrait}.
*/
@Extension
public static class DescriptorImpl extends SCMSourceTraitDescriptor {
/**
* {@inheritDoc}
*/
@Override
public String getDisplayName() {
return "Status Checks Properties";
}

/**
* {@inheritDoc}
*/
@Override
public Class<? extends SCMSourceContext> getContextClass() {
return GitHubSCMSourceContext.class;
}

/**
* {@inheritDoc}
*/
@Override
public Class<? extends SCMSource> getSourceClass() {
return GitHubSCMSource.class;
}

/**
* Checks if the name of status checks is valid.
*
* @param name
* name of status checks
* @return ok if the name is not empty
*/
public FormValidation doCheckName(@QueryParameter final String name) {
if (StringUtils.isBlank(name)) {
return FormValidation.error("Name should not be empty!");
}

return FormValidation.ok();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">

<f:entry title="Skip Publishing Status Checks" field="skip">
XiongKezhi marked this conversation as resolved.
Show resolved Hide resolved
<f:checkbox default="false"/>
</f:entry>

<f:entry title="Status Checks Name" field="name">
XiongKezhi marked this conversation as resolved.
Show resolved Hide resolved
<f:textbox default="Jenkins"/>
</f:entry>

</j:jelly>