| | | 1 | | package plugins |
| | | 2 | | |
| | | 3 | | import ( |
| | | 4 | | "bytes" |
| | | 5 | | "context" |
| | | 6 | | "fmt" |
| | | 7 | | "text/template" |
| | | 8 | | |
| | | 9 | | "github.com/jedi-knights/go-semantic-release/internal/domain" |
| | | 10 | | "github.com/jedi-knights/go-semantic-release/internal/ports" |
| | | 11 | | ) |
| | | 12 | | |
| | | 13 | | // Compile-time interface compliance checks. |
| | | 14 | | var ( |
| | | 15 | | _ ports.Plugin = (*GitPlugin)(nil) |
| | | 16 | | _ ports.VerifyConditionsPlugin = (*GitPlugin)(nil) |
| | | 17 | | _ ports.PublishPlugin = (*GitPlugin)(nil) |
| | | 18 | | ) |
| | | 19 | | |
| | | 20 | | // GitPlugin handles git operations: verifyConditions (git access), publish (stage → commit → push → tag). |
| | | 21 | | type GitPlugin struct { |
| | | 22 | | git ports.GitRepository |
| | | 23 | | tagService ports.TagService |
| | | 24 | | fs ports.FileSystem |
| | | 25 | | logger ports.Logger |
| | | 26 | | identity domain.GitIdentity |
| | | 27 | | gitConfig domain.GitConfig |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | // NewGitPlugin creates the built-in git plugin. |
| | | 31 | | func NewGitPlugin( |
| | | 32 | | git ports.GitRepository, |
| | | 33 | | tagService ports.TagService, |
| | | 34 | | fs ports.FileSystem, |
| | | 35 | | logger ports.Logger, |
| | | 36 | | identity domain.GitIdentity, |
| | | 37 | | gitConfig domain.GitConfig, |
| | | 38 | | ) *GitPlugin { |
| | | 39 | | return &GitPlugin{ |
| | | 40 | | git: git, |
| | | 41 | | tagService: tagService, |
| | | 42 | | fs: fs, |
| | | 43 | | logger: logger, |
| | | 44 | | identity: identity, |
| | | 45 | | gitConfig: gitConfig, |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | 1 | 49 | | func (p *GitPlugin) Name() string { return "git" } |
| | | 50 | | |
| | 2 | 51 | | func (p *GitPlugin) VerifyConditions(ctx context.Context, rc *domain.ReleaseContext) error { |
| | 2 | 52 | | _, err := p.git.CurrentBranch(ctx) |
| | 1 | 53 | | if err != nil { |
| | 1 | 54 | | return fmt.Errorf("unable to access git repository: %w", err) |
| | 1 | 55 | | } |
| | 1 | 56 | | return nil |
| | | 57 | | } |
| | | 58 | | |
| | 12 | 59 | | func (p *GitPlugin) Publish(ctx context.Context, rc *domain.ReleaseContext) (*domain.ProjectReleaseResult, error) { |
| | 1 | 60 | | if rc.CurrentProject == nil { |
| | 1 | 61 | | return nil, nil |
| | 1 | 62 | | } |
| | | 63 | | |
| | 11 | 64 | | tagName, err := p.tagService.FormatTag(rc.CurrentProject.Project.Name, rc.CurrentProject.NextVersion) |
| | 1 | 65 | | if err != nil { |
| | 1 | 66 | | return nil, fmt.Errorf("formatting tag: %w", err) |
| | 1 | 67 | | } |
| | 10 | 68 | | rc.TagName = tagName |
| | 10 | 69 | | |
| | 10 | 70 | | // Stage and commit release assets before tagging so the tag points to the release commit. |
| | 5 | 71 | | if len(p.gitConfig.Assets) > 0 { |
| | 1 | 72 | | if err = p.git.Stage(ctx, p.gitConfig.Assets); err != nil { |
| | 1 | 73 | | return nil, fmt.Errorf("staging release assets: %w", err) |
| | 1 | 74 | | } |
| | 4 | 75 | | commitMsg := renderCommitMessage(p.gitConfig.Message, tagName, rc.CurrentProject.NextVersion, rc.Notes) |
| | 1 | 76 | | if err = p.git.Commit(ctx, commitMsg); err != nil { |
| | 1 | 77 | | return nil, fmt.Errorf("committing release assets: %w", err) |
| | 1 | 78 | | } |
| | 1 | 79 | | if err = p.git.Push(ctx); err != nil { |
| | 1 | 80 | | return nil, fmt.Errorf("pushing release branch: %w", err) |
| | 1 | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | 7 | 84 | | headHash, err := p.git.HeadHash(ctx) |
| | 1 | 85 | | if err != nil { |
| | 1 | 86 | | return nil, fmt.Errorf("getting HEAD hash: %w", err) |
| | 1 | 87 | | } |
| | | 88 | | |
| | 6 | 89 | | tagMessage := fmt.Sprintf("chore(release): %s", tagName) |
| | 2 | 90 | | if rc.Notes != "" { |
| | 2 | 91 | | tagMessage = rc.Notes |
| | 2 | 92 | | } |
| | | 93 | | |
| | 1 | 94 | | if err := p.git.CreateTag(ctx, tagName, headHash, tagMessage); err != nil { |
| | 1 | 95 | | return nil, fmt.Errorf("creating tag %s: %w", tagName, err) |
| | 1 | 96 | | } |
| | | 97 | | |
| | 1 | 98 | | if err := p.git.PushTag(ctx, tagName); err != nil { |
| | 1 | 99 | | return nil, fmt.Errorf("pushing tag %s: %w", tagName, err) |
| | 1 | 100 | | } |
| | | 101 | | |
| | 4 | 102 | | p.logger.Info("created and pushed tag", "tag", tagName) |
| | 4 | 103 | | |
| | 4 | 104 | | return &domain.ProjectReleaseResult{ |
| | 4 | 105 | | Project: rc.CurrentProject.Project, |
| | 4 | 106 | | Version: rc.CurrentProject.NextVersion, |
| | 4 | 107 | | TagName: tagName, |
| | 4 | 108 | | TagCreated: true, |
| | 4 | 109 | | Changelog: rc.Notes, |
| | 4 | 110 | | }, nil |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | // renderCommitMessage renders the commit message template with release data. |
| | | 114 | | // Supports {{.Version}}, {{.Tag}}, and {{.Notes}} placeholders. |
| | | 115 | | // Falls back to "chore(release): {tagName}" on empty template or render error. |
| | | 116 | | func renderCommitMessage(tmpl, tagName string, version domain.Version, notes string) string { |
| | | 117 | | if tmpl == "" { |
| | | 118 | | return fmt.Sprintf("chore(release): %s", tagName) |
| | | 119 | | } |
| | | 120 | | data := struct { |
| | | 121 | | Version string |
| | | 122 | | Tag string |
| | | 123 | | Notes string |
| | | 124 | | }{ |
| | | 125 | | Version: version.String(), |
| | | 126 | | Tag: tagName, |
| | | 127 | | Notes: notes, |
| | | 128 | | } |
| | | 129 | | t, err := template.New("").Parse(tmpl) |
| | | 130 | | if err != nil { |
| | | 131 | | return fmt.Sprintf("chore(release): %s", tagName) |
| | | 132 | | } |
| | | 133 | | var buf bytes.Buffer |
| | | 134 | | if err := t.Execute(&buf, data); err != nil { |
| | | 135 | | return fmt.Sprintf("chore(release): %s", tagName) |
| | | 136 | | } |
| | | 137 | | return buf.String() |
| | | 138 | | } |