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
- Variables
- func Comment(input Input) (string, error)
- func Result(input Input) (string, error)
- func Summary(input Input) (string, error)
- func VerdictSentences(verdict policy.Verdict, shown bool) []string
- type Amounts
- type Block
- type BlockAgentError
- type BlockCIRun
- type BlockCap
- type BlockData
- type BlockFigure
- type CheckOutput
- type Config
- type Data
- type Figure
- type Input
- type Ledger
- type Output
- type Reason
- type Reconciliation
- type RowError
- type State
- type Trace
Constants ¶
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" )
const ( AccountGitHub = "github" AccountGitLab = "gitlab" )
The hosts an account link can name.
const Multiple = "multiple"
Multiple is the value of currency, cost_basis or catalog_version when the run's figures hold more than one. The figure outputs are then empty, because figures in two currencies or two bases are never summed.
Variables ¶
var OutputNames = []string{
"cost", "currency", "cost_basis",
"tokens_input_uncached", "tokens_cache_read", "tokens_cache_write", "tokens_output",
"enforcement_grade", "catalog_version", "check_run_id", "conclusion", "result",
}
OutputNames are the action's outputs, in the order it declares them.
Functions ¶
func Comment ¶
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.
func Result ¶
Result renders the result object as the check run carries it: every amount in it, and the check run's id once the check names it.
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"`
RunReason run.Reason `json:"run_reason,omitempty"`
ReaderError string `json:"reader_error,omitempty"`
// AgentError is the class of error the agent ended with, null when the
// run stated none.
AgentError *BlockAgentError `json:"agent_error"`
// AgentSkipped says whether the CI system skipped the agent's job, null
// when the report was not told.
AgentSkipped *bool `json:"agent_skipped"`
HeadSHA string `json:"head_sha"`
// CIRun is the run the environment names, a claim, null when it names
// none.
CIRun *BlockCIRun `json:"ci_run"`
Amounts string `json:"amounts"`
Rows int `json:"rows"`
RunIdentity string `json:"run_identity"`
Figures []BlockFigure `json:"figures"`
FiguresOmitted int `json:"figures_omitted"`
Cap *BlockCap `json:"cap"`
Data BlockData `json:"data"`
// Policy is the verdict, without its amounts when amounts are withheld.
Policy *policy.Verdict `json:"policy,omitempty"`
}
Block is the result object, the machine-readable block. Strings under Data originate in the pull request.
type BlockAgentError ¶
type BlockAgentError struct {
Class string `json:"class"`
}
BlockAgentError names an agent's error by its class from a closed list, never by the agent's own text.
type BlockCIRun ¶
BlockCIRun is GitHub Actions' run id and attempt as the environment states them.
type BlockCap ¶
type BlockCap struct {
Currency string `json:"currency"`
Amount string `json:"amount"`
EnforcementGrade *string `json:"enforcement_grade"`
Cost *string `json:"cost"`
CostBasis *string `json:"cost_basis"`
Remainder *string `json:"remainder"`
Overshoot *string `json:"overshoot"`
OvershootTokens *string `json:"overshoot_tokens"`
}
BlockCap is the run's cap beside the figure it holds. The fields after EnforcementGrade are null when no single figure compares with it.
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"`
Tokens map[string]string `json:"tokens,omitempty"`
}
BlockFigure is one Figure. ListCost and Tokens are empty when amounts are withheld.
type CheckOutput ¶
CheckOutput is the output of the check run.
func Check ¶
func Check(input Input) (CheckOutput, error)
Check renders the check output. Text carries the result object, 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 ¶
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.
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
// Tokens is every token count of the figure by class, with no cap.
Tokens map[focus.TokenClass]decimal.Decimal
// Catalogs are the catalog versions of the figure's rows.
Catalogs []Data
}
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 cap in USD, nil when the report was not told.
// Rows record the enforcement grade and do not record the cap. The
// policy's budget is a separate amount, read from costroid.yml.
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
// IDInCheck says the check run's own text names CheckRunID. The id exists
// only once the check is created, so the report writes it in a second
// request, and the result object names it once that request succeeds.
IDInCheck bool
Amounts Amounts
// Reason says why there are no figures, and ReasonLine names the line of
// an InvalidRow.
Reason Reason
ReasonLine int
// RunReason is why the run wrote no rows, as the run stated it. It is
// shown only when the rows file holds no rows.
RunReason run.Reason
// ReaderLine is the reader's first error line, shown only with the run
// reason reader_refused, and cleaned again before it is.
ReaderLine string
// AgentError is the class of error the agent ended in, as the run stated
// it. A class outside the closed list is dropped.
AgentError adapter.AgentError
// AgentSkipped says whether the CI system skipped the agent's job, nil
// when the report was not told. The reason agent_skipped says it too.
AgentSkipped *bool
// CIRun is the CI run as the environment states it, nil when it states
// none. A run whose id or attempt is below 1 is dropped.
CIRun *BlockCIRun
// 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
// BeforeCheck marks the object costroid run prints, which no check has
// concluded yet, so its conclusion is null.
BeforeCheck bool
// Reconciliations are the last reconciliation of each credential, shown
// under the budget when present.
Reconciliations []Reconciliation
// Verdict is the policy's, nil when no policy ran.
Verdict *policy.Verdict
}
Input is everything a render reads.
func (Input) Conclusion ¶
Conclusion is the check's conclusion: the verdict's, or neutral when no policy ran.
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
// contains filtered or unexported fields
}
Ledger is what a rows file says, reduced to the fields the comment shows.
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" // ForkNoRows is a fork's pull request whose workflow run left no rows // file, as the follow-up finds it. The recipes run no agent there. ForkNoRows Reason = "fork_no_rows" // AgentSkipped is a pull request whose agent job the CI system skipped, // so no agent ran and no rows were written. AgentSkipped Reason = "agent_skipped" )
The reasons. The zero value states only what the State says.
type Reconciliation ¶
type Reconciliation struct {
// Credential is the row's SubAccountId, a string from the rows.
Credential Data
// Day is the UTC day reconciled, as YYYY-MM-DD.
Day string
State reconcile.State
}
Reconciliation is the last reconciliation of one credential's day.
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.