2020-11-25 19:44:45 +00:00
|
|
|
// Copyright (c) 2020, the Drone Plugins project authors.
|
2021-09-13 21:00:00 +00:00
|
|
|
// Copyright (c) 2021, Robert Kaussow <mail@thegeeklab.de>
|
|
|
|
|
2020-11-25 19:44:45 +00:00
|
|
|
// Use of this source code is governed by an Apache 2.0 license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2024-05-10 06:42:59 +00:00
|
|
|
"fmt"
|
|
|
|
|
2024-05-17 19:50:20 +00:00
|
|
|
plugin_base "github.com/thegeeklab/wp-plugin-go/v3/plugin"
|
2024-05-10 06:42:59 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-11-25 19:44:45 +00:00
|
|
|
)
|
|
|
|
|
2024-05-10 06:42:59 +00:00
|
|
|
//go:generate go run ../internal/doc/main.go -output=../docs/data/data-raw.yaml
|
|
|
|
|
2023-12-06 20:53:01 +00:00
|
|
|
//nolint:lll
|
|
|
|
const DefaultMessageTemplate = `
|
|
|
|
Status: **{{ .Pipeline.Status }}**
|
|
|
|
Build: [{{ .Repository.Slug }}]({{ .Pipeline.URL }}){{ if .Curr.Branch }} ({{ .Curr.Branch }}){{ end }} by {{ .Curr.Author.Name }}
|
|
|
|
Message: {{ .Curr.Title }}{{ if .Curr.URL }} ([source]({{ .Curr.URL }})){{ end }}
|
|
|
|
`
|
|
|
|
|
2023-08-21 09:36:07 +00:00
|
|
|
// Plugin implements provide the plugin.
|
2020-11-25 19:44:45 +00:00
|
|
|
type Plugin struct {
|
2024-05-17 19:50:20 +00:00
|
|
|
*plugin_base.Plugin
|
2023-08-15 22:15:24 +00:00
|
|
|
Settings *Settings
|
2020-11-25 19:44:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 22:15:24 +00:00
|
|
|
// Settings for the plugin.
|
|
|
|
type Settings struct {
|
2023-12-06 20:53:01 +00:00
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
UserID string
|
|
|
|
AccessToken string
|
|
|
|
Homeserver string
|
|
|
|
RoomID string
|
|
|
|
Template string
|
|
|
|
TemplateUnsafe bool
|
2023-08-15 22:15:24 +00:00
|
|
|
}
|
|
|
|
|
2024-05-17 19:50:20 +00:00
|
|
|
func New(e plugin_base.ExecuteFunc, build ...string) *Plugin {
|
2024-05-10 06:42:59 +00:00
|
|
|
p := &Plugin{
|
|
|
|
Settings: &Settings{},
|
|
|
|
}
|
|
|
|
|
2024-05-17 19:50:20 +00:00
|
|
|
options := plugin_base.Options{
|
2024-05-10 06:42:59 +00:00
|
|
|
Name: "wp-matrix",
|
|
|
|
Description: "Send messages to a Matrix room",
|
2024-05-17 19:50:20 +00:00
|
|
|
Flags: Flags(p.Settings, plugin_base.FlagsPluginCategory),
|
2024-05-10 06:42:59 +00:00
|
|
|
Execute: p.run,
|
|
|
|
HideWoodpeckerFlags: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(build) > 0 {
|
|
|
|
options.Version = build[0]
|
|
|
|
}
|
2023-08-15 22:15:24 +00:00
|
|
|
|
2024-05-10 06:42:59 +00:00
|
|
|
if len(build) > 1 {
|
|
|
|
options.VersionMetadata = fmt.Sprintf("date=%s", build[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
if e != nil {
|
|
|
|
options.Execute = e
|
|
|
|
}
|
2023-08-15 22:15:24 +00:00
|
|
|
|
2024-05-17 19:50:20 +00:00
|
|
|
p.Plugin = plugin_base.New(options)
|
2023-08-15 22:15:24 +00:00
|
|
|
|
|
|
|
return p
|
2020-11-25 19:44:45 +00:00
|
|
|
}
|
2024-05-10 06:42:59 +00:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|