1861a351f8
Breaking changes addressed: - raw.NewClient: Use WithHTTPClient/WithEnterpriseURLs options, pass ctx to NewRequest, return (*Client, error) - internal/ghmcp/server.go: Use functional options for REST client creation, replace UserAgent field mutation with UserAgentTransport wrapper, add restUATransp field to githubClients struct - pkg/github/dependencies.go: Use functional options for REST client creation, handle raw.NewClient error return - pkg/github/actions.go: Handle new WorkflowDispatchRunDetails return value from CreateWorkflowDispatchEventByID/ByFileName - pkg/github/issues.go: Replace IssueListOptions with ListOptions for SubIssue.ListByIssue - pkg/github/notifications.go: MarkThreadDone now takes string instead of int64; remove ParseInt and strconv import - pkg/github/projects.go: Remove pointer indirection from ListProjectsPaginationOptions and ListProjectsOptions fields - pkg/github/issues_granular.go: Pass ctx to NewRequest, remove ctx from Do - Test files: Add mustNewGHClient helper, replace all NewClient calls, fix stubClientFnFromHTTP signature, fix lockdown_test.go BaseURL handling, fix raw_test.go, remove invalid threadID test case Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
74 lines
2.1 KiB
Go
74 lines
2.1 KiB
Go
// Package raw provides a client for interacting with the GitHub raw file API
|
|
package raw
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
gogithub "github.com/google/go-github/v87/github"
|
|
)
|
|
|
|
// GetRawClientFn is a function type that returns a RawClient instance.
|
|
type GetRawClientFn func(context.Context) (*Client, error)
|
|
|
|
// Client is a client for interacting with the GitHub raw content API.
|
|
type Client struct {
|
|
url *url.URL
|
|
client *gogithub.Client
|
|
}
|
|
|
|
// NewClient creates a new instance of the raw API Client with the provided GitHub client and provided URL.
|
|
func NewClient(client *gogithub.Client, rawURL *url.URL) (*Client, error) {
|
|
newClient, err := gogithub.NewClient(
|
|
gogithub.WithHTTPClient(client.Client()),
|
|
gogithub.WithEnterpriseURLs(rawURL.String(), rawURL.String()),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Client{client: newClient, url: rawURL}, nil
|
|
}
|
|
|
|
func (c *Client) newRequest(ctx context.Context, method string, urlStr string, body any, opts ...gogithub.RequestOption) (*http.Request, error) {
|
|
return c.client.NewRequest(ctx, method, urlStr, body, opts...)
|
|
}
|
|
|
|
func (c *Client) refURL(owner, repo, ref, path string) string {
|
|
if ref == "" {
|
|
return c.url.JoinPath(owner, repo, "HEAD", path).String()
|
|
}
|
|
return c.url.JoinPath(owner, repo, ref, path).String()
|
|
}
|
|
|
|
func (c *Client) URLFromOpts(opts *ContentOpts, owner, repo, path string) string {
|
|
if opts == nil {
|
|
opts = &ContentOpts{}
|
|
}
|
|
if opts.SHA != "" {
|
|
return c.commitURL(owner, repo, opts.SHA, path)
|
|
}
|
|
return c.refURL(owner, repo, opts.Ref, path)
|
|
}
|
|
|
|
// BlobURL returns the URL for a blob in the raw content API.
|
|
func (c *Client) commitURL(owner, repo, sha, path string) string {
|
|
return c.url.JoinPath(owner, repo, sha, path).String()
|
|
}
|
|
|
|
type ContentOpts struct {
|
|
Ref string
|
|
SHA string
|
|
}
|
|
|
|
// GetRawContent fetches the raw content of a file from a GitHub repository.
|
|
func (c *Client) GetRawContent(ctx context.Context, owner, repo, path string, opts *ContentOpts) (*http.Response, error) {
|
|
url := c.URLFromOpts(opts, owner, repo, path)
|
|
req, err := c.newRequest(ctx, "GET", url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return c.client.Client().Do(req)
|
|
}
|