comment

package
v1.0.0-alpha.5 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: Apache-2.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package comment turns FOCUS rows into the check output and the sticky pull request comment.

Everything rendered is a structured field. A string from the pull request reaches a template only as Data.

Index

Constants

View Source
const (
	// Marker opens the sticky comment, which is how the next run finds it.
	Marker = "<!-- costroid:comment -->"
	// CheckName is the one check run's name.
	CheckName = "costroid"
	// Conclusion is the check's conclusion until a policy decides one.
	Conclusion = "neutral"
)
View Source
const (
	AccountGitHub = "github"
	AccountGitLab = "gitlab"
)

The hosts an account link can name.

Variables

This section is empty.

Functions

func Comment

func Comment(input Input) (string, error)

Comment renders the sticky comment. Its block is a copy of the check's that names the check run, without the amounts unless they are shown.

Types

type Amounts

type Amounts int

Amounts says whether the comment carries amounts, and why not.

const (
	// AmountsUnsettled withholds, because the setting or the visibility could
	// not be read.
	AmountsUnsettled Amounts = iota
	// AmountsOff withholds on a public repository with comment.show_cost false.
	AmountsOff
	// AmountsShown is the default.
	AmountsShown
)

The zero value withholds, so an Input that never set the field cannot publish an amount.

type Block

type Block struct {
	Schema         string        `json:"schema"`
	CheckRunID     *int64        `json:"check_run_id,omitempty"`
	Conclusion     string        `json:"conclusion"`
	State          State         `json:"state"`
	Reason         Reason        `json:"reason,omitempty"`
	ReasonLine     int           `json:"reason_line,omitempty"`
	HeadSHA        string        `json:"head_sha"`
	Amounts        string        `json:"amounts"`
	Rows           int           `json:"rows"`
	RunIdentity    string        `json:"run_identity"`
	Figures        []BlockFigure `json:"figures"`
	FiguresOmitted int           `json:"figures_omitted"`
	Budget         *BlockAmount  `json:"budget"`
	Data           BlockData     `json:"data"`
}

Block is the machine-readable block. Strings under Data originate in the pull request.

type BlockAmount

type BlockAmount struct {
	Currency string `json:"currency"`
	Amount   string `json:"amount"`
}

BlockAmount is an amount with its currency.

type BlockData

type BlockData struct {
	Agents        []Data `json:"agents"`
	Models        []Data `json:"models"`
	ModelsOmitted int    `json:"models_omitted"`
	Catalogs      []Data `json:"catalog_versions"`
	// FallbackModels and FallbackPrices name the rows priced at the catalog's
	// fallback tier.
	FallbackModels        []Data `json:"fallback_models"`
	FallbackModelsOmitted int    `json:"fallback_models_omitted"`
	FallbackPrices        []Data `json:"fallback_price_ids"`
	FallbackPricesOmitted int    `json:"fallback_price_ids_omitted"`
}

BlockData holds the strings that originated in the pull request, labelled as data. Each one passed the allowlist.

type BlockFigure

type BlockFigure struct {
	Currency         string `json:"currency"`
	ListCost         string `json:"list_cost,omitempty"`
	CostBasis        string `json:"cost_basis"`
	AttributionGrade string `json:"attribution_grade"`
	EnforcementGrade string `json:"enforcement_grade"`
	Rows             int    `json:"rows"`
	FallbackRows     int    `json:"fallback_rows"`
}

BlockFigure is one Figure. ListCost is empty when amounts are withheld.

type CheckOutput

type CheckOutput struct {
	Title   string
	Summary string
	Text    string
}

CheckOutput is the output of the check run.

func Check

func Check(input Input) (CheckOutput, error)

Check renders the check output. Text carries the authoritative block, with every amount in it whatever the comment shows.

type Config

type Config struct {
	ShowCost bool
}

Config is what the comment reads from costroid.yml.

func ParseConfig

func ParseConfig(data []byte) (Config, error)

ParseConfig reads costroid.yml. An empty document or an absent key leaves the default. A key the comment does not read is named on stderr and never fails the read.

type Data

type Data string

