* avatar handling changed to work around BC break jira changes

This commit is contained in:
Dávid Danyi 2018-04-17 11:27:16 +02:00
parent 8f005718e0
commit f46056837e
6 changed files with 51 additions and 94 deletions

View File

@ -77,6 +77,7 @@ return [
'customfield_12500', 'customfield_12500',
], ],
], ],
'url.jiraAvatar' => 'https://jirapducc.mo.ca.am.ericsson.se/secure/useravatar?ownerId=%s',
'url.mhWebPraGoals' => 'https://mhweb.ericsson.se:443/SearchWeb/faces/search/query/resultPage.xhtml?&v=3&queryKey=74023&output=CSV&csvDelimiter=COMMA', 'url.mhWebPraGoals' => 'https://mhweb.ericsson.se:443/SearchWeb/faces/search/query/resultPage.xhtml?&v=3&queryKey=74023&output=CSV&csvDelimiter=COMMA',
'url.mhWebTrProgress' => 'https://mhweb.ericsson.se:443/SearchWeb/faces/search/query/resultPage.xhtml?&v=3&queryKey=68655&output=CSV&csvDelimiter=COMMA', 'url.mhWebTrProgress' => 'https://mhweb.ericsson.se:443/SearchWeb/faces/search/query/resultPage.xhtml?&v=3&queryKey=68655&output=CSV&csvDelimiter=COMMA',
'url.mhWebTrEdit' => 'https://mhweb.ericsson.se/TREditWeb/faces/tredit/tredit.xhtml?eriref=%s', 'url.mhWebTrEdit' => 'https://mhweb.ericsson.se/TREditWeb/faces/tredit/tredit.xhtml?eriref=%s',

View File

@ -18,15 +18,20 @@ class AvatarAction implements ServerMiddlewareInterface
$this->avatarService = $avatarService; $this->avatarService = $avatarService;
} }
/**
* @param ServerRequestInterface $request
* @param DelegateInterface $delegate
* @return \Psr\Http\Message\ResponseInterface|EmptyResponse|static
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate) public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{ {
$signum = $request->getAttribute('signum', false); $signum = $request->getAttribute('signum', false);
if(!$signum) { try {
return new EmptyResponse(); $avatarImageData = $this->avatarService->getAvatarImageData($signum);
} catch (\UnexpectedValueException $e) {
return new TextResponse("Avatar not found", 404);
} }
return (new TextResponse($avatarImageData, 200, [
$avatarData = $this->avatarService->getAvatarData($signum);
return (new TextResponse($avatarData, 200, [
'content-type' => 'image/png', 'content-type' => 'image/png',
]))->withHeader('Expires', '0') ]))->withHeader('Expires', '0')
->withHeader('Cache-Control', 'must-revalidate'); ->withHeader('Cache-Control', 'must-revalidate');

View File

@ -11,95 +11,54 @@ use Zend\Http\Client;
class AvatarService class AvatarService
{ {
/** /** @var Config */
* @var Config
*/
private $config; private $config;
/** /** @var Client */
* @var Client
*/
private $httpClient; private $httpClient;
/** /** @var StorageInterface */
* @var RouterInterface
*/
private $router;
/**
* @var StorageInterface
*/
private $cache; private $cache;
/** /**
* JiraClientService constructor. * JiraClientService constructor.
* @param Client $client * @param Client $client
* @param Config $config * @param Config $config
* @param RouterInterface $router
* @param StorageInterface $cache * @param StorageInterface $cache
*/ */
public function __construct(Client $client, Config $config, RouterInterface $router, StorageInterface $cache) public function __construct(Client $client, Config $config, StorageInterface $cache)
{ {
$this->httpClient = $client; $this->httpClient = $client;
$this->config = $config; $this->config = $config;
$this->router = $router;
$this->cache = $cache; $this->cache = $cache;
} }
/**
* @param string $jiraAvatarUrl
* @param string $signum
* @return string
*/
public function getJiraAvatarUrl(string $jiraAvatarUrl, string $signum): string
{
if (!$this->cache->hasItem($signum)) {
$user = $this->config->get('jira.user');
$password = $this->config->get('jira.password');
$response = $this->httpClient
->setAuth($user, $password)
->setUri($jiraAvatarUrl)
->send();
if (!$response->isSuccess()) {
throw new \UnexpectedValueException("Bad JIRA result", $response->getStatusCode());
}
$this->cache->setItem($signum, $response->getBody());
}
return $this->router->generateUri('user.avatar', [
'signum' => $signum,
]);
}
/** /**
* Returns avatar image data as string * Returns avatar image data as string
* *
* @param string $signum * @param string $signum
* @return string * @return string
*/ */
public function getAvatarData(string $signum): string public function getAvatarImageData(string $signum): string
{ {
if (!$this->cache->hasItem($signum)) { if (!$this->cache->hasItem($signum)) {
throw new \UnexpectedValueException("Missing avatar", 404); $user = $this->config->get('jira.user');
$password = $this->config->get('jira.password');
$jiraAvatarUrl = $this->config->get('url.jiraAvatar');
$response = $this->httpClient
->setAuth($user, $password)
->setUri(sprintf($jiraAvatarUrl, $signum))
->send();
if (!$response->isSuccess()) {
throw new \UnexpectedValueException("Missing avatar", 404);
}
$this->cache->setItem($signum, $response->getBody());
} }
return $this->cache->getItem($signum); return $this->cache->getItem($signum);
} }
public function getUserAvatarResponse(string $signum)
{
$localAvatarFile = "public/avatars/$signum";
if(file_exists($localAvatarFile)) {
$fp = fopen($localAvatarFile,"r+");
$response = new Response($fp);
} else {
$response = new TextResponse($this->getAvatarData($signum));
}
return $response
->withHeader('Content-type', 'image/png');
}
} }

