utils

package
v0.0.0-...-98ce35c Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package utils contains various utility functions/types.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidBuilderID = errors.New("the string is not a valid builder ID")
View Source
var ErrInvalidLegacyBuildID = errors.New("the string is not a valid legacy build ID")
View Source
var ErrInvalidLegacyBuilderID = errors.New("the string is not a valid legacy builder ID (format: buildbucket/luci.<project>.<bucket>/<builder>)")

Functions

func BucketResourceID

func BucketResourceID(project, bucket string) string

BucketResourceID returns a string identifying the bucket resource. It is used when checking bucket permission.

func Duration

func Duration(start *timestamppb.Timestamp, ends ...*timestamppb.Timestamp) string

Duration returns duration between start and the earliest of ends. Ignores nil ends. Fallbacks to "N/A" on insufficient data.

func GetJSONData

func GetJSONData(client *http.Client, url string, out any) (err error)

GetJSONData fetches data from the given URL, parses the response body to `out`. It follows redirection and returns an error if the status code is 4xx or 5xx.

func HumanDuration

func HumanDuration(d time.Duration) string

HumanDuration translates d into a human readable string of x units y units, where x and y could be in days, hours, minutes, or seconds, whichever is the largest.

func LegacyBuilderIDString

func LegacyBuilderIDString(bid *buildbucketpb.BuilderID) string

LegacyBuilderIDString returns a legacy string identifying the builder. It is used in the Milo datastore.

func MergeStrings

func MergeStrings(sss ...[]string) []string

MergeStrings merges multiple string slices together into a single slice, removing duplicates.

func ObfuscateEmail

func ObfuscateEmail(email string) template.HTML

ObfuscateEmail converts a string containing email address email@address.com into email<junk>@address.com.

func ParseBuildbucketBuildID

func ParseBuildbucketBuildID(bid string) (buildID int64, err error)

ParseBuildbucketBuildID parses the legacy build ID in the format of `buildbucket/<build_id>`

func ParseBuilderID

func ParseBuilderID(bid string) (*buildbucketpb.BuilderID, error)

ParseBuilderID parses the canonical builder ID (e.g. `<project>/<bucket>/<builder>`) and returns the BuilderID struct.

func ParseIntFromForm

func ParseIntFromForm(form url.Values, key string, base int, bitSize int) (int64, error)

ParseIntFromForm parses an integer from a form.

func ParseLegacyBuildbucketBuildID

func ParseLegacyBuildbucketBuildID(bid string) (builderID *buildbucketpb.BuilderID, number int32, err error)

ParseLegacyBuildbucketBuildID parses the legacy build ID in the format of `buildbucket/luci.<project>.<bucket>/<builder>/<number>`.

func ParseLegacyBuilderID

func ParseLegacyBuilderID(bid string) (*buildbucketpb.BuilderID, error)

ParseLegacyBuilderID parses the legacy builder ID (e.g. `buildbucket/luci.<project>.<bucket>/<builder>`) and returns the BuilderID struct.

func ReadExactOneFromForm

func ReadExactOneFromForm(form url.Values, key string) (string, error)

ReadExactOneFromForm read a string from a form. There must be exactly one and non-empty entry of the given key in the form.

func ReplaceNSEWith

func ReplaceNSEWith(err errors.MultiError, replacement error) error

ReplaceNSEWith takes an errors.MultiError returned by a datastore.Get() on a slice (which is always a MultiError), filters out all datastore.ErrNoSuchEntity or replaces it with replacement instances, and returns an error generated by errors.LazyMultiError.

func ShortenEmail

func ShortenEmail(email string) string

ShortenEmail shortens Google emails.

func TagGRPC

func TagGRPC(c context.Context, err error) error

TagGRPC annotates some gRPC with Milo specific semantics, specifically: * Marks the error as Unauthorized if the user is not logged in, and the underlying error was a 403 or 404. * Otherwise, tag the error with the original error code.

Types

type Interval

type Interval struct {
	Start time.Time `json:"start,omitempty"`
	End   time.Time `json:"end,omitempty"`
	Now   time.Time `json:"now,omitempty"`
}

func ToInterval

func ToInterval(start, end, now *timestamppb.Timestamp) (result Interval)

func (Interval) Duration

func (in Interval) Duration() time.Duration

func (Interval) Ended

func (in Interval) Ended() bool

func (Interval) Started

func (in Interval) Started() bool

type PubSubMessage

type PubSubMessage struct {
	Attributes map[string]any `json:"attributes"`
	Data       string         `json:"data"`
	MessageID  string         `json:"message_id"`
}

type PubSubSubscription

type PubSubSubscription struct {
	Message PubSubMessage `json:"message"`
}

func (*PubSubSubscription) GetData

func (m *PubSubSubscription) GetData() ([]byte, error)

GetData returns the expanded form of Data (decoded from base64).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL