| | | 1 | | package app |
| | | 2 | | |
| | | 3 | | import ( |
| | | 4 | | "context" |
| | | 5 | | "fmt" |
| | | 6 | | "strings" |
| | | 7 | | |
| | | 8 | | "github.com/jedi-knights/go-semantic-release/internal/domain" |
| | | 9 | | "github.com/jedi-knights/go-semantic-release/internal/ports" |
| | | 10 | | ) |
| | | 11 | | |
| | | 12 | | // ConditionVerifier checks that all prerequisites for a release are met. |
| | | 13 | | type ConditionVerifier struct { |
| | | 14 | | git ports.GitRepository |
| | | 15 | | config domain.Config |
| | | 16 | | logger ports.Logger |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | // NewConditionVerifier creates a condition verifier. |
| | | 20 | | func NewConditionVerifier(git ports.GitRepository, config domain.Config, logger ports.Logger) *ConditionVerifier { |
| | | 21 | | return &ConditionVerifier{git: git, config: config, logger: logger} |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | // VerificationResult captures the outcome of condition checks. |
| | | 25 | | type VerificationResult struct { |
| | | 26 | | Passed bool |
| | | 27 | | Failures []string |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | // Verify checks all release conditions. |
| | 7 | 31 | | func (v *ConditionVerifier) Verify(ctx context.Context) (*VerificationResult, error) { |
| | 7 | 32 | | result := &VerificationResult{Passed: true} |
| | 7 | 33 | | |
| | 7 | 34 | | branch, err := v.git.CurrentBranch(ctx) |
| | 1 | 35 | | if err != nil { |
| | 1 | 36 | | return nil, fmt.Errorf("getting current branch: %w", err) |
| | 1 | 37 | | } |
| | | 38 | | |
| | 6 | 39 | | v.checkBranch(branch, result) |
| | 6 | 40 | | v.checkGitHub(result) |
| | 6 | 41 | | |
| | 6 | 42 | | return result, nil |
| | | 43 | | } |
| | | 44 | | |
| | 6 | 45 | | func (v *ConditionVerifier) checkBranch(branch string, result *VerificationResult) { |
| | 6 | 46 | | policy := domain.FindBranchPolicy(v.config.Branches, branch) |
| | 1 | 47 | | if policy == nil { |
| | 1 | 48 | | result.Passed = false |
| | 1 | 49 | | result.Failures = append(result.Failures, |
| | 1 | 50 | | fmt.Sprintf("branch %q is not configured for releases", branch)) |
| | 1 | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | 6 | 54 | | func (v *ConditionVerifier) checkGitHub(result *VerificationResult) { |
| | 3 | 55 | | if !v.config.GitHub.CreateRelease { |
| | 3 | 56 | | return |
| | 3 | 57 | | } |
| | | 58 | | |
| | 3 | 59 | | var missing []string |
| | 2 | 60 | | if v.config.GitHub.Owner == "" { |
| | 2 | 61 | | missing = append(missing, "github.owner") |
| | 2 | 62 | | } |
| | 1 | 63 | | if v.config.GitHub.Repo == "" { |
| | 1 | 64 | | missing = append(missing, "github.repo") |
| | 1 | 65 | | } |
| | 2 | 66 | | if v.config.GitHub.Token == "" { |
| | 2 | 67 | | missing = append(missing, "github.token (or SEMANTIC_RELEASE_GITHUB_TOKEN)") |
| | 2 | 68 | | } |
| | | 69 | | |
| | 3 | 70 | | if len(missing) > 0 { |
| | 3 | 71 | | result.Passed = false |
| | 3 | 72 | | result.Failures = append(result.Failures, |
| | 3 | 73 | | fmt.Sprintf("missing GitHub config: %s", strings.Join(missing, ", "))) |
| | 3 | 74 | | } |
| | | 75 | | } |