public function testUpdatingQueueItem()
 {
     $data = $this->getTestData(__FUNCTION__);
     $this->mockServer->queueResponse($data['firstCall']);
     $this->mockServer->queueResponse($data['secondCall']);
     $qi = QueueItem::factory($this->jenkins, new JsonData(array('id' => "55")));
     $qi->update();
     $this->assertEquals(2, count($qi->actions));
     $this->assertEquals("Bob the Builder", $qi->actions[1]['causes'][0]['userName']);
     $this->assertTrue($qi->blocked);
     $this->assertTrue($qi->buildable);
     $this->assertFalse($qi->cancelled);
     $this->assertEquals(55, $qi->id);
     $this->assertEquals(1402689326588, $qi->inQueueSince);
     $this->assertEquals(array('branch' => "next_release", 'build_target' => "build"), $qi->params);
     $this->assertTrue($qi->stuck);
     $this->assertInstanceOf('mogman1\\Jenkins\\ApiObject\\Job', $qi->task);
     $this->assertEquals("jenkins-web-api", $qi->task->name);
     $this->assertEquals(1402689331588, $qi->timestamp);
     $this->assertEquals("In the quiet period. Expires in 4.7 sec", $qi->why);
     $this->assertEquals("/queue/item/55", $qi->url);
     $qi->update();
     $this->assertEquals(2, count($qi->actions));
     $this->assertEquals("Bob the Builder", $qi->actions[1]['causes'][0]['userName']);
     $this->assertFalse($qi->blocked);
     $this->assertFalse($qi->buildable);
     $this->assertTrue($qi->cancelled);
     $this->assertEquals(55, $qi->id);
     $this->assertEquals(1402689326588, $qi->inQueueSince);
     $this->assertEquals(array('branch' => "next_release", 'build_target' => "build"), $qi->params);
     $this->assertFalse($qi->stuck);
     $this->assertInstanceOf('mogman1\\Jenkins\\ApiObject\\Job', $qi->task);
     $this->assertEquals("jenkins-web-api", $qi->task->name);
     $this->assertEquals(0, $qi->timestamp);
     //timestamp gets replaced with executable later on, I don't know why
     $this->assertEquals("", $qi->why);
     $this->assertEquals("/queue/item/55", $qi->url);
     $this->assertInstanceOf('mogman1\\Jenkins\\ApiObject\\Build', $qi->executable);
     $this->assertEquals("442", $qi->executable->number);
 }
Example #2
0
 /**
  * Triggers a new remote build of this job on Jenkins.  $buildParams is passed on to Jenkins, in
  * case your builds accept parameters.  Also, if your build requires authentication token to be
  * passed, make sure you include it as part of these parameters, e.g. Array('token' => "<secret>", ...)
  *
  * NOTE: Because Jenkins does not return the build number on a triggered build request, all
  * that can be returned is a QueueItem, which sits in Jenkins' build queue until it is picked
  * up to be processed.  Generally Jenkins leaves a build trigger in the queue for 5 seconds before
  * finally picking it up to process, or perhaps even longer if a build is already being processed
  * and no resources are available to pick yours up.
  *
  * You can use the QueueItem to engage in some shenanigans and poll Jenkins until your QueueItem
  * registers an $executable, which is going to be the build you want to monitor.   Alternatively,
  * you could do a refresh of this job and use $nextBuildNumber, which probably will be what your
  * build uses when it spins up, but there are no guarantees there.
  *
  * In other words, here there be dragons...
  *
  * @link https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build
  * @link https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API
  * @link https://issues.jenkins-ci.org/browse/JENKINS-12827?focusedCommentId=201381&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-201381
  *
  * @throws JenkinsConnectionException if there's an issue connecting to Jenkins
  * @param array $buildParams Array of parameters to pass on.
  * @return QueueItem
  */
 public function triggerBuild(array $buildParams = array())
 {
     try {
         $response = $this->conn->get($this->url . "/buildWithParameters", $buildParams);
     } catch (JenkinsConnectionException $e) {
         throw new JenkinsConnectionException("Error triggering job build for " . $this->name, $e->getCode(), $e);
     }
     $queueUrl = $response->getHeader("Location");
     $path = parse_url($queueUrl, PHP_URL_PATH);
     try {
         $queueItem = QueueItem::factory($this->conn, $this->conn->get($path)->getJson());
     } catch (JenkinsConnectionException $e) {
         throw new JenkinsConnectionException("Error fetching queue item for triggered job [" . $this->name . "] {$path}", $e->getCode(), $e);
     }
     return $queueItem;
 }
Example #3
0
 /**
  * Constructs a Job from data assumed to have come from a Jenkins API call
  *
  * @param Jenkins $conn
  * @param JsonData $data
  * @return \mogman1\Jenkins\ApiObject\QueueItem
  */
 public static function factory(Jenkins $conn, JsonData $data)
 {
     $id = $data->get("id", "");
     if ($id == "") {
         throw new InvalidApiObjectException("'id' is required, but not found in build data");
     }
     $item = new QueueItem($conn, $id);
     $item->updateProperties($data);
     return $item;
 }