-
Notifications
You must be signed in to change notification settings - Fork 38
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
Changes from 2 commits
4ad9cf2
dd677dd
2fda3b1
80bb35b
c6b0ccf
c8465b4
a00cc2f
828fe29
f455924
34bc0ab
42d4482
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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} | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Such comments can be removed since the IDE is smart enough There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> |
There was a problem hiding this comment.
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/