-
Notifications
You must be signed in to change notification settings - Fork 577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add task notify-matrix #1171
Draft
sherine-k
wants to merge
1
commit into
tektoncd:main
Choose a base branch
from
sherine-k:matrix-notify
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add task notify-matrix #1171
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# `notify-matrix` | ||
|
||
The `notify-marix` `Task` sends messages to a [Matrix](https://matrix.org/) room on its corresponding endpoint. | ||
|
||
## Parameters | ||
|
||
This `Task` has the following required inputs: | ||
|
||
1. **`matrix-secret`**: the name of a secret, containing a valid matrix access token (see below) | ||
2. **`room`**: the matrix roomID where the notification will be sent, in the format `#ROOM_NAME:SERVER_NAME` | ||
3. **`endpoint`**: URI of the matrix server to connect and send the message from | ||
4. **`message`**: the message to be sent | ||
|
||
## Setting up the `matrix-secret` | ||
|
||
In order for the task to be able to send a message to the selected matrix room, make sure you create a secret, of type generic. It should contain a key `token`, containing the access token to the matrix endpoint. | ||
|
||
Ex: | ||
```yaml | ||
kind: Secret | ||
apiVersion: v1 | ||
metadata: | ||
name: matrix-access-token | ||
stringData: | ||
token: {OAuth token for the user/bot with access to the room} | ||
``` | ||
|
||
### Obtaining a Matrix `access_token` | ||
|
||
First, create a Matrix user with one of the Matrix servers. | ||
|
||
Once the registration process is done, start by setting the 3 following variables: | ||
* `MATRIX_USER`: username you just registered with | ||
* `PASSWORD`: corresponding password | ||
* `MATRIX_ENDPOINT`: Matrix server on which you registered the user | ||
|
||
Then, you can get the `access_token` through a simple login API call: | ||
```bash= | ||
curl -XPOST -d "{\"type\":\"m.login.password\", \"user\":\"$MATRIX_USER\", \"password\":\"$PASSWORD\"}" "https://$MATRIX_ENDPOINT/_matrix/client/r0/login" | ||
{"user_id":"@my.user:matrix.endpoint","access_token":"syt_c2hlcmluZS5raG91cnk_NFpzzGCtxFAHEDVKhYTl_123456","home_server":"matrix.endpoint","device_id":"CNYGHLSLQY","well_known":{"m.homeserver":{"base_url":"https://matrix-client.matrix.org/"}}} | ||
``` | ||
|
||
With the `access_token` in the output, you can create the secret of type generic with a single key, `token`, containing the `access_token` you just obtained above. | ||
|
||
## Platforms | ||
|
||
The Task can be run on `linux/amd64`, `linux/s390x`, `linux/386`, and `linux/ppc64le` platforms. | ||
|
||
## Usage | ||
|
||
[This TaskRun](./samples/notify-matrix-run.yaml) demonstrate usage of the notify-matrix Task. | ||
|
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,45 @@ | ||
apiVersion: tekton.dev/v1beta1 | ||
kind: Task | ||
metadata: | ||
name: notify-matrix | ||
labels: | ||
app.kubernetes.io/version: "0.1" | ||
annotations: | ||
tekton.dev/pipelines.minVersion: "0.12.1" | ||
tekton.dev/categories: Messaging | ||
tekton.dev/tags: messaging | ||
tekton.dev/platforms: "linux/386,linux/amd64,linux/s390x,linux/ppc64le" | ||
tekton.dev/displayName: "Notify Matrix Room" | ||
spec: | ||
description: >- | ||
These tasks post a simple message to a matrix room. | ||
This task uses Matrix's Client-Server REST api to send the message. | ||
params: | ||
- name: matrix-secret | ||
type: string | ||
description: secret name containing matrix access token (key is token) | ||
- name: room | ||
type: string | ||
description: room id (in the format !<ROOM_ID>:<SERVER_NAME>) | ||
- name: endpoint | ||
type: string | ||
description: Matrix server URL to which to send the message | ||
- name: message | ||
type: string | ||
description: plain text message | ||
steps: | ||
- name: post | ||
image: docker.io/curlimages/curl:7.70.0@sha256:031df77a11e5edded840bc761a845eab6e3c2edee22669fb8ad6d59484b6a1c4 #tag: 7.70.0 | ||
script: | | ||
#!/usr/bin/env bash | ||
if [[ -z "$(params.room)" || -z "$(params.endpoint)" ]]; then | ||
echo "No Matrix parameters found - no notification sent" | ||
else | ||
/usr/bin/curl -X POST -H 'Content-type: application/json' --data "{\"msgtype\":\"m.text\", \"body\":\"$(params.message)\"}" "https://$(params.endpoint)/_matrix/client/r0/rooms/$(params.room)/send/m.room.message?access_token=$TOKEN" | ||
fi | ||
env: | ||
- name: TOKEN | ||
valueFrom: | ||
secretKeyRef: | ||
name: $(params.matrix-secret) | ||
key: token |
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,6 @@ | ||
kind: Secret | ||
apiVersion: v1 | ||
metadata: | ||
name: matrix-access-token | ||
stringData: | ||
token: {OAuth token for the bot app} |
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,20 @@ | ||
apiVersion: tekton.dev/v1beta1 | ||
kind: TaskRun | ||
metadata: | ||
name: notify-matrix-run | ||
spec: | ||
params: | ||
- name: matrix-secret | ||
value: matrix-access-token | ||
- name: room | ||
value: "!yKXXPqFwfCOTipZMxp:matrix.org" | ||
- name: endpoint | ||
value: matrix.org | ||
- name: message | ||
value: hello | ||
resources: {} | ||
serviceAccountName: default | ||
taskRef: | ||
kind: Task | ||
name: notify-matrix | ||
timeout: 1h0m0s |
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,4 @@ | ||
approvers: | ||
- sherine-k | ||
reviewers: | ||
- sherine-k |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be cool to have a recipe on how to retrieve an access token - e.g. an example with a curl to execute in local shell in order to have a secret that could be added into
kind: Secret
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure! I'll update the Readme