-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
318 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Aeliot\TodoRegistrar\Service\Registrar\JIRA; | ||
|
||
final class IssueConfig | ||
{ | ||
private bool $addTagToLabels; | ||
/** | ||
* @var string[] | ||
*/ | ||
private array $components; | ||
private string $issueType; | ||
/** | ||
* @var string[] | ||
*/ | ||
private array $labels; | ||
private string $projectKey; | ||
private string $tagPrefix; | ||
|
||
/** | ||
* @param array<string,mixed> $config | ||
*/ | ||
public function __construct(array $config) | ||
{ | ||
$issue = $config['issue']; | ||
$this->addTagToLabels = $issue['addTagToLabels'] ?? false; | ||
$this->components = (array) ($issue['components'] ?? []); | ||
$this->issueType = $issue['type']; | ||
$this->labels = (array) ($issue['labels'] ?? []); | ||
$this->projectKey = $config['projectKey']; | ||
$this->tagPrefix = $issue['tagPrefix'] ?? ''; | ||
} | ||
|
||
public function addTagToLabels(): bool | ||
{ | ||
return $this->addTagToLabels; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getComponents(): array | ||
{ | ||
return $this->components; | ||
} | ||
|
||
public function getIssueType(): string | ||
{ | ||
return $this->issueType; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getLabels(): array | ||
{ | ||
return $this->labels; | ||
} | ||
|
||
public function getProjectKey(): string | ||
{ | ||
return $this->projectKey; | ||
} | ||
|
||
public function getTagPrefix(): string | ||
{ | ||
return $this->tagPrefix; | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
src/Service/Registrar/JIRA/IssueServiceArrayConfigPreparer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Aeliot\TodoRegistrar\Service\Registrar\JIRA; | ||
|
||
final class IssueServiceArrayConfigPreparer | ||
{ | ||
/** | ||
* @param array<string, mixed> $config | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function prepare(array $config): array | ||
{ | ||
$serviceConfig = [ | ||
'jiraHost' => $config['host'], | ||
'useTokenBasedAuth' => $config['tokenBasedAuth'] ?? false, | ||
'jiraLogEnabled' => $config['logEnabled'] ?? false, | ||
'cookieAuthEnabled' => $config['cookieAuthEnabled'] ?? false, | ||
]; | ||
|
||
$this->addAuthOptions($serviceConfig, $config); | ||
$this->addCookieOptions($serviceConfig, $config); | ||
$this->addCurlOptions($serviceConfig, $config); | ||
$this->addLogOptions($serviceConfig, $config); | ||
$this->addProxyOptions($serviceConfig, $config); | ||
|
||
if ($config['serviceDeskId']) { | ||
$serviceConfig['serviceDeskId'] = $config['serviceDeskId']; | ||
} | ||
|
||
return $serviceConfig; | ||
} | ||
|
||
|
||
/** | ||
* @param array<string, mixed> $serviceConfig | ||
* @param array<string, mixed> $config | ||
*/ | ||
private function addAuthOptions(array &$serviceConfig, array $config): void | ||
{ | ||
if ($serviceConfig['tokenBasedAuth']) { | ||
$serviceConfig['jiraUser'] = $config['jiraUser']; | ||
$serviceConfig['jiraPassword'] = $config['jiraPassword']; | ||
} else { | ||
$serviceConfig['personalAccessToken'] = $config['personalAccessToken']; | ||
} | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $serviceConfig | ||
* @param array<string, mixed> $config | ||
*/ | ||
private function addCookieOptions(array &$serviceConfig, array $config): void | ||
{ | ||
if ($serviceConfig['cookieAuthEnabled']) { | ||
$serviceConfig['cookieFile'] = $config['cookieFile']; | ||
} | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $serviceConfig | ||
* @param array<string, mixed> $config | ||
*/ | ||
private function addCurlOptions(array &$serviceConfig, array $config): void | ||
{ | ||
$fields = [ | ||
'sslVerifyHost', | ||
'sslVerifyPeer', | ||
'sslCert', | ||
'sslCertPassword', | ||
'sslKey', | ||
'sslKeyPassword', | ||
'verbose', | ||
'userAgent', | ||
]; | ||
|
||
$options = array_intersect_key($config['curl'] ?? [], array_flip($fields)); | ||
$options = array_filter($options, static fn(mixed $x): bool => isset($x)); | ||
$keys = array_map(static fn(string $x): string => 'curlOpt' . ucfirst($x), array_keys($options)); | ||
|
||
$serviceConfig += array_combine($keys, $options); | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $serviceConfig | ||
* @param array<string, mixed> $config | ||
*/ | ||
private function addLogOptions(array &$serviceConfig, array $config): void | ||
{ | ||
if ($serviceConfig['jiraLogEnabled']) { | ||
$serviceConfig['jiraLogFile'] = $config['logFile']; | ||
$serviceConfig['jiraLogLevel'] = $config['logLevel'] ?? 'WARNING'; | ||
} | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $serviceConfig | ||
* @param array<string, mixed> $config | ||
*/ | ||
private function addProxyOptions(array &$serviceConfig, array $config): void | ||
{ | ||
if ($config['proxyEnabled'] ?? false) { | ||
$serviceConfig['proxyServer'] = $config['proxyServer']; | ||
$serviceConfig['proxyPort'] = $config['proxyPort']; | ||
$serviceConfig['proxyUser'] = $config['proxyUser']; | ||
$serviceConfig['proxyPassword'] = $config['proxyPassword']; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Aeliot\TodoRegistrar\Service\Registrar\JIRA; | ||
|
||
use Aeliot\TodoRegistrar\Dto\Comment\CommentPart; | ||
use Aeliot\TodoRegistrar\Service\Registrar\RegistrarInterface; | ||
use JiraRestApi\Issue\IssueField; | ||
use JiraRestApi\Issue\IssueService; | ||
|
||
final class JiraRegistrar implements RegistrarInterface | ||
{ | ||
public function __construct( | ||
private IssueConfig $issueConfig, | ||
private IssueService $issueService, | ||
) { | ||
} | ||
|
||
public function isRegistered(CommentPart $commentPart): bool | ||
{ | ||
$lineWithoutPrefix = substr($commentPart->getFirstLine(), $commentPart->getPrefixLength()); | ||
|
||
return preg_match('/^\\s*\\b[A-Z]+-\\d+\\b/i', $lineWithoutPrefix); | ||
} | ||
|
||
public function register(CommentPart $commentPart): void | ||
{ | ||
$issueField = $this->createIssueField($commentPart); | ||
$issue = $this->issueService->create($issueField); | ||
$commentPart->injectKey($issue->key); | ||
} | ||
|
||
private function createIssueField(CommentPart $commentPart): IssueField | ||
{ | ||
$issueField = new IssueField(); | ||
$issueField | ||
->setProjectKey($this->issueConfig->getProjectKey()) | ||
->setIssueTypeAsString($this->issueConfig->getIssueType()) | ||
->setSummary($commentPart->getFirstLine()) | ||
->setDescription($commentPart->getContent()) | ||
->addComponentsAsArray($this->issueConfig->getComponents()); | ||
|
||
$assignee = $commentPart->getTagMetadata()?->getAssignee(); | ||
if ($assignee) { | ||
$issueField->setAssigneeNameAsString($assignee); | ||
} | ||
|
||
$labels = $this->issueConfig->getLabels(); | ||
if ($this->issueConfig->addTagToLabels()) { | ||
$labels[] = strtolower(sprintf('%s%s', $this->issueConfig->getTagPrefix(), $commentPart->getTag())); | ||
} | ||
|
||
foreach ($labels as $label) { | ||
$issueField->addLabelAsString($label); | ||
} | ||
|
||
return $issueField; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Aeliot\TodoRegistrar\Service\Registrar\JIRA; | ||
|
||
use Aeliot\TodoRegistrar\Service\Registrar\RegistrarFactoryInterface; | ||
use Aeliot\TodoRegistrar\Service\Registrar\RegistrarInterface; | ||
use JiraRestApi\Configuration\ArrayConfiguration; | ||
use JiraRestApi\Issue\IssueService; | ||
|
||
final class JiraRegistrarFactory implements RegistrarFactoryInterface | ||
{ | ||
public function create(array $config): RegistrarInterface | ||
{ | ||
return new JiraRegistrar( | ||
new IssueConfig($config), | ||
$this->createIssueService($config['service']), | ||
); | ||
} | ||
|
||
private function createIssueService(array $config): IssueService | ||
{ | ||
$serviceConfig = (new IssueServiceArrayConfigPreparer())->prepare($config); | ||
|
||
return new IssueService(new ArrayConfiguration($serviceConfig)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters