Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
sort generated items by name on the seed job's summary and build pages
[FIXES JENKINS-38648]
  • Loading branch information
daspilker committed Oct 14, 2016
1 parent 361a1cc commit f4f1d93
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 23 deletions.
2 changes: 2 additions & 0 deletions docs/Home.md
Expand Up @@ -45,6 +45,8 @@ Browse the Jenkins issue tracker to see any [open issues](https://issues.jenkins
([#920](https://github.com/jenkinsci/job-dsl-plugin/pull/920))
* Fixed problem with embedded API viewer when a plugin is not available in the Update Center
([JENKINS-38456](https://issues.jenkins-ci.org/browse/JENKINS-38456))
* Sort generated items by name on the seed job's summary and build pages
([JENKINS-38648](https://issues.jenkins-ci.org/browse/JENKINS-38648))
* Support for the older versions of the [Git Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin) is
deprecated, see [Migration](Migration#migrating-to-152)
* Support for the older versions of the [Multijob Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Multijob+Plugin)
Expand Down
@@ -1,6 +1,6 @@
package javaposse.jobdsl.dsl

class GeneratedConfigFile {
class GeneratedConfigFile implements Comparable<GeneratedConfigFile> {
final String id
final String name

Expand Down Expand Up @@ -34,4 +34,9 @@ class GeneratedConfigFile {
String toString() {
"GeneratedConfigFile{name='${name}', id='${id}'}"
}

@Override
int compareTo(GeneratedConfigFile o) {
name <=> o.name
}
}
@@ -1,6 +1,6 @@
package javaposse.jobdsl.dsl

class GeneratedJob {
class GeneratedJob implements Comparable<GeneratedJob> {
final String templateName
final String jobName

Expand Down Expand Up @@ -34,4 +34,9 @@ class GeneratedJob {
String toString() {
"GeneratedJob{name='${jobName}'${templateName == null ? '' : ", template='${templateName}'"}}"
}

@Override
int compareTo(GeneratedJob o) {
jobName <=> o.jobName
}
}
@@ -1,6 +1,6 @@
package javaposse.jobdsl.dsl

class GeneratedUserContent {
class GeneratedUserContent implements Comparable<GeneratedUserContent> {
final String path

GeneratedUserContent(String path) {
Expand Down Expand Up @@ -32,4 +32,9 @@ class GeneratedUserContent {
String toString() {
"GeneratedUserContent{path='${path}'}"
}

@Override
int compareTo(GeneratedUserContent o) {
path <=> o.path
}
}
@@ -1,6 +1,6 @@
package javaposse.jobdsl.dsl

class GeneratedView {
class GeneratedView implements Comparable<GeneratedView> {
final String name

GeneratedView(String name) {
Expand Down Expand Up @@ -32,4 +32,9 @@ class GeneratedView {
String toString() {
"GeneratedView{name='${name}'}"
}

@Override
int compareTo(GeneratedView o) {
name <=> o.name
}
}
Expand Up @@ -28,6 +28,7 @@ class GeneratedConfigFileSpec extends Specification {
configFile.name == 'foo'
}

@SuppressWarnings('ChangeToOperator')
def 'only id is relevant for hashCode and equals'() {
when:
GeneratedConfigFile configFile1 = new GeneratedConfigFile('235421345', 'foo')
Expand All @@ -37,8 +38,8 @@ class GeneratedConfigFileSpec extends Specification {
then:
configFile1.hashCode() == configFile2.hashCode()
configFile2.hashCode() != configFile3.hashCode()
configFile1 == configFile2
configFile2 != configFile3
configFile1.equals(configFile2)
!configFile2.equals(configFile3)
}

def 'test toString'() {
Expand All @@ -48,4 +49,16 @@ class GeneratedConfigFileSpec extends Specification {
then:
configFile.toString() == "GeneratedConfigFile{name='foo', id='235421345'}"
}

@SuppressWarnings('ChangeToOperator')
def 'test compare'() {
when:
GeneratedConfigFile configFile1 = new GeneratedConfigFile('235421345', 'foo')
GeneratedConfigFile configFile2 = new GeneratedConfigFile('235421345', 'new name')

then:
configFile1.compareTo(configFile1) == 0
configFile1.compareTo(configFile2) < 0
configFile2.compareTo(configFile1) > 0
}
}
Expand Up @@ -18,4 +18,16 @@ class GeneratedJobSpec extends Specification {
then:
generatedJob.toString() == "GeneratedJob{name='test', template='foo'}"
}

@SuppressWarnings('ChangeToOperator')
def 'test compare'() {
when:
GeneratedJob job1 = new GeneratedJob('235421345', 'foo')
GeneratedJob job2 = new GeneratedJob('235421345', 'new name')

then:
job1.compareTo(job1) == 0
job1.compareTo(job2) < 0
job2.compareTo(job1) > 0
}
}
@@ -0,0 +1,25 @@
package javaposse.jobdsl.dsl

import spock.lang.Specification

class GeneratedUserContentSpec extends Specification {
def 'test toString'() {
when:
GeneratedUserContent generatedUserContent = new GeneratedUserContent('test')

then:
generatedUserContent.toString() == "GeneratedUserContent{path='test'}"
}

@SuppressWarnings('ChangeToOperator')
def 'test compare'() {
when:
GeneratedUserContent userContent1 = new GeneratedUserContent('foo')
GeneratedUserContent userContent2 = new GeneratedUserContent('other')

then:
userContent1.compareTo(userContent1) == 0
userContent1.compareTo(userContent2) < 0
userContent2.compareTo(userContent1) > 0
}
}
Expand Up @@ -19,6 +19,7 @@ class GeneratedViewSpec extends Specification {
thrown(IllegalArgumentException)
}

@SuppressWarnings(['ChangeToOperator', 'GrEqualsBetweenInconvertibleTypes'])
def 'test equals'() {
when:
GeneratedView view = new GeneratedView('test')
Expand Down Expand Up @@ -46,4 +47,16 @@ class GeneratedViewSpec extends Specification {
then:
view.toString() == "GeneratedView{name='test'}"
}

@SuppressWarnings('ChangeToOperator')
def 'test compare'() {
when:
GeneratedView view1 = new GeneratedView('foo')
GeneratedView view2 = new GeneratedView('other')

then:
view1.compareTo(view1) == 0
view1.compareTo(view2) < 0
view2.compareTo(view1) > 0
}
}
@@ -0,0 +1,27 @@
package javaposse.jobdsl.plugin.actions

import groovy.transform.PackageScope
import hudson.model.Item
import hudson.model.View

@PackageScope
class Comparators {
static final Comparator<Item> ITEM_COMPARATOR = new Comparator<Item>() {
@Override
int compare(Item o1, Item o2) {
o1.fullDisplayName <=> o2.fullDisplayName
}
}

static final Comparator<View> VIEW_COMPARATOR = new Comparator<View>() {
@Override
int compare(View o1, View o2) {
getFullDisplayName(o1) <=> getFullDisplayName(o2)
}

private static String getFullDisplayName(View view) {
String ownerDisplayName = view.ownerItemGroup.fullDisplayName
ownerDisplayName.length() == 0 ? view.displayName : "${ownerDisplayName} » ${view.displayName}"
}
}
}
Expand Up @@ -11,7 +11,7 @@ class GeneratedJobsAction extends GeneratedObjectsAction<GeneratedJob, Generated
}

Set<Item> getItems() {
Set<Item> result = []
Set<Item> result = new TreeSet<>(Comparators.ITEM_COMPARATOR)
for (Run run : job.builds) {
GeneratedJobsBuildAction action = run.getAction(GeneratedJobsBuildAction)
if (action != null) {
Expand Down
Expand Up @@ -20,7 +20,7 @@ class GeneratedJobsBuildAction extends GeneratedObjectsRunAction<GeneratedJob> {
}

Set<Item> getItems() {
Set<Item> result = []
Set<Item> result = new TreeSet<>(Comparators.ITEM_COMPARATOR)
for (GeneratedJob job : modifiedObjects) {
Item item = lookupStrategy.getItem(owner.parent, job.jobName, Item)
if (item != null) {
Expand Down
Expand Up @@ -29,7 +29,7 @@ abstract class GeneratedObjectsAction<T, B extends GeneratedObjectsRunAction<T>>

@SuppressWarnings('GroovyUnusedDeclaration') // used by some Jelly views
Set<T> findAllGeneratedObjects() {
Set<T> result = []
Set<T> result = [] as SortedSet<T>
for (Run run : job.builds) {
B action = run.getAction(buildActionClass)
if (action != null && action.modifiedObjects != null) {
Expand Down
Expand Up @@ -5,6 +5,7 @@
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;

/**
* @deprecated use {@code javaposse.jobdsl.plugin.actions.GeneratedObjectsRunAction} instead
Expand Down Expand Up @@ -32,7 +33,8 @@ public String getUrlName() {
return null;
}

@SuppressWarnings("ConstantConditions") // modifiedObjects can be null when this is deserialized by XStream
public Collection<T> getModifiedObjects() {
return modifiedObjects;
return modifiedObjects == null ? null : new TreeSet<T>(modifiedObjects);
}
}
Expand Up @@ -11,7 +11,7 @@ class GeneratedViewsAction extends GeneratedObjectsAction<GeneratedView, Generat
}

Set<View> getViews() {
Set<View> result = []
Set<View> result = new TreeSet<>(Comparators.VIEW_COMPARATOR)
for (Run run : job.builds) {
GeneratedViewsBuildAction action = run.getAction(GeneratedViewsBuildAction)
if (action != null) {
Expand Down
Expand Up @@ -23,7 +23,7 @@ class GeneratedViewsBuildAction extends GeneratedObjectsRunAction<GeneratedView>
}

Set<View> getViews() {
Set<View> allGeneratedViews = []
Set<View> allGeneratedViews = new TreeSet<>(Comparators.VIEW_COMPARATOR)
for (GeneratedView generatedView : modifiedObjects) {
ItemGroup itemGroup = lookupStrategy.getParent(owner.parent, generatedView.name)
if (itemGroup instanceof ViewGroup) {
Expand Down
Expand Up @@ -56,21 +56,24 @@ class GeneratedJobsBuildActionSpec extends Specification {
setup:
Item jobOne = jenkinsRule.jenkins.createProject(FreeStyleProject, 'one')
Item jobTwo = jenkinsRule.jenkins.createProject(FreeStyleProject, 'two')
Item jobThree = jenkinsRule.jenkins.createProject(FreeStyleProject, 'aaa')
GeneratedJob one = new GeneratedJob(null, 'one')
GeneratedJob two = new GeneratedJob(null, 'two')
GeneratedJob three = new GeneratedJob(null, 'aaa')
Run run = Mock(Run)
run.parent >> Mock(Job)
GeneratedJobsBuildAction action = new GeneratedJobsBuildAction([one, two], LookupStrategy.JENKINS_ROOT)
GeneratedJobsBuildAction action = new GeneratedJobsBuildAction([one, two, three], LookupStrategy.JENKINS_ROOT)
action.onLoad(run)

when:
Set<Item> items = action.items
List<Item> items = action.items as List

then:
items != null
items.size() == 2
items.contains(jobOne)
items.contains(jobTwo)
items.size() == 3
items[0] == jobThree
items[1] == jobOne
items[2] == jobTwo
}

def 'project actions'() {
Expand Down
Expand Up @@ -2,6 +2,7 @@ package javaposse.jobdsl.plugin.actions

import hudson.model.AbstractBuild
import hudson.model.AbstractProject
import hudson.model.ItemGroup
import hudson.model.View
import javaposse.jobdsl.dsl.GeneratedView
import spock.lang.Specification
Expand All @@ -14,7 +15,7 @@ class GeneratedViewsActionSpec extends Specification {
AbstractBuild build2 = Mock(AbstractBuild)
AbstractProject project = Mock(AbstractProject)
Collection<GeneratedView> modifiedViews = [Mock(GeneratedView), Mock(GeneratedView)]
Collection<View> views = [Mock(View), Mock(View)]
List<View> views = [Mock(View), Mock(View)]

def 'interface methods'() {
when:
Expand Down Expand Up @@ -103,6 +104,12 @@ class GeneratedViewsActionSpec extends Specification {

def 'getViews'() {
setup:
ItemGroup itemGroup = Mock(ItemGroup)
itemGroup.fullDisplayName >> 'foo'
views[0].displayName >> 'one'
views[0].ownerItemGroup >> itemGroup
views[1].displayName >> 'two'
views[1].ownerItemGroup >> itemGroup
buildAction.views >> views
build1.getAction(GeneratedViewsBuildAction) >> buildAction
build2.getAction(GeneratedViewsBuildAction) >> buildAction
Expand Down
Expand Up @@ -56,23 +56,27 @@ class GeneratedViewsBuildActionSpec extends Specification {
setup:
View viewOne = new ListView('one')
View viewTwo = new ListView('two')
View viewThree = new ListView('aaa')
jenkinsRule.jenkins.addView(viewOne)
jenkinsRule.jenkins.addView(viewTwo)
jenkinsRule.jenkins.addView(viewThree)
GeneratedView one = new GeneratedView('one')
GeneratedView two = new GeneratedView('two')
GeneratedView three = new GeneratedView('aaa')
Run run = Mock(Run)
run.parent >> Mock(Job)
GeneratedViewsBuildAction action = new GeneratedViewsBuildAction([one, two], LookupStrategy.JENKINS_ROOT)
GeneratedViewsBuildAction action = new GeneratedViewsBuildAction([one, two, three], LookupStrategy.JENKINS_ROOT)
action.onLoad(run)

when:
Set<View> views = action.views
List<View> views = action.views as List

then:
views != null
views.size() == 2
views.contains(viewOne)
views.contains(viewTwo)
views.size() == 3
views[0] == viewThree
views[1] == viewOne
views[2] == viewTwo
}

def 'project actions'() {
Expand Down

0 comments on commit f4f1d93

Please sign in to comment.