0
0
mirror of https://github.com/thegeeklab/wp-matrix.git synced 2024-06-03 04:49:41 +02:00

Compare commits

...

32 Commits
v1.0.8 ... main

Author SHA1 Message Date
renovate[bot]
728bc0a268
chore(deps): update docker.io/library/alpine docker tag to v3.20 (#119)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-06-01 13:21:14 +02:00
renovate[bot]
4f9dee12c5 chore(deps): update dependency golangci/golangci-lint to v1.59.0 2024-05-27 03:37:17 +00:00
renovate[bot]
79f55e0b3e
fix(deps): update module github.com/rs/zerolog to v1.33.0 (#120)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-05-24 09:36:31 +02:00
renovate[bot]
7db4c14b96 chore(deps): update dependency golangci/golangci-lint to v1.58.2 2024-05-20 10:07:40 +00:00
renovate[bot]
b5980344c7 chore(docker): update docker.io/library/golang:1.22 docker digest to f43c6f0 2024-05-18 06:44:59 +00:00
76f5076b0a
chore: migrate to wp-plugin-go v3 (#116) 2024-05-17 21:50:20 +02:00
renovate[bot]
dc41f2d9ff
chore(docker): update docker.io/library/golang:1.22 docker digest to 91ad6f9 (#114)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-05-16 13:40:32 +02:00
renovate[bot]
9695526a8d chore(docker): update docker.io/library/golang:1.22 docker digest to ab48cd7 2024-05-15 20:52:20 +00:00
renovate[bot]
2f805d6fca chore(deps): update dependency golangci/golangci-lint to v1.58.1 2024-05-13 04:22:20 +00:00
a534efd107
ci: fix golangci-lint deprecations 2024-05-12 11:08:26 +02:00
3c3fb393b8
refactor: use dedicated matrix package (#111) 2024-05-11 10:25:15 +02:00
117f681b10
refactor: switch to plugin Cmd and add tests (#110) 2024-05-10 08:42:59 +02:00
renovate[bot]
4aa20c5f0e chore(docker): update docker.io/library/golang:1.22 docker digest to b1e05e2 2024-05-08 03:12:06 +00:00
renovate[bot]
1b9e7caee0 chore(deps): update dependency golangci/golangci-lint to v1.58.0 2024-05-06 04:14:06 +00:00
renovate[bot]
312b75d247
fix(deps): update module github.com/urfave/cli/v2 to v2.27.2 (#105)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-04-29 10:27:20 +02:00
renovate[bot]
350694d2e3 chore(docker): update docker.io/library/golang:1.22 docker digest to d5302d4 2024-04-25 03:01:24 +00:00
renovate[bot]
a7bb53a553
fix(deps): update module github.com/thegeeklab/wp-plugin-go to v1.7.1 (#103)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-04-22 12:38:46 +02:00
renovate[bot]
9fb64d43c3
fix(deps): update module maunium.net/go/mautrix to v0.18.1 (#102)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-04-17 08:45:27 +02:00
renovate[bot]
0ce44c0fc7 chore(docker): update docker.io/library/golang:1.22 docker digest to 450e382 2024-04-11 08:02:02 +00:00
renovate[bot]
7d9cd7b14c chore(docker): update docker.io/library/golang:1.22 docker digest to c4fb952 2024-04-04 05:02:33 +00:00
renovate[bot]
f92be16c5c chore(deps): update dependency golangci/golangci-lint to v1.57.2 2024-04-01 04:51:28 +00:00
renovate[bot]
a7f5a30e4a
chore(deps): update quay.io/thegeeklab/wp-docker-buildx docker tag to v4 (#98)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-03-27 08:55:46 +01:00
renovate[bot]
9f473db3f5
fix(deps): update module github.com/thegeeklab/wp-plugin-go to v1.7.0 (#94)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-03-26 21:33:55 +01:00
renovate[bot]
60095c2d59 chore(deps): update dependency golangci/golangci-lint to v1.57.1 2024-03-25 03:21:50 +00:00
renovate[bot]
ee37017679
fix(deps): update module maunium.net/go/mautrix to v0.18.0 (#96)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-03-17 12:04:35 +01:00
renovate[bot]
9be2f1fe39 chore(docker): update docker.io/library/golang:1.22 docker digest to 0b55ab8 2024-03-13 07:53:01 +00:00
4e6b959d04
cleanup docs and examples 2024-03-12 20:43:35 +01:00
renovate[bot]
3cfde357ae
fix(deps): update module github.com/thegeeklab/wp-plugin-go to v1.6.1 (#93)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-03-07 21:26:47 +01:00
renovate[bot]
4b7c33039e chore(docker): update docker.io/library/golang:1.22 docker digest to 34ce21a 2024-03-06 03:05:27 +00:00
renovate[bot]
96d388179b chore(deps): update dependency golangci/golangci-lint to v1.56.2 2024-02-19 03:35:22 +00:00
renovate[bot]
3066c3aa38 chore(docker): update docker.io/library/golang:1.22 docker digest to 7b297d9 2024-02-16 04:45:26 +00:00
6801e73ebe
[skip ci] revert renovate automerge config 2024-02-15 12:19:27 +01:00
26 changed files with 653 additions and 357 deletions

View File

@ -23,7 +23,6 @@ linters:
- errchkjson
- errname
- errorlint
- execinquery
- exhaustive
- exportloopref
- forcetypeassert
@ -37,12 +36,12 @@ linters:
- gocyclo
- godot
- godox
- goerr113
- err113
- gofmt
- gofumpt
- goheader
- goimports
- gomnd
- mnd
- gomoddirectives
- gomodguard
- goprintffuncname

6
.mockery.yaml Normal file
View File

@ -0,0 +1,6 @@
---
all: True
dir: "{{.PackageName}}/mocks"
outpkg: "mocks"
packages:
github.com/thegeeklab/wp-matrix/matrix:

View File

@ -4,13 +4,10 @@ when:
- event: [push, manual]
branch:
- ${CI_REPO_DEFAULT_BRANCH}
- event: [push]
branch:
- renovate/auto/*
steps:
- name: dryrun
image: quay.io/thegeeklab/wp-docker-buildx:3
image: quay.io/thegeeklab/wp-docker-buildx:4
settings:
containerfile: Containerfile.multiarch
dry_run: true
@ -23,7 +20,7 @@ steps:
- event: [pull_request]
- name: publish-dockerhub
image: quay.io/thegeeklab/wp-docker-buildx:3
image: quay.io/thegeeklab/wp-docker-buildx:4
group: container
settings:
auto_tag: true
@ -44,7 +41,7 @@ steps:
- ${CI_REPO_DEFAULT_BRANCH}
- name: publish-quay
image: quay.io/thegeeklab/wp-docker-buildx:3
image: quay.io/thegeeklab/wp-docker-buildx:4
group: container
settings:
auto_tag: true

View File

@ -4,9 +4,6 @@ when:
- event: [push, manual]
branch:
- ${CI_REPO_DEFAULT_BRANCH}
- event: [push]
branch:
- renovate/auto/*
steps:
- name: build

View File

@ -4,9 +4,6 @@ when:
- event: [push, manual]
branch:
- ${CI_REPO_DEFAULT_BRANCH}
- event: [push]
branch:
- renovate/auto/*
steps:
- name: markdownlint

View File

@ -4,9 +4,6 @@ when:
- event: [push, manual]
branch:
- ${CI_REPO_DEFAULT_BRANCH}
- event: [push]
branch:
- renovate/auto/*
steps:
- name: lint

View File

@ -1,4 +1,4 @@
FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.22@sha256:ef61a20960397f4d44b0e729298bf02327ca94f1519239ddc6d91689615b1367 as build
FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.22@sha256:f43c6f049f04cbbaeb28f0aad3eea15274a7d0a7899a617d0037aec48d7ab010 as build
ARG TARGETOS
ARG TARGETARCH
@ -8,7 +8,7 @@ WORKDIR /src
RUN make build
FROM docker.io/library/alpine:3.19@sha256:c5b1261d6d3e43071626931fc004f70149baeba2c8ec672bd4f27761f8e1ad6b
FROM docker.io/library/alpine:3.20@sha256:77726ef6b57ddf65bb551896826ec38bc3e53f75cdde31354fbffb4f25238ebd
LABEL maintainer="Robert Kaussow <mail@thegeeklab.de>"
LABEL org.opencontainers.image.authors="Robert Kaussow <mail@thegeeklab.de>"

View File

@ -1,7 +1,7 @@
# renovate: datasource=github-releases depName=mvdan/gofumpt
GOFUMPT_PACKAGE_VERSION := v0.6.0
# renovate: datasource=github-releases depName=golangci/golangci-lint
GOLANGCI_LINT_PACKAGE_VERSION := v1.56.1
GOLANGCI_LINT_PACKAGE_VERSION := v1.59.0
EXECUTABLE := wp-matrix
@ -18,8 +18,8 @@ GOFUMPT_PACKAGE ?= mvdan.cc/gofumpt@$(GOFUMPT_PACKAGE_VERSION)
GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_PACKAGE_VERSION)
XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest
GOTESTSUM_PACKAGE ?= gotest.tools/gotestsum@latest
MOCKERY_PACKAGE ?= github.com/vektra/mockery/v2@latest
GENERATE ?=
XGO_VERSION := go-1.22.x
XGO_TARGETS ?= linux/amd64,linux/arm-6,linux/arm-7,linux/arm64
@ -65,11 +65,8 @@ lint: golangci-lint
.PHONY: generate
generate:
$(GO) generate $(GENERATE)
.PHONY: generate-docs
generate-docs:
$(GO) generate ./cmd/$(EXECUTABLE)/flags.go
$(GO) generate $(PACKAGES)
$(GO) run $(MOCKERY_PACKAGE)
.PHONY: test
test:

View File

@ -1,58 +0,0 @@
//go:build generate
// +build generate
package main
import (
"bytes"
"embed"
"fmt"
"os"
"text/template"
"github.com/thegeeklab/wp-matrix/plugin"
"github.com/thegeeklab/wp-plugin-go/docs"
wp "github.com/thegeeklab/wp-plugin-go/plugin"
wp_template "github.com/thegeeklab/wp-plugin-go/template"
"github.com/urfave/cli/v2"
)
//go:embed templates/docs-data.yaml.tmpl
var yamlTemplate embed.FS
func main() {
settings := &plugin.Settings{}
app := &cli.App{
Flags: settingsFlags(settings, wp.FlagsPluginCategory),
}
out, err := toYAML(app)
if err != nil {
panic(err)
}
fi, err := os.Create("../../docs/data/data-raw.yaml")
if err != nil {
panic(err)
}
defer fi.Close()
if _, err := fi.WriteString(out); err != nil {
panic(err)
}
}
func toYAML(app *cli.App) (string, error) {
var w bytes.Buffer
yamlTmpl, err := template.New("docs").Funcs(wp_template.LoadFuncMap()).ParseFS(yamlTemplate, "templates/docs-data.yaml.tmpl")
if err != nil {
fmt.Println(yamlTmpl)
return "", err
}
if err := yamlTmpl.ExecuteTemplate(&w, "docs-data.yaml.tmpl", docs.GetTemplateData(app)); err != nil {
return "", err
}
return w.String(), nil
}

View File

@ -1,78 +0,0 @@
// Copyright (c) 2020, the Drone Plugins project authors.
// Copyright (c) 2021, Robert Kaussow <mail@thegeeklab.de>
// Use of this source code is governed by an Apache 2.0 license that can be
// found in the LICENSE file.
package main
import (
"github.com/thegeeklab/wp-matrix/plugin"
"github.com/urfave/cli/v2"
)
// settingsFlags has the cli.Flags for the plugin.Settings.
//
//go:generate go run docs.go flags.go
func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "username",
EnvVars: []string{"PLUGIN_USERNAME", "MATRIX_USERNAME"},
Usage: "authentication username",
Destination: &settings.Username,
Category: category,
},
&cli.StringFlag{
Name: "password",
EnvVars: []string{"PLUGIN_PASSWORD", "MATRIX_PASSWORD"},
Usage: "authentication password",
Destination: &settings.Password,
Category: category,
},
&cli.StringFlag{
Name: "userid",
EnvVars: []string{"PLUGIN_USER_ID", "PLUGIN_USERID", "MATRIX_USER_ID", "MATRIX_USERID"},
Usage: "authentication user ID",
Destination: &settings.UserID,
Category: category,
},
&cli.StringFlag{
Name: "accesstoken",
EnvVars: []string{"PLUGIN_ACCESS_TOKEN", "PLUGIN_ACCESSTOKEN", "MATRIX_ACCESS_TOKEN", "MATRIX_ACCESSTOKEN"},
Usage: "authentication access token",
Destination: &settings.AccessToken,
Category: category,
},
&cli.StringFlag{
Name: "homeserver",
EnvVars: []string{"PLUGIN_HOMESERVER", "MATRIX_HOMESERVER"},
Usage: "matrix home server url",
Value: "https://matrix.org",
Destination: &settings.Homeserver,
Category: category,
},
&cli.StringFlag{
Name: "roomid",
EnvVars: []string{"PLUGIN_ROOMID", "MATRIX_ROOMID"},
Usage: "roomid to send messages to",
Destination: &settings.RoomID,
Category: category,
},
&cli.StringFlag{
Name: "template",
EnvVars: []string{"PLUGIN_TEMPLATE", "MATRIX_TEMPLATE"},
Usage: "golang template for the message",
Value: plugin.DefaultMessageTemplate,
Destination: &settings.Template,
Category: category,
},
&cli.BoolFlag{
Name: "template-unsafe",
EnvVars: []string{"PLUGIN_TEMPLATE_UNSAFE", "MATRIX_TEMPLATE_UNSAFE"},
Usage: "render raw HTML and potentially dangerous links in template",
Destination: &settings.TemplateUnsafe,
Category: category,
},
}
}

View File

@ -7,11 +7,7 @@
package main
import (
"fmt"
"github.com/thegeeklab/wp-matrix/plugin"
wp "github.com/thegeeklab/wp-plugin-go/plugin"
)
//nolint:gochecknoglobals
@ -21,14 +17,5 @@ var (
)
func main() {
settings := &plugin.Settings{}
options := wp.Options{
Name: "wp-matrix",
Description: "Send messages to a Matrix room",
Version: BuildVersion,
VersionMetadata: fmt.Sprintf("date=%s", BuildDate),
Flags: settingsFlags(settings, wp.FlagsPluginCategory),
}
plugin.New(options, settings).Run()
plugin.New(nil, BuildVersion, BuildDate).Run()
}

View File

@ -1,18 +0,0 @@
---
{{- if .GlobalArgs }}
properties:
{{- range $v := .GlobalArgs }}
- name: {{ $v.Name }}
{{- with $v.Description }}
description: |
{{ . | ToSentence }}
{{- end }}
{{- with $v.Type }}
type: {{ . }}
{{- end }}
{{- with $v.Default }}
defaultValue: {{ . }}
{{- end }}
required: {{ default false $v.Required }}
{{ end -}}
{{ end -}}

View File

@ -21,17 +21,14 @@ Woodpecker CI plugin to send messages to a Matrix room.
## Usage
```YAML
kind: pipeline
name: default
steps:
- name: notify
image: quay.io/thegeeklab/matrix
settings:
homeserver: https://matrix.org
roomid: abcdefghijklmnopqrstuvwxyz:matrix.org
roomid: randomstring:matrix.org
username: octocat
password: secret
password: random-secret
```
### Parameters
@ -60,9 +57,9 @@ docker build --file Containerfile.multiarch --tag thegeeklab/wp-matrix .
```Shell
docker run --rm \
-e PLUGIN_ROOMID=0123456789abcdef:matrix.org \
-e PLUGIN_USERNAME=yourbot \
-e PLUGIN_PASSWORD=p455w0rd \
-e PLUGIN_ROOMID=randomstring:matrix.org \
-e PLUGIN_USERNAME=octocat \
-e PLUGIN_PASSWORD=random-secret \
-v $(pwd):/build:z \
-w /build \
thegeeklab/wp-matrix

View File

@ -13,6 +13,20 @@ properties:
defaultValue: "https://matrix.org"
required: false
- name: insecure_skip_verify
description: |
Skip SSL verification.
type: bool
defaultValue: false
required: false
- name: log_level
description: |
Plugin log level.
type: string
defaultValue: "info"
required: false
- name: password
description: |
Authentication password.

32
go.mod
View File

@ -4,10 +4,11 @@ go 1.22
require (
github.com/microcosm-cc/bluemonday v1.0.26
github.com/rs/zerolog v1.32.0
github.com/thegeeklab/wp-plugin-go v1.6.0
github.com/urfave/cli/v2 v2.27.1
maunium.net/go/mautrix v0.17.0
github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.9.0
github.com/thegeeklab/wp-plugin-go/v3 v3.0.2
github.com/urfave/cli/v2 v2.27.2
maunium.net/go/mautrix v0.18.1
)
require (
@ -15,7 +16,8 @@ require (
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
@ -25,20 +27,22 @@ require (
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/tidwall/gjson v1.17.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/yuin/goldmark v1.6.0 // indirect
go.mau.fi/util v0.3.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
github.com/yuin/goldmark v1.7.1 // indirect
go.mau.fi/util v0.4.2 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
maunium.net/go/maulogger/v2 v2.4.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

63
go.sum
View File

@ -8,14 +8,12 @@ github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBa
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@ -45,8 +43,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
@ -56,44 +54,46 @@ github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkU
github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/thegeeklab/wp-plugin-go v1.6.0 h1:DQ+oLs67FusgeZe3881x9pxH+UrddWygE0eGHLM7jgg=
github.com/thegeeklab/wp-plugin-go v1.6.0/go.mod h1:r+Oi0CpnnlG59u4Qp2n6xHGTxd6Krk+OPBZ9F7LQT8A=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/thegeeklab/wp-plugin-go/v3 v3.0.2 h1:Mv5i8S1WY+BUNjTjX6lOnB3p8S9mvM+XwfY4R98gx0g=
github.com/thegeeklab/wp-plugin-go/v3 v3.0.2/go.mod h1:ij1iJcAVgzerBTqXnmq0bu1VA+hhVVwzXKqiqfoGjjg=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68=
github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mau.fi/util v0.3.0 h1:Lt3lbRXP6ZBqTINK0EieRWor3zEwwwrDT14Z5N8RUCs=
go.mau.fi/util v0.3.0/go.mod h1:9dGsBCCbZJstx16YgnVMVi3O2bOizELoKpugLD4FoGs=
github.com/yuin/goldmark v1.7.1 h1:3bajkSilaCbjdKVsKdZjZCLBNPL9pYzrCakKaf4U49U=
github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
go.mau.fi/util v0.4.2 h1:RR3TOcRHmCF9Bx/3YG4S65MYfa+nV6/rn8qBWW4Mi30=
go.mau.fi/util v0.4.2/go.mod h1:PlAVfUUcPyHPrwnvjkJM9UFcPE7qGPDJqk+Oufa1Gtw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 h1:hNQpMuAJe5CtcUqCXaWga3FHu+kQvCqcsoVaQgSV60o=
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8 h1:ESSUROHIBHg7USnszlcdmjBEwdMj9VUvU+OPk4yl2mc=
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@ -105,8 +105,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
@ -118,6 +118,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
@ -125,7 +126,5 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
maunium.net/go/maulogger/v2 v2.4.1 h1:N7zSdd0mZkB2m2JtFUsiGTQQAdP0YeFWT7YMc80yAL8=
maunium.net/go/maulogger/v2 v2.4.1/go.mod h1:omPuYwYBILeVQobz8uO3XC8DIRuEb5rXYlQSuqrbCho=
maunium.net/go/mautrix v0.17.0 h1:scc1qlUbzPn+wc+3eAPquyD+3gZwwy/hBANBm+iGKK8=
maunium.net/go/mautrix v0.17.0/go.mod h1:j+puTEQCEydlVxhJ/dQP5chfa26TdvBO7X6F3Ataav8=
maunium.net/go/mautrix v0.18.1 h1:a6mUsJixegBNTXUoqC5RQ9gsumIPzKvCubKwF+zmCt4=
maunium.net/go/mautrix v0.18.1/go.mod h1:2oHaq792cSXFGvxLvYw3Gf1L4WVVP4KZcYys5HVk/h8=

42
internal/doc/main.go Normal file
View File

@ -0,0 +1,42 @@
//go:build generate
// +build generate
package main
import (
"context"
"flag"
"net/http"
"os"
"time"
"github.com/thegeeklab/wp-matrix/plugin"
plugin_docs "github.com/thegeeklab/wp-plugin-go/v3/docs"
plugin_template "github.com/thegeeklab/wp-plugin-go/v3/template"
)
func main() {
tmpl := "https://raw.githubusercontent.com/thegeeklab/woodpecker-plugins/main/templates/docs-data.yaml.tmpl"
client := http.Client{
Timeout: 30 * time.Second,
}
p := plugin.New(nil)
out, err := plugin_template.Render(context.Background(), client, tmpl, plugin_docs.GetTemplateData(p.App))
if err != nil {
panic(err)
}
outputFile := flag.String("output", "", "Output file path")
flag.Parse()
if *outputFile == "" {
panic("no output file specified")
}
err = os.WriteFile(*outputFile, []byte(out), 0o644)
if err != nil {
panic(err)
}
}

14
matrix/api.go Normal file
View File

@ -0,0 +1,14 @@
package matrix
import (
"context"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
//nolint:lll
type APIClient interface {
SendMessageEvent(ctx context.Context, roomID id.RoomID, eventType event.Type, contentJSON interface{}, extra ...mautrix.ReqSendEvent) (resp *mautrix.RespSendEvent, err error)
}

89
matrix/matrix.go Normal file
View File

@ -0,0 +1,89 @@
package matrix
import (
"context"
"fmt"
"github.com/microcosm-cc/bluemonday"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id"
)
type Client struct {
client APIClient
Message *Message
}
type Message struct {
client APIClient
Opt MessageOptions
}
type MessageOptions struct {
RoomID id.RoomID
Message string
TemplateUnsafe bool
}
// NewClient creates a new Matrix client with the given parameters and joins the specified room.
// It authenticates the user if the userID and token are not provided, and returns a Client struct
// that can be used to send messages to the room.
func NewClient(ctx context.Context, url, roomID, userID, token, username, password string) (*Client, error) {
muid := id.NewUserID(EnsurePrefix("@", userID), url)
c, err := mautrix.NewClient(url, muid, token)
if err != nil {
return nil, err
}
if userID == "" || token == "" {
_, err := c.Login(
ctx,
&mautrix.ReqLogin{
Type: "m.login.password",
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: username},
Password: password,
InitialDeviceDisplayName: "Woodpecker CI",
StoreCredentials: true,
},
)
if err != nil {
return nil, fmt.Errorf("failed to authenticate user: %w", err)
}
}
joinResp, err := c.JoinRoom(ctx, EnsurePrefix("!", roomID), "", nil)
if err != nil {
return nil, fmt.Errorf("failed to join room: %w", err)
}
return &Client{
client: c,
Message: &Message{
client: c,
Opt: MessageOptions{
RoomID: joinResp.RoomID,
},
},
}, nil
}
// Send sends a message to the specified room. It sanitizes the message content
// to remove potentially unsafe HTML.
func (m *Message) Send(ctx context.Context) error {
content := format.RenderMarkdown(m.Opt.Message, true, m.Opt.TemplateUnsafe)
if content.FormattedBody != "" {
content.Body = format.HTMLToMarkdown(bluemonday.UGCPolicy().Sanitize(content.FormattedBody))
content.FormattedBody = bluemonday.UGCPolicy().Sanitize(content.FormattedBody)
}
_, err := m.client.SendMessageEvent(ctx, m.Opt.RoomID, event.EventMessage, content)
if err != nil {
return err
}
return nil
}

106
matrix/matrix_test.go Normal file
View File

@ -0,0 +1,106 @@
package matrix
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/thegeeklab/wp-matrix/matrix/mocks"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
)
func TestMessageSend(t *testing.T) {
tests := []struct {
name string
messageOpt MessageOptions
want event.MessageEventContent
wantErr bool
}{
{
name: "plain text message",
messageOpt: MessageOptions{
RoomID: "test-room",
Message: "hello world",
},
want: event.MessageEventContent{
MsgType: "m.text",
Body: "hello world",
},
},
{
name: "markdown message",
messageOpt: MessageOptions{
RoomID: "test-room",
Message: "**hello world**",
},
want: event.MessageEventContent{
MsgType: "m.text",
Body: "**hello world**",
Format: "org.matrix.custom.html",
FormattedBody: "<strong>hello world</strong>",
},
},
{
name: "html message",
messageOpt: MessageOptions{
RoomID: "test-room",
Message: "hello<br>world",
TemplateUnsafe: true,
},
want: event.MessageEventContent{
MsgType: "m.text",
Body: "hello\nworld",
Format: "org.matrix.custom.html",
FormattedBody: "hello<br>world",
},
},
{
name: "safe html message",
messageOpt: MessageOptions{
RoomID: "test-room",
Message: "hello world<script>alert('XSS')</script>",
TemplateUnsafe: false,
},
want: event.MessageEventContent{
MsgType: "m.text",
Body: "hello world<script>alert('XSS')</script>",
Format: "org.matrix.custom.html",
FormattedBody: "hello world&lt;script&gt;alert(&#39;XSS&#39;)&lt;/script&gt;",
},
},
{
name: "unsafe html message",
messageOpt: MessageOptions{
RoomID: "test-room",
Message: "hello world<script>alert('XSS')</script>",
TemplateUnsafe: true,
},
want: event.MessageEventContent{
MsgType: "m.text",
Body: "hello world",
Format: "org.matrix.custom.html",
FormattedBody: "hello world",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
mockClient := mocks.NewMockAPIClient(t)
m := &Message{
Opt: tt.messageOpt,
client: mockClient,
}
mockClient.
On("SendMessageEvent", mock.Anything, tt.messageOpt.RoomID, event.EventMessage, tt.want).
Return(&mautrix.RespSendEvent{}, nil)
err := m.Send(ctx)
assert.Equal(t, tt.wantErr, err != nil)
})
}
}

View File

@ -0,0 +1,117 @@
// Code generated by mockery v2.43.0. DO NOT EDIT.
package mocks
import (
context "context"
event "maunium.net/go/mautrix/event"
id "maunium.net/go/mautrix/id"
mautrix "maunium.net/go/mautrix"
mock "github.com/stretchr/testify/mock"
)
// MockAPIClient is an autogenerated mock type for the APIClient type
type MockAPIClient struct {
mock.Mock
}
type MockAPIClient_Expecter struct {
mock *mock.Mock
}
func (_m *MockAPIClient) EXPECT() *MockAPIClient_Expecter {
return &MockAPIClient_Expecter{mock: &_m.Mock}
}
// SendMessageEvent provides a mock function with given fields: ctx, roomID, eventType, contentJSON, extra
func (_m *MockAPIClient) SendMessageEvent(ctx context.Context, roomID id.RoomID, eventType event.Type, contentJSON interface{}, extra ...mautrix.ReqSendEvent) (*mautrix.RespSendEvent, error) {
_va := make([]interface{}, len(extra))
for _i := range extra {
_va[_i] = extra[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, roomID, eventType, contentJSON)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
if len(ret) == 0 {
panic("no return value specified for SendMessageEvent")
}
var r0 *mautrix.RespSendEvent
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, id.RoomID, event.Type, interface{}, ...mautrix.ReqSendEvent) (*mautrix.RespSendEvent, error)); ok {
return rf(ctx, roomID, eventType, contentJSON, extra...)
}
if rf, ok := ret.Get(0).(func(context.Context, id.RoomID, event.Type, interface{}, ...mautrix.ReqSendEvent) *mautrix.RespSendEvent); ok {
r0 = rf(ctx, roomID, eventType, contentJSON, extra...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*mautrix.RespSendEvent)
}
}
if rf, ok := ret.Get(1).(func(context.Context, id.RoomID, event.Type, interface{}, ...mautrix.ReqSendEvent) error); ok {
r1 = rf(ctx, roomID, eventType, contentJSON, extra...)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockAPIClient_SendMessageEvent_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'SendMessageEvent'
type MockAPIClient_SendMessageEvent_Call struct {
*mock.Call
}
// SendMessageEvent is a helper method to define mock.On call
// - ctx context.Context
// - roomID id.RoomID
// - eventType event.Type
// - contentJSON interface{}
// - extra ...mautrix.ReqSendEvent
func (_e *MockAPIClient_Expecter) SendMessageEvent(ctx interface{}, roomID interface{}, eventType interface{}, contentJSON interface{}, extra ...interface{}) *MockAPIClient_SendMessageEvent_Call {
return &MockAPIClient_SendMessageEvent_Call{Call: _e.mock.On("SendMessageEvent",
append([]interface{}{ctx, roomID, eventType, contentJSON}, extra...)...)}
}
func (_c *MockAPIClient_SendMessageEvent_Call) Run(run func(ctx context.Context, roomID id.RoomID, eventType event.Type, contentJSON interface{}, extra ...mautrix.ReqSendEvent)) *MockAPIClient_SendMessageEvent_Call {
_c.Call.Run(func(args mock.Arguments) {
variadicArgs := make([]mautrix.ReqSendEvent, len(args)-4)
for i, a := range args[4:] {
if a != nil {
variadicArgs[i] = a.(mautrix.ReqSendEvent)
}
}
run(args[0].(context.Context), args[1].(id.RoomID), args[2].(event.Type), args[3].(interface{}), variadicArgs...)
})
return _c
}
func (_c *MockAPIClient_SendMessageEvent_Call) Return(resp *mautrix.RespSendEvent, err error) *MockAPIClient_SendMessageEvent_Call {
_c.Call.Return(resp, err)
return _c
}
func (_c *MockAPIClient_SendMessageEvent_Call) RunAndReturn(run func(context.Context, id.RoomID, event.Type, interface{}, ...mautrix.ReqSendEvent) (*mautrix.RespSendEvent, error)) *MockAPIClient_SendMessageEvent_Call {
_c.Call.Return(run)
return _c
}
// NewMockAPIClient creates a new instance of MockAPIClient. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
// The first argument is typically a *testing.T value.
func NewMockAPIClient(t interface {
mock.TestingT
Cleanup(func())
}) *MockAPIClient {
mock := &MockAPIClient{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}

16
matrix/util.go Normal file
View File

@ -0,0 +1,16 @@
package matrix
import "strings"
// EnsurePrefix ensures that the given input string starts with the provided prefix.
func EnsurePrefix(prefix, input string) string {
if strings.TrimSpace(input) == "" {
return input
}
if strings.HasPrefix(input, prefix) {
return input
}
return prefix + input
}

46
matrix/util_test.go Normal file
View File

@ -0,0 +1,46 @@
package matrix
import "testing"
func TestEnsurePrefix(t *testing.T) {
tests := []struct {
name string
prefix string
input string
want string
}{
{
name: "empty input",
prefix: "pre_",
input: "",
want: "",
},
{
name: "input already has prefix",
prefix: "pre_",
input: "pre_value",
want: "pre_value",
},
{
name: "input needs prefix",
prefix: "pre_",
input: "value",
want: "pre_value",
},
{
name: "input with leading/trailing spaces",
prefix: "pre_",
input: " value ",
want: "pre_ value ",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := EnsurePrefix(tt.prefix, tt.input)
if got != tt.want {
t.Errorf("EnsurePrefix(%q, %q) = %q, want %q", tt.prefix, tt.input, got, tt.want)
}
})
}
}

View File

@ -10,16 +10,10 @@ import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"github.com/microcosm-cc/bluemonday"
"github.com/rs/zerolog/log"
"github.com/thegeeklab/wp-plugin-go/template"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id"
"github.com/thegeeklab/wp-matrix/matrix"
plugin_template "github.com/thegeeklab/wp-plugin-go/v3/template"
)
var ErrAuthSourceNotSet = errors.New("either username and password or userid and accesstoken are required")
@ -49,43 +43,32 @@ func (p *Plugin) Validate() error {
// Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error {
muid := id.NewUserID(prepend("@", p.Settings.UserID), p.Settings.Homeserver)
msg, err := p.CreateMessage()
if err != nil {
return fmt.Errorf("failed to create message: %w", err)
}
client, err := mautrix.NewClient(p.Settings.Homeserver, muid, p.Settings.AccessToken)
client, err := matrix.NewClient(
p.Network.Context,
p.Settings.Homeserver,
p.Settings.RoomID,
p.Settings.UserID,
p.Settings.AccessToken,
p.Settings.Username,
p.Settings.Password,
)
if err != nil {
return fmt.Errorf("failed to initialize client: %w", err)
}
if p.Settings.UserID == "" || p.Settings.AccessToken == "" {
_, err := client.Login(
p.Network.Context,
&mautrix.ReqLogin{
Type: "m.login.password",
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: p.Settings.Username},
Password: p.Settings.Password,
InitialDeviceDisplayName: "Woodpecker CI",
StoreCredentials: true,
},
)
if err != nil {
return fmt.Errorf("failed to authenticate user: %w", err)
}
client.Message.Opt = matrix.MessageOptions{
RoomID: client.Message.Opt.RoomID,
Message: msg,
TemplateUnsafe: p.Settings.TemplateUnsafe,
}
log.Info().Msg("logged in successfully")
joined, err := client.JoinRoom(p.Network.Context, prepend("!", p.Settings.RoomID), "", nil)
if err != nil {
return fmt.Errorf("failed to join room: %w", err)
}
content, err := p.messageContent(p.Network.Context, *p.Network.Client)
if err != nil {
return fmt.Errorf("failed to render template: %w", err)
}
if _, err := client.SendMessageEvent(p.Network.Context, joined.RoomID, event.EventMessage, content); err != nil {
return fmt.Errorf("failed to submit message: %w", err)
if err := client.Message.Send(p.Network.Context); err != nil {
return fmt.Errorf("failed to send message: %w", err)
}
log.Info().Msg("message sent successfully")
@ -93,34 +76,7 @@ func (p *Plugin) Execute() error {
return nil
}
func (p *Plugin) messageContent(ctx context.Context, client http.Client) (event.MessageEventContent, error) {
message, err := template.RenderTrim(ctx, client, p.Settings.Template, p.Metadata)
if err != nil {
return event.MessageEventContent{}, err
}
content := format.RenderMarkdown(message, true, p.Settings.TemplateUnsafe)
safeBody := format.HTMLToMarkdown(bluemonday.UGCPolicy().Sanitize(content.FormattedBody))
if content.Body != safeBody {
content.Body = safeBody
}
if content.FormattedBody != "" {
content.FormattedBody = bluemonday.UGCPolicy().Sanitize(content.FormattedBody)
}
return content, nil
}
func prepend(prefix, input string) string {
if strings.TrimSpace(input) == "" {
return input
}
if strings.HasPrefix(input, prefix) {
return input
}
return prefix + input
// CreateMessage generates a message string based on the plugin's template and metadata.
func (p *Plugin) CreateMessage() (string, error) {
return plugin_template.RenderTrim(p.Network.Context, *p.Network.Client, p.Settings.Template, p.Metadata)
}

View File

@ -5,7 +5,8 @@ import (
"net/http"
"testing"
wp "github.com/thegeeklab/wp-plugin-go/plugin"
"github.com/stretchr/testify/assert"
plugin_base "github.com/thegeeklab/wp-plugin-go/v3/plugin"
)
func Test_messageContent(t *testing.T) {
@ -13,9 +14,7 @@ func Test_messageContent(t *testing.T) {
tests := []struct {
name string
want string
unsafe bool
template string
meta wp.Metadata
}{
{
name: "render default template",
@ -25,54 +24,40 @@ func Test_messageContent(t *testing.T) {
{
name: "render unsafe html template",
want: "Status: **success**\nBuild: octocat/demo",
unsafe: true,
template: "Status: **{{ .Pipeline.Status }}**<br>Build: {{ .Repository.Slug }}",
},
{
name: "render html xss template",
want: "Status: **success**\nBuild: octocat/demo",
unsafe: true,
template: "Status: **{{ .Pipeline.Status }}**<br>Build: <a href=\"javascript:alert('XSS1')\" onmouseover=\"alert('XSS2')\">{{ .Repository.Slug }}<a>",
},
{
name: "render markdown xss template",
want: "Status: **success**\nBuild: octocat/demo",
unsafe: true,
template: "Status: **{{ .Pipeline.Status }}**<br>Build: [{{ .Repository.Slug }}](javascript:alert(XSS1'))",
template: "Status: **{{ .Pipeline.Status }}**\nBuild: {{ .Repository.Slug }}",
},
}
options := wp.Options{
Name: "wp-matrix",
Execute: func(_ context.Context) error { return nil },
p := New(func(_ context.Context) error { return nil })
p.Network = plugin_base.Network{
Context: context.Background(),
Client: &http.Client{},
}
p := New(options, &Settings{})
p.Metadata = wp.Metadata{
Curr: wp.Commit{
p.Metadata = plugin_base.Metadata{
Curr: plugin_base.Commit{
Branch: "main",
Title: "feat: demo commit title",
URL: "https://git.example.com",
Author: wp.Author{
Author: plugin_base.Author{
Name: "octobot",
},
},
Pipeline: wp.Pipeline{
Pipeline: plugin_base.Pipeline{
Status: "success",
URL: "https://ci.example.com",
},
Repository: wp.Repository{
Repository: plugin_base.Repository{
Slug: "octocat/demo",
},
}
for _, tt := range tests {
p.Settings.Template = tt.template
p.Settings.TemplateUnsafe = tt.unsafe
content, _ := p.messageContent(context.Background(), http.Client{})
t.Run(tt.name, func(t *testing.T) {
p.Settings.Template = tt.template
if content.Body != tt.want {
t.Errorf("messageContent: %q got: %q, want: %q", tt.name, content.Body, tt.want)
}
content, err := p.CreateMessage()
assert.NoError(t, err)
assert.Equal(t, tt.want, content)
})
}
}

View File

@ -7,9 +7,14 @@
package plugin
import (
wp "github.com/thegeeklab/wp-plugin-go/plugin"
"fmt"
plugin_base "github.com/thegeeklab/wp-plugin-go/v3/plugin"
"github.com/urfave/cli/v2"
)
//go:generate go run ../internal/doc/main.go -output=../docs/data/data-raw.yaml
//nolint:lll
const DefaultMessageTemplate = `
Status: **{{ .Pipeline.Status }}**
@ -19,7 +24,7 @@ Message: {{ .Curr.Title }}{{ if .Curr.URL }} ([source]({{ .Curr.URL }})){{ end }
// Plugin implements provide the plugin.
type Plugin struct {
*wp.Plugin
*plugin_base.Plugin
Settings *Settings
}
@ -35,13 +40,96 @@ type Settings struct {
TemplateUnsafe bool
}
func New(options wp.Options, settings *Settings) *Plugin {
p := &Plugin{}
func New(e plugin_base.ExecuteFunc, build ...string) *Plugin {
p := &Plugin{
Settings: &Settings{},
}
options.Execute = p.run
options := plugin_base.Options{
Name: "wp-matrix",
Description: "Send messages to a Matrix room",
Flags: Flags(p.Settings, plugin_base.FlagsPluginCategory),
Execute: p.run,
HideWoodpeckerFlags: true,
}
p.Plugin = wp.New(options)
p.Settings = settings
if len(build) > 0 {
options.Version = build[0]
}
if len(build) > 1 {
options.VersionMetadata = fmt.Sprintf("date=%s", build[1])
}
if e != nil {
options.Execute = e
}
p.Plugin = plugin_base.New(options)
return p
}
// Flags returns a slice of CLI flags for the plugin.
func Flags(settings *Settings, category string) []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "username",
EnvVars: []string{"PLUGIN_USERNAME", "MATRIX_USERNAME"},
Usage: "authentication username",
Destination: &settings.Username,
Category: category,
},
&cli.StringFlag{
Name: "password",
EnvVars: []string{"PLUGIN_PASSWORD", "MATRIX_PASSWORD"},
Usage: "authentication password",
Destination: &settings.Password,
Category: category,
},
&cli.StringFlag{
Name: "userid",
EnvVars: []string{"PLUGIN_USER_ID", "PLUGIN_USERID", "MATRIX_USER_ID", "MATRIX_USERID"},
Usage: "authentication user ID",
Destination: &settings.UserID,
Category: category,
},
&cli.StringFlag{
Name: "accesstoken",
EnvVars: []string{"PLUGIN_ACCESS_TOKEN", "PLUGIN_ACCESSTOKEN", "MATRIX_ACCESS_TOKEN", "MATRIX_ACCESSTOKEN"},
Usage: "authentication access token",
Destination: &settings.AccessToken,
Category: category,
},
&cli.StringFlag{
Name: "homeserver",
EnvVars: []string{"PLUGIN_HOMESERVER", "MATRIX_HOMESERVER"},
Usage: "matrix home server url",
Value: "https://matrix.org",
Destination: &settings.Homeserver,
Category: category,
},
&cli.StringFlag{
Name: "roomid",
EnvVars: []string{"PLUGIN_ROOMID", "MATRIX_ROOMID"},
Usage: "roomid to send messages to",
Destination: &settings.RoomID,
Category: category,
},
&cli.StringFlag{
Name: "template",
EnvVars: []string{"PLUGIN_TEMPLATE", "MATRIX_TEMPLATE"},
Usage: "golang template for the message",
Value: DefaultMessageTemplate,
Destination: &settings.Template,
Category: category,
},
&cli.BoolFlag{
Name: "template-unsafe",
EnvVars: []string{"PLUGIN_TEMPLATE_UNSAFE", "MATRIX_TEMPLATE_UNSAFE"},
Usage: "render raw HTML and potentially dangerous links in template",
Destination: &settings.TemplateUnsafe,
Category: category,
},
}
}