Data is a string that originated in the pull request. Clean is the only way to make one from untrusted text, and the allowlist holds no character that can close a code span, open a tag or start a mention.

func Clean

func Clean(text string) Data

Clean returns text when it passes the allowlist and the length cap.

func (Data) Code

func (d Data) Code() string

Code renders the string as a Markdown code span, so it never renders as Markdown or HTML.

type Figure

type Figure struct {
	Currency    string
	Basis       string
	Attribution string
	Enforcement string
	Cost        decimal.Decimal
	Rows        int
	// FallbackRows counts the rows the catalog priced at its fallback tier.
	FallbackRows int
	// Trace is the arithmetic behind Cost, one line per catalog, class and
	// price. TraceOmitted counts the lines left out of it.
	Trace        []Trace
	TraceOmitted int
}

Figure is the exact sum of ListCost over rows that share a currency, a basis and both grades. No figure spans two currencies.

type Input

type Input struct {
	Ledger  Ledger
	State   State
	HeadSHA string
	// Budget is the run's budget in USD, nil when the report was not told.
	// Rows record the enforcement grade and do not record the budget.
	Budget *decimal.Decimal
	// Installation is the SCM installation id, empty when no App is installed.
	// On GitLab it is the project id.
	Installation string
	// AccountHost is the host the installation id belongs to, GitHub when
	// empty. It is a name, never an instance's address.
	AccountHost string
	// CheckRunID names the posted check, nil when the post failed.
	CheckRunID *int64
	Amounts    Amounts
	// Reason says why there are no figures, and ReasonLine names the line of
	// an InvalidRow.
	Reason     Reason
	ReasonLine int
	// RunURL is the CI run's page as the environment names it, empty when it
	// names none. A value outside runURLPattern is dropped.
	RunURL string
}

Input is everything a render reads.

type Ledger

type Ledger struct {
	Rows           int
	Figures        []Figure
	FiguresOmitted int
	Agents         []Data
	Models         []Data
	ModelsOmitted  int
	Catalogs       []Data
	// FallbackModels are the models of the rows priced at the fallback tier,
	// FallbackPrices the price ids that priced them and FallbackCatalogs the
	// catalogs those rows name.
	FallbackModels        []Data
	FallbackModelsOmitted int
	FallbackPrices        []Data
	FallbackPricesOmitted int
	FallbackCatalogs      []Data
}

Ledger is what a rows file says, reduced to the fields the comment shows.

func ReadRows

func ReadRows(source io.Reader) (Ledger, error)

ReadRows reads FOCUS rows as JSON lines. One bad row refuses the file: a partial total would read as a smaller cost. A line at fault comes back as a RowError.

type Reason

type Reason string

Reason says why a report shows no figures. The list is closed, so a comment never prints an error's text.

const (
	NoRowsFile     Reason = "no_rows_file"
	EmptyRowsFile  Reason = "empty_rows_file"
	NotAFile       Reason = "rows_path_not_a_file"
	UnreadRowsFile Reason = "rows_file_unread"
	InvalidRow     Reason = "invalid_row"
)

The reasons. The zero value states only what the State says.

type RowError

type RowError struct {
	Line int
	Err  error
}

RowError names the first line of a rows file that is not a valid row.

func (*RowError) Error

func (e *RowError) Error() string

func (*RowError) Unwrap

func (e *RowError) Unwrap() error

type State

type State string

State says what the rows file gave the report.

const (
	Reported   State = "reported"
	NoRows     State = "no_rows"
	Unreadable State = "rows_unreadable"
)

The three states. An unreadable file is never shown as a zero.

type Trace

type Trace struct {
	Catalog   Data
	Class     focus.TokenClass
	PriceID   Data
	UnitPrice decimal.Decimal
	// Unit is a count of tokens, or informationalUnit for a class that carries
	// no price. Both forms are checked, so Unit is never free text.
	Unit   string
	Tokens decimal.Decimal
	Cost   decimal.Decimal
}

Trace is the rows of one Figure that share a catalog, a token class and a price. Cost is UnitPrice times Tokens over the size of Unit, exactly.

Jump to

Keyboard shortcuts

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