codegen

package
v1.4.5 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package codegen provides code generation tooling for GitLab CI/CD types.

Index

Constants

View Source
const (
	// GitLabCISchemaURL is the URL to the GitLab CI JSON schema.
	GitLabCISchemaURL = "https://gitlab.com/gitlab-org/gitlab/-/raw/master/app/assets/javascripts/editor/schema/ci.json"

	// ComponentBaseURL is the base URL for GitLab CI/CD components.
	ComponentBaseURL = "https://gitlab.com/components"
)

Schema source URLs

Variables

View Source
var DefaultComponents = []string{
	"sast",
	"secret-detection",
	"dependency-scanning",
	"container-scanning",
	"dast",
	"license-scanning",
	"coverage-report",
}

DefaultComponents is the list of official GitLab CI/CD components.

Functions

func FieldNameFromInput

func FieldNameFromInput(s string) string

FieldNameFromInput converts an input name to a Go field name. Examples: "stage" -> "Stage", "scan_type" -> "ScanType", "SAST_EXCLUDED_PATHS" -> "SastExcludedPaths"

func ToPascalCase

func ToPascalCase(s string) string

ToPascalCase converts a string to PascalCase. Examples: "sast" -> "Sast", "secret-detection" -> "SecretDetection"

func ToSnakeCase

func ToSnakeCase(s string) string

ToSnakeCase converts a string to snake_case. Examples: "SecretDetection" -> "secret_detection", "secret-detection" -> "secret_detection"

Types

type CISchemaIR

type CISchemaIR struct {
	// TopLevel contains top-level keywords like stages, include, variables
	TopLevel map[string]PropertyIR `json:"top_level"`
	// JobProperties contains properties available in job definitions
	JobProperties map[string]PropertyIR `json:"job_properties"`
	// Definitions contains reusable schema definitions
	Definitions map[string]DefinitionIR `json:"definitions"`
}

CISchemaIR is the intermediate representation of the parsed CI schema.

type ComponentIR

type ComponentIR struct {
	Name        string           `json:"name"`
	Description string           `json:"description,omitempty"`
	Inputs      []ComponentInput `json:"inputs,omitempty"`
	Outputs     []string         `json:"outputs,omitempty"`
}

ComponentIR is the intermediate representation of a component specification.

type ComponentInput

type ComponentInput struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	Type        string `json:"type,omitempty"`
	Default     any    `json:"default,omitempty"`
	Required    bool   `json:"required,omitempty"`
}

ComponentInput represents an input parameter for a component.

type DefinitionIR

type DefinitionIR struct {
	Name        string       `json:"name"`
	Description string       `json:"description,omitempty"`
	Type        string       `json:"type"`
	Properties  []PropertyIR `json:"properties,omitempty"`
	Enum        []string     `json:"enum,omitempty"`
}

DefinitionIR is the intermediate representation of a schema definition.

type FetchResult

type FetchResult struct {
	URL      string `json:"url"`
	Path     string `json:"path"`
	Size     int64  `json:"size"`
	Duration string `json:"duration"`
	Error    string `json:"error,omitempty"`
}

FetchResult represents the result of a fetch operation.

type Fetcher

type Fetcher struct {
	Client     *http.Client
	SpecsDir   string
	MaxRetries int
	RetryDelay time.Duration
}

Fetcher handles downloading schemas and specs from remote sources.

func NewFetcher

func NewFetcher(specsDir string) *Fetcher

NewFetcher creates a new Fetcher with default settings.

func (*Fetcher) Fetch

func (f *Fetcher) Fetch(url string) ([]byte, error)

Fetch downloads content from a URL with retry logic.

func (*Fetcher) FetchAll

func (f *Fetcher) FetchAll() (*Manifest, error)

FetchAll downloads the CI schema and all default components.

func (*Fetcher) FetchCISchema

func (f *Fetcher) FetchCISchema() (*FetchResult, error)

FetchCISchema downloads the GitLab CI JSON schema.

func (*Fetcher) FetchComponent

func (f *Fetcher) FetchComponent(name string) (*FetchResult, error)

FetchComponent downloads a component specification.

func (*Fetcher) ReadManifest

func (f *Fetcher) ReadManifest() (*Manifest, error)

ReadManifest reads the manifest from specs/manifest.json.

func (*Fetcher) WriteManifest

func (f *Fetcher) WriteManifest(m *Manifest) error

WriteManifest writes the manifest to specs/manifest.json.

type GenerateResult

type GenerateResult struct {
	Name string
	Path string
	Size int64
	Err  error
}

GenerateResult represents the result of a code generation operation.

type Generator

type Generator struct {
	OutputDir string
}

Generator generates Go code from parsed schemas and component specs.

func NewGenerator

func NewGenerator(outputDir string) *Generator

NewGenerator creates a new Generator.

func (*Generator) GenerateAll

func (g *Generator) GenerateAll(components map[string]*ComponentIR) (map[string]*GenerateResult, error)

GenerateAll generates Go files for all components.

func (*Generator) GenerateComponentCode

func (g *Generator) GenerateComponentCode(ir *ComponentIR) (string, error)

GenerateComponentCode generates Go code for a component.

func (*Generator) GenerateComponentFile

func (g *Generator) GenerateComponentFile(ir *ComponentIR) error

GenerateComponentFile generates a Go file for a component.

type JSONSchema

type JSONSchema struct {
	Schema      string                `json:"$schema,omitempty"`
	ID          string                `json:"$id,omitempty"`
	Title       string                `json:"title,omitempty"`
	Description string                `json:"description,omitempty"`
	Type        any                   `json:"type,omitempty"`
	Properties  map[string]JSONSchema `json:"properties,omitempty"`
	Items       *JSONSchema           `json:"items,omitempty"`
	Ref         string                `json:"$ref,omitempty"`
	OneOf       []JSONSchema          `json:"oneOf,omitempty"`
	AnyOf       []JSONSchema          `json:"anyOf,omitempty"`
	AllOf       []JSONSchema          `json:"allOf,omitempty"`
	Enum        []any                 `json:"enum,omitempty"`
	Const       any                   `json:"const,omitempty"`
	Default     any                   `json:"default,omitempty"`
	Required    []string              `json:"required,omitempty"`
	Defs        map[string]JSONSchema `json:"$defs,omitempty"`
	Definitions map[string]JSONSchema `json:"definitions,omitempty"`
	Pattern     string                `json:"pattern,omitempty"`
	MinLength   *int                  `json:"minLength,omitempty"`
	MaxLength   *int                  `json:"maxLength,omitempty"`
	Minimum     *float64              `json:"minimum,omitempty"`
	Maximum     *float64              `json:"maximum,omitempty"`
	MinItems    *int                  `json:"minItems,omitempty"`
	MaxItems    *int                  `json:"maxItems,omitempty"`
	Format      string                `json:"format,omitempty"`
}

JSONSchema represents a JSON Schema document.

type Manifest

type Manifest struct {
	FetchedAt  time.Time              `json:"fetched_at"`
	CISchema   *FetchResult           `json:"ci_schema,omitempty"`
	Components map[string]FetchResult `json:"components,omitempty"`
}

Manifest tracks fetched schemas and their metadata.

type Parser

type Parser struct {
	SpecsDir string
}

Parser parses GitLab CI schemas and component specs.

func NewParser

func NewParser(specsDir string) *Parser

NewParser creates a new Parser.

func (*Parser) ParseAllComponents

func (p *Parser) ParseAllComponents() (map[string]*ComponentIR, error)

ParseAllComponents parses all component specifications.

func (*Parser) ParseCISchema

func (p *Parser) ParseCISchema() (*CISchemaIR, error)

ParseCISchema parses the GitLab CI JSON schema.

func (*Parser) ParseComponent

func (p *Parser) ParseComponent(name string) (*ComponentIR, error)

ParseComponent parses a component specification from YAML.

type PropertyIR

type PropertyIR struct {
	Name        string       `json:"name"`
	Description string       `json:"description,omitempty"`
	Type        string       `json:"type"`
	Required    bool         `json:"required,omitempty"`
	Enum        []string     `json:"enum,omitempty"`
	Default     any          `json:"default,omitempty"`
	Items       *PropertyIR  `json:"items,omitempty"`
	Properties  []PropertyIR `json:"properties,omitempty"`
	RefName     string       `json:"ref_name,omitempty"`
}

PropertyIR is the intermediate representation of a schema property.

Jump to

Keyboard shortcuts

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