|
| 1 | +// |
| 2 | +// Copyright 2026 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +// Package repositoryconfig reads the Chainloop configuration committed to a repository. |
| 17 | +package repositoryconfig |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + |
| 25 | + tracegit "github.com/chainloop-dev/chainloop/app/cli/internal/trace/git" |
| 26 | + "gopkg.in/yaml.v3" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + chainloopYMLFile = ".chainloop.yml" |
| 31 | + chainloopYAMLFile = ".chainloop.yaml" |
| 32 | +) |
| 33 | + |
| 34 | +// ErrChainloopYMLNotFound indicates that neither supported config filename exists. |
| 35 | +var ErrChainloopYMLNotFound = errors.New(".chainloop.yml not found") |
| 36 | + |
| 37 | +// ChainloopYML represents the relevant fields in .chainloop.yml. |
| 38 | +type ChainloopYML struct { |
| 39 | + ProjectName string `yaml:"projectName"` |
| 40 | + ProjectVersion string `yaml:"projectVersion"` |
| 41 | + Organization string `yaml:"organization,omitempty"` |
| 42 | + RequireTrace *bool `yaml:"requireTrace,omitempty"` |
| 43 | + WorkflowName string `yaml:"workflowName,omitempty"` |
| 44 | +} |
| 45 | + |
| 46 | +// LoadChainloopYML loads the nearest .chainloop.yml (or .chainloop.yaml), |
| 47 | +// walking from dir to the git repository root. In each directory, .yml takes |
| 48 | +// precedence over .yaml. A config without projectName is still valid. |
| 49 | +func LoadChainloopYML(dir string) (*ChainloopYML, string, error) { |
| 50 | + dir = resolveDir(dir) |
| 51 | + _, repoRoot, err := tracegit.FindGitDirAndRootFrom(dir) |
| 52 | + if err != nil { |
| 53 | + if !errors.Is(err, tracegit.ErrNotARepository) { |
| 54 | + return nil, "", err |
| 55 | + } |
| 56 | + repoRoot = dir |
| 57 | + } |
| 58 | + repoRoot = resolveDir(repoRoot) |
| 59 | + |
| 60 | + for { |
| 61 | + cfg, path, found, err := loadChainloopYMLFromDir(dir) |
| 62 | + if err != nil { |
| 63 | + return nil, path, err |
| 64 | + } |
| 65 | + if found { |
| 66 | + return cfg, path, nil |
| 67 | + } |
| 68 | + if dir == repoRoot { |
| 69 | + break |
| 70 | + } |
| 71 | + parent := filepath.Dir(dir) |
| 72 | + if parent == dir { |
| 73 | + break |
| 74 | + } |
| 75 | + dir = parent |
| 76 | + } |
| 77 | + |
| 78 | + return nil, "", ErrChainloopYMLNotFound |
| 79 | +} |
| 80 | + |
| 81 | +// FindChainloopYML returns the nearest repository config, or nil when it |
| 82 | +// cannot be loaded. Call LoadChainloopYML when the path or error is needed. |
| 83 | +func FindChainloopYML(dir string) *ChainloopYML { |
| 84 | + cfg, _, err := LoadChainloopYML(dir) |
| 85 | + if err != nil { |
| 86 | + return nil |
| 87 | + } |
| 88 | + return cfg |
| 89 | +} |
| 90 | + |
| 91 | +// LoadProjectFromYML returns projectName from the nearest repository config. |
| 92 | +func LoadProjectFromYML(dir string) string { |
| 93 | + if cfg := FindChainloopYML(dir); cfg != nil { |
| 94 | + return cfg.ProjectName |
| 95 | + } |
| 96 | + return "" |
| 97 | +} |
| 98 | + |
| 99 | +func resolveDir(dir string) string { |
| 100 | + abs, err := filepath.Abs(dir) |
| 101 | + if err != nil { |
| 102 | + return dir |
| 103 | + } |
| 104 | + resolved, err := filepath.EvalSymlinks(abs) |
| 105 | + if err != nil { |
| 106 | + return abs |
| 107 | + } |
| 108 | + return resolved |
| 109 | +} |
| 110 | + |
| 111 | +func loadChainloopYMLFromDir(dir string) (*ChainloopYML, string, bool, error) { |
| 112 | + for _, name := range []string{chainloopYMLFile, chainloopYAMLFile} { |
| 113 | + path := filepath.Join(dir, name) |
| 114 | + data, err := os.ReadFile(path) |
| 115 | + if errors.Is(err, os.ErrNotExist) { |
| 116 | + continue |
| 117 | + } |
| 118 | + if err != nil { |
| 119 | + return nil, path, true, fmt.Errorf("read %s: %w", filepath.Base(path), err) |
| 120 | + } |
| 121 | + |
| 122 | + var cfg ChainloopYML |
| 123 | + if err := yaml.Unmarshal(data, &cfg); err != nil { |
| 124 | + return nil, path, true, fmt.Errorf("parse %s: %w", filepath.Base(path), err) |
| 125 | + } |
| 126 | + return &cfg, path, true, nil |
| 127 | + } |
| 128 | + return nil, "", false, nil |
| 129 | +} |
0 commit comments