Refactor HttpClient out of JIRA controller
[MAILPOET-1885]
This commit is contained in:
45
tasks/release/HttpClient.php
Normal file
45
tasks/release/HttpClient.php
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoetTasks\Release;
|
||||||
|
|
||||||
|
class HttpClient {
|
||||||
|
/** @var string|null */
|
||||||
|
private $base_uri;
|
||||||
|
|
||||||
|
public function __construct($base_uri = null) {
|
||||||
|
$this->base_uri = $base_uri === null ? null : rtrim($base_uri, '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($url) {
|
||||||
|
return $this->request($url, 'GET');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function post($url, array $data) {
|
||||||
|
return $this->request($url, 'POST', $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function put($url, array $data) {
|
||||||
|
return $this->request($url, 'PUT', $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function request($path, $method, array $data = null) {
|
||||||
|
$url = $this->base_uri === null ? $path : ("$this->base_uri/" . ltrim($path, '/'));
|
||||||
|
$options = [];
|
||||||
|
if ($method === 'POST' || $method === 'PUT') {
|
||||||
|
$options = [
|
||||||
|
'http' => [
|
||||||
|
'method' => $method,
|
||||||
|
'header' => "Content-type: application/json\r\n",
|
||||||
|
'content' => json_encode($data),
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
$context = stream_context_create($options);
|
||||||
|
$result = file_get_contents($url, false, $context);
|
||||||
|
if ($result === false) {
|
||||||
|
$error = error_get_last();
|
||||||
|
throw new \Exception('Request error: ' . $error['message']);
|
||||||
|
}
|
||||||
|
return json_decode($result, true);
|
||||||
|
}
|
||||||
|
}
|
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace MailPoetTasks\Release;
|
namespace MailPoetTasks\Release;
|
||||||
|
|
||||||
|
require_once __DIR__ . '/HttpClient.php';
|
||||||
|
|
||||||
class Jira {
|
class Jira {
|
||||||
|
|
||||||
const CHANGELOG_FIELD_ID = 'customfield_10500';
|
const CHANGELOG_FIELD_ID = 'customfield_10500';
|
||||||
@@ -24,17 +26,27 @@ class Jira {
|
|||||||
/** @var string */
|
/** @var string */
|
||||||
private $project;
|
private $project;
|
||||||
|
|
||||||
|
/** @var HttpClient */
|
||||||
|
private $http_client;
|
||||||
|
|
||||||
public function __construct($token, $user, $project) {
|
public function __construct($token, $user, $project) {
|
||||||
$this->token = $token;
|
$this->token = $token;
|
||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
$this->project = $project;
|
$this->project = $project;
|
||||||
|
|
||||||
|
$url_user = urlencode($this->user);
|
||||||
|
$url_token = urlencode($this->token);
|
||||||
|
$jira_domain = self::JIRA_DOMAIN;
|
||||||
|
$jira_api_version = self::JIRA_API_VERSION;
|
||||||
|
$base_uri = "https://$url_user:$url_token@$jira_domain/rest/api/$jira_api_version/";
|
||||||
|
$this->http_client = new HttpClient($base_uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-api-3-project-projectIdOrKey-versions-get
|
* @see https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-api-3-project-projectIdOrKey-versions-get
|
||||||
*/
|
*/
|
||||||
function getVersion($version_name = null) {
|
function getVersion($version_name = null) {
|
||||||
$versions = $this->makeJiraRequest("project/$this->project/versions");
|
$versions = $this->http_client->get("project/$this->project/versions");
|
||||||
if ($version_name === null) {
|
if ($version_name === null) {
|
||||||
return end($versions);
|
return end($versions);
|
||||||
}
|
}
|
||||||
@@ -56,7 +68,7 @@ class Jira {
|
|||||||
'released' => false,
|
'released' => false,
|
||||||
'project' => $this->project,
|
'project' => $this->project,
|
||||||
];
|
];
|
||||||
return $this->makeJiraRequest('/version', 'POST', $data);
|
return $this->http_client->post('/version', $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getIssuesDataForVersion($version) {
|
function getIssuesDataForVersion($version) {
|
||||||
@@ -83,38 +95,13 @@ class Jira {
|
|||||||
if ($fields) {
|
if ($fields) {
|
||||||
$params['fields'] = join(',', $fields);
|
$params['fields'] = join(',', $fields);
|
||||||
}
|
}
|
||||||
return $this->makeJiraRequest("/search?" . http_build_query($params));
|
return $this->http_client->get('/search?' . http_build_query($params));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-api-3-issue-issueIdOrKey-put
|
* @see https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-api-3-issue-issueIdOrKey-put
|
||||||
*/
|
*/
|
||||||
function updateIssue($key, $data) {
|
function updateIssue($key, $data) {
|
||||||
return $this->makeJiraRequest("/issue/$key", 'PUT', $data);
|
$this->http_client->put("/issue/$key", $data);
|
||||||
}
|
|
||||||
|
|
||||||
private function makeJiraRequest($path, $method = 'GET', array $data = null) {
|
|
||||||
$url_user = urlencode($this->user);
|
|
||||||
$url_token = urlencode($this->token);
|
|
||||||
$jira_domain = self::JIRA_DOMAIN;
|
|
||||||
$jira_api_version = self::JIRA_API_VERSION;
|
|
||||||
$jira_url = "https://$url_user:$url_token@$jira_domain/rest/api/$jira_api_version/$path";
|
|
||||||
$options = [];
|
|
||||||
if ($method === 'POST' || $method === 'PUT') {
|
|
||||||
$options = [
|
|
||||||
'http' => [
|
|
||||||
'method' => $method,
|
|
||||||
'header' => "Content-type: application/json\r\n",
|
|
||||||
'content' => json_encode($data),
|
|
||||||
]
|
|
||||||
];
|
|
||||||
}
|
|
||||||
$context = stream_context_create($options);
|
|
||||||
$result = file_get_contents($jira_url, false, $context);
|
|
||||||
if ($result === false) {
|
|
||||||
$error = error_get_last();
|
|
||||||
throw new \Exception('JIRA request error: ' . $error['message']);
|
|
||||||
}
|
|
||||||
return json_decode($result, true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user