| 1 | // Package github provides a client for the Github REST API. |
| 2 | package github |
| 3 | |
| 4 | import ( |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "net/http" |
| 10 | "os" |
| 11 | ) |
| 12 | |
| 13 | // Client for the Github REST API. |
| 14 | type Client struct { |
| 15 | client *http.Client |
| 16 | base string |
| 17 | token string |
| 18 | } |
| 19 | |
| 20 | // Option configures a Github client. |
| 21 | type Option func(*Client) |
| 22 | |
| 23 | // WithHTTPClient configures the HTTP client that should be used for Github API |
| 24 | // requests. |
| 25 | func WithHTTPClient(h *http.Client) Option { |
| 26 | return func(c *Client) { c.client = h } |
| 27 | } |
| 28 | |
| 29 | // WithToken configures a Client with an authentication token for Github API |
| 30 | // requests. |
| 31 | func WithToken(token string) Option { |
| 32 | return func(c *Client) { c.token = token } |
| 33 | } |
| 34 | |
| 35 | // WithTokenFromEnvironment configures a Client using the GITHUB_TOKEN |
| 36 | // environment variable. |
| 37 | func WithTokenFromEnvironment() Option { |
| 38 | return WithToken(os.Getenv("GITHUB_TOKEN")) |
| 39 | } |
| 40 | |
| 41 | // NewClient initializes a client using the given HTTP client. |
| 42 | func NewClient(opts ...Option) *Client { |
| 43 | c := &Client{ |
| 44 | client: http.DefaultClient, |
| 45 | base: "https://api.github.com", |
| 46 | } |
| 47 | for _, opt := range opts { |
| 48 | opt(c) |
| 49 | } |
| 50 | return c |
| 51 | } |
| 52 | |
| 53 | // Repository gets information about the given Github repository. |
| 54 | func (c *Client) Repository(ctx context.Context, owner, name string) (*Repository, error) { |
| 55 | // Build request. |
| 56 | u := c.base + "/repos/" + owner + "/" + name |
| 57 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil) |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | |
| 62 | // Execute. |
| 63 | repo := &Repository{} |
| 64 | if err := c.request(req, repo); err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | |
| 68 | return repo, nil |
| 69 | } |
| 70 | |
| 71 | // Issue gets information about the given Github issue. |
| 72 | func (c *Client) Issue(ctx context.Context, owner, name string, number int) (*Issue, error) { |
| 73 | // Build request. |
| 74 | u := fmt.Sprintf("%s/repos/%s/%s/issues/%d", c.base, owner, name, number) |
| 75 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil) |
| 76 | if err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | |
| 80 | // Execute. |
| 81 | issue := &Issue{} |
| 82 | if err := c.request(req, issue); err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | |
| 86 | return issue, nil |
| 87 | } |
| 88 | |
| 89 | func (c *Client) request(req *http.Request, payload any) (err error) { |
| 90 | // Add common headers. |
| 91 | if c.token != "" { |
| 92 | req.Header.Set("Authorization", "Bearer "+c.token) |
| 93 | } |
| 94 | req.Header.Set("Accept", "application/vnd.github.v3+json") |
| 95 | |
| 96 | // Execute the request. |
| 97 | res, err := c.client.Do(req) |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | defer func() { |
| 102 | if errc := res.Body.Close(); errc != nil && err == nil { |
| 103 | err = errc |
| 104 | } |
| 105 | }() |
| 106 | |
| 107 | // Check status. |
| 108 | if res.StatusCode != http.StatusOK { |
| 109 | return fmt.Errorf("http status %d: %s", res.StatusCode, http.StatusText(res.StatusCode)) |
| 110 | } |
| 111 | |
| 112 | // Parse response body. |
| 113 | d := json.NewDecoder(res.Body) |
| 114 | |
| 115 | if err := d.Decode(payload); err != nil { |
| 116 | return err |
| 117 | } |
| 118 | |
| 119 | // Should not have trailing data. |
| 120 | if d.More() { |
| 121 | return errors.New("unexpected extra data after JSON") |
| 122 | } |
| 123 | |
| 124 | return nil |
| 125 | } |