VB Magic

2013/10/23

Accessing Jenkins API from VB.net

Filed under: .NET, ASP.NET MVC, VB.NET — Tags: , , , , , , — vbmagic @ 3:17 pm

As this was one of those things it took a while to track down I though a quick post would help others trying the same.

I have an up-to-date Jenkins server running builds and I would like to pull a list of failed jobs from the server and display them on a website.
First you need to look up the correct URL to access the list of jobs so after seeing a few posts in blogs and going to the Jenkins Website I worked out it follows this principle.

http{s}//{your Jenkins URL}/api/{xml/json}

If you leave off the xml/json parameter you will get a help page.

I my case I wanted the result in XML as VB.net has good xml integration. So my URL is:

http://mydomain.com:8080/api/xml

This will return something similar to the following XML code:

<hudson>
  <assignedLabel/>
  <mode>NORMAL</mode>
    <nodeDescription>the master Jenkins node</nodeDescription>
  <nodeName/>
  <numExecutors>2</numExecutors>
  <job>
    <name>ProjectBuild</name>
    <url>http://myhost.com:8080/job/ProjectBuild/</url>
    <color>blue</color>
  </job>
  <job>
    <name>IntegrationTests</name>
    <url>http://myhost.com:8080/job/IntegrationTests/</url>
    <color>red</color>
  </job>
  <overallLoad/>
  <primaryView>
    <name>All</name>
    <url>http://myhost.com:8080/</url>
  </primaryView>
  <quietingDown>false</quietingDown>
  <slaveAgentPort>0</slaveAgentPort>
  <unlabeledLoad/>
  <useCrumbs>false</useCrumbs>
  <useSecurity>true</useSecurity>
  <view>
    <name>All</name>
    <url>http://myhost.com:8080/</url>
  </view>
  <view>
    <name>Status</name>
    <url>http://myhost.com:8080/view/Status/</url>
  </view>
</hudson>

We can see that the job information is under the job element so the following code goes into the controller action. This code can be easily modified to work in a non ASP.net MVC environment.

        (...)
        Dim jenkinsURI As New Uri("http://myhost.com:8080/api/xml")
        Dim request As HttpWebRequest
        Dim xmlJobList As XElement
        model.failedJobs = New List(Of JenkinsJob)
        Try
            request = WebRequest.Create(jenkinsURI)
            Dim res As HttpWebResponse = request.GetResponse
            Dim reader As New StreamReader(res.GetResponseStream)
            xmlJobList = XElement.Parse(reader.ReadToEnd)
            reader.Close()
            For Each job In xmlJobList...<job>
                Dim c As String = job.<color>.Value
                If c = "red" Then
                    Dim jj As New JenkinsJob
                    jj.Name = job.<name>.Value
                    jj.URL = job.<url>.Value
                    jj.failed = True
                    model.failedJobs.Add(jj)
                End If
            Next
        Catch ex As Exception
            ' ignore for now
        End Try

        Return View(model)

It will loop through all the job nodes and if the color element is set to red then this indicates a filed job so add the name and URL to the view model to pass to the view.

This is the section of the view which will display the failed job. It uses the new bootstrap template that ships with Visual Studio 2013 and will only appear if the are any failed jobs.

    (...)
    @If Model.failedJobs.Count > 0 Then
        @<div class="col-md-3">
            <h2 class="alert-danger"><a href="http://myhost.com:8080/view/Status/" target="_blank" title="http://myhost.com:8080/view/Status/">Failed Jenkins Jobs</a></h2>
            <table cellpadding="2">
                <tr>
                    <th>Job Name</th>
                </tr>
                @For Each job In Model.failedJobs
                    @<tr>
                        <td><a href="@job.URL" title="@job.URL">@job.Name</a></td>
                    </tr>
                Next
            </table>
        </div>
    End If
    (...)

2013/08/09

2012/11/19

Hit a 404 error with a dot in an MVC 4 URL

Filed under: .NET, ASP.NET MVC — Tags: , , , , , — vbmagic @ 6:13 pm

Another quick post regarding passing a parameter that contains a “.” in it.

For example the below URL

http://myhost/home/edituser/me@mail.com

Started to cause a 404 error. I wasn’t sure why this was happening and after a lot of web surfing I came across the following information on Stack Overflow

Adding the following line into the HTTP <handlers> section for the path that contains the error:

<system.webServer>
    ....
    <handlers>
      ....
      <add name="ApiURIs-ISAPI-Integrated-4.0" path="/home/edituser/*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>

Will fix the issue.

Blog at WordPress.com.