diff --git a/github.go b/github.go index 33372ccc..c05a1ebe 100644 --- a/github.go +++ b/github.go @@ -37,6 +37,32 @@ type GithubClient struct { Owner string } +type withPreviewHeader struct { + http.Header + rt http.RoundTripper + previewHeaders []string +} + +func WithPreviewHeader(rt http.RoundTripper, previewHeaders []string) withPreviewHeader { + if rt == nil { + rt = http.DefaultTransport + } + + headers := make(http.Header) + return withPreviewHeader{Header: headers, rt: rt, previewHeaders: previewHeaders} +} + +func (h withPreviewHeader) RoundTrip(req *http.Request) (*http.Response, error) { + for k, v := range h.Header { + req.Header[k] = v + } + + currentAccept := req.Header.Get("Accept") + // shadow-cat-preview: Draft pull requests + req.Header.Set("Accept", fmt.Sprintf("%s,%s", currentAccept, strings.Join(h.previewHeaders, ","))) + return h.rt.RoundTrip(req) +} + // NewGithubClient ... func NewGithubClient(s *Source) (*GithubClient, error) { owner, repository, err := parseRepository(s.Repository) @@ -47,6 +73,9 @@ func NewGithubClient(s *Source) (*GithubClient, error) { // Skip SSL verification for self-signed certificates // source: https://github.com/google/go-github/pull/598#issuecomment-333039238 var ctx context.Context + + // Support Enterprise Server + if s.SkipSSLVerification { insecureClient := &http.Client{Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, @@ -81,6 +110,9 @@ func NewGithubClient(s *Source) (*GithubClient, error) { if err != nil { return nil, fmt.Errorf("failed to parse v4 endpoint: %s", err) } + previewV4Headers := []string{"application/vnd.github.antiope-preview+json", "application/vnd.github.shadow-cat-preview+json", "application/vnd.github.bane-preview+json"} + rt := WithPreviewHeader(client.Transport, previewV4Headers) + client.Transport = rt v4 = githubv4.NewEnterpriseClient(endpoint.String(), client) if err != nil { return nil, err