Skip to content
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/build integer response #18

Merged
merged 4 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.common;

import java.util.List;

import org.jclouds.javax.annotation.Nullable;
import org.jclouds.json.SerializedNames;

import com.cdancy.jenkins.rest.JenkinsUtils;
import com.google.auto.value.AutoValue;

/**
* Integer response to be returned when an endpoint returns
* an integer.
*
* <p>When the HTTP response code is valid the `value` parameter will
* be set to the integer value while a non-valid response has the `value` set to
* null along with any potential `error` objects returned from Jenkins.
*/
@AutoValue
public abstract class IntegerResponse implements Value<Integer>, ErrorsHolder {

@SerializedNames({ "value", "errors" })
public static IntegerResponse create(@Nullable final Integer value,
final List<Error> errors) {

return new AutoValue_IntegerResponse(value,
JenkinsUtils.nullToEmpty(errors));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import static org.jclouds.http.HttpUtils.returnValueOnCodeOrNull;

import com.cdancy.jenkins.rest.domain.common.IntegerResponse;
import com.cdancy.jenkins.rest.domain.common.RequestStatus;
import com.cdancy.jenkins.rest.domain.common.Error;
import com.cdancy.jenkins.rest.domain.crumb.Crumb;
Expand Down Expand Up @@ -80,6 +81,20 @@ public Object createOrPropagate(final Throwable throwable) throws Exception {
}
}

public static final class IntegerResponseOnError implements Fallback<Object> {
@Override
public Object createOrPropagate(final Throwable throwable) throws Exception {
if (checkNotNull(throwable, "throwable") != null) {
try {
return IntegerResponse.create(null, getErrors(throwable));
} catch (JsonSyntaxException e) {
return IntegerResponse.create(null, getErrors(e));
}
}
throw propagate(throwable);
}
}

public static final class CrumbOnError implements Fallback<Object> {
@Override
public Object createOrPropagate(final Throwable throwable) throws Exception {
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/cdancy/jenkins/rest/features/JobsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.jclouds.rest.annotations.ResponseParser;

import com.cdancy.jenkins.rest.binders.BindMapToForm;
import com.cdancy.jenkins.rest.domain.common.IntegerResponse;
import com.cdancy.jenkins.rest.domain.common.RequestStatus;
import com.cdancy.jenkins.rest.domain.job.BuildInfo;
import com.cdancy.jenkins.rest.domain.job.JobInfo;
Expand Down Expand Up @@ -137,19 +138,19 @@ boolean description(@PathParam("name") String jobName,

@Named("jobs:build")
@Path("/job/{name}/build")
@Fallback(Fallbacks.NullOnNotFoundOr404.class)
@Fallback(JenkinsFallbacks.IntegerResponseOnError.class)
@ResponseParser(LocationToQueueId.class)
@Consumes("application/unknown")
@POST
Integer build(@PathParam("name") String jobName);
IntegerResponse build(@PathParam("name") String jobName);

@Named("jobs:build-with-params")
@Path("/job/{name}/buildWithParameters")
@Fallback(Fallbacks.NullOnNotFoundOr404.class)
@Fallback(JenkinsFallbacks.IntegerResponseOnError.class)
@ResponseParser(LocationToQueueId.class)
@Consumes("application/unknown")
@POST
Integer buildWithParameters(@PathParam("name") String jobName,
IntegerResponse buildWithParameters(@PathParam("name") String jobName,
@BinderParam(BindMapToForm.class) Map<String, List<String>> properties);

@Named("jobs:last-build-number")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,47 @@

package com.cdancy.jenkins.rest.parsers;

import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.inject.Singleton;

import org.jclouds.http.HttpResponse;

import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.common.io.CharStreams;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these imports needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will fix this.


import com.cdancy.jenkins.rest.domain.common.Error;
import com.cdancy.jenkins.rest.domain.common.IntegerResponse;

/**
* Created by dancc on 3/11/16.
*/
@Singleton
public class LocationToQueueId implements Function<HttpResponse, Integer> {
public class LocationToQueueId implements Function<HttpResponse, IntegerResponse> {

private static final Pattern pattern = Pattern.compile("^.*/queue/item/(\\d+)/$");

public Integer apply(HttpResponse response) {
public IntegerResponse apply(HttpResponse response) {

String url = response.getFirstHeaderOrNull("Location");
List<Error> errors = Lists.newArrayList();
if (url != null) {
Matcher matcher = pattern.matcher(url);
if (matcher.find() && matcher.groupCount() == 1) {
return Integer.valueOf(matcher.group(1));
return IntegerResponse.create(Integer.valueOf(matcher.group(1)), errors);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the code further above lets just pass a null like so:

   return IntegerResponse.create(Integer.valueOf(matcher.group(1)), null);

I was trying to apply the same approach as in the Bitbucket Rest Understanding Error objects where the error object is never null so it can always be easily checked.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair but lets let the underlying process take care of that instead of aggressively creating it up front.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I get it, we let IntegerResponse convert null to an empty list.

}
}

return 0;
errors.add(Error.create("No context",
"No queue item Location header could be found despite getting a valid HTTP response.",
"None"));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cdancy, in creating this error, is it possible to have the original context (the POST)?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martinda whatever is in the httpResponse is really all we have to work with. What were you thinknig?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cdancy I was thinking of printing the HTTP response, in case it might give a hint to the user.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets take the Error.create call outside and do something like:

final Error error = Error.create(null,
    "No queue item Location header could be found despite getting a valid HTTP response.", 
    NumberFormatException.class.getCanonicalName());
List<Error> errors = Lists.newArrayList(error);
return IntegerResponse.create(null, errors);

I'd prefer to delay the creation of the list until the last possible minute as this is an exceptional case. In the code further above lets just pass a null like so:

        return IntegerResponse.create(Integer.valueOf(matcher.group(1)), null);

return IntegerResponse.create(null, errors);
}
}