-
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
Merged
XiongKezhi
merged 11 commits into
master
from
add-scm-source-implementation-for-status-checks-properties
Oct 25, 2020
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4ad9cf2
Add GitHub scm source implementation for status checks properties
XiongKezhi dd677dd
Seperate checks status trait and properties
XiongKezhi 2fda3b1
Avoid throwing exceptions when no trait is found
XiongKezhi 80bb35b
Add test for GitHubSCMSourceStatusChecksProperties.
XiongKezhi c6b0ccf
Merge branch 'master' into add-scm-source-implementation-for-status-c…
XiongKezhi c8465b4
Change trait titles.
XiongKezhi a00cc2f
Add doc in README for "Status Checks Properties" behaviour of GitHub …
XiongKezhi 828fe29
Set default to skip publishing checks status.
XiongKezhi f455924
Check the trait of job when judge if the properties is applicable to …
XiongKezhi 34bc0ab
Improve docs for status checks.
XiongKezhi 42d4482
Set publishing status checks for GitHub SCM source as a default feature.
XiongKezhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/io/jenkins/plugins/checks/github/GitHubSCMSourceStatusChecksProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
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; | ||
} | ||
|
||
/** | ||
* Only applicable to jobs using {@link GitHubSCMSource}. | ||
* | ||
* @param job | ||
* a jenkins job | ||
* @return true if {@code job} is using {@link GitHubSCMSource} | ||
*/ | ||
@Override | ||
public boolean isApplicable(final Job<?, ?> job) { | ||
return scmFacade.findGitHubSCMSource(job).isPresent(); | ||
} | ||
|
||
/** | ||
* Returns the name configured by user in the {@link GitHubSCMSourceStatusChecksTrait}. | ||
* | ||
* @param job | ||
* a jenkins job | ||
* @return name of status checks if configured, or a default "Jenkins" will be used. | ||
*/ | ||
@Override | ||
public String getName(final Job<?, ?> job) { | ||
return scmFacade.findGitHubSCMSource(job) | ||
.map(gitHubSCMSource -> getStatusChecksTrait(gitHubSCMSource) | ||
.map(GitHubSCMSourceStatusChecksTrait::getName).orElse("Jenkins")) | ||
.orElse("Jenkins"); | ||
} | ||
|
||
/** | ||
* Returns if skip publishing status checks as user configured in the {@link GitHubSCMSourceStatusChecksTrait}. | ||
* | ||
* @param job | ||
* a jenkins job | ||
* @return true to skip publishing checks if configured. | ||
*/ | ||
@Override | ||
public boolean isSkip(final Job<?, ?> job) { | ||
return scmFacade.findGitHubSCMSource(job) | ||
.map(s -> getStatusChecksTrait(s) | ||
.map(GitHubSCMSourceStatusChecksTrait::isSkip).orElse(false)) | ||
.orElse(false); | ||
|
||
} | ||
|
||
private Optional<GitHubSCMSourceStatusChecksTrait> getStatusChecksTrait(final GitHubSCMSource source) { | ||
return source.getTraits() | ||
.stream() | ||
.filter(t -> t instanceof GitHubSCMSourceStatusChecksTrait) | ||
.findFirst() | ||
.map(t -> (GitHubSCMSourceStatusChecksTrait)t); | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
src/main/java/io/jenkins/plugins/checks/github/GitHubSCMSourceStatusChecksTrait.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package io.jenkins.plugins.checks.github; | ||
|
||
import hudson.Extension; | ||
import hudson.util.FormValidation; | ||
import io.jenkins.plugins.checks.status.StatusChecksProperties; | ||
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}. | ||
*/ | ||
@SuppressWarnings("PMD.DataClass") | ||
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 { | ||
/** | ||
* Returns the display name. | ||
* | ||
* @return "Status Checks Properties" | ||
*/ | ||
@Override | ||
public String getDisplayName() { | ||
return "Status Checks Properties"; | ||
} | ||
|
||
/** | ||
* The {@link GitHubSCMSourceStatusChecksTrait} is only applicable to {@link GitHubSCMSourceContext}. | ||
* | ||
* @return {@link GitHubSCMSourceContext}.class | ||
*/ | ||
@Override | ||
public Class<? extends SCMSourceContext> getContextClass() { | ||
return GitHubSCMSourceContext.class; | ||
} | ||
|
||
/** | ||
* The {@link GitHubSCMSourceStatusChecksTrait} is only applicable to {@link GitHubSCMSource}. | ||
* | ||
* @return {@link GitHubSCMSource}.class | ||
*/ | ||
@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(); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
.../resources/io/jenkins/plugins/checks/github/GitHubSCMSourceStatusChecksTrait/config.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> | ||
<f:checkbox default="false"/> | ||
</f:entry> | ||
|
||
<f:entry title="Status checks name" field="name"> | ||
<f:textbox default="Jenkins"/> | ||
</f:entry> | ||
|
||
</j:jelly> |
65 changes: 65 additions & 0 deletions
65
...test/java/io/jenkins/plugins/checks/github/GitHubSCMSourceStatusChecksPropertiesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.jenkins.plugins.checks.github; | ||
|
||
import hudson.model.Job; | ||
import hudson.util.FormValidation; | ||
import org.jenkinsci.plugins.github_branch_source.GitHubSCMSource; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class GitHubSCMSourceStatusChecksPropertiesTest { | ||
@Test | ||
void shouldReturnPropertiesAsTraitConfigured() { | ||
Job job = mock(Job.class); | ||
SCMFacade scmFacade = mock(SCMFacade.class); | ||
GitHubSCMSource source = mock(GitHubSCMSource.class); | ||
GitHubSCMSourceStatusChecksTrait trait = new GitHubSCMSourceStatusChecksTrait(); | ||
|
||
when(scmFacade.findGitHubSCMSource(job)).thenReturn(java.util.Optional.ofNullable(source)); | ||
when(source.getTraits()).thenReturn(Collections.singletonList(trait)); | ||
|
||
trait.setName("status checks"); | ||
trait.setSkip(false); | ||
|
||
GitHubSCMSourceStatusChecksProperties properties = new GitHubSCMSourceStatusChecksProperties(scmFacade); | ||
assertThat(properties.isApplicable(job)).isTrue(); | ||
assertThat(properties.getName(job)).isEqualTo("status checks"); | ||
assertThat(properties.isSkip(job)).isFalse(); | ||
} | ||
|
||
@Test | ||
void shouldReturnDefaultPropertiesWhenNoTraitAdded() { | ||
Job job = mock(Job.class); | ||
SCMFacade scmFacade = mock(SCMFacade.class); | ||
GitHubSCMSource source = mock(GitHubSCMSource.class); | ||
|
||
when(scmFacade.findGitHubSCMSource(job)).thenReturn(java.util.Optional.ofNullable(source)); | ||
|
||
GitHubSCMSourceStatusChecksProperties properties = new GitHubSCMSourceStatusChecksProperties(scmFacade); | ||
assertThat(properties.isApplicable(job)).isTrue(); | ||
assertThat(properties.getName(job)).isEqualTo("Jenkins"); | ||
assertThat(properties.isSkip(job)).isFalse(); | ||
} | ||
|
||
@Test | ||
void shouldNotApplicableToJobWithoutGitHubSCMSource() { | ||
Job job = mock(Job.class); | ||
assertThat(new GitHubSCMSourceStatusChecksProperties().isApplicable(job)).isFalse(); | ||
} | ||
|
||
@Test | ||
void shouldValidStatusChecksNameInDescriptor() { | ||
GitHubSCMSourceStatusChecksTrait.DescriptorImpl descriptor = new GitHubSCMSourceStatusChecksTrait.DescriptorImpl(); | ||
|
||
assertThat(descriptor.doCheckName("Name Should Not Be Blank")) | ||
.hasFieldOrPropertyWithValue("kind", FormValidation.Kind.OK); | ||
|
||
assertThat(descriptor.doCheckName(" ")) | ||
.hasFieldOrPropertyWithValue("kind", FormValidation.Kind.ERROR) | ||
.hasMessage("Name should not be empty!"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
might want to fix your spelling of customized in the screenshot