| | | 1 | | package github |
| | | 2 | | |
| | | 3 | | import ( |
| | | 4 | | "bytes" |
| | | 5 | | "context" |
| | | 6 | | "encoding/json" |
| | | 7 | | "fmt" |
| | | 8 | | "io" |
| | | 9 | | "net/http" |
| | | 10 | | "os" |
| | | 11 | | |
| | | 12 | | "github.com/jedi-knights/go-semantic-release/internal/domain" |
| | | 13 | | "github.com/jedi-knights/go-semantic-release/internal/ports" |
| | | 14 | | ) |
| | | 15 | | |
| | | 16 | | // Compile-time interface compliance check. |
| | | 17 | | var _ ports.ReleasePublisher = (*Publisher)(nil) |
| | | 18 | | |
| | | 19 | | // Publisher implements ports.ReleasePublisher for GitHub Releases. |
| | | 20 | | type Publisher struct { |
| | | 21 | | owner string |
| | | 22 | | repo string |
| | | 23 | | token string |
| | | 24 | | client *http.Client |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | // NewPublisher creates a GitHub release publisher. |
| | | 28 | | // If token is empty, it is resolved from GH_TOKEN, GITHUB_TOKEN, or |
| | | 29 | | // SEMANTIC_RELEASE_GITHUB_TOKEN environment variables. |
| | | 30 | | func NewPublisher(owner, repo, token string) *Publisher { |
| | | 31 | | if token == "" { |
| | | 32 | | for _, key := range []string{"GH_TOKEN", "GITHUB_TOKEN", "SEMANTIC_RELEASE_GITHUB_TOKEN"} { |
| | | 33 | | if v := os.Getenv(key); v != "" { |
| | | 34 | | token = v |
| | | 35 | | break |
| | | 36 | | } |
| | | 37 | | } |
| | | 38 | | } |
| | | 39 | | return &Publisher{ |
| | | 40 | | owner: owner, |
| | | 41 | | repo: repo, |
| | | 42 | | token: token, |
| | | 43 | | client: &http.Client{}, |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | type createReleaseRequest struct { |
| | | 48 | | TagName string `json:"tag_name"` |
| | | 49 | | Name string `json:"name"` |
| | | 50 | | Body string `json:"body"` |
| | | 51 | | Prerelease bool `json:"prerelease"` |
| | | 52 | | Draft bool `json:"draft"` |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | type createReleaseResponse struct { |
| | | 56 | | HTMLURL string `json:"html_url"` |
| | | 57 | | ID int `json:"id"` |
| | | 58 | | } |
| | | 59 | | |
| | 3 | 60 | | func (p *Publisher) Publish(ctx context.Context, params ports.PublishParams) (domain.ProjectReleaseResult, error) { |
| | 3 | 61 | | name := params.TagName |
| | 1 | 62 | | if params.Project != "" { |
| | 1 | 63 | | name = fmt.Sprintf("%s %s", params.Project, params.Version.String()) |
| | 1 | 64 | | } |
| | | 65 | | |
| | 3 | 66 | | reqBody := createReleaseRequest{ |
| | 3 | 67 | | TagName: params.TagName, |
| | 3 | 68 | | Name: name, |
| | 3 | 69 | | Body: params.Changelog, |
| | 3 | 70 | | Prerelease: params.Prerelease, |
| | 3 | 71 | | } |
| | 3 | 72 | | |
| | 3 | 73 | | jsonData, err := json.Marshal(reqBody) |
| | 0 | 74 | | if err != nil { |
| | 0 | 75 | | return domain.ProjectReleaseResult{}, fmt.Errorf("marshaling release request: %w", err) |
| | 0 | 76 | | } |
| | | 77 | | |
| | 3 | 78 | | url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases", p.owner, p.repo) |
| | 3 | 79 | | req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(jsonData)) |
| | 0 | 80 | | if err != nil { |
| | 0 | 81 | | return domain.ProjectReleaseResult{}, fmt.Errorf("creating request: %w", err) |
| | 0 | 82 | | } |
| | | 83 | | |
| | 3 | 84 | | req.Header.Set("Authorization", "token "+p.token) |
| | 3 | 85 | | req.Header.Set("Content-Type", "application/json") |
| | 3 | 86 | | req.Header.Set("Accept", "application/vnd.github+json") |
| | 3 | 87 | | |
| | 3 | 88 | | resp, err := p.client.Do(req) |
| | 0 | 89 | | if err != nil { |
| | 0 | 90 | | return domain.ProjectReleaseResult{}, fmt.Errorf("publishing release: %w", err) |
| | 0 | 91 | | } |
| | 3 | 92 | | defer func() { _ = resp.Body.Close() }() |
| | | 93 | | |
| | 1 | 94 | | if resp.StatusCode != http.StatusCreated { |
| | 1 | 95 | | body, _ := io.ReadAll(resp.Body) |
| | 1 | 96 | | return domain.ProjectReleaseResult{}, fmt.Errorf("github release failed (%d): %s", resp.StatusCode, string(body)) |
| | 1 | 97 | | } |
| | | 98 | | |
| | 2 | 99 | | var releaseResp createReleaseResponse |
| | 0 | 100 | | if err := json.NewDecoder(resp.Body).Decode(&releaseResp); err != nil { |
| | 0 | 101 | | return domain.ProjectReleaseResult{}, fmt.Errorf("decoding release response: %w", err) |
| | 0 | 102 | | } |
| | | 103 | | |
| | 2 | 104 | | return domain.ProjectReleaseResult{ |
| | 2 | 105 | | TagName: params.TagName, |
| | 2 | 106 | | Published: true, |
| | 2 | 107 | | PublishURL: releaseResp.HTMLURL, |
| | 2 | 108 | | Changelog: params.Changelog, |
| | 2 | 109 | | }, nil |
| | | 110 | | } |