# ArgoCD Notification Subscription

## Definitions

- templates: used to define subscription service and messages, see https://argocd-notifications.readthedocs.io/en/stable/templates/
- triggers: ArgoCD events and conditions to trigger notifications, see https://argocd-notifications.readthedocs.io/en/stable/triggers/
- subscriptions: bind notification service to target events, see https://argocd-notifications.readthedocs.io/en/stable/subscriptions/

## Getting Started

### Subscribe to ArgoCD Events by Feishu group

0. Create your branch and follow bellow modification.
1. Add _user-defined robot/自定义机器人_ to target Feishu group and record the webhook url of the robot.
   <img width="1170" alt="image" src="https://github.com/tidbcloud/app-delivery/assets/11363886/b62da7fb-43f5-4aea-89a1-0c3dc0599b75">

2. Define your unique webhook name and add `service.webhook.<unique name>` file under this directory.(like `service.webhook.feishu-demo`)
3. Save `url: <your Feishu robot webhook url>` in this file.
   <img width="1284" alt="image" src="https://github.com/tidbcloud/app-delivery/assets/11363886/a36a03d2-c223-4065-9f56-be0a5fbd1c9f">

4. Add your webhook under target template files whose name is correspond to the trigger, like ⬇️
   <img width="1295" alt="image" src="https://github.com/tidbcloud/app-delivery/assets/11363886/379dc157-1e93-4f7c-84b9-ea6c5622f817">

```yaml
webhook:
  <unique name>: # Replace this key with your unique webhook name, like feishu-demo
    method: POST
    # You can just custom the message as you like or simply copy from webhook.feishu-demo
    body: |
      {"msg_type":"text","content":{"text":"Application {{.app.metadata.name}} has been successfully synced."}}
```

5. Add `- service.webhook.<unique name>` to `configMapGenerator.files` in `kustomization.yaml` in this directory, like ⬇️

```yaml
configMapGenerator:
  - files:
      - service.webhook.feishu-demo
```

6. Add annotation `notifications.argoproj.io/subscribe.<trigger>.<unique name>: ""` to your Application/ApplicationSet files in `../../app-registry`, like ⬇️
   You can get the trigger name(<trigger>) in file `./kustomization.yaml` or by the file whose name starts with `trigger.`

<img width="1269" alt="image" src="https://github.com/tidbcloud/app-delivery/assets/11363886/bf4297aa-a6c5-4446-a45d-9b9ddd5a3ec9">

```yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: ...
spec:
  template:
    metadata:
      annotations:
        notifications.argoproj.io/subscribe.on-sync-succeeded.feishu-demo: ""
```

7. Create/Merge PR to default branch and trigger github action [Update subscription to ArgoCD Events · Workflow runs · tidbcloud/app\-delivery](https://github.com/tidbcloud/app-delivery/actions/workflows/sub-notification.yaml) in this repo.

## Advanced Settings

### How to disable certain applications (in a ApplicationSet)

1. Add `notifications.argocd.tidbcloud/subscribe.<trigger>.disabled: "{{notification_disabled}}"` to `.spec.template.metadata.annotations`. You can modify the key(`notification_disabled`) as you like.
2. Add `notification_disabled: "true"` to the envs you'd like to disable notifications in `.spec.generators.[@]list.elements`.
   Don't forget the **""** around `true` or you'll get parsing error!!

You can modify the switch-key name(`notification_disabled`) as you like.
Demo ⬇️

```yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: xxx
spec:
  generators:
    - list:
        elements:
          - env: dev
            notification_disabled: "true"
          - env: stg
...
  template:
    metadata:
      name: "{{env}}-xxx"
      annotations:
        notifications.argoproj.io/subscribe.on-deployed.infra-provider: ""
        notifications.argocd.tidbcloud/subscribe.on-deployed.disabled: "{{notification_disabled}}"
...
```

> TODO(if needed):

> How to add/customize triggers.

> How to subscribe by other services like email.

> How to subscribe to all applications.

> How to subscribe to sub application of applicationset.
