Skip to content

Commit

Permalink
bug #39310 [Notifier] Add exception for deprecated slack dsn (maltesc…
Browse files Browse the repository at this point in the history
…hlueter)

This PR was merged into the 5.2 branch.

Discussion
----------

[Notifier] Add exception for deprecated slack dsn

| Q             | A
| ------------- | ---
| Branch?       | 5.2
| Bug fix?      | no
| New feature?  | no
| Deprecations? | yes
| Tickets       | Fix #39204
| License       | MIT
| Doc PR        | -

The DSN for the Slack integration changed again from 5.1 to 5.2. There was the idea to output an exception for that.

Commits
-------

6b56b4c [Notifier] Add exception for deprecated slack dsn
  • Loading branch information
fabpot committed Dec 8, 2020
2 parents cd3993e + 6b56b4c commit 619d543
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Slack/README.md
Expand Up @@ -3,6 +3,18 @@ Slack Notifier

Provides Slack integration for Symfony Notifier.

DSN example
-----------

```
// .env file
SLACK_DSN=slack://TOKEN@default?channel=CHANNEL
```

where:
- `TOKEN` is your Bot User OAuth Access Token
- `CHANNEL` is a Channel, private group, or IM channel to send message to. Can be an encoded ID, or a name

Resources
---------

Expand Down
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Notifier\Bridge\Slack;

use Symfony\Component\Notifier\Exception\IncompleteDsnException;
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;
Expand All @@ -28,17 +29,20 @@ final class SlackTransportFactory extends AbstractTransportFactory
*/
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
if ('slack' !== $dsn->getScheme()) {
throw new UnsupportedSchemeException($dsn, 'slack', $this->getSupportedSchemes());
}

if ('/' !== $dsn->getPath()) {
throw new IncompleteDsnException('Support for Slack webhook DSN has been dropped since 5.2 (maybe you haven\'t updated the DSN when upgrading from 5.1).');
}

$accessToken = $this->getUser($dsn);
$channel = $dsn->getOption('channel');
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

if ('slack' === $scheme) {
return (new SlackTransport($accessToken, $channel, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

throw new UnsupportedSchemeException($dsn, 'slack', $this->getSupportedSchemes());
return (new SlackTransport($accessToken, $channel, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

protected function getSupportedSchemes(): array
Expand Down
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\Dsn;

Expand All @@ -30,6 +31,15 @@ public function testCreateWithDsn(): void
$this->assertSame(sprintf('slack://%s?channel=%s', $host, $channel), (string) $transport);
}

public function testCreateWithDeprecatedDsn(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Support for Slack webhook DSN has been dropped since 5.2 (maybe you haven\'t updated the DSN when upgrading from 5.1).');

$factory = new SlackTransportFactory();
$factory->create(Dsn::fromString('slack://default/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'));
}

public function testCreateWithNoTokenThrowsMalformed(): void
{
$factory = new SlackTransportFactory();
Expand Down

0 comments on commit 619d543

Please sign in to comment.