.. include:: /Includes.rst.txt .. _extending-plan-sources: ============ Plan sources ============ Resource planning plans hours on projects and issues from plan sources. The Planning package brings adsum's own projects where the Projects package is installed; adsum-youtrack adds YouTrack. A package for Jira, Redmine, GitLab or another tracker adds its own source. What a source does ================== A source finds and describes the targets of plans, projects and issues. It only reads; it never writes into the system it reads. A plan keeps a snapshot of key, title, link and project of its target. The source is asked when a planner picks a target, when a plan keeps its target in a change, and every night for current and future plans. A target the source no longer describes is marked as missing and keeps its plans. Declare the package =================== A source is a class in a package that requires the Planning package (see :ref:`extending-packages`): .. code-block:: json { "name": "acme/adsum-redmine", "type": "adsum-package", "require": { "web-vision/adsum-core": "^1.0", "web-vision/adsum-planning": "^1.0" } } Implement the source ==================== Implement ``WebVision\Adsum\Planning\Source\Contract\PlanSource``; the interface is tagged automatically. .. code-block:: php use Symfony\Component\Validator\Constraints as Assert; use WebVision\Adsum\Core\Shared\Domain\ValueObject\CompanyId; use WebVision\Adsum\Planning\Source\Contract\Dto\PlanTarget; use WebVision\Adsum\Planning\Source\Contract\Dto\SourceConnection; use WebVision\Adsum\Planning\Source\Contract\Dto\TargetKind; use WebVision\Adsum\Planning\Source\Contract\Exception\PlanSourceUnavailableException; use WebVision\Adsum\Planning\Source\Contract\PlanSource; final readonly class RedminePlanSource implements PlanSource { public function __construct( private RedmineClient $client, ) {} public function name(): string { return 'redmine'; } public function needsConnection(): bool { return true; } public function settings(): array { return ['baseUrl' => [new Assert\NotNull(), new Assert\Url(requireTld: false), new Assert\Length(max: 500)]]; } public function secrets(): array { return ['apiKey']; } public function search(CompanyId $companyId, ?SourceConnection $connection, TargetKind $kind, string $text, int $limit): array { $redmine = $this->instance($connection); return $kind === TargetKind::Project ? \array_map(static fn(array $project): PlanTarget => self::project($redmine, $project), $redmine->projects($text, $limit)) : \array_map(static fn(array $issue): PlanTarget => self::issue($redmine, $issue), $redmine->issues($text, $limit)); } public function describe(CompanyId $companyId, ?SourceConnection $connection, TargetKind $kind, string $key): ?PlanTarget { $redmine = $this->instance($connection); $found = $kind === TargetKind::Project ? $redmine->project($key) : $redmine->issue($key); return match (true) { $found === null => null, $kind === TargetKind::Project => self::project($redmine, $found), default => self::issue($redmine, $found), }; } private function instance(?SourceConnection $connection): RedmineInstance { if ($connection === null) { throw new PlanSourceUnavailableException('Redmine is read through a connection.', 1791863000); } return $this->client->instance((string)$connection->settings['baseUrl'], $connection->secrets['apiKey'] ?? ''); } // project() and issue() build a PlanTarget from Redmine's answer. } ``name()`` The name of the source, stored with every connection and plan. Never change it once plans use it. ``needsConnection()`` Whether the source reads through a connection. A source inside adsum needs none and gets ``null``. For an external system, administrators add any number of connections, such as the company's own instance and a customer's. ``settings()`` and ``secrets()`` The fields of a connection. Each setting has Symfony validator constraints; the API refuses a connection that breaks them. Secrets, such as an API key, are stored encrypted and never returned by the API; the source gets them decrypted in ``SourceConnection::$secrets``. ``search()`` Up to ``$limit`` targets of the kind whose key or title contains the text, ignoring case. An empty text lists the first targets. ``describe()`` The target with the key, or ``null`` when the source does not know it. Use a key that never changes, such as an issue's readable ID. A ``PlanTarget`` has the kind (``TargetKind::Project`` or ``TargetKind::Issue``), the key, the title, a browser link, and for an issue its project as a ``PlanTarget``. Return the project of an issue whenever known; accounts assign targets to customers by the project key. When the system cannot be reached or refuses the credentials, throw ``PlanSourceUnavailableException``; the API answers 503 and the nightly refresh keeps the snapshots. Send a ``User-Agent`` and use short timeouts; ``search()`` runs while a planner types. Check a connection ================== A source can also implement ``ConnectionCheck``. Its ``check()`` runs before a connection is saved, with the secrets in plain text (a secret left empty on change is the stored one). .. code-block:: php public function check(SourceConnection $connection): void { if (!$this->client->answersAt((string)$connection->settings['baseUrl'])) { throw new InvalidConnectionException('No Redmine answers at the base URL.', 1791863001, 'baseUrl'); } } ``InvalidConnectionException`` names the wrong setting or secret; the API answers 422 with a violation of that field and saves nothing. Show an image of a target ========================= A source with images of its targets, such as project icons, also implements ``PlanTargetImages``. Its ``image()`` returns the image bytes of a target (for an issue, of its project) or ``null``: .. code-block:: php public function image(CompanyId $companyId, ?SourceConnection $connection, TargetKind $kind, string $key): ?string { $redmine = $this->instance($connection); $project = $kind === TargetKind::Project ? $key : $redmine->issue($key)['project'] ?? null; return $project === null ? null : $redmine->projectIcon($project, PlanTargetImages::MAX_BYTES); } Planning caches the image for a day per connection and target and serves it from its own address. Only PNG, JPEG, GIF and WebP of at most ``MAX_BYTES`` (256 KB) are used; stop reading an image beyond the limit. Throw ``PlanSourceUnavailableException`` when the system cannot be reached; the target is then shown without image. The bars of the target in the grid take the main colour of the image. Labels of the source ==================== The screens name the source and the fields of its connections with translation keys of the package (see :ref:`extending-translations-icons`): - ``screen.planning.source.``, such as ``Redmine``, - ``screen.planning.connection..`` for every setting and secret, such as ``Base URL`` and ``API key``. Without them the screens show the raw names. Test the source =============== The test of a source extends the contract test ``PlanSourceContract`` (see :ref:`extending-contracts`) with a project and an issue the source knows. A source with a check also extends ``ConnectionCheckContract`` with an accepted and a refused connection, a source with images ``PlanTargetImagesContract`` with a project that has an image and an issue of it: .. code-block:: php final class RedminePlanSourceTest extends PlanSourceContract { protected function source(): PlanSource { $source = static::getContainer()->get(RedminePlanSource::class); $this->assertInstanceOf(RedminePlanSource::class, $source); return $source; } protected function givenTargets(): ?SourceConnection { return new SourceConnection('0192a3c4-0000-7000-8000-00000000c001', ['baseUrl' => 'https://redmine.test'], ['apiKey' => 'test']); } protected function knownProject(): PlanTarget { return new PlanTarget(TargetKind::Project, 'shop', 'Web shop', 'https://redmine.test/projects/shop'); } protected function knownIssue(): PlanTarget { return new PlanTarget(TargetKind::Issue, '4711', 'Checkout', 'https://redmine.test/issues/4711', $this->knownProject()); } } Use recorded answers instead of a real instance: register a ``MockHttpClient`` for the package's HTTP client in its ``config/services_test.yaml``.