ci

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package ci provides shared CI/CD types and interfaces for provider-agnostic plan result handling and PR/MR comment rendering.

Index

Constants

View Source
const CommentMarker = "<!-- terraci-plan-comment -->"

CommentMarker is used to identify terraci comments for updates

Variables

This section is empty.

Functions

func FilterPlanOutput

func FilterPlanOutput(rawOutput string) string

FilterPlanOutput extracts only the diff portion from terraform plan output

func FormatCostCell

func FormatCostCell(p *ModulePlan) string

FormatCostCell formats the cost cell for a module plan

func FormatCostDiff

func FormatCostDiff(diff float64) string

FormatCostDiff formats a cost difference with sign

func FormatMonthlyCost

func FormatMonthlyCost(cost float64) string

FormatMonthlyCost formats a monthly cost value

func FormatPlanDetails

func FormatPlanDetails(p *plan.ParsedPlan) string

FormatPlanDetails formats a parsed plan as structured resource list grouped by action

func FormatPlanSummary

func FormatPlanSummary(p *plan.ParsedPlan) string

FormatPlanSummary formats a parsed plan as a compact summary line

func HasReportableChanges

func HasReportableChanges(plans []ModulePlan, policySummary *PolicySummary) bool

HasReportableChanges returns true if there are plan changes, failures, or policy violations that warrant posting a comment.

func ParseModulePathComponents

func ParseModulePathComponents(modulePath string, segments []string) map[string]string

ParseModulePathComponents parses a module path using the given segment names and returns a map of component name to value. Extra path parts beyond the defined segments are joined as "submodule".

func Truncate

func Truncate(s string, maxLen int) string

Truncate truncates a string to maxLen characters

Types

type CommentData

type CommentData struct {
	Plans         []ModulePlan
	PolicySummary *PolicySummary
	PipelineURL   string
	PipelineID    string
	CommitSHA     string
	GeneratedAt   time.Time
	TotalModules  int
}

CommentData contains all data needed to render a PR/MR comment

type CommentRenderer

type CommentRenderer struct{}

CommentRenderer renders PR/MR comments

func NewCommentRenderer

func NewCommentRenderer() *CommentRenderer

NewCommentRenderer creates a new comment renderer

func (*CommentRenderer) Render

func (r *CommentRenderer) Render(data *CommentData) string

Render generates the comment body

type CommentService

type CommentService interface {
	IsEnabled() bool
	UpsertComment(plans []ModulePlan, policySummary *PolicySummary) error
}

CommentService defines the interface for posting plan summaries to PRs/MRs

type ModulePlan

type ModulePlan struct {
	ModuleID          string
	ModulePath        string
	Components        map[string]string
	Status            PlanStatus
	Summary           string // Compact summary e.g., "+2 ~1 -1"
	StructuredDetails string // Structured resource list by action (markdown)
	RawPlanOutput     string // Filtered raw plan output (diff only)
	Error             string // Error message if plan failed
	Duration          time.Duration
	// Cost estimation fields
	CostBefore float64 // Monthly cost before changes (USD)
	CostAfter  float64 // Monthly cost after changes (USD)
	CostDiff   float64 // Cost difference (after - before)
	HasCost    bool    // True if cost was calculated
}

ModulePlan represents terraform plan output for a single module

func (*ModulePlan) Get

func (p *ModulePlan) Get(name string) string

Get returns the value of a named component from the Components map.

type PlanResult

type PlanResult struct {
	ModuleID          string            `json:"module_id"`
	ModulePath        string            `json:"module_path"`
	Components        map[string]string `json:"components,omitempty"`
	Status            PlanStatus        `json:"status"`
	Summary           string            `json:"summary"`
	StructuredDetails string            `json:"structured_details,omitempty"`
	RawPlanOutput     string            `json:"raw_plan_output,omitempty"`
	Error             string            `json:"error,omitempty"`
	ExitCode          int               `json:"exit_code"`
	// Cost estimation fields
	CostBefore float64 `json:"cost_before,omitempty"`
	CostAfter  float64 `json:"cost_after,omitempty"`
	CostDiff   float64 `json:"cost_diff,omitempty"`
	HasCost    bool    `json:"has_cost,omitempty"`
}

PlanResult represents the result of a terraform plan for a single module

func (*PlanResult) Get

func (r *PlanResult) Get(name string) string

Get returns the value of a named component from the Components map.

type PlanResultCollection

type PlanResultCollection struct {
	Results     []PlanResult `json:"results"`
	PipelineID  string       `json:"pipeline_id,omitempty"`
	CommitSHA   string       `json:"commit_sha,omitempty"`
	GeneratedAt time.Time    `json:"generated_at"`
}

PlanResultCollection is a collection of plan results from multiple jobs

func ScanPlanResults

func ScanPlanResults(rootDir string, segments []string) (*PlanResultCollection, error)

ScanPlanResults scans for plan.json files in module directories and builds a collection of plan results from their contents. If segments is nil or empty, default segments (service/environment/region/module) are used.

func (*PlanResultCollection) ToModulePlans

func (c *PlanResultCollection) ToModulePlans() []ModulePlan

ToModulePlans converts plan results to ModulePlan for comment rendering

type PlanStatus

type PlanStatus string

PlanStatus represents the status of a terraform plan

const (
	PlanStatusPending   PlanStatus = "pending"
	PlanStatusRunning   PlanStatus = "running"
	PlanStatusSuccess   PlanStatus = "success"
	PlanStatusNoChanges PlanStatus = "no_changes"
	PlanStatusChanges   PlanStatus = "changes"
	PlanStatusFailed    PlanStatus = "failed"
)

type PolicyResult

type PolicyResult struct {
	Module   string
	Failures []PolicyViolation
	Warnings []PolicyViolation
}

PolicyResult contains policy check results for a single module.

type PolicySummary

type PolicySummary struct {
	TotalModules  int
	PassedModules int
	WarnedModules int
	FailedModules int
	TotalFailures int
	TotalWarnings int
	Results       []PolicyResult
}

PolicySummary contains policy check data needed for rendering PR/MR comments. This is a CI-local type to avoid coupling with the policy package.

func (*PolicySummary) HasFailures

func (s *PolicySummary) HasFailures() bool

HasFailures returns true if any module has failures.

func (*PolicySummary) HasWarnings

func (s *PolicySummary) HasWarnings() bool

HasWarnings returns true if any module has warnings.

type PolicyViolation

type PolicyViolation struct {
	Namespace string
	Message   string
}

PolicyViolation represents a single policy violation.

Jump to

Keyboard shortcuts

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