| | | 1 | | package app |
| | | 2 | | |
| | | 3 | | import ( |
| | | 4 | | "context" |
| | | 5 | | "errors" |
| | | 6 | | "fmt" |
| | | 7 | | |
| | | 8 | | "github.com/jedi-knights/go-semantic-release/internal/domain" |
| | | 9 | | "github.com/jedi-knights/go-semantic-release/internal/ports" |
| | | 10 | | ) |
| | | 11 | | |
| | | 12 | | // ReleaseExecutor executes a release plan by creating tags and publishing releases. |
| | | 13 | | type ReleaseExecutor struct { |
| | | 14 | | git ports.GitRepository |
| | | 15 | | tagService ports.TagService |
| | | 16 | | changelog ports.ChangelogGenerator |
| | | 17 | | publisher ports.ReleasePublisher |
| | | 18 | | logger ports.Logger |
| | | 19 | | sections []domain.ChangelogSectionConfig |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | // MustNewReleaseExecutor creates a release executor. |
| | | 23 | | // All parameters are required and must be non-nil. For publisher, pass a |
| | | 24 | | // noopPublisher (available from the DI container via di.Container.ReleasePublisher) |
| | | 25 | | // when publishing is disabled rather than passing nil. |
| | | 26 | | // Panics on any nil argument — these are programming errors, not runtime errors. |
| | | 27 | | func MustNewReleaseExecutor( |
| | | 28 | | git ports.GitRepository, |
| | | 29 | | tagService ports.TagService, |
| | | 30 | | changelog ports.ChangelogGenerator, |
| | | 31 | | publisher ports.ReleasePublisher, |
| | | 32 | | logger ports.Logger, |
| | | 33 | | sections []domain.ChangelogSectionConfig, |
| | | 34 | | ) *ReleaseExecutor { |
| | | 35 | | if git == nil { |
| | | 36 | | panic("MustNewReleaseExecutor: git must not be nil") |
| | | 37 | | } |
| | | 38 | | if tagService == nil { |
| | | 39 | | panic("MustNewReleaseExecutor: tagService must not be nil") |
| | | 40 | | } |
| | | 41 | | if changelog == nil { |
| | | 42 | | panic("MustNewReleaseExecutor: changelog must not be nil") |
| | | 43 | | } |
| | | 44 | | if publisher == nil { |
| | | 45 | | panic("MustNewReleaseExecutor: publisher must not be nil; use noopPublisher for no-op behavior") |
| | | 46 | | } |
| | | 47 | | if logger == nil { |
| | | 48 | | panic("MustNewReleaseExecutor: logger must not be nil") |
| | | 49 | | } |
| | | 50 | | return &ReleaseExecutor{ |
| | | 51 | | git: git, |
| | | 52 | | tagService: tagService, |
| | | 53 | | changelog: changelog, |
| | | 54 | | publisher: publisher, |
| | | 55 | | logger: logger, |
| | | 56 | | sections: sections, |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | // Execute runs the release for all releasable projects in the plan. |
| | | 61 | | // |
| | | 62 | | // Error model: |
| | | 63 | | // - Context cancellation and tag/push failures are returned directly and abort |
| | | 64 | | // the loop immediately. These are hard failures: git state may be partially |
| | | 65 | | // mutated (e.g. a local tag exists without a corresponding push), so continuing |
| | | 66 | | // to the next project would compound the inconsistency. |
| | | 67 | | // - Publish failures (e.g. GitHub release creation) are soft: the tag is already |
| | | 68 | | // pushed, so the release is technically done. These are collected into |
| | | 69 | | // result.Projects[i].Error so the caller can report all failures before exiting. |
| | | 70 | | // |
| | | 71 | | // Use result.HasErrors() to check whether any per-project publish error occurred. |
| | 5 | 72 | | func (e *ReleaseExecutor) Execute(ctx context.Context, plan *domain.ReleasePlan) (*domain.ReleaseResult, error) { |
| | 5 | 73 | | result := &domain.ReleaseResult{DryRun: plan.DryRun} |
| | 5 | 74 | | |
| | 5 | 75 | | releasable := plan.ReleasableProjects() |
| | 5 | 76 | | for i := range releasable { |
| | 5 | 77 | | // Cancellation is checked between projects, not during an in-progress |
| | 5 | 78 | | // executeProject call. If createAndPushTag is blocked on a slow network |
| | 5 | 79 | | // operation the context is not respected until the current project finishes. |
| | 5 | 80 | | // This is intentional: aborting mid-tag would leave git state inconsistent. |
| | 0 | 81 | | if err := ctx.Err(); err != nil { |
| | 0 | 82 | | return nil, fmt.Errorf("release cancelled: %w", err) |
| | 0 | 83 | | } |
| | 5 | 84 | | pr, err := e.executeProject(ctx, releasable[i], plan) |
| | 0 | 85 | | if err != nil { |
| | 0 | 86 | | // Hard failure (tag/push): abort immediately rather than continuing to |
| | 0 | 87 | | // create more tags in an inconsistent state. |
| | 0 | 88 | | return nil, fmt.Errorf("tagging %s: %w", releasable[i].Project.Name, err) |
| | 0 | 89 | | } |
| | 1 | 90 | | if pr.Error != nil { |
| | 1 | 91 | | e.logger.Error("publish failed", "project", pr.Project.Name, "error", pr.Error) |
| | 1 | 92 | | } |
| | 5 | 93 | | result.Projects = append(result.Projects, pr) |
| | | 94 | | } |
| | | 95 | | |
| | 5 | 96 | | return result, nil |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | func (e *ReleaseExecutor) executeProject( |
| | | 100 | | ctx context.Context, |
| | | 101 | | pp domain.ProjectReleasePlan, |
| | | 102 | | plan *domain.ReleasePlan, |
| | 5 | 103 | | ) (domain.ProjectReleaseResult, error) { |
| | 5 | 104 | | result := domain.ProjectReleaseResult{ |
| | 5 | 105 | | Project: pp.Project, |
| | 5 | 106 | | CurrentVersion: pp.CurrentVersion, |
| | 5 | 107 | | Version: pp.NextVersion, |
| | 5 | 108 | | } |
| | 5 | 109 | | |
| | 5 | 110 | | // Generate changelog. |
| | 5 | 111 | | notes, err := e.changelog.Generate(pp.NextVersion, pp.Project.Name, pp.Commits, e.sections) |
| | 0 | 112 | | if err != nil { |
| | 0 | 113 | | return result, domain.NewReleaseError("generate-notes", err) |
| | 0 | 114 | | } |
| | 5 | 115 | | result.Changelog = notes |
| | 5 | 116 | | |
| | 5 | 117 | | // Format tag name. |
| | 5 | 118 | | tagName, err := e.tagService.FormatTag(pp.Project.Name, pp.NextVersion) |
| | 0 | 119 | | if err != nil { |
| | 0 | 120 | | return result, domain.NewReleaseError("format-tag", err) |
| | 0 | 121 | | } |
| | 5 | 122 | | result.TagName = tagName |
| | 5 | 123 | | |
| | 1 | 124 | | if plan.DryRun { |
| | 1 | 125 | | result.Skipped = true |
| | 1 | 126 | | result.SkipReason = "dry run" |
| | 1 | 127 | | e.logger.Info("dry run: would create tag", "tag", tagName, "version", pp.NextVersion) |
| | 1 | 128 | | return result, nil |
| | 1 | 129 | | } |
| | | 130 | | |
| | | 131 | | // Create and push tag. |
| | 0 | 132 | | if err := e.createAndPushTag(ctx, tagName, notes); err != nil { |
| | 0 | 133 | | return result, err |
| | 0 | 134 | | } |
| | 4 | 135 | | result.TagCreated = true |
| | 4 | 136 | | |
| | 4 | 137 | | // Publish release. Publish failures are soft: the tag is already pushed so |
| | 4 | 138 | | // the release is technically done. Store the error in result rather than |
| | 4 | 139 | | // returning it so the caller can continue with remaining projects. |
| | 4 | 140 | | published, publishURL, publishErr := e.publish(ctx, pp, tagName, notes, plan.Policy) |
| | 1 | 141 | | if publishErr != nil { |
| | 1 | 142 | | result.SetError(publishErr) |
| | 1 | 143 | | } else { |
| | 3 | 144 | | result.Published = published |
| | 3 | 145 | | result.PublishURL = publishURL |
| | 3 | 146 | | } |
| | | 147 | | |
| | | 148 | | // Log at different levels so operators can distinguish a full success from |
| | | 149 | | // a partial one (tag pushed, publish failed) without parsing the error. |
| | 3 | 150 | | if result.Error == nil { |
| | 3 | 151 | | e.logger.Info("release completed", "project", pp.Project.Name, "version", pp.NextVersion, "tag", tagName) |
| | 1 | 152 | | } else { |
| | 1 | 153 | | e.logger.Warn("release partially completed (publish failed)", "project", pp.Project.Name, "version", pp.NextVersion, |
| | 1 | 154 | | } |
| | 4 | 155 | | return result, nil |
| | | 156 | | } |
| | | 157 | | |
| | 4 | 158 | | func (e *ReleaseExecutor) createAndPushTag(ctx context.Context, tagName, message string) error { |
| | 4 | 159 | | headHash, err := e.git.HeadHash(ctx) |
| | 0 | 160 | | if err != nil { |
| | 0 | 161 | | return domain.NewReleaseError("get-head", err) |
| | 0 | 162 | | } |
| | | 163 | | |
| | 2 | 164 | | if err := e.git.CreateTag(ctx, tagName, headHash, message); err != nil { |
| | 0 | 165 | | if !errors.Is(err, domain.ErrTagAlreadyExists) { |
| | 0 | 166 | | return domain.NewReleaseError("create-tag", err) |
| | 0 | 167 | | } |
| | | 168 | | // Tag already exists at this commit — idempotent re-run. |
| | | 169 | | // Still push in case the tag was created locally but not yet pushed. |
| | 2 | 170 | | e.logger.Info("tag already exists at current commit, skipping create", "tag", tagName) |
| | | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | if err := e.git.PushTag(ctx, tagName); err != nil { |
| | 0 | 174 | | return domain.NewReleaseError("push-tag", err) |
| | 0 | 175 | | } |
| | 4 | 176 | | return nil |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | // publish calls the publisher and returns (published, publishURL, err). |
| | | 180 | | // Returning only the two fields callers actually use avoids implying that the |
| | | 181 | | // other ProjectReleaseResult zero-value fields (TagCreated, Project, …) are meaningful. |
| | | 182 | | func (e *ReleaseExecutor) publish( |
| | | 183 | | ctx context.Context, |
| | | 184 | | pp domain.ProjectReleasePlan, |
| | | 185 | | tagName, notes string, |
| | | 186 | | policy *domain.BranchPolicy, |
| | 4 | 187 | | ) (published bool, publishURL string, err error) { |
| | 4 | 188 | | isPrerelease := policy != nil && policy.Prerelease |
| | 4 | 189 | | |
| | 4 | 190 | | result, err := e.publisher.Publish(ctx, ports.PublishParams{ |
| | 4 | 191 | | TagName: tagName, |
| | 4 | 192 | | Version: pp.NextVersion, |
| | 4 | 193 | | Project: pp.Project.Name, |
| | 4 | 194 | | Changelog: notes, |
| | 4 | 195 | | Prerelease: isPrerelease, |
| | 4 | 196 | | }) |
| | 1 | 197 | | if err != nil { |
| | 1 | 198 | | return false, "", domain.NewReleaseError("publish", err) |
| | 1 | 199 | | } |
| | 3 | 200 | | return result.Published, result.PublishURL, nil |
| | | 201 | | } |