-
Notifications
You must be signed in to change notification settings - Fork 174
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
Feature/get build actions #29
Changes from 23 commits
a6b2659
6c87f66
419d91a
c000735
77c31ad
c2a1468
265566c
ef62f58
529386b
f02e443
733dde3
89f5ae9
ba324d7
ba92bf2
ac9e189
22c630b
af01590
c75a699
ffd3dbd
c82f329
f563208
5c6adbc
3295870
c2552c4
a66ba2e
59dd3f3
06f4974
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,43 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.cdancy.jenkins.rest.domain.job; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.google.common.collect.ImmutableList; | ||
import org.jclouds.json.SerializedNames; | ||
|
||
import java.util.List; | ||
|
||
@AutoValue | ||
public abstract class Action { | ||
|
||
public abstract List<Cause> causes(); | ||
|
||
public abstract List<Parameter> parameters(); | ||
|
||
Action() { | ||
} | ||
|
||
@SerializedNames({"causes", "parameters"}) | ||
public static Action create(final List<Cause> causes, final List<Parameter> parameters) { | ||
return new AutoValue_Action( | ||
causes != null ? ImmutableList.copyOf(causes) : ImmutableList.<Cause>of(), | ||
parameters != null ? ImmutableList.copyOf(parameters) : ImmutableList.<Parameter>of()); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.cdancy.jenkins.rest.domain.job; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import org.jclouds.javax.annotation.Nullable; | ||
import org.jclouds.json.SerializedNames; | ||
|
||
@AutoValue | ||
public abstract class Cause { | ||
|
||
@Nullable | ||
public abstract String clazz(); | ||
|
||
public abstract String shortDescription(); | ||
|
||
@Nullable | ||
public abstract String userId(); | ||
|
||
@Nullable | ||
public abstract String userName(); | ||
|
||
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. The There is a precedent in Plugins.java. |
||
Cause() { | ||
} | ||
|
||
@SerializedNames({"_class", "shortDescription", "userId", "userName"}) | ||
public static Cause create(final String clazz, final String shortDescription, final String userId, final String userName) { | ||
return new AutoValue_Cause(clazz, shortDescription, userId, userName); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.cdancy.jenkins.rest.domain.job; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import org.jclouds.javax.annotation.Nullable; | ||
import org.jclouds.json.SerializedNames; | ||
|
||
@AutoValue | ||
public abstract class Parameter { | ||
|
||
@Nullable | ||
public abstract String clazz(); | ||
|
||
public abstract String name(); | ||
|
||
@Nullable | ||
public abstract String value(); | ||
|
||
Parameter() { | ||
} | ||
|
||
@SerializedNames({"_class", "name", "value"}) | ||
public static Parameter create(final String clazz, final String name, final String value) { | ||
return new AutoValue_Parameter(clazz, name, value); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -25,6 +25,8 @@ | |||||||||||||||||||
import java.util.List; | ||||||||||||||||||||
import java.util.Map; | ||||||||||||||||||||
|
||||||||||||||||||||
import com.cdancy.jenkins.rest.domain.job.Cause; | ||||||||||||||||||||
import com.cdancy.jenkins.rest.domain.job.Parameter; | ||||||||||||||||||||
import com.cdancy.jenkins.rest.domain.plugins.Plugin; | ||||||||||||||||||||
import com.cdancy.jenkins.rest.domain.plugins.Plugins; | ||||||||||||||||||||
import org.testng.annotations.Test; | ||||||||||||||||||||
|
@@ -119,6 +121,12 @@ public void testGetBuildInfo() { | |||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Test(dependsOnMethods = "testGetBuildInfo") | ||||||||||||||||||||
public void testGetBuildParametersOfLastJob() { | ||||||||||||||||||||
List<Parameter> parameters = api().buildInfo(null, "DevTest", 1).actions().get(0).parameters(); | ||||||||||||||||||||
assertTrue(parameters.size() == 0); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Test(dependsOnMethods = "testGetBuildParametersOfLastJob") | ||||||||||||||||||||
public void testCreateJobThatAlreadyExists() { | ||||||||||||||||||||
String config = payloadFromResource("/freestyle-project.xml"); | ||||||||||||||||||||
RequestStatus success = api().create(null, "DevTest", config); | ||||||||||||||||||||
|
@@ -307,6 +315,59 @@ public void testGetBuildInfoOfJobInFolder() { | |||||||||||||||||||
assertTrue(output.queueId() == queueIdForAnotherJob.value()); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Test(dependsOnMethods = "testGetProgressiveText") | ||||||||||||||||||||
public void testGetBuildParametersofJob() { | ||||||||||||||||||||
List<Parameter> parameters = api().buildInfo("test-folder/test-folder-1", "JobInFolder",1).actions().get(0).parameters(); | ||||||||||||||||||||
assertNotNull(parameters); | ||||||||||||||||||||
assertTrue(parameters.get(0).name().equals("SomeKey")); | ||||||||||||||||||||
assertTrue(parameters.get(0).value().equals("SomeVeryNewValue")); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Test(dependsOnMethods = "testGetProgressiveText") | ||||||||||||||||||||
public void testGetBuildCausesOfJob() { | ||||||||||||||||||||
List<Cause> causes = api().buildInfo("test-folder/test-folder-1", "JobInFolder",1).actions().get(1).causes(); | ||||||||||||||||||||
assertNotNull(causes); | ||||||||||||||||||||
assertTrue(causes.get(0).shortDescription().equals("Started by user admin")); | ||||||||||||||||||||
assertTrue(causes.get(0).userId().equals("admin")); | ||||||||||||||||||||
assertTrue(causes.get(0).userName().equals("admin")); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public void testCreateJobForEmptyAndNullParams() { | ||||||||||||||||||||
String config = payloadFromResource("/freestyle-project-empty-and-null-params.xml"); | ||||||||||||||||||||
RequestStatus success = api().create(null, "JobForEmptyAndNullParams", config); | ||||||||||||||||||||
assertTrue(success.value()); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Test(dependsOnMethods = "testCreateJobForEmptyAndNullParams") | ||||||||||||||||||||
public void testBuildWithParametersOfJobForEmptyAndNullParams() { | ||||||||||||||||||||
Map<String, List<String>> params = new HashMap<>(); | ||||||||||||||||||||
params.put("SomeKey1", Lists.newArrayList("")); | ||||||||||||||||||||
params.put("SomeKey2", null); | ||||||||||||||||||||
IntegerResponse job1 = api.jobsApi().buildWithParameters(null, "JobForEmptyAndNullParams", params); | ||||||||||||||||||||
assertNotNull(job1); | ||||||||||||||||||||
assertTrue(job1.value() > 0); | ||||||||||||||||||||
assertTrue(job1.errors().size() == 0); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Test(dependsOnMethods = "testBuildWithParametersOfJobForEmptyAndNullParams") | ||||||||||||||||||||
public void testGetBuildParametersOfJobForEmptyAndNullParams() { | ||||||||||||||||||||
while (api().jobInfo(null, "JobForEmptyAndNullParams").lastBuild() == null) { | ||||||||||||||||||||
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. The reason I add this check is to make sure the build has started running. If I don't add it, I am getting a null pointer exception on the next line i.e. line 357 while trying to get the parameters. 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. @richabindra if you look at the 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. sure. 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. @cdancy - So, the thing is I am not able to make the getRunningQueueItem as static because jenkins-rest/src/test/java/com/cdancy/jenkins/rest/features/QueueApiLiveTest.java Lines 233 to 235 in 00cb735
If I make api() method as static, it complains because it refers to api property of jclouds.apis class at line 264, which obviously I cannot make static. jenkins-rest/src/test/java/com/cdancy/jenkins/rest/features/QueueApiLiveTest.java Lines 263 to 265 in 00cb735
I tried following approaches:
jenkins-rest/src/test/java/com/cdancy/jenkins/rest/features/QueueApiLiveTest.java Lines 263 to 265 in 00cb735
Possible alternatives:
I am not sure what's the best way possible. 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. @richabindra what about moving the method to the 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. @martinda - Yes, that works. I'll push the changes. |
||||||||||||||||||||
continue; | ||||||||||||||||||||
} | ||||||||||||||||||||
List<Parameter> parameters = api().buildInfo(null, "JobForEmptyAndNullParams", 1).actions().get(0).parameters(); | ||||||||||||||||||||
assertNotNull(parameters); | ||||||||||||||||||||
assertTrue(parameters.get(0).name().equals("SomeKey1")); | ||||||||||||||||||||
assertTrue(parameters.get(0).value().isEmpty()); | ||||||||||||||||||||
assertTrue(parameters.get(1).name().equals("SomeKey2")); | ||||||||||||||||||||
assertTrue(parameters.get(1).value().isEmpty()); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Test(dependsOnMethods = "testGetBuildParametersOfJobForEmptyAndNullParams") | ||||||||||||||||||||
public void testDeleteJobForEmptyAndNullParams() { | ||||||||||||||||||||
RequestStatus success = api().delete(null, "JobForEmptyAndNullParams"); | ||||||||||||||||||||
assertTrue(success.value()); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Test(dependsOnMethods = "testCreateFoldersInJenkins") | ||||||||||||||||||||
public void testCreateJobWithLeadingAndTrailingForwardSlashes() { | ||||||||||||||||||||
String config = payloadFromResource("/freestyle-project-no-params.xml"); | ||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,12 +21,16 @@ | |
import static org.testng.Assert.assertNull; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.ws.rs.core.MediaType; | ||
|
||
import com.cdancy.jenkins.rest.domain.job.Action; | ||
import com.cdancy.jenkins.rest.domain.job.Cause; | ||
import com.cdancy.jenkins.rest.domain.job.Parameter; | ||
import org.testng.annotations.Test; | ||
|
||
import com.cdancy.jenkins.rest.JenkinsApi; | ||
|
@@ -536,6 +540,85 @@ public void testBuildJobWithParamsNonExistentJob() throws Exception { | |
} | ||
} | ||
|
||
public void testGetParams() throws Exception { | ||
MockWebServer server = mockWebServer(); | ||
|
||
String body = payloadFromResource("/build-info.json"); | ||
server.enqueue(new MockResponse().setBody(body).setResponseCode(200)); | ||
JenkinsApi jenkinsApi = api(server.getUrl("/")); | ||
JobsApi api = jenkinsApi.jobsApi(); | ||
try { | ||
List<Parameter> output = api.buildInfo(null,"fish", 10).actions().get(0).parameters(); | ||
assertNotNull(output); | ||
assertTrue(output.get(0).name().equals("bear")); | ||
assertTrue(output.get(0).value().equals("true")); | ||
assertTrue(output.get(1).name().equals("fish")); | ||
assertTrue(output.get(1).value().equals("salmon")); | ||
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. Although unlikely to break, there should be a test for an empty parameter value. Maybe Jenkins returns null instead of an empty space. 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. Agreed to all of the above. 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. Yes, you are right. I'll incorporate the suggested changes in the next commit. |
||
assertSent(server, "GET", "/job/fish/10/api/json"); | ||
} finally { | ||
jenkinsApi.close(); | ||
server.shutdown(); | ||
} | ||
} | ||
|
||
public void testGetParamsWhenNoBuildParams() throws Exception { | ||
MockWebServer server = mockWebServer(); | ||
|
||
String body = payloadFromResource("/build-info-no-params.json"); | ||
server.enqueue(new MockResponse().setBody(body).setResponseCode(200)); | ||
JenkinsApi jenkinsApi = api(server.getUrl("/")); | ||
JobsApi api = jenkinsApi.jobsApi(); | ||
try { | ||
List<Parameter> output = api.buildInfo(null,"fish", 10).actions().get(0).parameters(); | ||
assertTrue(output.size() == 0); | ||
assertSent(server, "GET", "/job/fish/10/api/json"); | ||
} finally { | ||
jenkinsApi.close(); | ||
server.shutdown(); | ||
} | ||
} | ||
|
||
public void testGetParamsWhenEmptyorNullParams() throws Exception { | ||
MockWebServer server = mockWebServer(); | ||
|
||
String body = payloadFromResource("/build-info-empty-and-null-params.json"); | ||
server.enqueue(new MockResponse().setBody(body).setResponseCode(200)); | ||
JenkinsApi jenkinsApi = api(server.getUrl("/")); | ||
JobsApi api = jenkinsApi.jobsApi(); | ||
try { | ||
List<Parameter> output = api.buildInfo(null,"fish", 10).actions().get(0).parameters(); | ||
assertNotNull(output); | ||
assertTrue(output.get(0).name().equals("bear")); | ||
assertTrue(output.get(0).value().equals("null")); | ||
assertTrue(output.get(1).name().equals("fish")); | ||
assertTrue(output.get(1).value().isEmpty()); | ||
assertSent(server, "GET", "/job/fish/10/api/json"); | ||
} finally { | ||
jenkinsApi.close(); | ||
server.shutdown(); | ||
} | ||
} | ||
|
||
public void testGetCause() throws Exception { | ||
MockWebServer server = mockWebServer(); | ||
|
||
String body = payloadFromResource("/build-info-no-params.json"); | ||
server.enqueue(new MockResponse().setBody(body).setResponseCode(200)); | ||
JenkinsApi jenkinsApi = api(server.getUrl("/")); | ||
JobsApi api = jenkinsApi.jobsApi(); | ||
try { | ||
List<Cause> output = api.buildInfo(null,"fish", 10).actions().get(0).causes(); | ||
assertNotNull(output); | ||
assertTrue(output.get(0).shortDescription().equals("Started by user anonymous")); | ||
assertNull(output.get(0).userId()); | ||
assertTrue(output.get(0).userName().equals("anonymous")); | ||
assertSent(server, "GET", "/job/fish/10/api/json"); | ||
} finally { | ||
jenkinsApi.close(); | ||
server.shutdown(); | ||
} | ||
} | ||
|
||
public void testGetLastBuildNumber() throws Exception { | ||
MockWebServer server = mockWebServer(); | ||
|
||
|
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.
I wonder if there is a way to make parameters a
Map<String, String>
instead of a plain list, it would make retrieval more natural.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.
There is a way we could marshal it into something else but I'd actually rather not do so and instead hand back to the user the exact response we get from jenkins for better or worse.
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.
No problem, I am okay with that.
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.
Maybe I should have dismissed the review... oh well.
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.
Yes, I was confused about this too whether to leave it as is or should we convert it to something more useful for the user. But, it makes sense to leave it as is.
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.
We're doing this mainly to keep things sane on our end with the least amount of confusion and gotchas. There are definitely good reasons to marshal this into something else but then I don't want to get into the habit of having these one-off cases where the user expects things to be a certain way and then the implementation provides something different. I'd rather hand back to the user, in AutoValue object format, exactly what was given to us by Jenkins.
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.
Yes, agreed. So, just clarifying, I don't need to change anything in the PR?
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.
Nothing on this end though please do address @martinda comments below and then we can merge and kick a new patch release.