mirror of
https://github.com/thegeeklab/wp-matrix.git
synced 2024-06-02 18:39:40 +02:00
Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
728bc0a268 | ||
|
4f9dee12c5 | ||
|
79f55e0b3e | ||
|
7db4c14b96 | ||
|
b5980344c7 | ||
76f5076b0a | |||
|
dc41f2d9ff | ||
|
9695526a8d | ||
|
2f805d6fca | ||
a534efd107 | |||
3c3fb393b8 | |||
117f681b10 | |||
|
4aa20c5f0e | ||
|
1b9e7caee0 | ||
|
312b75d247 | ||
|
350694d2e3 | ||
|
a7bb53a553 | ||
|
9fb64d43c3 | ||
|
0ce44c0fc7 | ||
|
7d9cd7b14c | ||
|
f92be16c5c | ||
|
a7f5a30e4a |
|
@ -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
6
.mockery.yaml
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
all: True
|
||||
dir: "{{.PackageName}}/mocks"
|
||||
outpkg: "mocks"
|
||||
packages:
|
||||
github.com/thegeeklab/wp-matrix/matrix:
|
|
@ -7,7 +7,7 @@ when:
|
|||
|
||||
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
|
||||
|
@ -20,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
|
||||
|
@ -41,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
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.22@sha256:0b55ab82ac2a54a6f8f85ec8b943b9e470c39e32c109b766bbc1b801f3fa8d3b 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>"
|
||||
|
|
11
Makefile
11
Makefile
|
@ -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.57.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:
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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,
|
||||
},
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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 -}}
|
|
@ -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.
|
||||
|
|
29
go.mod
29
go.mod
|
@ -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.7.0
|
||||
github.com/urfave/cli/v2 v2.27.1
|
||||
maunium.net/go/mautrix v0.18.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,19 +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/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.7.0 // indirect
|
||||
go.mau.fi/util v0.4.1 // indirect
|
||||
golang.org/x/crypto v0.21.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20240314144324-c7f7c6466f7f // indirect
|
||||
golang.org/x/net v0.22.0 // indirect
|
||||
golang.org/x/sys v0.18.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
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
|
51
go.sum
51
go.sum
|
@ -8,8 +8,8 @@ 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=
|
||||
|
@ -43,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=
|
||||
|
@ -54,12 +54,14 @@ 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.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 v1.7.0 h1:YXE8WkFeoSqMhBR4WYMetlEzCp5cYwP6lZfrHH6rpIc=
|
||||
github.com/thegeeklab/wp-plugin-go v1.7.0/go.mod h1:5ZCnuq/bOryxCfi9nsTPW6V+hw6L5uILVoU6OFy8E4U=
|
||||
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.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
|
||||
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
|
@ -69,29 +71,29 @@ 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.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA=
|
||||
github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
|
||||
go.mau.fi/util v0.4.1 h1:3EC9KxIXo5+h869zDGf5OOZklRd/FjeVnimTwtm3owg=
|
||||
go.mau.fi/util v0.4.1/go.mod h1:GjkTEBsehYZbSh2LlE6cWEn+6ZIZTGrTMM/5DMNlmFY=
|
||||
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.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
|
||||
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
|
||||
golang.org/x/exp v0.0.0-20240314144324-c7f7c6466f7f h1:3CW0unweImhOzd5FmYuRsD4Y4oQFKZIjAnKbjV4WIrw=
|
||||
golang.org/x/exp v0.0.0-20240314144324-c7f7c6466f7f/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
|
||||
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.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
|
||||
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||
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=
|
||||
|
@ -103,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.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||
golang.org/x/sys v0.18.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=
|
||||
|
@ -116,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=
|
||||
|
@ -123,5 +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/mautrix v0.18.0 h1:sNsApeSWB8x0hLjGcdmi5JqO6Tvp2PVkiSStz+Yas6k=
|
||||
maunium.net/go/mautrix v0.18.0/go.mod h1:STwJZ+6CAeiEQs7fYCkd5aC12XR5DXANE6Swy/PBKGo=
|
||||
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
42
internal/doc/main.go
Normal 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
14
matrix/api.go
Normal 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
89
matrix/matrix.go
Normal 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
106
matrix/matrix_test.go
Normal 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<script>alert('XSS')</script>",
|
||||
},
|
||||
},
|
||||
{
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
117
matrix/mocks/mock_APIClient.go
Normal file
117
matrix/mocks/mock_APIClient.go
Normal 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
16
matrix/util.go
Normal 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
46
matrix/util_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
102
plugin/plugin.go
102
plugin/plugin.go
|
@ -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,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user