• Icon: New Feature New Feature
    • Resolution: Fixed
    • Icon: Major Major
    • core
    • 2.396

      Hello,

      It's not possible to create a view from the API, since the views are part of the Hudson config.xml itself, it seems that the createItem URL doesn't match the need as it looks designed to create the new config.xml for a dedicated job.

      The REST API could be some thing like:

      GET http://myserver:8080/view (All views)
      GET http://myserver:8080/view/myView (switch to Specific view - myView)
      POST http://myserver:8080/view (Create new view as defined in the posted XML)

      Can you please have a look?

      Thanks and regards,

      Julien

          [JENKINS-8927] Create a view from the XML API

          Julien Faucher created issue -
          William Zhang made changes -
          Labels New: view
          Rüdiger zu Dohna made changes -
          Link New: This issue is related to JENKINS-9264 [ JENKINS-9264 ]

          Simon Endele added a comment -

          It is possible to create a view, though it's a bit complicated:
          wget --post-data='name=ViewX&mode=hudson.model.ListView&json=

          {"name": "ViewX", "mode": "hudson.model.ListView"}

          ' http://.../jenkins/createView

          Both parameters "name" and "mode" are required twice, as POST parameter and in the json argument. If one is missing, Jenkins responds with "400 Bad Request".
          I think this URL is not meant to be used as remote REST API, but it actually can.

          I would suggest to fix the problem above, so that either POST parameters can be used or a JSON snippet can be posted.

          Simon Endele added a comment - It is possible to create a view, though it's a bit complicated: wget --post-data='name=ViewX&mode=hudson.model.ListView&json= {"name": "ViewX", "mode": "hudson.model.ListView"} ' http://.../jenkins/createView Both parameters "name" and "mode" are required twice, as POST parameter and in the json argument. If one is missing, Jenkins responds with "400 Bad Request". I think this URL is not meant to be used as remote REST API, but it actually can. I would suggest to fix the problem above, so that either POST parameters can be used or a JSON snippet can be posted.

          Nalin Makar added a comment -

          another option:

          java -jar jenkins-cli.jar -s <jenkins_url> groovy createView.groovy --username <user> --password <passwd>

          where createView.groovy contains

          hudson.model.Hudson.instance.addView(new hudson.model.ListView("ViewX"))

          Nalin Makar added a comment - another option: java -jar jenkins-cli.jar -s <jenkins_url> groovy createView.groovy --username <user> --password <passwd> where createView.groovy contains hudson.model.Hudson.instance.addView(new hudson.model.ListView("ViewX"))

          Patrik Boström added a comment - - edited

          Found this by looking through the source code:
          It is possible in Jenkins 1.509.4 to create views via the REST api

          view.xml:

           
          <?xml version="1.0" encoding="UTF-8"?>
          <hudson.model.ListView>
          <name>MyView</name>
          <filterExecutors>false</filterExecutors>
          <filterQueue>false</filterQueue>
          <properties class="hudson.model.View$PropertyList"/>
          <jobNames>
          <comparator class="hudson.util.CaseInsensitiveComparator"/>
          </jobNames>
          <jobFilters/>
          <columns>
          <hudson.views.StatusColumn/>
          <hudson.views.WeatherColumn/>
          <hudson.views.JobColumn/>
          <hudson.views.LastSuccessColumn/>
          <hudson.views.LastFailureColumn/>
          <hudson.views.LastDurationColumn/>
          <hudson.views.BuildButtonColumn/>
          </columns>
          </hudson.model.ListView>
          

          Create the view:

          curl -vvv -X POST -d @view.xml -H "Content-Type: text/xml" http://localhost:8080/jenkins/createView?name=MyView
          

          Get the configuration:

          curl http://localhost:8080/jenkins/view/MyView/config.xml
          

          Update the view:

          curl -X POST -d @view.xml -H "Content-Type: text/xml" http://localhost:8080/jenkins/view/MyView/config.xml
          

          Patrik Boström added a comment - - edited Found this by looking through the source code: It is possible in Jenkins 1.509.4 to create views via the REST api view.xml: <?xml version= "1.0" encoding= "UTF-8" ?> <hudson.model.ListView> <name> MyView </name> <filterExecutors> false </filterExecutors> <filterQueue> false </filterQueue> <properties class= "hudson.model.View$PropertyList" /> <jobNames> <comparator class= "hudson.util.CaseInsensitiveComparator" /> </jobNames> <jobFilters/> <columns> <hudson.views.StatusColumn/> <hudson.views.WeatherColumn/> <hudson.views.JobColumn/> <hudson.views.LastSuccessColumn/> <hudson.views.LastFailureColumn/> <hudson.views.LastDurationColumn/> <hudson.views.BuildButtonColumn/> </columns> </hudson.model.ListView> Create the view: curl -vvv -X POST -d @view.xml -H "Content-Type: text/xml" http://localhost:8080/jenkins/createView?name=MyView Get the configuration: curl http://localhost:8080/jenkins/view/MyView/config.xml Update the view: curl -X POST -d @view.xml -H "Content-Type: text/xml" http://localhost:8080/jenkins/view/MyView/config.xml

          Oleg Nenashev added a comment -

          @Patrik
          Should we consider the issue as "Resolved"?

          Oleg Nenashev added a comment - @Patrik Should we consider the issue as "Resolved"?

          Daniel Beck added a comment -

          Oleg: Needs to be documented on the /view/Foo/api page, similar to 'Create Job' on /api.

          Daniel Beck added a comment - Oleg: Needs to be documented on the /view/Foo/api page, similar to 'Create Job' on /api.
          Daniel Beck made changes -
          Link New: This issue is related to JENKINS-23172 [ JENKINS-23172 ]

          Lukasz Tasz added a comment -

          this solution works pretty well with few limitation:

          • nestedView plugin does not register it's own class and curl end with exception that NestedView is not resolved.
          • in case of subviews, xml needs to be extended with <parent> node, unfortunately in case of coping view a to b, parent node is not visible.

          Lukasz Tasz added a comment - this solution works pretty well with few limitation: nestedView plugin does not register it's own class and curl end with exception that NestedView is not resolved. in case of subviews, xml needs to be extended with <parent> node, unfortunately in case of coping view a to b, parent node is not visible.

            duemir Denys Digtiar
            foch Julien Faucher
            Votes:
            9 Vote for this issue
            Watchers:
            15 Start watching this issue

              Created:
              Updated:
              Resolved: