trunk

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package trunk provides utilities for the Trunk.io API.

Index

Constants

View Source
const (
	// TestCaseStatusHealthy is the status of a test that is healthy.
	TestCaseStatusHealthy = "healthy"
	// TestCaseStatusFlaky is the status of a test that is flaky.
	TestCaseStatusFlaky = "flaky"
	// TestCaseStatusBroken is the status of a test that is broken.
	TestCaseStatusBroken = "broken"
)

Variables

This section is empty.

Functions

func HandleTestCaseStatusChanged

func HandleTestCaseStatusChanged(
	l zerolog.Logger,
	statusChange TestCaseStatusChange,
	jiraClient jira.IClient,
	trunkClient IClient,
	githubClient github.IClient,
) error

HandleTestCaseStatusChanged processes when a test case's status changes. This can be one of: healthy, flaky, or broken.

func ReceiveWebhook

func ReceiveWebhook(
	l zerolog.Logger,
	req *http.Request,
	signingSecret string,
	jiraClient jira.IClient,
	trunkClient IClient,
	githubClient github.IClient,
) error

ReceiveWebhook processes a Trunk webhook, typically triggered by a test being marked/unmarked as flaky. It will create a

func SelfSignWebhookRequest

func SelfSignWebhookRequest(l zerolog.Logger, req *http.Request, signingSecret string) (*http.Request, error)

SelfSignWebhookRequest self-signs a request to create a valid svix webhook call. This is useful for testing and for the webhook command.

func VerifyWebhookRequest

func VerifyWebhookRequest(l zerolog.Logger, req *http.Request, signingSecret string) error

VerifyWebhookRequest verifies a request as a valid svix webhook call. https://docs.svix.com/receiving/verifying-payloads/how

Types

type Client

type Client struct {
	BaseURL    *url.URL
	HTTPClient *http.Client
	// contains filtered or unexported fields
}

Client is the Trunk.io client.

func NewClient

func NewClient(options ...Option) (*Client, error)

NewClient creates a new Trunk.io client.

func (*Client) LinkTicketToTestCase

func (c *Client) LinkTicketToTestCase(testCaseID string, ticket *jira.TicketResponse, repoURL string) error

LinkTicketToTestCase links a Jira ticket to a test case in Trunk.io See: https://docs.trunk.io/references/apis/flaky-tests#post-flaky-tests-link-ticket-to-test-case

type IClient

type IClient interface {
	LinkTicketToTestCase(testCaseID string, ticket *jira.TicketResponse, repoURL string) error
}

IClient is the interface that wraps the basic Trunk.io client methods. Helpful for mocking in tests.

type LinkTicketRequest

type LinkTicketRequest struct {
	TestCaseID       string        `json:"test_case_id"`
	ExternalTicketID string        `json:"external_ticket_id"`
	Repo             RepoReference `json:"repo"`
}

LinkTicketRequest represents the request to link a Jira ticket to a test case in Trunk.io See: https://docs.trunk.io/references/apis/flaky-tests#post-flaky-tests-link-ticket-to-test-case

type MostCommonFailure

type MostCommonFailure struct {
	LastOccurrence  string `json:"last_occurrence"`
	OccurrenceCount int    `json:"occurrence_count"`
	Summary         string `json:"summary"`
}

MostCommonFailure represents the most common failure for a test case.

type Option

type Option func(*trunkClientOptions)

Option is a function that sets a configuration option for the Trunk.io client.

func WithBaseURL

func WithBaseURL(baseURL *url.URL) Option

WithBaseURL sets the base URL for the Trunk.io client. Useful for testing.

func WithConfig

func WithConfig(config config.Config) Option

WithConfig sets the config to use for the Trunk.io client.

func WithLogger

func WithLogger(logger zerolog.Logger) Option

WithLogger sets the logger to use for the Trunk.io client.

type QuarantiningSettingChanged

type QuarantiningSettingChanged struct {
	QuarantineSettingChanged struct {
		Actor struct {
			Email    string `json:"email"`
			FullName string `json:"full_name"`
		} `json:"actor"`
		PreviousQuarantiningSetting string    `json:"previous_quarantining_setting"`
		Reason                      string    `json:"reason"`
		Timestamp                   time.Time `json:"timestamp"`
		UpdatedQuarantiningSetting  string    `json:"updated_quarantining_setting"`
	} `json:"quarantine_setting_changed"`
	TestCase TestCase `json:"test_case"`
	Type     string   `json:"type"`
}

