mailtmpl

package
Version: v0.0.0-...-51f9457 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2021 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package mailtmpl implements email template bundling and execution.

Package mailtmpl implements email template bundling and execution.

Index

Constants

View Source
const (
	// FileExt is a file extension of template files.
	FileExt = ".template"

	// DefaultTemplateName of the default template.
	DefaultTemplateName = "default"
)

Variables

View Source
var Funcs = map[string]interface{}{
	"time": func(ts *timestamppb.Timestamp) time.Time {
		t := ts.AsTime()
		return t
	},

	"formatBuilderID": protoutil.FormatBuilderID,

	"markdown": func(inputMD string) html.HTML {

		r := blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
			Flags: blackfriday.UseXHTML,
		})
		untrusted := blackfriday.Run(
			[]byte(inputMD),
			blackfriday.WithRenderer(r),
			blackfriday.WithExtensions(
				blackfriday.NoIntraEmphasis|
					blackfriday.FencedCode|
					blackfriday.Autolink,
			))
		out := bytes.NewBuffer(nil)
		if err := sanitizehtml.Sanitize(out, bytes.NewReader(untrusted)); err != nil {
			return html.HTML(fmt.Sprintf("Failed to render markdown: %s", html.HTMLEscapeString(err.Error())))
		}
		return html.HTML(out.String())
	},

	"stepNames": func(steps []*buildbucketpb.Step) string {
		var sb strings.Builder
		for i, step := range steps {
			if i != 0 {
				sb.WriteString(", ")
			}
			fmt.Fprintf(&sb, "%q", step.Name)
		}

		return sb.String()
	},

	"buildUrl": func(input *config.TemplateInput) string {
		return fmt.Sprintf("https://%s/build/%d",
			input.BuildbucketHostname, input.Build.Id)
	},
}

Funcs is functions available to email subject and body templates.

Functions

func SplitTemplateFile

func SplitTemplateFile(content string) (subject, body string, err error)

SplitTemplateFile splits an email template file into subject and body. Does not validate their syntaxes. See notify.proto for file format.

Types

type Bundle

type Bundle struct {
	// Error found among templates.
	// If non-nil, GenerateEmail will generate error emails.
	Err error
	// contains filtered or unexported fields
}

Bundle is a collection of email templates bundled together, so they can use each other.

func NewBundle

func NewBundle(templates []*Template) *Bundle

NewBundle bundles templates together and makes them renderable. If templates do not have a template "default", bundles in one. May return a bundle with an non-nil Err.

func (*Bundle) GenerateEmail

func (b *Bundle) GenerateEmail(templateName string, input *config.TemplateInput) (subject, body string)

GenerateEmail generates an email using the named template. If the template fails, an error template is used, which includes error details and a link to the definition of the failed template.

func (*Bundle) GenerateStatusMessage

func (b *Bundle) GenerateStatusMessage(c context.Context, templateName string, input *config.TemplateInput) (message string)

GenerateStatusMessage generates a message to be posted to a tree status instance. If the template fails, a default template is used.

type Template

type Template struct {
	// Name identifies the email template. It is unique within a bundle.
	Name string

	// SubjectTextTemplate is a text.Template of the email subject.
	// See Funcs for available functions.
	SubjectTextTemplate string

	// BodyHTMLTemplate is a html.Template of the email body.
	// See Funcs for available functions.
	BodyHTMLTemplate string

	// URL to the template definition.
	// Will be used in template error reports.
	DefinitionURL string
}

Template is an email template. To render it, use NewBundle.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto