| | | 1 | | package gitlab |
| | | 2 | | |
| | | 3 | | import ( |
| | | 4 | | "bytes" |
| | | 5 | | "context" |
| | | 6 | | "encoding/json" |
| | | 7 | | "fmt" |
| | | 8 | | "io" |
| | | 9 | | "net/http" |
| | | 10 | | "net/url" |
| | | 11 | | "os" |
| | | 12 | | |
| | | 13 | | "github.com/jedi-knights/go-semantic-release/internal/domain" |
| | | 14 | | "github.com/jedi-knights/go-semantic-release/internal/ports" |
| | | 15 | | ) |
| | | 16 | | |
| | | 17 | | // Compile-time interface compliance checks. |
| | | 18 | | var ( |
| | | 19 | | _ ports.Plugin = (*Plugin)(nil) |
| | | 20 | | _ ports.VerifyConditionsPlugin = (*Plugin)(nil) |
| | | 21 | | _ ports.PublishPlugin = (*Plugin)(nil) |
| | | 22 | | _ ports.AddChannelPlugin = (*Plugin)(nil) |
| | | 23 | | _ ports.SuccessPlugin = (*Plugin)(nil) |
| | | 24 | | _ ports.FailPlugin = (*Plugin)(nil) |
| | | 25 | | ) |
| | | 26 | | |
| | | 27 | | // PluginConfig holds configuration for the GitLab plugin. |
| | | 28 | | type PluginConfig struct { |
| | | 29 | | ProjectID string `mapstructure:"project_id"` |
| | | 30 | | Token string `mapstructure:"token"` |
| | | 31 | | APIURL string `mapstructure:"api_url"` |
| | | 32 | | Assets []string `mapstructure:"assets"` |
| | | 33 | | Milestones []string `mapstructure:"milestones"` |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | // Plugin implements lifecycle interfaces for GitLab integration. |
| | | 37 | | type Plugin struct { |
| | | 38 | | config PluginConfig |
| | | 39 | | client *http.Client |
| | | 40 | | logger ports.Logger |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | // NewPlugin creates a GitLab lifecycle plugin. |
| | | 44 | | func NewPlugin(cfg PluginConfig, logger ports.Logger) *Plugin { |
| | | 45 | | if cfg.APIURL == "" { |
| | | 46 | | cfg.APIURL = "https://gitlab.com/api/v4" |
| | | 47 | | } |
| | | 48 | | if cfg.Token == "" { |
| | | 49 | | cfg.Token = resolveToken() |
| | | 50 | | } |
| | | 51 | | return &Plugin{ |
| | | 52 | | config: cfg, |
| | | 53 | | client: &http.Client{}, |
| | | 54 | | logger: logger, |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | func resolveToken() string { |
| | | 59 | | for _, key := range []string{"GL_TOKEN", "GITLAB_TOKEN", "SEMANTIC_RELEASE_GITLAB_TOKEN"} { |
| | | 60 | | if v := os.Getenv(key); v != "" { |
| | | 61 | | return v |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | return "" |
| | | 65 | | } |
| | | 66 | | |
| | 2 | 67 | | func (p *Plugin) Name() string { return "gitlab" } |
| | | 68 | | |
| | | 69 | | // VerifyConditions checks that GitLab credentials and config are valid. |
| | 4 | 70 | | func (p *Plugin) VerifyConditions(ctx context.Context, rc *domain.ReleaseContext) error { |
| | 1 | 71 | | if p.config.Token == "" { |
| | 1 | 72 | | return fmt.Errorf("GitLab token not found (set GL_TOKEN, GITLAB_TOKEN, or SEMANTIC_RELEASE_GITLAB_TOKEN)") |
| | 1 | 73 | | } |
| | 1 | 74 | | if p.config.ProjectID == "" { |
| | 1 | 75 | | return fmt.Errorf("GitLab project_id must be configured") |
| | 1 | 76 | | } |
| | | 77 | | |
| | 2 | 78 | | apiURL := fmt.Sprintf("%s/projects/%s", p.config.APIURL, url.PathEscape(p.config.ProjectID)) |
| | 2 | 79 | | req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, http.NoBody) |
| | 0 | 80 | | if err != nil { |
| | 0 | 81 | | return fmt.Errorf("creating request: %w", err) |
| | 0 | 82 | | } |
| | 2 | 83 | | p.setHeaders(req) |
| | 2 | 84 | | |
| | 2 | 85 | | resp, err := p.client.Do(req) |
| | 0 | 86 | | if err != nil { |
| | 0 | 87 | | return fmt.Errorf("verifying GitLab access: %w", err) |
| | 0 | 88 | | } |
| | 2 | 89 | | defer func() { _ = resp.Body.Close() }() |
| | | 90 | | |
| | 1 | 91 | | if resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden { |
| | 1 | 92 | | return fmt.Errorf("GitLab token is invalid or lacks permissions (HTTP %d)", resp.StatusCode) |
| | 1 | 93 | | } |
| | 0 | 94 | | if resp.StatusCode != http.StatusOK { |
| | 0 | 95 | | return fmt.Errorf("GitLab API returned HTTP %d for project verification", resp.StatusCode) |
| | 0 | 96 | | } |
| | | 97 | | |
| | 1 | 98 | | return nil |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | // Publish creates a GitLab release. |
| | 3 | 102 | | func (p *Plugin) Publish(ctx context.Context, rc *domain.ReleaseContext) (*domain.ProjectReleaseResult, error) { |
| | 1 | 103 | | if rc.CurrentProject == nil { |
| | 1 | 104 | | return nil, nil |
| | 1 | 105 | | } |
| | | 106 | | |
| | 2 | 107 | | tagName := rc.TagName |
| | 2 | 108 | | releaseName := tagName |
| | 2 | 109 | | if rc.CurrentProject.Project.Name != "" { |
| | 2 | 110 | | releaseName = fmt.Sprintf("%s %s", rc.CurrentProject.Project.Name, rc.CurrentProject.NextVersion.String()) |
| | 2 | 111 | | } |
| | | 112 | | |
| | 2 | 113 | | reqBody := glCreateReleaseRequest{ |
| | 2 | 114 | | TagName: tagName, |
| | 2 | 115 | | Name: releaseName, |
| | 2 | 116 | | Description: rc.Notes, |
| | 2 | 117 | | Milestones: p.config.Milestones, |
| | 2 | 118 | | } |
| | 2 | 119 | | |
| | 2 | 120 | | jsonData, err := json.Marshal(reqBody) |
| | 0 | 121 | | if err != nil { |
| | 0 | 122 | | return nil, fmt.Errorf("marshaling release request: %w", err) |
| | 0 | 123 | | } |
| | | 124 | | |
| | 2 | 125 | | apiURL := fmt.Sprintf("%s/projects/%s/releases", p.config.APIURL, url.PathEscape(p.config.ProjectID)) |
| | 2 | 126 | | req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(jsonData)) |
| | 0 | 127 | | if err != nil { |
| | 0 | 128 | | return nil, fmt.Errorf("creating request: %w", err) |
| | 0 | 129 | | } |
| | 2 | 130 | | p.setHeaders(req) |
| | 2 | 131 | | |
| | 2 | 132 | | resp, err := p.client.Do(req) |
| | 0 | 133 | | if err != nil { |
| | 0 | 134 | | return nil, fmt.Errorf("publishing release: %w", err) |
| | 0 | 135 | | } |
| | 2 | 136 | | defer func() { _ = resp.Body.Close() }() |
| | | 137 | | |
| | 1 | 138 | | if resp.StatusCode != http.StatusCreated { |
| | 1 | 139 | | body, _ := io.ReadAll(resp.Body) |
| | 1 | 140 | | return nil, fmt.Errorf("GitLab create release failed (%d): %s", resp.StatusCode, string(body)) |
| | 1 | 141 | | } |
| | | 142 | | |
| | 1 | 143 | | var release glRelease |
| | 0 | 144 | | if err := json.NewDecoder(resp.Body).Decode(&release); err != nil { |
| | 0 | 145 | | return nil, fmt.Errorf("decoding release response: %w", err) |
| | 0 | 146 | | } |
| | | 147 | | |
| | 1 | 148 | | p.logger.Info("created GitLab release", "tag", tagName, "url", release.Links.Self) |
| | 1 | 149 | | |
| | 1 | 150 | | return &domain.ProjectReleaseResult{ |
| | 1 | 151 | | Project: rc.CurrentProject.Project, |
| | 1 | 152 | | Version: rc.CurrentProject.NextVersion, |
| | 1 | 153 | | TagName: tagName, |
| | 1 | 154 | | Published: true, |
| | 1 | 155 | | PublishURL: release.Links.Self, |
| | 1 | 156 | | Changelog: rc.Notes, |
| | 1 | 157 | | }, nil |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | // AddChannel is a no-op for GitLab (releases don't have a channel/prerelease distinction). |
| | 1 | 161 | | func (p *Plugin) AddChannel(_ context.Context, _ *domain.ReleaseContext) error { |
| | 1 | 162 | | return nil |
| | 1 | 163 | | } |
| | | 164 | | |
| | | 165 | | // Success logs the successful release. |
| | 1 | 166 | | func (p *Plugin) Success(_ context.Context, rc *domain.ReleaseContext) error { |
| | 1 | 167 | | p.logger.Info("GitLab release successful", "tag", rc.TagName) |
| | 1 | 168 | | return nil |
| | 1 | 169 | | } |
| | | 170 | | |
| | | 171 | | // Fail logs the failed release. |
| | 2 | 172 | | func (p *Plugin) Fail(_ context.Context, rc *domain.ReleaseContext) error { |
| | 1 | 173 | | if rc.Error != nil { |
| | 1 | 174 | | p.logger.Error("GitLab release failed", "error", rc.Error) |
| | 1 | 175 | | } |
| | 2 | 176 | | return nil |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | // --- Helper types --- |
| | | 180 | | |
| | | 181 | | type glCreateReleaseRequest struct { |
| | | 182 | | TagName string `json:"tag_name"` |
| | | 183 | | Name string `json:"name"` |
| | | 184 | | Description string `json:"description"` |
| | | 185 | | Milestones []string `json:"milestones,omitempty"` |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | type glRelease struct { |
| | | 189 | | TagName string `json:"tag_name"` |
| | | 190 | | Description string `json:"description"` |
| | | 191 | | Links struct { |
| | | 192 | | Self string `json:"self"` |
| | | 193 | | } `json:"_links"` |
| | | 194 | | } |
| | | 195 | | |
| | 4 | 196 | | func (p *Plugin) setHeaders(req *http.Request) { |
| | 4 | 197 | | req.Header.Set("PRIVATE-TOKEN", p.config.Token) |
| | 4 | 198 | | req.Header.Set("Content-Type", "application/json") |
| | 4 | 199 | | } |