| | | 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 | | // Pipeline orchestrates the semantic release lifecycle. |
| | | 13 | | type Pipeline struct { |
| | | 14 | | plugins []ports.Plugin |
| | | 15 | | logger ports.Logger |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | // NewPipeline creates a new lifecycle pipeline with the given plugins. |
| | | 19 | | func NewPipeline(plugins []ports.Plugin, logger ports.Logger) *Pipeline { |
| | | 20 | | return &Pipeline{plugins: plugins, logger: logger} |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | // Execute runs all lifecycle steps in order against the release context. |
| | 10 | 24 | | func (p *Pipeline) Execute(ctx context.Context, rc *domain.ReleaseContext) error { |
| | 1 | 25 | | if err := p.runVerifyConditions(ctx, rc); err != nil { |
| | 1 | 26 | | return p.handleFailure(ctx, rc, err) |
| | 1 | 27 | | } |
| | | 28 | | |
| | 9 | 29 | | releaseType, err := p.runAnalyzeCommits(ctx, rc) |
| | 0 | 30 | | if err != nil { |
| | 0 | 31 | | return p.handleFailure(ctx, rc, err) |
| | 0 | 32 | | } |
| | | 33 | | |
| | 1 | 34 | | if !releaseType.IsReleasable() { |
| | 1 | 35 | | p.logger.Info("no releasable changes found") |
| | 1 | 36 | | return nil |
| | 1 | 37 | | } |
| | | 38 | | |
| | 1 | 39 | | if verifyErr := p.runVerifyRelease(ctx, rc); verifyErr != nil { |
| | 1 | 40 | | return p.handleFailure(ctx, rc, verifyErr) |
| | 1 | 41 | | } |
| | | 42 | | |
| | 7 | 43 | | notes, err := p.runGenerateNotes(ctx, rc) |
| | 1 | 44 | | if err != nil { |
| | 1 | 45 | | return p.handleFailure(ctx, rc, err) |
| | 1 | 46 | | } |
| | 6 | 47 | | rc.Notes = notes |
| | 6 | 48 | | |
| | 2 | 49 | | if rc.DryRun { |
| | 2 | 50 | | p.logger.Info("dry run complete, skipping prepare/publish/addChannel/success steps") |
| | 2 | 51 | | return nil |
| | 2 | 52 | | } |
| | | 53 | | |
| | 1 | 54 | | if err := p.runPrepare(ctx, rc); err != nil { |
| | 1 | 55 | | return p.handleFailure(ctx, rc, err) |
| | 1 | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | if err := p.runPublish(ctx, rc); err != nil { |
| | 0 | 59 | | return p.handleFailure(ctx, rc, err) |
| | 0 | 60 | | } |
| | | 61 | | |
| | 1 | 62 | | if err := p.runAddChannel(ctx, rc); err != nil { |
| | 1 | 63 | | p.logger.Warn("addChannel failed", "error", err) |
| | 1 | 64 | | // Non-fatal — continue to success notification. |
| | 1 | 65 | | } |
| | | 66 | | |
| | 3 | 67 | | return p.runSuccess(ctx, rc) |
| | | 68 | | } |
| | | 69 | | |
| | 10 | 70 | | func (p *Pipeline) runVerifyConditions(ctx context.Context, rc *domain.ReleaseContext) error { |
| | 10 | 71 | | for _, plugin := range p.plugins { |
| | 10 | 72 | | if vp, ok := plugin.(ports.VerifyConditionsPlugin); ok { |
| | 10 | 73 | | p.logger.Debug("running verifyConditions", "plugin", plugin.Name()) |
| | 1 | 74 | | if err := vp.VerifyConditions(ctx, rc); err != nil { |
| | 1 | 75 | | return domain.NewReleaseError("verifyConditions:"+plugin.Name(), err) |
| | 1 | 76 | | } |
| | | 77 | | } |
| | | 78 | | } |
| | 9 | 79 | | return nil |
| | | 80 | | } |
| | | 81 | | |
| | 9 | 82 | | func (p *Pipeline) runAnalyzeCommits(ctx context.Context, rc *domain.ReleaseContext) (domain.ReleaseType, error) { |
| | 9 | 83 | | highest := domain.ReleaseNone |
| | 9 | 84 | | for _, plugin := range p.plugins { |
| | 10 | 85 | | if ap, ok := plugin.(ports.AnalyzeCommitsPlugin); ok { |
| | 10 | 86 | | p.logger.Debug("running analyzeCommits", "plugin", plugin.Name()) |
| | 10 | 87 | | rt, err := ap.AnalyzeCommits(ctx, rc) |
| | 0 | 88 | | if err != nil { |
| | 0 | 89 | | return domain.ReleaseNone, domain.NewReleaseError("analyzeCommits:"+plugin.Name(), err) |
| | 0 | 90 | | } |
| | 10 | 91 | | highest = highest.Higher(rt) |
| | | 92 | | } |
| | | 93 | | } |
| | 9 | 94 | | return highest, nil |
| | | 95 | | } |
| | | 96 | | |
| | 8 | 97 | | func (p *Pipeline) runVerifyRelease(ctx context.Context, rc *domain.ReleaseContext) error { |
| | 8 | 98 | | for _, plugin := range p.plugins { |
| | 1 | 99 | | if vp, ok := plugin.(ports.VerifyReleasePlugin); ok { |
| | 1 | 100 | | p.logger.Debug("running verifyRelease", "plugin", plugin.Name()) |
| | 1 | 101 | | if err := vp.VerifyRelease(ctx, rc); err != nil { |
| | 1 | 102 | | return domain.NewReleaseError("verifyRelease:"+plugin.Name(), err) |
| | 1 | 103 | | } |
| | | 104 | | } |
| | | 105 | | } |
| | 7 | 106 | | return nil |
| | | 107 | | } |
| | | 108 | | |
| | 7 | 109 | | func (p *Pipeline) runGenerateNotes(ctx context.Context, rc *domain.ReleaseContext) (string, error) { |
| | 7 | 110 | | var parts []string |
| | 7 | 111 | | for _, plugin := range p.plugins { |
| | 8 | 112 | | if gp, ok := plugin.(ports.GenerateNotesPlugin); ok { |
| | 8 | 113 | | p.logger.Debug("running generateNotes", "plugin", plugin.Name()) |
| | 8 | 114 | | notes, err := gp.GenerateNotes(ctx, rc) |
| | 1 | 115 | | if err != nil { |
| | 1 | 116 | | return "", domain.NewReleaseError("generateNotes:"+plugin.Name(), err) |
| | 1 | 117 | | } |
| | 5 | 118 | | if notes != "" { |
| | 5 | 119 | | parts = append(parts, notes) |
| | 5 | 120 | | } |
| | | 121 | | } |
| | | 122 | | } |
| | 6 | 123 | | return strings.Join(parts, "\n\n"), nil |
| | | 124 | | } |
| | | 125 | | |
| | 4 | 126 | | func (p *Pipeline) runPrepare(ctx context.Context, rc *domain.ReleaseContext) error { |
| | 4 | 127 | | for _, plugin := range p.plugins { |
| | 4 | 128 | | if pp, ok := plugin.(ports.PreparePlugin); ok { |
| | 4 | 129 | | p.logger.Debug("running prepare", "plugin", plugin.Name()) |
| | 1 | 130 | | if err := pp.Prepare(ctx, rc); err != nil { |
| | 1 | 131 | | return domain.NewReleaseError("prepare:"+plugin.Name(), err) |
| | 1 | 132 | | } |
| | | 133 | | } |
| | | 134 | | } |
| | 3 | 135 | | return nil |
| | | 136 | | } |
| | | 137 | | |
| | 3 | 138 | | func (p *Pipeline) runPublish(ctx context.Context, rc *domain.ReleaseContext) error { |
| | 3 | 139 | | for _, plugin := range p.plugins { |
| | 3 | 140 | | if pp, ok := plugin.(ports.PublishPlugin); ok { |
| | 3 | 141 | | p.logger.Debug("running publish", "plugin", plugin.Name()) |
| | 3 | 142 | | result, err := pp.Publish(ctx, rc) |
| | 0 | 143 | | if err != nil { |
| | 0 | 144 | | return domain.NewReleaseError("publish:"+plugin.Name(), err) |
| | 0 | 145 | | } |
| | 1 | 146 | | if result != nil && rc.Result != nil { |
| | 1 | 147 | | rc.Result.Projects = append(rc.Result.Projects, *result) |
| | 1 | 148 | | } |
| | | 149 | | } |
| | | 150 | | } |
| | 3 | 151 | | return nil |
| | | 152 | | } |
| | | 153 | | |
| | 3 | 154 | | func (p *Pipeline) runAddChannel(ctx context.Context, rc *domain.ReleaseContext) error { |
| | 3 | 155 | | for _, plugin := range p.plugins { |
| | 1 | 156 | | if ap, ok := plugin.(ports.AddChannelPlugin); ok { |
| | 1 | 157 | | p.logger.Debug("running addChannel", "plugin", plugin.Name()) |
| | 1 | 158 | | if err := ap.AddChannel(ctx, rc); err != nil { |
| | 1 | 159 | | return fmt.Errorf("addChannel:%s: %w", plugin.Name(), err) |
| | 1 | 160 | | } |
| | | 161 | | } |
| | | 162 | | } |
| | 2 | 163 | | return nil |
| | | 164 | | } |
| | | 165 | | |
| | 3 | 166 | | func (p *Pipeline) runSuccess(ctx context.Context, rc *domain.ReleaseContext) error { |
| | 3 | 167 | | for _, plugin := range p.plugins { |
| | 3 | 168 | | if sp, ok := plugin.(ports.SuccessPlugin); ok { |
| | 3 | 169 | | p.logger.Debug("running success", "plugin", plugin.Name()) |
| | 1 | 170 | | if err := sp.Success(ctx, rc); err != nil { |
| | 1 | 171 | | p.logger.Warn("success notification failed", "plugin", plugin.Name(), "error", err) |
| | 1 | 172 | | // Non-fatal — don't fail the release for notification failures. |
| | 1 | 173 | | } |
| | | 174 | | } |
| | | 175 | | } |
| | 3 | 176 | | return nil |
| | | 177 | | } |
| | | 178 | | |
| | 4 | 179 | | func (p *Pipeline) handleFailure(ctx context.Context, rc *domain.ReleaseContext, releaseErr error) error { |
| | 4 | 180 | | rc.Error = releaseErr |
| | 4 | 181 | | for _, plugin := range p.plugins { |
| | 4 | 182 | | if fp, ok := plugin.(ports.FailPlugin); ok { |
| | 4 | 183 | | p.logger.Debug("running fail", "plugin", plugin.Name()) |
| | 0 | 184 | | if err := fp.Fail(ctx, rc); err != nil { |
| | 0 | 185 | | p.logger.Warn("fail notification failed", "plugin", plugin.Name(), "error", err) |
| | 0 | 186 | | } |
| | | 187 | | } |
| | | 188 | | } |
| | 4 | 189 | | return releaseErr |
| | | 190 | | } |