QuarantiningSettingChanged is the event type for when a test case's quarantining setting is changed. https://www.svix.com/event-types/us/org_2eQPL41Ew5XSHxiXZIamIUIXg8H/#test_case.quarantining_setting_changed

func (QuarantiningSettingChanged) GetTestCase

func (q QuarantiningSettingChanged) GetTestCase() TestCase

GetTestCase implements the WebhookEvent interface

func (QuarantiningSettingChanged) GetType

GetType implements the WebhookEvent interface

type RepoReference

type RepoReference struct {
	Host  string `json:"host"`
	Owner string `json:"owner"`
	Name  string `json:"name"`
}

RepoReference represents the repository information for Trunk.io API

type Repository

type Repository struct {
	HTMLURL string `json:"html_url"`
}

Repository represents the repository URL associated with a test case.

type Status

type Status struct {
	Reason    string `json:"reason"`
	Timestamp string `json:"timestamp"`
	Value     string `json:"value"`
}

Status represents the current status of a test case See: https://www.svix.com/event-types/us/org_2eQPL41Ew5XSHxiXZIamIUIXg8H/#test_case.status_changed

type StatusChange

type StatusChange struct {
	CurrentStatus  Status `json:"current_status"`
	PreviousStatus string `json:"previous_status"`
}

StatusChange represents the status change event from Trunk.io See: https://www.svix.com/event-types/us/org_2eQPL41Ew5XSHxiXZIamIUIXg8H/#test_case.status_changed

type TestCase

type TestCase struct {
	Codeowners                 []string            `json:"codeowners"`
	FailureRateLast7D          float64             `json:"failure_rate_last_7d"`
	FilePath                   string              `json:"file_path"`
	HTMLURL                    string              `json:"html_url"`
	ID                         string              `json:"id"`
	MostCommonFailures         []MostCommonFailure `json:"most_common_failures"`
	Name                       string              `json:"name"`
	PullRequestsImpactedLast7D int                 `json:"pull_requests_impacted_last_7d"`
	Quarantine                 bool                `json:"quarantine"`
	Repository                 Repository          `json:"repository"`
	Status                     Status              `json:"status"`
	TestSuite                  string              `json:"test_suite"`
	Ticket                     Ticket              `json:"ticket"`
	Variant                    string              `json:"variant"`
}

TestCase is the common structure for all test case events.

type TestCaseStatusChange

type TestCaseStatusChange struct {
	StatusChange StatusChange `json:"status_change"`
	TestCase     TestCase     `json:"test_case"` // Reuse the existing TestCase struct
}

TestCaseStatusChange represents the payload for test_case.status_changed events from Trunk.io See: https://www.svix.com/event-types/us/org_2eQPL41Ew5XSHxiXZIamIUIXg8H/#test_case.status_changed

func (TestCaseStatusChange) GetTestCase

func (s TestCaseStatusChange) GetTestCase() TestCase

GetTestCase implements the WebhookEvent interface

func (TestCaseStatusChange) GetType

func (s TestCaseStatusChange) GetType() WebhookType

GetType implements the WebhookEvent interface

type Ticket

type Ticket struct {
	HTMLURL string `json:"html_url"`
}

Ticket represents the Jira ticket associated with a test case.

type WebhookEnvelope

type WebhookEnvelope struct {
	Type string          `json:"type"`
	Data json.RawMessage `json:"-"` // The entire JSON payload for specific parsing
}

WebhookEnvelope is the common structure for all webhook events

type WebhookEvent

type WebhookEvent interface {
	GetType() WebhookType
	GetTestCase() TestCase // Common test case information
}

WebhookEvent is an interface that all webhook events must implement

func ParseWebhookEvent

func ParseWebhookEvent(data []byte) (WebhookEvent, error)

ParseWebhookEvent parses raw JSON into the appropriate webhook event type

type WebhookType

type WebhookType string

WebhookType represents the type of webhook event

const (
	// WebhookTypeQuarantiningSettingChanged represents a quarantining setting change event
	WebhookTypeQuarantiningSettingChanged WebhookType = "test_case.quarantining_setting_changed"
	// WebhookTypeStatusChanged represents a status change event
	WebhookTypeStatusChanged WebhookType = "test_case.status_changed"
)

func GetWebhookType

func GetWebhookType(data []byte) (WebhookType, error)

GetWebhookType quickly extracts just the type from raw JSON without full parsing

Jump to

Keyboard shortcuts

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