Skip to content

Commit

Permalink
Automate formatting (#195)
Browse files Browse the repository at this point in the history
* Format code and pom with spotless

* Describe formatting in CONTRIBUTING
  • Loading branch information
MarkEWaite committed Jan 6, 2023
1 parent 5356a89 commit f39ec8b
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 102 deletions.
41 changes: 30 additions & 11 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Contributing to the Schedule Build Plugin
# Contributing to the Schedule Build plugin

Plugin source code is hosted on [GitHub](https://github.com/jenkinsci/schedule-build-plugin).
New feature proposals and bug fix proposals should be submitted as
Expand All @@ -7,25 +7,44 @@ Your pull request will be evaluated by the [Jenkins job](https://ci.jenkins.io/j

Before submitting your change, please assure that you've added tests that verify the change.

## Code Coverage
## Code formatting

Code coverage reporting is available as a maven target.
Please try to improve code coverage with tests when you submit.
Source code and pom file formatting is maintained by the `spotless` maven plugin.
Before submitting a pull request, confirm the formatting is correct with:

* `mvn -P enable-jacoco clean install jacoco:report` to report code coverage
* `mvn spotless:apply`

## Static Analysis
## Spotbugs checks

Please don't introduce new spotbugs output.

* `mvn spotbugs:check` to analyze project using [Spotbugs](https://spotbugs.github.io)
* `mvn spotbugs:gui` to review report using GUI
* `mvn spotbugs:check` analyzes the project using [Spotbugs](https://spotbugs.github.io)
* `mvn spotbugs:gui` displays the spotbugs report using GUI

## Code coverage

Code coverage reporting is available as a maven target.
Please try to improve code coverage with tests when you submit pull requests.

* `mvn -P enable-jacoco clean install jacoco:report` reports code coverage

### Reviewing code coverage

The code coverage report is a set of HTML files that show methods and lines executed.
The following commands will open the `index.html` file in the browser.

* Windows - `start target\site\jacoco\index.html`
* Linux - `xdg-open target/site/jacoco/index.html`
* Gitpod - `cd target/site/jacoco && python -m http.server 8000`

## File format
The file will have a list of package names.
Click on them to find a list of class names.

Files in the repository are in Unix format (LF line terminators).
Please continue using Unix file format for consistency.
The lines of the code will be covered in three different colors, red, green, and orange.
Red lines are not covered in the tests.
Green lines are covered with tests.

## Reporting Issues

Report issues in the [Jenkins issue tracker](https://www.jenkins.io/participate/report-issue/redirect/#18422).
Please use the link:https://www.jenkins.io/participate/report-issue/["How to Report an Issue"] guidelines when reporting issues.
25 changes: 21 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.53</version>
<relativePath></relativePath>
<relativePath />
</parent>

<artifactId>schedule-build</artifactId>
Expand Down Expand Up @@ -102,14 +102,31 @@
<!-- define a language-specific format -->
<java>
<!-- no need to specify files, inferred automatically -->
<endWithNewline></endWithNewline>
<removeUnusedImports></removeUnusedImports>
<!-- apply a specific flavor of google-java-format -->
<googleJavaFormat>
<version>1.15.0</version>
<style>AOSP</style>
</googleJavaFormat>
<endWithNewline />
<removeUnusedImports />
</java>
<pom>
<indent>
<spaces>true</spaces>
</indent>
<sortPom></sortPom>
<includes>
<include>pom.xml</include>
</includes>
<sortPom>
<expandEmptyElements>false</expandEmptyElements>
<sortDependencies>groupId,artifactId</sortDependencies>
<sortDependencyExclusions>groupId,artifactId</sortDependencyExclusions>
<sortExecutions>true</sortExecutions>
<sortModules>true</sortModules>
<sortPlugins>groupId,artifactId</sortPlugins>
<sortProperties>true</sortProperties>
<spaceBeforeCloseEmptyElement>true</spaceBeforeCloseEmptyElement>
</sortPom>
</pom>
</configuration>
<executions>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package org.jenkinsci.plugins.schedulebuild;

import hudson.model.Action;
import hudson.model.Descriptor.FormException;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.ParametersDefinitionProperty;
import hudson.util.FormValidation;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;

import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.HttpResponses;
Expand All @@ -19,22 +25,13 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.interceptor.RequirePOST;

import hudson.model.Action;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Descriptor.FormException;
import hudson.util.FormValidation;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;

public class ScheduleBuildAction implements Action, StaplerProxy {

private static final Logger LOGGER = Logger.getLogger(ScheduleBuildAction.class.getName());

private final Job<?, ?> target;
private final static long SECURITY_MARGIN = 120 * 1000;
private final static long ONE_DAY = 24 * 3600 * 1000;
private static final long SECURITY_MARGIN = 120 * 1000;
private static final long ONE_DAY = 24 * 3600 * 1000;

private long quietperiod;

Expand All @@ -48,12 +45,16 @@ public ScheduleBuildAction(final Job<?, ?> target) {

@Override
public String getIconFileName() {
return target.hasPermission(Job.BUILD) && this.target.isBuildable() ? "/plugin/schedule-build/images/schedule.svg" : null;
return target.hasPermission(Job.BUILD) && this.target.isBuildable()
? "/plugin/schedule-build/images/schedule.svg"
: null;
}

@Override
public String getDisplayName() {
return target.hasPermission(Job.BUILD) && this.target.isBuildable() ? Messages.ScheduleBuildAction_DisplayName() : null;
return target.hasPermission(Job.BUILD) && this.target.isBuildable()
? Messages.ScheduleBuildAction_DisplayName()
: null;
}

@Override
Expand Down Expand Up @@ -88,7 +89,10 @@ public String getDefaultDate() {
}

public Date getDefaultDateObject() {
Date buildtime = new Date(), now = new Date(), defaultScheduleTime = new ScheduleBuildGlobalConfiguration().getDefaultScheduleTimeObject();
Date buildtime = new Date(),
now = new Date(),
defaultScheduleTime =
new ScheduleBuildGlobalConfiguration().getDefaultScheduleTimeObject();
DateFormat dateFormat = dateFormat();
try {
now = dateFormat.parse(dateFormat.format(now));
Expand Down Expand Up @@ -134,7 +138,8 @@ public long getQuietPeriodInSeconds() {
}

@RequirePOST
public HttpResponse doNext(StaplerRequest req, @AncestorInPath Item item) throws FormException, ServletException, IOException {
public HttpResponse doNext(StaplerRequest req, @AncestorInPath Item item)
throws FormException, ServletException, IOException {
if (item == null) {
return FormValidation.ok();
}
Expand Down Expand Up @@ -167,14 +172,18 @@ public HttpResponse doNext(StaplerRequest req, @AncestorInPath Item item) throws
}

public boolean isJobParameterized() {
ParametersDefinitionProperty paramDefinitions = target.getProperty(ParametersDefinitionProperty.class);
return paramDefinitions != null && paramDefinitions.getParameterDefinitions() != null && paramDefinitions.getParameterDefinitions().size() > 0;
ParametersDefinitionProperty paramDefinitions =
target.getProperty(ParametersDefinitionProperty.class);
return paramDefinitions != null
&& paramDefinitions.getParameterDefinitions() != null
&& paramDefinitions.getParameterDefinitions().size() > 0;
}

private DateFormat dateFormat() {
Locale locale = Stapler.getCurrentRequest() != null ?
Stapler.getCurrentRequest().getLocale() :
Locale.getDefault();
Locale locale =
Stapler.getCurrentRequest() != null
? Stapler.getCurrentRequest().getLocale()
: Locale.getDefault();
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);
df.setTimeZone(new ScheduleBuildGlobalConfiguration().getTimeZoneObject());
return df;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package org.jenkinsci.plugins.schedulebuild;

import org.kohsuke.stapler.StaplerRequest;

import hudson.Extension;
import hudson.model.Descriptor;
import hudson.views.ListViewColumn;
import hudson.views.ListViewColumnDescriptor;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;

public class ScheduleBuildButtonColumn extends ListViewColumn {
public static final class DescriptorImpl extends ListViewColumnDescriptor {
Expand All @@ -16,7 +15,8 @@ public String getDisplayName() {
}

@Override
public ListViewColumn newInstance(final StaplerRequest request, final JSONObject formData) throws FormException {
public ListViewColumn newInstance(final StaplerRequest request, final JSONObject formData)
throws FormException {
return new ScheduleBuildButtonColumn();
}

Expand All @@ -26,12 +26,10 @@ public boolean shownByDefault() {
}
}

@Extension
public static final Descriptor<ListViewColumn> DESCRIPTOR = new DescriptorImpl();
@Extension public static final Descriptor<ListViewColumn> DESCRIPTOR = new DescriptorImpl();

@Override
public Descriptor<ListViewColumn> getDescriptor() {
return DESCRIPTOR;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import hudson.Extension;
import hudson.util.FormValidation;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
Expand All @@ -10,12 +15,6 @@
import org.kohsuke.stapler.*;
import org.kohsuke.stapler.interceptor.RequirePOST;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

@Extension
@Symbol("scheduleBuild")
public class ScheduleBuildGlobalConfiguration extends GlobalConfiguration {
Expand Down Expand Up @@ -52,9 +51,10 @@ public TimeZone getTimeZoneObject() {
}

private DateFormat getTimeFormat() {
Locale locale = Stapler.getCurrentRequest() != null ?
Stapler.getCurrentRequest().getLocale() :
Locale.getDefault();
Locale locale =
Stapler.getCurrentRequest() != null
? Stapler.getCurrentRequest().getLocale()
: Locale.getDefault();
return DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
}

Expand All @@ -64,7 +64,8 @@ public Date getDefaultScheduleTimeObject() {

@RequirePOST
public FormValidation doCheckDefaultScheduleTime(@QueryParameter String value) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER); // Admin permission required for global config
Jenkins.get()
.checkPermission(Jenkins.ADMINISTER); // Admin permission required for global config
try {
getTimeFormat().parse(value);
} catch (ParseException ex) {
Expand All @@ -75,11 +76,12 @@ public FormValidation doCheckDefaultScheduleTime(@QueryParameter String value) {

@RequirePOST
public FormValidation doCheckTimeZone(@QueryParameter String value) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER); // Admin permission required for global config
Jenkins.get()
.checkPermission(Jenkins.ADMINISTER); // Admin permission required for global config
TimeZone zone = TimeZone.getTimeZone(value);
if(StringUtils.equals(zone.getID(), value)) {
if (StringUtils.equals(zone.getID(), value)) {
return FormValidation.ok();
}else {
} else {
return FormValidation.error(Messages.ScheduleBuildGlobalConfiguration_TimeZoneError());
}
}
Expand All @@ -91,7 +93,8 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
this.timeZone = null;
if (json.containsKey("defaultScheduleTime") && json.containsKey("timeZone")) {
try {
this.defaultScheduleTime = getTimeFormat().parse(json.getString("defaultScheduleTime"));
this.defaultScheduleTime =
getTimeFormat().parse(json.getString("defaultScheduleTime"));
this.timeZone = json.getString("timeZone");
save();
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
package org.jenkinsci.plugins.schedulebuild;

import java.util.Collection;
import java.util.Collections;

import hudson.Extension;
import hudson.model.Action;
import hudson.model.Job;
import java.util.Collection;
import java.util.Collections;
import jenkins.model.TransientActionFactory;

@Extension
public final class ScheduleBuildTransientProjectActionFactory extends TransientActionFactory<Job> {

@Override
public Class<Job> type()
{
public Class<Job> type() {
return Job.class;
}

@Override
public Collection<? extends Action> createFor(Job target)
{
public Collection<? extends Action> createFor(Job target) {
return Collections.singleton(new ScheduleBuildAction(target));
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package org.jenkinsci.plugins.schedulebuild;

import io.jenkins.plugins.casc.misc.RoundTripAbstractTest;
import jenkins.model.GlobalConfiguration;
import org.jvnet.hudson.test.RestartableJenkinsRule;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import io.jenkins.plugins.casc.misc.RoundTripAbstractTest;
import jenkins.model.GlobalConfiguration;
import org.jvnet.hudson.test.RestartableJenkinsRule;

public class JCasCGlobalAllFields extends RoundTripAbstractTest {

@Override
protected void assertConfiguredAsExpected(RestartableJenkinsRule j, String configContent) {
ScheduleBuildGlobalConfiguration globalConfig = GlobalConfiguration.all().getInstance(ScheduleBuildGlobalConfiguration.class);
ScheduleBuildGlobalConfiguration globalConfig =
GlobalConfiguration.all().getInstance(ScheduleBuildGlobalConfiguration.class);
assertThat(globalConfig, is(not(nullValue())));
assertThat(globalConfig.getDefaultScheduleTime(), is("12:34:56 AM"));
assertThat(globalConfig.getTimeZone(), is("Europe/Rome"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
public class JCasCGlobalConfigurationTest extends RoundTripAbstractTest {

@Override
protected void assertConfiguredAsExpected(RestartableJenkinsRule j, String configContent) {
}
protected void assertConfiguredAsExpected(RestartableJenkinsRule j, String configContent) {}

@Override
protected String stringInLogExpected() {
Expand Down

0 comments on commit f39ec8b

Please sign in to comment.