Skip to content

Commit 8797138

Browse files
bug symfony#39545 [Notifier] [Mattermost] Host is required (OskarStark)
This PR was squashed before being merged into the 5.1 branch. Discussion ---------- [Notifier] [Mattermost] Host is required | Q | A | ------------- | --- | Branch? | 5.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | --- | License | MIT | Doc PR | --- This bridge is the only one right now which cannot use `default` as host in the DSN, otherwise it would fall back to: https://github.com/symfony/symfony/blob/090b4256f0032e1309d086b873743b330fd21df1/src/Symfony/Component/Notifier/Transport/AbstractTransport.php#L30 it could also not use: https://github.com/symfony/symfony/blob/090b4256f0032e1309d086b873743b330fd21df1/src/Symfony/Component/Notifier/Transport/AbstractTransport.php#L83-L86 Based on the [documentation](https://api.mattermost.com/#tag/authentication) you must use your specific url like: `your-mattermost-url.com/api/v4/...` Using `localhost` would have weird side-effects. Can you confirm this @thePanz , as you provided the bridge? friendly ping @seb37800, you fixed some bugs in this transport ### Todos after merge * [ ] adjust recipes with new DSN * [ ] update the docs Commits ------- cd5b480 [Notifier] [Mattermost] Host is required
2 parents 090b425 + cd5b480 commit 8797138

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransport.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ final class MattermostTransport extends AbstractTransport
2828
{
2929
private $token;
3030
private $channel;
31+
private $path;
3132

32-
public function __construct(string $token, string $channel, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
33+
public function __construct(string $token, string $channel, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, string $path = null)
3334
{
3435
$this->token = $token;
3536
$this->channel = $channel;
37+
$this->path = $path;
3638

3739
parent::__construct($client, $dispatcher);
3840
}
@@ -56,14 +58,15 @@ protected function doSend(MessageInterface $message): void
5658
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, get_debug_type($message)));
5759
}
5860

59-
$endpoint = sprintf('https://%s/api/v4/posts', $this->getEndpoint());
60-
6161
$options = ($opts = $message->getOptions()) ? $opts->toArray() : [];
6262
$options['message'] = $message->getSubject();
6363

6464
if (!isset($options['channel_id'])) {
6565
$options['channel_id'] = $message->getRecipientId() ?: $this->channel;
6666
}
67+
68+
$endpoint = sprintf('https://%s/api/v4/posts', $this->getEndpoint());
69+
6770
$response = $this->client->request('POST', $endpoint, [
6871
'auth_bearer' => $this->token,
6972
'json' => array_filter($options),
@@ -75,4 +78,9 @@ protected function doSend(MessageInterface $message): void
7578
throw new TransportException(sprintf('Unable to post the Mattermost message: %s (%s).', $result['message'], $result['id']), $response);
7679
}
7780
}
81+
82+
protected function getEndpoint(): ?string
83+
{
84+
return rtrim($this->host.($this->port ? ':'.$this->port : '').($this->path ?? ''), '/');
85+
}
7886
}

src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransportFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function create(Dsn $dsn): TransportInterface
3232
throw new UnsupportedSchemeException($dsn, 'mattermost', $this->getSupportedSchemes());
3333
}
3434

35+
$path = $dsn->getPath();
3536
$token = $this->getUser($dsn);
3637
$channel = $dsn->getOption('channel');
3738

@@ -42,7 +43,7 @@ public function create(Dsn $dsn): TransportInterface
4243
$host = $dsn->getHost();
4344
$port = $dsn->getPort();
4445

45-
return (new MattermostTransport($token, $channel, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
46+
return (new MattermostTransport($token, $channel, $this->client, $this->dispatcher, $path))->setHost($host)->setPort($port);
4647
}
4748

4849
protected function getSupportedSchemes(): array

src/Symfony/Component/Notifier/Bridge/Mattermost/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ DSN example
77
-----------
88

99
```
10-
MATTERMOST_DSN=mattermost://ACCESS_TOKEN@default?channel=CHANNEL
10+
MATTERMOST_DSN=mattermost://ACCESS_TOKEN@HOST/PATH?channel=CHANNEL
1111
```
1212

1313
where:
1414
- `ACCESS_TOKEN` is your Mattermost access token
15+
- `HOST` is your Mattermost host
16+
- `PATH` is your Mattermost sub-path (optional)
1517
- `CHANNEL` is your Mattermost channel
1618

1719
Resources

src/Symfony/Component/Notifier/Bridge/Mattermost/Tests/MattermostTransportFactoryTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ public function testCreateWithDsn()
3131
$this->assertSame('mattermost://host.test?channel=testChannel', (string) $transport);
3232
}
3333

34+
public function testCreateWithDsnHostWithSubfolder()
35+
{
36+
$factory = $this->createFactory();
37+
38+
$transport = $factory->create(Dsn::fromString('mattermost://[email protected]/sub?channel=testChannel'));
39+
40+
$this->assertSame('mattermost://example.com/sub?channel=testChannel', (string) $transport);
41+
}
42+
43+
public function testCreateWithDsnHostWithSubfolderWithTrailingSlash()
44+
{
45+
$factory = $this->createFactory();
46+
47+
$transport = $factory->create(Dsn::fromString('mattermost://[email protected]/sub/?channel=testChannel'));
48+
49+
$this->assertSame('mattermost://example.com/sub?channel=testChannel', (string) $transport);
50+
}
51+
3452
public function testCreateWithMissingOptionChannelThrowsIncompleteDsnException()
3553
{
3654
$factory = $this->createFactory();

src/Symfony/Component/Notifier/Bridge/Mattermost/Tests/MattermostTransportTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Notifier\Exception\LogicException;
1717
use Symfony\Component\Notifier\Message\ChatMessage;
1818
use Symfony\Component\Notifier\Message\MessageInterface;
19+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
1920
use Symfony\Contracts\HttpClient\HttpClientInterface;
2021

2122
/**

0 commit comments

Comments
 (0)