Jenkins & Khttp | Use REST API to find out the Last Successful Build

Manserpatrice
Geek Culture
Published in
3 min readMar 2, 2021

--

Photo by alevision.co on Unsplash

This Article is about using Jenksins REST API for getting information about the last successful build. Jenkins has no official Java/Kotlin API. This means, that we either have to use some third party Github developers java API, or just to use Jenkins REST API, that is guaranteed to work.

The Problem

I have a Jenkins Instance, on which different jobs are running. I need the git hash of the last successful build for another project.

What can I do? I can go to Jenkins, scroll down to the last successful build of a branch and click on it. The git hash is under Revision and only needs to be copied from there. If you need to do that 20 times a day, it can be pretty nerve wracking. Another usecase for this would be, that you want to automate something that requires the last successful build hash and you can’t put that in manually every time.

The solution

As I said, there are at least two ways to solve this problem. For me, the safety that the solution is reliable and has a big community backing it off, was really important. I decided to not use the unofficial Java API and instead stick with the REST API calls. If the Java API gets official support at the time of the reading, make sure to check that out too.

Now let’s start with the configuration of the build.gradle.

Add this dependency to the build.gradle file:

implementation ("khttp:khttp:0.1.0")

Create a class called Jenkins, which has two functions.

Generate a Response

  1. import all the necessary dependencies from khttp.
  2. create a simple get request with your jenkins-server-address and job and branchName.
  3. The last part of this url must be /lastSuccessfulBuild.
  4. Create a Basic Authorization with a Jenkins Username and Password.

Get the last Successful Build

  1. Replace the “/” with “%252F” (since this is a special caracter in a url)
  2. Create the response by calling the private method defined before
  3. Get the substring out of the HTML if the statusCode is 200. (This substring is the git hash.)
  4. If the statusCode is not 200, return an error message

How to test it

Create a new Testclass with Junit.

To test the functionality of this class, we need to mock a new Response. I used Mockk for this, which is basically just Mockito for Kotlin. Since we only check the statusCode and the text of the response, so we only have to define the behaviour for them.

Make sure you’ve added the correct khttp imports for the Response.

Next, we need to create a spy of our class with spyk and define the behavour of our private method. Since this is a private method, we cant use it like we’re used to. We need to call it like this:

jenkins["generateResponse"]("someJob", "someBranch")

Then we create a hash variable which is just the output of the getLastSuccessfulBuild function.

The last step is to use assertThat for comparing the hash to the desired value.

Test the Error message

This is practically the same as the test before, with the difference that you change the statusCode to something other than 200.

Now instead of asserting a hash, you need to assert the error message with the right job and branchname.

Reflection

Would I do this the same way next time?

Yes, kind of. I think I used the right components for this project. The only thing that I would change next time is to first read the documentation of mockk a little more. I had some problems with mocking the private function. At the beginning i didn’t even knew that this was possible, but it is documented pretty well in the documentation.

What went good?

The calls to the REST API worked pretty good right at the beginning. The mocking worked also pretty well with the exception of that private function.

What needs improvement?

Next time i would research a little more about mocking special methods and read more of the documentation itself.

I hope this article could help you 😺

--

--