Replace custom HTTP client with Guzzle

[MAILPOET-1883]
This commit is contained in:
Jan Jakeš
2019-04-09 14:22:12 +02:00
committed by wxa
parent 9414ec0d45
commit 720ab23eca
3 changed files with 19 additions and 55 deletions

View File

@@ -1,45 +0,0 @@
<?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, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
]
];
}
$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);
}
}

View File

@@ -2,6 +2,8 @@
namespace MailPoetTasks\Release; namespace MailPoetTasks\Release;
use GuzzleHttp\Client;
class JiraController { class JiraController {
const CHANGELOG_FIELD_ID = 'customfield_10500'; const CHANGELOG_FIELD_ID = 'customfield_10500';
@@ -24,7 +26,7 @@ class JiraController {
/** @var string */ /** @var string */
private $project; private $project;
/** @var HttpClient */ /** @var Client */
private $http_client; private $http_client;
public function __construct($token, $user, $project) { public function __construct($token, $user, $project) {
@@ -37,14 +39,15 @@ class JiraController {
$jira_domain = self::JIRA_DOMAIN; $jira_domain = self::JIRA_DOMAIN;
$jira_api_version = self::JIRA_API_VERSION; $jira_api_version = self::JIRA_API_VERSION;
$base_uri = "https://$url_user:$url_token@$jira_domain/rest/api/$jira_api_version/"; $base_uri = "https://$url_user:$url_token@$jira_domain/rest/api/$jira_api_version/";
$this->http_client = new HttpClient($base_uri); $this->http_client = new Client(['base_uri' => $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->http_client->get("project/$this->project/versions"); $response = $this->http_client->get("project/$this->project/versions");
$versions = json_decode($response->getBody()->getContents(), true);
if ($version_name === null) { if ($version_name === null) {
return end($versions); return end($versions);
} }
@@ -66,7 +69,8 @@ class JiraController {
'released' => false, 'released' => false,
'project' => $this->project, 'project' => $this->project,
]; ];
return $this->http_client->post('/version', $data); $response = $this->http_client->post('version', ['json' => $data]);
return json_decode($response->getBody()->getContents(), true);
} }
function getIssuesDataForVersion($version) { function getIssuesDataForVersion($version) {
@@ -93,13 +97,14 @@ class JiraController {
if ($fields) { if ($fields) {
$params['fields'] = join(',', $fields); $params['fields'] = join(',', $fields);
} }
return $this->http_client->get('/search?' . http_build_query($params)); $response = $this->http_client->get('search', ['query' => $params]);
return json_decode($response->getBody()->getContents(), true);
} }
/** /**
* @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) {
$this->http_client->put("/issue/$key", $data); $this->http_client->put("issue/$key", ['json' => $data]);
} }
} }

View File

@@ -2,6 +2,8 @@
namespace MailPoetTasks\Release; namespace MailPoetTasks\Release;
use GuzzleHttp\Client;
class SlackNotifier { class SlackNotifier {
const PROJECT_MAILPOET = 'MAILPOET'; const PROJECT_MAILPOET = 'MAILPOET';
const PROJECT_PREMIUM = 'PREMIUM'; const PROJECT_PREMIUM = 'PREMIUM';
@@ -12,13 +14,13 @@ class SlackNotifier {
/** @var string */ /** @var string */
private $project; private $project;
/** @var HttpClient */ /** @var Client */
private $http_client; private $http_client;
public function __construct($webhook_url, $project) { public function __construct($webhook_url, $project) {
$this->webhook_url = $webhook_url; $this->webhook_url = $webhook_url;
$this->project = $project; $this->project = $project;
$this->http_client = new HttpClient(); $this->http_client = new Client();
} }
public function notify($version, $changelog, $release_id) { public function notify($version, $changelog, $release_id) {
@@ -50,8 +52,10 @@ class SlackNotifier {
$message = preg_replace(['/&/u', '/</u', '/>/u'], ['&amp;', '&lt;', '&gt;'], $message); $message = preg_replace(['/&/u', '/</u', '/>/u'], ['&amp;', '&lt;', '&gt;'], $message);
$this->http_client->post($this->webhook_url, [ $this->http_client->post($this->webhook_url, [
'text' => $message, 'json' => [
'unfurl_links' => false, 'text' => $message,
'unfurl_links' => false,
],
]); ]);
} }
} }