/** * Job constructor. Loads metadata about a project. * @param Connection $connection * @param string $projectPath This parameter specifies the location of the Jenkins Job. * For example, if your Job is defined in the following third level project: Base->Build->Example, * you must pass in the full path to the project, 'job/Base/job/Build/job/Example'. */ public function __construct(Connection $connection, $projectPath) { if (!is_string($projectPath)) { throw new \InvalidArgumentException('The projectPath must be of type string'); } $this->connection = $connection; $this->logger = Log4PHP::getLogger(__CLASS__); if (!Strings::startsWith($projectPath, '/')) { $projectPath = "/{$projectPath}"; } if (Strings::endsWith($projectPath, '/')) { $projectPath = substr($projectPath, 0, strlen($projectPath) - 1); } $projects = explode('/job/', $projectPath); unset($projects[0]); $this->baseApiPath = '/'; foreach ($projects as $project) { $projectEncoded = rawurlencode($project); $this->baseApiPath .= "job/{$projectEncoded}/"; } $this->metadata = $this->getJson(array()); if (!isset($this->metadata['buildable'])) { throw new \InvalidArgumentException("The project at path '{$this->baseApiPath}'' is disabled"); } $this->setDefaultParameters(); }
/** * Curl Jenkins JSON API * * NOTE: This method is not meant to be used on its own. It is used by other classes, * e.g. \Bart\Jenkins\Job, to make API calls against Jenkins. * * @param string $apiPath The full API path to curl against. For example, to do a * simple GET against the Jenkins Job 'Example', the full path, 'job/Example/api/json' * must be passed in. * @param array $postData if null, then curl uses GET, otherwise POSTs data * @return array JSON data decoded as PHP array * @throws JenkinsApiException */ public function curlJenkinsApi($apiPath, array $postData = null) { if (!Strings::startsWith($apiPath, '/')) { $apiPath = "/{$apiPath}"; } $fullUrl = "{$this->baseUrl}{$apiPath}"; $isPost = $postData !== null; $this->logger->debug('Curling ' . ($isPost ? 'POST ' : 'GET ') . $fullUrl); /** @var \Bart\Curl $curl */ $curl = Diesel::create('\\Bart\\Curl', $fullUrl, $this->port); if ($this->curlOptions !== []) { $curl->setPhpCurlOpts($this->curlOptions); } $response = $isPost ? $curl->post('', [], $postData) : $curl->get('', []); $httpCode = $response['info']['http_code']; $content = $response['content']; if ($httpCode !== 200 && $httpCode !== 201 && $httpCode !== 202) { throw new JenkinsApiException("The Jenkins API call returned a {$httpCode}, " . "with the following content: {$content}"); } return JSON::decode($content); }
/** * @dataProvider dataProviderTestSummarizeWithSuffix */ public function testSummarizeWithSuffix($subject, $maxLength, $suffix, $expected) { $this->assertEquals($expected, Strings::summarize($subject, $maxLength, $suffix)); }