View File

@ -4,18 +4,16 @@ namespace App\Service;
use Interop\Container\ContainerInterface; use Interop\Container\ContainerInterface;
use Zend\Config\Config; use Zend\Config\Config;
use Zend\Expressive\Router\RouterInterface;
use Zend\Http\Client; use Zend\Http\Client;
class AvatarServiceFactory class AvatarServiceFactory
{ {
public function __invoke(ContainerInterface $container) public function __invoke(ContainerInterface $container)
{ {
$router = $container->get(RouterInterface::class);
$httpClient = $container->get(Client::class); $httpClient = $container->get(Client::class);
$cache = $container->get('service.cache'); $cache = $container->get('service.cache');
$configArray = $container->get('config'); $configArray = $container->get('config');
$config = new Config($configArray['app.config']); $config = new Config($configArray['app.config']);
return new AvatarService($httpClient, $config, $router, $cache); return new AvatarService($httpClient, $config, $cache);
} }
} }

View File

@ -9,6 +9,7 @@ use App\Entity\KanbanBoard;
use App\Entity\KanbanEntry; use App\Entity\KanbanEntry;
use Zend\Cache\Storage\StorageInterface; use Zend\Cache\Storage\StorageInterface;
use Zend\Config\Config; use Zend\Config\Config;
use Zend\Expressive\Router\RouterInterface;
use Zend\Http\Client; use Zend\Http\Client;
use Zend\Json\Decoder; use Zend\Json\Decoder;
use Zend\Json\Json; use Zend\Json\Json;
@ -18,36 +19,29 @@ class JiraCollectorService
const CACHE_KEY_KANBANBOARD = 'kanbanBoard'; const CACHE_KEY_KANBANBOARD = 'kanbanBoard';
/** /** @var StorageInterface */
* @var StorageInterface
*/
private $cache; private $cache;
/**
* @var Config /** @var Config */
*/
private $config; private $config;
/** /** @var Client */
* @var Client
*/
private $httpClient; private $httpClient;
/** /** @var RouterInterface */
* @var AvatarService private $router;
*/
private $avatarService;
/** /**
* JiraClientService constructor. * JiraClientService constructor.
* @param StorageInterface $cache * @param StorageInterface $cache
* @param Client $client * @param Client $client
* @param Config $config * @param Config $config
* @param AvatarService $avatarService * @param RouterInterface $router
*/ */
public function __construct(StorageInterface $cache, Client $client, Config $config, AvatarService $avatarService) public function __construct(StorageInterface $cache, Client $client, Config $config, RouterInterface $router)
{ {
$this->cache = $cache; $this->cache = $cache;
$this->avatarService = $avatarService; $this->router = $router;
$this->httpClient = $client; $this->httpClient = $client;
$this->config = $config; $this->config = $config;
} }
@ -94,6 +88,7 @@ class JiraCollectorService
/** /**
* @param bool $forceReload * @param bool $forceReload
* @return KanbanBoard * @return KanbanBoard
* @throws \Zend\Cache\Exception\ExceptionInterface
*/ */
public function getKanbanBoard(bool $forceReload = false): KanbanBoard public function getKanbanBoard(bool $forceReload = false): KanbanBoard
{ {
@ -171,10 +166,9 @@ class JiraCollectorService
// additional assignees : customfield_10401 // additional assignees : customfield_10401
if (isset($jsonIssue['fields']['customfield_10401'])) { if (isset($jsonIssue['fields']['customfield_10401'])) {
foreach ($jsonIssue['fields']['customfield_10401'] as $assignee) { foreach ($jsonIssue['fields']['customfield_10401'] as $assignee) {
$avatarUrl = $this->avatarService->getJiraAvatarUrl( $avatarUrl = $this->router->generateUri('user.avatar', [
$assignee['avatarUrls']['48x48'], 'signum' => $assignee['key'],
$assignee['key'] ]);
);
$jiraAssignee = new JiraAssignee(); $jiraAssignee = new JiraAssignee();
$jiraAssignee->setName($assignee['displayName']) $jiraAssignee->setName($assignee['displayName'])
@ -238,10 +232,9 @@ class JiraCollectorService
// assignee // assignee
if ($jsonIssue['fields']['assignee']) { if ($jsonIssue['fields']['assignee']) {
$avatarUrl = $this->avatarService->getJiraAvatarUrl( $avatarUrl = $this->router->generateUri('user.avatar', [
$jsonIssue['fields']['assignee']['avatarUrls']['48x48'], 'signum' => $jsonIssue['fields']['assignee']['key'],
$jsonIssue['fields']['assignee']['key'] ]);
);
$jiraAssignee = new JiraAssignee(); $jiraAssignee = new JiraAssignee();
$jiraAssignee->setName($jsonIssue['fields']['assignee']['displayName']) $jiraAssignee->setName($jsonIssue['fields']['assignee']['displayName'])

View File

@ -4,6 +4,7 @@ namespace App\Service;
use Interop\Container\ContainerInterface; use Interop\Container\ContainerInterface;
use Zend\Config\Config; use Zend\Config\Config;
use Zend\Expressive\Router\RouterInterface;
use Zend\Http\Client; use Zend\Http\Client;
class JiraCollectorServiceFactory class JiraCollectorServiceFactory
@ -14,7 +15,7 @@ class JiraCollectorServiceFactory
$configArray = $container->get('config'); $configArray = $container->get('config');
$httpClient = $container->get(Client::class); $httpClient = $container->get(Client::class);
$config = new Config($configArray['app.config']); $config = new Config($configArray['app.config']);
$avatarService = $container->get(AvatarService::class); $router = $container->get(RouterInterface::class);
return new JiraCollectorService($cache,$httpClient, $config, $avatarService); return new JiraCollectorService($cache,$httpClient, $config, $router);
} }
} }