| | | 1 | | package github |
| | | 2 | | |
| | | 3 | | import ( |
| | | 4 | | "bytes" |
| | | 5 | | "context" |
| | | 6 | | "encoding/json" |
| | | 7 | | "fmt" |
| | | 8 | | "io" |
| | | 9 | | "mime" |
| | | 10 | | "net/http" |
| | | 11 | | neturl "net/url" |
| | | 12 | | "os" |
| | | 13 | | "path/filepath" |
| | | 14 | | "strings" |
| | | 15 | | "time" |
| | | 16 | | |
| | | 17 | | "github.com/jedi-knights/go-semantic-release/internal/domain" |
| | | 18 | | "github.com/jedi-knights/go-semantic-release/internal/ports" |
| | | 19 | | ) |
| | | 20 | | |
| | | 21 | | // Compile-time interface compliance checks. |
| | | 22 | | var ( |
| | | 23 | | _ ports.Plugin = (*Plugin)(nil) |
| | | 24 | | _ ports.VerifyConditionsPlugin = (*Plugin)(nil) |
| | | 25 | | _ ports.PublishPlugin = (*Plugin)(nil) |
| | | 26 | | _ ports.AddChannelPlugin = (*Plugin)(nil) |
| | | 27 | | _ ports.SuccessPlugin = (*Plugin)(nil) |
| | | 28 | | _ ports.FailPlugin = (*Plugin)(nil) |
| | | 29 | | ) |
| | | 30 | | |
| | | 31 | | // PluginConfig holds configuration for the GitHub plugin. |
| | | 32 | | type PluginConfig struct { |
| | | 33 | | Owner string `mapstructure:"owner"` |
| | | 34 | | Repo string `mapstructure:"repo"` |
| | | 35 | | Token string `mapstructure:"token"` |
| | | 36 | | APIURL string `mapstructure:"api_url"` |
| | | 37 | | Assets []domain.GitHubAsset `mapstructure:"assets"` |
| | | 38 | | DraftRelease bool `mapstructure:"draft_release"` |
| | | 39 | | DiscussionCategoryName string `mapstructure:"discussion_category_name"` |
| | | 40 | | SuccessComment string `mapstructure:"success_comment"` |
| | | 41 | | FailComment string `mapstructure:"fail_comment"` |
| | | 42 | | ReleasedLabels []string `mapstructure:"released_labels"` |
| | | 43 | | FailLabels []string `mapstructure:"fail_labels"` |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | // Plugin implements multiple lifecycle interfaces for GitHub integration. |
| | | 47 | | type Plugin struct { |
| | | 48 | | config PluginConfig |
| | | 49 | | client *http.Client |
| | | 50 | | logger ports.Logger |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | // NewPlugin creates a GitHub lifecycle plugin. |
| | | 54 | | func NewPlugin(cfg PluginConfig, logger ports.Logger) *Plugin { |
| | | 55 | | if cfg.APIURL == "" { |
| | | 56 | | cfg.APIURL = "https://api.github.com" |
| | | 57 | | } |
| | | 58 | | if cfg.Token == "" { |
| | | 59 | | cfg.Token = resolveToken() |
| | | 60 | | } |
| | | 61 | | if cfg.SuccessComment == "" { |
| | | 62 | | cfg.SuccessComment = "🎉 This issue has been resolved in version {{.Version}} 🎉\n\nThe release is available on [Git |
| | | 63 | | } |
| | | 64 | | if cfg.FailComment == "" { |
| | | 65 | | cfg.FailComment = "The release from branch `{{.Branch}}` has failed.\n\nError: {{.Error}}" |
| | | 66 | | } |
| | | 67 | | if len(cfg.ReleasedLabels) == 0 { |
| | | 68 | | cfg.ReleasedLabels = []string{"released"} |
| | | 69 | | } |
| | | 70 | | if len(cfg.FailLabels) == 0 { |
| | | 71 | | cfg.FailLabels = []string{"semantic-release"} |
| | | 72 | | } |
| | | 73 | | return &Plugin{ |
| | | 74 | | config: cfg, |
| | | 75 | | client: &http.Client{Timeout: 30 * time.Second}, |
| | | 76 | | logger: logger, |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | func resolveToken() string { |
| | | 81 | | for _, key := range []string{"GH_TOKEN", "GITHUB_TOKEN", "SEMANTIC_RELEASE_GITHUB_TOKEN"} { |
| | | 82 | | if v := os.Getenv(key); v != "" { |
| | | 83 | | return v |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | return "" |
| | | 87 | | } |
| | | 88 | | |
| | 2 | 89 | | func (p *Plugin) Name() string { return "github" } |
| | | 90 | | |
| | | 91 | | // VerifyConditions checks that GitHub credentials and config are valid. |
| | 9 | 92 | | func (p *Plugin) VerifyConditions(ctx context.Context, rc *domain.ReleaseContext) error { |
| | 1 | 93 | | if p.config.Token == "" { |
| | 1 | 94 | | return fmt.Errorf("GitHub token not found (set GH_TOKEN, GITHUB_TOKEN, or SEMANTIC_RELEASE_GITHUB_TOKEN)") |
| | 1 | 95 | | } |
| | | 96 | | |
| | 8 | 97 | | owner, repo := p.config.Owner, p.config.Repo |
| | 1 | 98 | | if owner == "" || repo == "" { |
| | 1 | 99 | | return fmt.Errorf("GitHub owner and repo must be configured") |
| | 1 | 100 | | } |
| | | 101 | | |
| | | 102 | | // Verify token is valid with a lightweight API call. |
| | 7 | 103 | | url := fmt.Sprintf("%s/repos/%s/%s", p.config.APIURL, neturl.PathEscape(owner), neturl.PathEscape(repo)) |
| | 7 | 104 | | req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody) |
| | 1 | 105 | | if err != nil { |
| | 1 | 106 | | return fmt.Errorf("creating request: %w", err) |
| | 1 | 107 | | } |
| | 6 | 108 | | p.setHeaders(req) |
| | 6 | 109 | | |
| | 6 | 110 | | resp, err := p.client.Do(req) |
| | 1 | 111 | | if err != nil { |
| | 1 | 112 | | return fmt.Errorf("verifying GitHub access: %w", err) |
| | 1 | 113 | | } |
| | 5 | 114 | | defer func() { _ = resp.Body.Close() }() |
| | | 115 | | |
| | 2 | 116 | | if resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden { |
| | 2 | 117 | | return fmt.Errorf("GitHub token is invalid or lacks permissions (HTTP %d)", resp.StatusCode) |
| | 2 | 118 | | } |
| | 1 | 119 | | if resp.StatusCode != http.StatusOK { |
| | 1 | 120 | | return fmt.Errorf("GitHub API returned HTTP %d for repo verification", resp.StatusCode) |
| | 1 | 121 | | } |
| | 2 | 122 | | _, _ = io.Copy(io.Discard, resp.Body) |
| | 2 | 123 | | return nil |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | // Publish creates a GitHub release, optionally uploads assets. |
| | 4 | 127 | | func (p *Plugin) Publish(ctx context.Context, rc *domain.ReleaseContext) (*domain.ProjectReleaseResult, error) { |
| | 1 | 128 | | if rc.CurrentProject == nil { |
| | 1 | 129 | | return nil, nil |
| | 1 | 130 | | } |
| | | 131 | | |
| | 3 | 132 | | tagName := rc.TagName |
| | 3 | 133 | | releaseName := tagName |
| | 3 | 134 | | if rc.CurrentProject.Project.Name != "" { |
| | 3 | 135 | | releaseName = fmt.Sprintf("%s %s", rc.CurrentProject.Project.Name, rc.CurrentProject.NextVersion.String()) |
| | 3 | 136 | | } |
| | | 137 | | |
| | 3 | 138 | | isPrerelease := rc.BranchPolicy != nil && rc.BranchPolicy.Prerelease |
| | 3 | 139 | | |
| | 3 | 140 | | reqBody := ghCreateReleaseRequest{ |
| | 3 | 141 | | TagName: tagName, |
| | 3 | 142 | | Name: releaseName, |
| | 3 | 143 | | Body: rc.Notes, |
| | 3 | 144 | | Prerelease: isPrerelease, |
| | 3 | 145 | | Draft: p.config.DraftRelease, |
| | 3 | 146 | | DiscussionCategoryName: p.config.DiscussionCategoryName, |
| | 3 | 147 | | } |
| | 3 | 148 | | |
| | 3 | 149 | | releaseResp, err := p.createGHRelease(ctx, reqBody) |
| | 1 | 150 | | if err != nil { |
| | 1 | 151 | | return nil, err |
| | 1 | 152 | | } |
| | | 153 | | |
| | | 154 | | // Upload assets. |
| | 1 | 155 | | for _, asset := range p.config.Assets { |
| | 1 | 156 | | if err := p.uploadAssetGlob(ctx, releaseResp.UploadURL, asset); err != nil { |
| | 1 | 157 | | p.logger.Warn("failed to upload asset", "pattern", asset.Path, "error", err) |
| | 1 | 158 | | } |
| | | 159 | | } |
| | | 160 | | |
| | 2 | 161 | | return &domain.ProjectReleaseResult{ |
| | 2 | 162 | | Project: rc.CurrentProject.Project, |
| | 2 | 163 | | Version: rc.CurrentProject.NextVersion, |
| | 2 | 164 | | TagName: tagName, |
| | 2 | 165 | | Published: true, |
| | 2 | 166 | | PublishURL: releaseResp.HTMLURL, |
| | 2 | 167 | | Changelog: rc.Notes, |
| | 2 | 168 | | }, nil |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | // AddChannel updates a release's prerelease status based on the channel. |
| | 6 | 172 | | func (p *Plugin) AddChannel(ctx context.Context, rc *domain.ReleaseContext) error { |
| | 1 | 173 | | if rc.TagName == "" { |
| | 1 | 174 | | return nil |
| | 1 | 175 | | } |
| | | 176 | | |
| | | 177 | | // Find existing release by tag. |
| | 5 | 178 | | release, err := p.getReleaseByTag(ctx, rc.TagName) |
| | 1 | 179 | | if err != nil { |
| | 1 | 180 | | return fmt.Errorf("finding release for tag %s: %w", rc.TagName, err) |
| | 1 | 181 | | } |
| | 1 | 182 | | if release == nil { |
| | 1 | 183 | | return nil |
| | 1 | 184 | | } |
| | | 185 | | |
| | 3 | 186 | | isPrerelease := rc.BranchPolicy != nil && rc.BranchPolicy.Prerelease |
| | 3 | 187 | | |
| | 3 | 188 | | // Update the prerelease field. |
| | 3 | 189 | | updateBody := map[string]any{ |
| | 3 | 190 | | "prerelease": isPrerelease, |
| | 3 | 191 | | } |
| | 3 | 192 | | jsonData, err := json.Marshal(updateBody) |
| | 0 | 193 | | if err != nil { |
| | 0 | 194 | | return fmt.Errorf("marshaling release update: %w", err) |
| | 0 | 195 | | } |
| | | 196 | | |
| | 3 | 197 | | url := fmt.Sprintf("%s/repos/%s/%s/releases/%d", p.config.APIURL, neturl.PathEscape(p.config.Owner), neturl.PathEscape |
| | 3 | 198 | | req, err := http.NewRequestWithContext(ctx, http.MethodPatch, url, bytes.NewReader(jsonData)) |
| | 0 | 199 | | if err != nil { |
| | 0 | 200 | | return err |
| | 0 | 201 | | } |
| | 3 | 202 | | p.setHeaders(req) |
| | 3 | 203 | | |
| | 3 | 204 | | resp, err := p.client.Do(req) |
| | 1 | 205 | | if err != nil { |
| | 1 | 206 | | return fmt.Errorf("updating release: %w", err) |
| | 1 | 207 | | } |
| | 2 | 208 | | defer func() { _ = resp.Body.Close() }() |
| | | 209 | | |
| | 1 | 210 | | if resp.StatusCode != http.StatusOK { |
| | 1 | 211 | | body, _ := io.ReadAll(resp.Body) |
| | 1 | 212 | | return fmt.Errorf("updating release failed (%d): %s", resp.StatusCode, string(body)) |
| | 1 | 213 | | } |
| | | 214 | | |
| | 1 | 215 | | p.logger.Info("updated release channel", "tag", rc.TagName, "prerelease", isPrerelease) |
| | 1 | 216 | | return nil |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | // Success comments on merged PRs and resolved issues. |
| | 4 | 220 | | func (p *Plugin) Success(ctx context.Context, rc *domain.ReleaseContext) error { |
| | 1 | 221 | | if rc.CurrentProject == nil || rc.Result == nil { |
| | 1 | 222 | | return nil |
| | 1 | 223 | | } |
| | | 224 | | |
| | | 225 | | // Find the publish URL for this project. |
| | 3 | 226 | | releaseURL := "" |
| | 3 | 227 | | for i := range rc.Result.Projects { |
| | 3 | 228 | | if rc.Result.Projects[i].Project.Name == rc.CurrentProject.Project.Name { |
| | 3 | 229 | | releaseURL = rc.Result.Projects[i].PublishURL |
| | 3 | 230 | | break |
| | | 231 | | } |
| | | 232 | | } |
| | | 233 | | |
| | 3 | 234 | | comment := strings.NewReplacer( |
| | 3 | 235 | | "{{.Version}}", rc.CurrentProject.NextVersion.String(), |
| | 3 | 236 | | "{{.ReleaseURL}}", releaseURL, |
| | 3 | 237 | | "{{.Branch}}", rc.Branch, |
| | 3 | 238 | | "{{.TagName}}", rc.TagName, |
| | 3 | 239 | | ).Replace(p.config.SuccessComment) |
| | 3 | 240 | | |
| | 3 | 241 | | // Comment on commits' associated PRs. |
| | 3 | 242 | | for i := range rc.CurrentProject.Commits { |
| | 3 | 243 | | prs, err := p.getPRsForCommit(ctx, rc.CurrentProject.Commits[i].Hash) |
| | 1 | 244 | | if err != nil { |
| | 1 | 245 | | p.logger.Debug("failed to get PRs for commit", "hash", rc.CurrentProject.Commits[i].Hash, "error", err) |
| | 1 | 246 | | continue |
| | | 247 | | } |
| | 2 | 248 | | for _, pr := range prs { |
| | 1 | 249 | | if err := p.commentOnIssue(ctx, pr.Number, comment); err != nil { |
| | 1 | 250 | | p.logger.Debug("failed to comment on PR", "number", pr.Number, "error", err) |
| | 1 | 251 | | } |
| | 0 | 252 | | if err := p.addLabelsToIssue(ctx, pr.Number, p.config.ReleasedLabels); err != nil { |
| | 0 | 253 | | p.logger.Warn("failed to add labels to PR", "number", pr.Number, "error", err) |
| | 0 | 254 | | } |
| | | 255 | | } |
| | | 256 | | } |
| | | 257 | | |
| | 3 | 258 | | return nil |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | // Fail opens or updates a GitHub issue documenting the failure. |
| | 6 | 262 | | func (p *Plugin) Fail(ctx context.Context, rc *domain.ReleaseContext) error { |
| | 1 | 263 | | if rc.Error == nil { |
| | 1 | 264 | | return nil |
| | 1 | 265 | | } |
| | | 266 | | |
| | 5 | 267 | | body := strings.NewReplacer( |
| | 5 | 268 | | "{{.Branch}}", rc.Branch, |
| | 5 | 269 | | "{{.Error}}", rc.Error.Error(), |
| | 5 | 270 | | ).Replace(p.config.FailComment) |
| | 5 | 271 | | |
| | 5 | 272 | | title := "The automated release is failing" |
| | 5 | 273 | | |
| | 5 | 274 | | // Check for existing failure issue. |
| | 5 | 275 | | existing, err := p.findFailureIssue(ctx, title) |
| | 1 | 276 | | if err != nil { |
| | 1 | 277 | | p.logger.Debug("failed to search for existing failure issue", "error", err) |
| | 1 | 278 | | } |
| | | 279 | | |
| | 1 | 280 | | if existing != nil { |
| | 1 | 281 | | return p.commentOnIssue(ctx, existing.Number, body) |
| | 1 | 282 | | } |
| | | 283 | | |
| | 4 | 284 | | return p.createIssue(ctx, title, body, p.config.FailLabels) |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | // --- Helper methods --- |
| | | 288 | | |
| | | 289 | | type ghCreateReleaseRequest struct { |
| | | 290 | | TagName string `json:"tag_name"` |
| | | 291 | | Name string `json:"name"` |
| | | 292 | | Body string `json:"body"` |
| | | 293 | | Prerelease bool `json:"prerelease"` |
| | | 294 | | Draft bool `json:"draft"` |
| | | 295 | | DiscussionCategoryName string `json:"discussion_category_name,omitempty"` |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | type ghRelease struct { |
| | | 299 | | ID int `json:"id"` |
| | | 300 | | HTMLURL string `json:"html_url"` |
| | | 301 | | TagName string `json:"tag_name"` |
| | | 302 | | UploadURL string `json:"upload_url"` |
| | | 303 | | } |
| | | 304 | | |
| | | 305 | | type ghPR struct { |
| | | 306 | | Number int `json:"number"` |
| | | 307 | | } |
| | | 308 | | |
| | | 309 | | type ghIssue struct { |
| | | 310 | | Number int `json:"number"` |
| | | 311 | | Title string `json:"title"` |
| | | 312 | | State string `json:"state"` |
| | | 313 | | } |
| | | 314 | | |
| | 51 | 315 | | func (p *Plugin) setHeaders(req *http.Request) { |
| | 51 | 316 | | req.Header.Set("Authorization", "token "+p.config.Token) |
| | 51 | 317 | | req.Header.Set("Content-Type", "application/json") |
| | 51 | 318 | | req.Header.Set("Accept", "application/vnd.github+json") |
| | 51 | 319 | | } |
| | | 320 | | |
| | 6 | 321 | | func (p *Plugin) createGHRelease(ctx context.Context, reqBody ghCreateReleaseRequest) (*ghRelease, error) { |
| | 6 | 322 | | jsonData, err := json.Marshal(reqBody) |
| | 0 | 323 | | if err != nil { |
| | 0 | 324 | | return nil, fmt.Errorf("marshaling release request: %w", err) |
| | 0 | 325 | | } |
| | | 326 | | |
| | 6 | 327 | | url := fmt.Sprintf("%s/repos/%s/%s/releases", p.config.APIURL, neturl.PathEscape(p.config.Owner), neturl.PathEscape(p. |
| | 6 | 328 | | req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(jsonData)) |
| | 1 | 329 | | if err != nil { |
| | 1 | 330 | | return nil, fmt.Errorf("creating request: %w", err) |
| | 1 | 331 | | } |
| | 5 | 332 | | p.setHeaders(req) |
| | 5 | 333 | | |
| | 5 | 334 | | resp, err := p.client.Do(req) |
| | 1 | 335 | | if err != nil { |
| | 1 | 336 | | return nil, fmt.Errorf("publishing release: %w", err) |
| | 1 | 337 | | } |
| | 4 | 338 | | defer func() { _ = resp.Body.Close() }() |
| | | 339 | | |
| | 1 | 340 | | if resp.StatusCode != http.StatusCreated { |
| | 1 | 341 | | respBody, _ := io.ReadAll(resp.Body) |
| | 1 | 342 | | return nil, fmt.Errorf("github create release failed (%d): %s", resp.StatusCode, string(respBody)) |
| | 1 | 343 | | } |
| | | 344 | | |
| | 3 | 345 | | var release ghRelease |
| | 1 | 346 | | if err := json.NewDecoder(resp.Body).Decode(&release); err != nil { |
| | 1 | 347 | | return nil, fmt.Errorf("decoding release response: %w", err) |
| | 1 | 348 | | } |
| | 2 | 349 | | return &release, nil |
| | | 350 | | } |
| | | 351 | | |
| | 9 | 352 | | func (p *Plugin) getReleaseByTag(ctx context.Context, tag string) (*ghRelease, error) { |
| | 9 | 353 | | url := fmt.Sprintf("%s/repos/%s/%s/releases/tags/%s", p.config.APIURL, neturl.PathEscape(p.config.Owner), neturl.PathE |
| | 9 | 354 | | req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody) |
| | 1 | 355 | | if err != nil { |
| | 1 | 356 | | return nil, err |
| | 1 | 357 | | } |
| | 8 | 358 | | p.setHeaders(req) |
| | 8 | 359 | | |
| | 8 | 360 | | resp, err := p.client.Do(req) |
| | 1 | 361 | | if err != nil { |
| | 1 | 362 | | return nil, err |
| | 1 | 363 | | } |
| | 7 | 364 | | defer func() { _ = resp.Body.Close() }() |
| | | 365 | | |
| | 1 | 366 | | if resp.StatusCode == http.StatusNotFound { |
| | 1 | 367 | | return nil, nil |
| | 1 | 368 | | } |
| | 2 | 369 | | if resp.StatusCode != http.StatusOK { |
| | 2 | 370 | | return nil, fmt.Errorf("unexpected status %d", resp.StatusCode) |
| | 2 | 371 | | } |
| | | 372 | | |
| | 4 | 373 | | var release ghRelease |
| | 1 | 374 | | if err := json.NewDecoder(resp.Body).Decode(&release); err != nil { |
| | 1 | 375 | | return nil, err |
| | 1 | 376 | | } |
| | 3 | 377 | | return &release, nil |
| | | 378 | | } |
| | | 379 | | |
| | 6 | 380 | | func (p *Plugin) uploadAssetGlob(ctx context.Context, uploadURL string, asset domain.GitHubAsset) error { |
| | 6 | 381 | | matches, err := filepath.Glob(asset.Path) |
| | 1 | 382 | | if err != nil { |
| | 1 | 383 | | return fmt.Errorf("globbing %s: %w", asset.Path, err) |
| | 1 | 384 | | } |
| | | 385 | | |
| | 5 | 386 | | for _, path := range matches { |
| | 2 | 387 | | if err := p.uploadAsset(ctx, uploadURL, path, asset.Label); err != nil { |
| | 2 | 388 | | return err |
| | 2 | 389 | | } |
| | | 390 | | } |
| | 3 | 391 | | return nil |
| | | 392 | | } |
| | | 393 | | |
| | 12 | 394 | | func (p *Plugin) uploadAsset(ctx context.Context, uploadURL, filePath, label string) error { |
| | 12 | 395 | | file, err := os.Open(filePath) |
| | 1 | 396 | | if err != nil { |
| | 1 | 397 | | return fmt.Errorf("opening %s: %w", filePath, err) |
| | 1 | 398 | | } |
| | 11 | 399 | | defer func() { _ = file.Close() }() |
| | | 400 | | |
| | 11 | 401 | | stat, err := file.Stat() |
| | 0 | 402 | | if err != nil { |
| | 0 | 403 | | return fmt.Errorf("stat %s: %w", filePath, err) |
| | 0 | 404 | | } |
| | | 405 | | |
| | 11 | 406 | | name := filepath.Base(filePath) |
| | 11 | 407 | | contentType := mime.TypeByExtension(filepath.Ext(filePath)) |
| | 1 | 408 | | if contentType == "" { |
| | 1 | 409 | | contentType = "application/octet-stream" |
| | 1 | 410 | | } |
| | | 411 | | |
| | | 412 | | // Strip the URI template suffix (e.g. "{?name,label}") that GitHub appends to upload_url. |
| | 11 | 413 | | base := uploadURL |
| | 0 | 414 | | if i := strings.Index(base, "{"); i >= 0 { |
| | 0 | 415 | | base = base[:i] |
| | 0 | 416 | | } |
| | 11 | 417 | | q := neturl.Values{} |
| | 11 | 418 | | q.Set("name", name) |
| | 2 | 419 | | if label != "" { |
| | 2 | 420 | | q.Set("label", label) |
| | 2 | 421 | | } |
| | 11 | 422 | | fullUploadURL := base + "?" + q.Encode() |
| | 11 | 423 | | |
| | 11 | 424 | | req, err := http.NewRequestWithContext(ctx, http.MethodPost, fullUploadURL, file) |
| | 0 | 425 | | if err != nil { |
| | 0 | 426 | | return fmt.Errorf("creating upload request: %w", err) |
| | 0 | 427 | | } |
| | | 428 | | |
| | 11 | 429 | | req.Header.Set("Authorization", "token "+p.config.Token) |
| | 11 | 430 | | req.Header.Set("Content-Type", contentType) |
| | 11 | 431 | | req.ContentLength = stat.Size() |
| | 11 | 432 | | |
| | 11 | 433 | | resp, err := p.client.Do(req) |
| | 1 | 434 | | if err != nil { |
| | 1 | 435 | | return fmt.Errorf("uploading asset %s: %w", name, err) |
| | 1 | 436 | | } |
| | 10 | 437 | | defer func() { _ = resp.Body.Close() }() |
| | | 438 | | |
| | 3 | 439 | | if resp.StatusCode != http.StatusCreated { |
| | 3 | 440 | | body, _ := io.ReadAll(resp.Body) |
| | 3 | 441 | | return fmt.Errorf("upload asset failed (%d): %s", resp.StatusCode, string(body)) |
| | 3 | 442 | | } |
| | | 443 | | |
| | 7 | 444 | | p.logger.Info("uploaded asset", "file", name) |
| | 7 | 445 | | return nil |
| | | 446 | | } |
| | | 447 | | |
| | 7 | 448 | | func (p *Plugin) getPRsForCommit(ctx context.Context, sha string) ([]ghPR, error) { |
| | 7 | 449 | | url := fmt.Sprintf("%s/repos/%s/%s/commits/%s/pulls", p.config.APIURL, neturl.PathEscape(p.config.Owner), neturl.PathE |
| | 7 | 450 | | req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody) |
| | 1 | 451 | | if err != nil { |
| | 1 | 452 | | return nil, err |
| | 1 | 453 | | } |
| | 6 | 454 | | p.setHeaders(req) |
| | 6 | 455 | | |
| | 6 | 456 | | resp, err := p.client.Do(req) |
| | 2 | 457 | | if err != nil { |
| | 2 | 458 | | return nil, err |
| | 2 | 459 | | } |
| | 4 | 460 | | defer func() { _ = resp.Body.Close() }() |
| | | 461 | | |
| | 1 | 462 | | if resp.StatusCode != http.StatusOK { |
| | 1 | 463 | | return nil, nil |
| | 1 | 464 | | } |
| | | 465 | | |
| | 3 | 466 | | var prs []ghPR |
| | 1 | 467 | | if err := json.NewDecoder(resp.Body).Decode(&prs); err != nil { |
| | 1 | 468 | | return nil, err |
| | 1 | 469 | | } |
| | 2 | 470 | | return prs, nil |
| | | 471 | | } |
| | | 472 | | |
| | 6 | 473 | | func (p *Plugin) commentOnIssue(ctx context.Context, number int, body string) error { |
| | 6 | 474 | | payload := map[string]string{"body": body} |
| | 6 | 475 | | jsonData, err := json.Marshal(payload) |
| | 0 | 476 | | if err != nil { |
| | 0 | 477 | | return fmt.Errorf("marshaling comment: %w", err) |
| | 0 | 478 | | } |
| | | 479 | | |
| | 6 | 480 | | url := fmt.Sprintf("%s/repos/%s/%s/issues/%d/comments", p.config.APIURL, neturl.PathEscape(p.config.Owner), neturl.Pat |
| | 6 | 481 | | req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(jsonData)) |
| | 1 | 482 | | if err != nil { |
| | 1 | 483 | | return err |
| | 1 | 484 | | } |
| | 5 | 485 | | p.setHeaders(req) |
| | 5 | 486 | | |
| | 5 | 487 | | resp, err := p.client.Do(req) |
| | 1 | 488 | | if err != nil { |
| | 1 | 489 | | return err |
| | 1 | 490 | | } |
| | 4 | 491 | | defer func() { _ = resp.Body.Close() }() |
| | | 492 | | |
| | 2 | 493 | | if resp.StatusCode != http.StatusCreated { |
| | 2 | 494 | | return fmt.Errorf("comment failed (%d)", resp.StatusCode) |
| | 2 | 495 | | } |
| | 2 | 496 | | return nil |
| | | 497 | | } |
| | | 498 | | |
| | 6 | 499 | | func (p *Plugin) addLabelsToIssue(ctx context.Context, number int, labels []string) error { |
| | 1 | 500 | | if len(labels) == 0 { |
| | 1 | 501 | | return nil |
| | 1 | 502 | | } |
| | 5 | 503 | | payload := map[string][]string{"labels": labels} |
| | 5 | 504 | | jsonData, err := json.Marshal(payload) |
| | 0 | 505 | | if err != nil { |
| | 0 | 506 | | return fmt.Errorf("marshaling labels: %w", err) |
| | 0 | 507 | | } |
| | | 508 | | |
| | 5 | 509 | | url := fmt.Sprintf("%s/repos/%s/%s/issues/%d/labels", |
| | 5 | 510 | | p.config.APIURL, |
| | 5 | 511 | | neturl.PathEscape(p.config.Owner), |
| | 5 | 512 | | neturl.PathEscape(p.config.Repo), |
| | 5 | 513 | | number) |
| | 5 | 514 | | req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(jsonData)) |
| | 1 | 515 | | if err != nil { |
| | 1 | 516 | | return fmt.Errorf("creating request: %w", err) |
| | 1 | 517 | | } |
| | 4 | 518 | | p.setHeaders(req) |
| | 4 | 519 | | |
| | 4 | 520 | | resp, err := p.client.Do(req) |
| | 1 | 521 | | if err != nil { |
| | 1 | 522 | | return fmt.Errorf("adding labels: %w", err) |
| | 1 | 523 | | } |
| | 3 | 524 | | defer func() { _ = resp.Body.Close() }() |
| | 1 | 525 | | if resp.StatusCode != http.StatusOK { |
| | 1 | 526 | | body, _ := io.ReadAll(resp.Body) |
| | 1 | 527 | | return fmt.Errorf("adding labels failed (HTTP %d): %s", resp.StatusCode, strings.TrimSpace(string(body))) |
| | 1 | 528 | | } |
| | 2 | 529 | | _, _ = io.Copy(io.Discard, resp.Body) |
| | 2 | 530 | | return nil |
| | | 531 | | } |
| | | 532 | | |
| | 9 | 533 | | func (p *Plugin) findFailureIssue(ctx context.Context, title string) (*ghIssue, error) { |
| | 9 | 534 | | q := neturl.Values{ |
| | 9 | 535 | | "state": {"open"}, |
| | 9 | 536 | | "labels": {strings.Join(p.config.FailLabels, ",")}, |
| | 9 | 537 | | } |
| | 9 | 538 | | url := fmt.Sprintf("%s/repos/%s/%s/issues?%s", |
| | 9 | 539 | | p.config.APIURL, |
| | 9 | 540 | | neturl.PathEscape(p.config.Owner), |
| | 9 | 541 | | neturl.PathEscape(p.config.Repo), |
| | 9 | 542 | | q.Encode()) |
| | 9 | 543 | | req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody) |
| | 1 | 544 | | if err != nil { |
| | 1 | 545 | | return nil, err |
| | 1 | 546 | | } |
| | 8 | 547 | | p.setHeaders(req) |
| | 8 | 548 | | |
| | 8 | 549 | | resp, err := p.client.Do(req) |
| | 1 | 550 | | if err != nil { |
| | 1 | 551 | | return nil, err |
| | 1 | 552 | | } |
| | 7 | 553 | | defer func() { _ = resp.Body.Close() }() |
| | | 554 | | |
| | 2 | 555 | | if resp.StatusCode != http.StatusOK { |
| | 2 | 556 | | body, _ := io.ReadAll(resp.Body) |
| | 2 | 557 | | return nil, fmt.Errorf("listing issues failed (%d): %s", resp.StatusCode, strings.TrimSpace(string(body))) |
| | 2 | 558 | | } |
| | | 559 | | |
| | 5 | 560 | | var issues []ghIssue |
| | 1 | 561 | | if err := json.NewDecoder(resp.Body).Decode(&issues); err != nil { |
| | 1 | 562 | | return nil, err |
| | 1 | 563 | | } |
| | | 564 | | |
| | 1 | 565 | | for _, issue := range issues { |
| | 1 | 566 | | if issue.Title == title { |
| | 1 | 567 | | return &issue, nil |
| | 1 | 568 | | } |
| | | 569 | | } |
| | 3 | 570 | | return nil, nil |
| | | 571 | | } |
| | | 572 | | |
| | 7 | 573 | | func (p *Plugin) createIssue(ctx context.Context, title, body string, labels []string) error { |
| | 7 | 574 | | payload := map[string]any{ |
| | 7 | 575 | | "title": title, |
| | 7 | 576 | | "body": body, |
| | 7 | 577 | | "labels": labels, |
| | 7 | 578 | | } |
| | 7 | 579 | | jsonData, err := json.Marshal(payload) |
| | 0 | 580 | | if err != nil { |
| | 0 | 581 | | return fmt.Errorf("marshaling issue: %w", err) |
| | 0 | 582 | | } |
| | | 583 | | |
| | 7 | 584 | | url := fmt.Sprintf("%s/repos/%s/%s/issues", p.config.APIURL, neturl.PathEscape(p.config.Owner), neturl.PathEscape(p.co |
| | 7 | 585 | | req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(jsonData)) |
| | 1 | 586 | | if err != nil { |
| | 1 | 587 | | return err |
| | 1 | 588 | | } |
| | 6 | 589 | | p.setHeaders(req) |
| | 6 | 590 | | |
| | 6 | 591 | | resp, err := p.client.Do(req) |
| | 1 | 592 | | if err != nil { |
| | 1 | 593 | | return err |
| | 1 | 594 | | } |
| | 5 | 595 | | defer func() { _ = resp.Body.Close() }() |
| | | 596 | | |
| | 2 | 597 | | if resp.StatusCode != http.StatusCreated { |
| | 2 | 598 | | respBody, _ := io.ReadAll(resp.Body) |
| | 2 | 599 | | return fmt.Errorf("create issue failed (%d): %s", resp.StatusCode, string(respBody)) |
| | 2 | 600 | | } |
| | | 601 | | |
| | 3 | 602 | | p.logger.Info("created failure issue", "title", title) |
| | 3 | 603 | | return nil |
| | | 604 | | } |