Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create MarkdownService, EmojisService, CodesOfConductService and MetaService #2937

Merged
merged 15 commits into from Oct 9, 2023
4 changes: 2 additions & 2 deletions github/examples_test.go
Expand Up @@ -15,14 +15,14 @@ import (
"github.com/google/go-github/v55/github"
)

func ExampleClient_Markdown() {
func ExampleMarkdownService_Markdown() {
client := github.NewClient(nil)

input := "# heading #\n\nLink to issue #1"
opt := &github.MarkdownOptions{Mode: "gfm", Context: "google/go-github"}

ctx := context.Background()
output, _, err := client.Markdown(ctx, input, opt)
output, _, err := client.Markdown.Markdown(ctx, input, opt)
if err != nil {
fmt.Println(err)
}
Expand Down
8 changes: 8 additions & 0 deletions github/github.go
Expand Up @@ -183,9 +183,11 @@ type Client struct {
Billing *BillingService
Checks *ChecksService
CodeScanning *CodeScanningService
CodesOfConduct *CodesOfConductService
Codespaces *CodespacesService
Dependabot *DependabotService
DependencyGraph *DependencyGraphService
Emojis *EmojisService
Enterprise *EnterpriseService
Gists *GistsService
Git *GitService
Expand All @@ -194,7 +196,9 @@ type Client struct {
IssueImport *IssueImportService
Issues *IssuesService
Licenses *LicensesService
Markdown *MarkdownService
Marketplace *MarketplaceService
Meta *MetaService
Migrations *MigrationService
Organizations *OrganizationsService
Projects *ProjectsService
Expand Down Expand Up @@ -401,8 +405,10 @@ func (c *Client) initialize() {
c.Checks = (*ChecksService)(&c.common)
c.CodeScanning = (*CodeScanningService)(&c.common)
c.Codespaces = (*CodespacesService)(&c.common)
c.CodesOfConduct = (*CodesOfConductService)(&c.common)
c.Dependabot = (*DependabotService)(&c.common)
c.DependencyGraph = (*DependencyGraphService)(&c.common)
c.Emojis = (*EmojisService)(&c.common)
c.Enterprise = (*EnterpriseService)(&c.common)
c.Gists = (*GistsService)(&c.common)
c.Git = (*GitService)(&c.common)
Expand All @@ -411,7 +417,9 @@ func (c *Client) initialize() {
c.IssueImport = (*IssueImportService)(&c.common)
c.Issues = (*IssuesService)(&c.common)
c.Licenses = (*LicensesService)(&c.common)
c.Markdown = (*MarkdownService)(&c.common)
c.Marketplace = &MarketplaceService{client: c}
c.Meta = (*MetaService)(&c.common)
c.Migrations = (*MigrationService)(&c.common)
c.Organizations = (*OrganizationsService)(&c.common)
c.Projects = (*ProjectsService)(&c.common)
Expand Down
105 changes: 79 additions & 26 deletions github/misc.go
Expand Up @@ -37,10 +37,13 @@ type markdownRequest struct {
Context *string `json:"context,omitempty"`
}

// MarkdownService provides access to markdown-related functions in the GitHub API.
type MarkdownService service

// Markdown renders an arbitrary Markdown document.
//
// GitHub API docs: https://docs.github.com/en/rest/markdown/
func (c *Client) Markdown(ctx context.Context, text string, opts *MarkdownOptions) (string, *Response, error) {
// GitHub API docs: https://docs.github.com/rest/markdown/markdown#render-a-markdown-document
func (s *MarkdownService) Markdown(ctx context.Context, text string, opts *MarkdownOptions) (string, *Response, error) {
request := &markdownRequest{Text: String(text)}
if opts != nil {
if opts.Mode != "" {
Expand All @@ -51,38 +54,48 @@ func (c *Client) Markdown(ctx context.Context, text string, opts *MarkdownOption
}
}

req, err := c.NewRequest("POST", "markdown", request)
req, err := s.client.NewRequest("POST", "markdown", request)
if err != nil {
return "", nil, err
}

buf := new(bytes.Buffer)
resp, err := c.Do(ctx, req, buf)
resp, err := s.client.Do(ctx, req, buf)
if err != nil {
return "", resp, err
}

return buf.String(), resp, nil
}

// EmojisService provides access to emoji-related functions in the GitHub API.
type EmojisService service

// ListEmojis returns the emojis available to use on GitHub.
//
// GitHub API docs: https://docs.github.com/en/rest/emojis/
func (c *Client) ListEmojis(ctx context.Context) (map[string]string, *Response, error) {
req, err := c.NewRequest("GET", "emojis", nil)
// GitHub API docs: https://docs.github.com/rest/emojis/emojis#get-emojis
func (s *EmojisService) ListEmojis(ctx context.Context) (map[string]string, *Response, error) {
req, err := s.client.NewRequest("GET", "emojis", nil)
if err != nil {
return nil, nil, err
}

var emoji map[string]string
resp, err := c.Do(ctx, req, &emoji)
resp, err := s.client.Do(ctx, req, &emoji)
if err != nil {
return nil, resp, err
}

return emoji, resp, nil
}

// ListEmojis returns the emojis available to use on GitHub.
//
// Deprecated: Use EmojisService.ListEmojis instead
func (c *Client) ListEmojis(ctx context.Context) (map[string]string, *Response, error) {
return c.Emojis.ListEmojis(ctx)
}

// CodeOfConduct represents a code of conduct.
type CodeOfConduct struct {
Name *string `json:"name,omitempty"`
Expand All @@ -95,11 +108,14 @@ func (c *CodeOfConduct) String() string {
return Stringify(c)
}

// CodesOfConductService provides access to code-of-conduct-related functions in the GitHub API.
type CodesOfConductService service

// ListCodesOfConduct returns all codes of conduct.
//
// GitHub API docs: https://docs.github.com/en/rest/codes_of_conduct/#list-all-codes-of-conduct
func (c *Client) ListCodesOfConduct(ctx context.Context) ([]*CodeOfConduct, *Response, error) {
req, err := c.NewRequest("GET", "codes_of_conduct", nil)
// GitHub API docs: https://docs.github.com/rest/codes-of-conduct/codes-of-conduct#get-all-codes-of-conduct
func (s *CodesOfConductService) ListCodesOfConduct(ctx context.Context) ([]*CodeOfConduct, *Response, error) {
req, err := s.client.NewRequest("GET", "codes_of_conduct", nil)
if err != nil {
return nil, nil, err
}
Expand All @@ -108,20 +124,26 @@ func (c *Client) ListCodesOfConduct(ctx context.Context) ([]*CodeOfConduct, *Res
req.Header.Set("Accept", mediaTypeCodesOfConductPreview)

var cs []*CodeOfConduct
resp, err := c.Do(ctx, req, &cs)
resp, err := s.client.Do(ctx, req, &cs)
if err != nil {
return nil, resp, err
}

return cs, resp, nil
}

// ListCodesOfConduct
// Deprecated: Use CodesOfConductService.ListCodesOfConduct instead
func (c *Client) ListCodesOfConduct(ctx context.Context) ([]*CodeOfConduct, *Response, error) {
return c.CodesOfConduct.ListCodesOfConduct(ctx)
}

// GetCodeOfConduct returns an individual code of conduct.
//
// https://docs.github.com/en/rest/codes_of_conduct/#get-an-individual-code-of-conduct
func (c *Client) GetCodeOfConduct(ctx context.Context, key string) (*CodeOfConduct, *Response, error) {
// GitHub API docs: https://docs.github.com/rest/codes-of-conduct/codes-of-conduct#get-a-code-of-conduct
func (s *CodesOfConductService) GetCodeOfConduct(ctx context.Context, key string) (*CodeOfConduct, *Response, error) {
u := fmt.Sprintf("codes_of_conduct/%s", key)
req, err := c.NewRequest("GET", u, nil)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
Expand All @@ -130,14 +152,23 @@ func (c *Client) GetCodeOfConduct(ctx context.Context, key string) (*CodeOfCondu
req.Header.Set("Accept", mediaTypeCodesOfConductPreview)

coc := new(CodeOfConduct)
resp, err := c.Do(ctx, req, coc)
resp, err := s.client.Do(ctx, req, coc)
if err != nil {
return nil, resp, err
}

return coc, resp, nil
}

// GetCodeOfConduct
// Deprecated: Use CodesOfConductService.GetCodeOfConduct instead
func (c *Client) GetCodeOfConduct(ctx context.Context, key string) (*CodeOfConduct, *Response, error) {
return c.CodesOfConduct.GetCodeOfConduct(ctx, key)
}

// MetaService provides access to functions in the GitHub API that GitHub categorizes as "meta".
type MetaService service

// APIMeta represents metadata about the GitHub API.
type APIMeta struct {
// An Array of IP addresses in CIDR format specifying the addresses
Expand Down Expand Up @@ -190,58 +221,80 @@ type APIMeta struct {
// this endpoint on your organization’s GitHub Enterprise installation, this
// endpoint provides information about that installation.
//
// GitHub API docs: https://docs.github.com/en/rest/meta#get-github-meta-information
func (c *Client) APIMeta(ctx context.Context) (*APIMeta, *Response, error) {
req, err := c.NewRequest("GET", "meta", nil)
// GitHub API docs: https://docs.github.com/rest/meta/meta#get-github-meta-information
func (s *MetaService) APIMeta(ctx context.Context) (*APIMeta, *Response, error) {
req, err := s.client.NewRequest("GET", "meta", nil)
if err != nil {
return nil, nil, err
}

meta := new(APIMeta)
resp, err := c.Do(ctx, req, meta)
resp, err := s.client.Do(ctx, req, meta)
if err != nil {
return nil, resp, err
}

return meta, resp, nil
}

// APIMeta
// Deprecated: Use MetaService.APIMeta instead.
func (c *Client) APIMeta(ctx context.Context) (*APIMeta, *Response, error) {
return c.Meta.APIMeta(ctx)
}

// Octocat returns an ASCII art octocat with the specified message in a speech
// bubble. If message is empty, a random zen phrase is used.
func (c *Client) Octocat(ctx context.Context, message string) (string, *Response, error) {
//
// GitHub API docs: https://docs.github.com/rest/meta/meta#get-octocat
func (s *MetaService) Octocat(ctx context.Context, message string) (string, *Response, error) {
u := "octocat"
if message != "" {
u = fmt.Sprintf("%s?s=%s", u, url.QueryEscape(message))
}

req, err := c.NewRequest("GET", u, nil)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return "", nil, err
}

buf := new(bytes.Buffer)
resp, err := c.Do(ctx, req, buf)
resp, err := s.client.Do(ctx, req, buf)
if err != nil {
return "", resp, err
}

return buf.String(), resp, nil
}

// Octocat
// Deprecated: Use MetaService.Octocat instead.
func (c *Client) Octocat(ctx context.Context, message string) (string, *Response, error) {
return c.Meta.Octocat(ctx, message)
}

// Zen returns a random line from The Zen of GitHub.
//
// see also: http://warpspire.com/posts/taste/
func (c *Client) Zen(ctx context.Context) (string, *Response, error) {
req, err := c.NewRequest("GET", "zen", nil)
//
// GitHub API docs: https://docs.github.com/rest/meta/meta#get-the-zen-of-github
func (s *MetaService) Zen(ctx context.Context) (string, *Response, error) {
req, err := s.client.NewRequest("GET", "zen", nil)
if err != nil {
return "", nil, err
}

buf := new(bytes.Buffer)
resp, err := c.Do(ctx, req, buf)
resp, err := s.client.Do(ctx, req, buf)
if err != nil {
return "", resp, err
}

return buf.String(), resp, nil
}

// Zen
// Deprecated: Use MetaService.Zen instead.
func (c *Client) Zen(ctx context.Context) (string, *Response, error) {
return c.Meta.Zen(ctx)
}
18 changes: 9 additions & 9 deletions github/misc_test.go
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestMarkdown(t *testing.T) {
func TestMarkdownService_Markdown(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

Expand All @@ -36,7 +36,7 @@ func TestMarkdown(t *testing.T) {
})

ctx := context.Background()
md, _, err := client.Markdown(ctx, "# text #", &MarkdownOptions{
md, _, err := client.Markdown.Markdown(ctx, "# text #", &MarkdownOptions{
Mode: "gfm",
Context: "google/go-github",
})
Expand All @@ -50,7 +50,7 @@ func TestMarkdown(t *testing.T) {

const methodName = "Markdown"
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Markdown(ctx, "# text #", &MarkdownOptions{
got, resp, err := client.Markdown.Markdown(ctx, "# text #", &MarkdownOptions{
Mode: "gfm",
Context: "google/go-github",
})
Expand All @@ -61,7 +61,7 @@ func TestMarkdown(t *testing.T) {
})
}

func TestListEmojis(t *testing.T) {
func TestEmojisService_ListEmojis(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

Expand Down Expand Up @@ -91,7 +91,7 @@ func TestListEmojis(t *testing.T) {
})
}

func TestListCodesOfConduct(t *testing.T) {
func TestCodesOfConductService_ListCodesOfConduct(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

Expand Down Expand Up @@ -131,7 +131,7 @@ func TestListCodesOfConduct(t *testing.T) {
})
}

func TestGetCodeOfConduct(t *testing.T) {
func TestCodesOfConductService_GetCodeOfConduct(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

Expand Down Expand Up @@ -210,7 +210,7 @@ func TestAPIMeta_Marshal(t *testing.T) {
testJSONMarshal(t, a, want)
}

func TestAPIMeta(t *testing.T) {
func TestMetaService_APIMeta(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

Expand Down Expand Up @@ -251,7 +251,7 @@ func TestAPIMeta(t *testing.T) {
})
}

func TestOctocat(t *testing.T) {
func TestMetaService_Octocat(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

Expand Down Expand Up @@ -285,7 +285,7 @@ func TestOctocat(t *testing.T) {
})
}

func TestZen(t *testing.T) {
func TestMetaService_Zen(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

Expand Down
4 changes: 2 additions & 2 deletions test/integration/misc_test.go
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestEmojis(t *testing.T) {
emoji, _, err := client.ListEmojis(context.Background())
emoji, _, err := client.Emojis.ListEmojis(context.Background())
if err != nil {
t.Fatalf("ListEmojis returned error: %v", err)
}
Expand All @@ -30,7 +30,7 @@ func TestEmojis(t *testing.T) {
}

func TestAPIMeta(t *testing.T) {
meta, _, err := client.APIMeta(context.Background())
meta, _, err := client.Meta.APIMeta(context.Background())
if err != nil {
t.Fatalf("APIMeta returned error: %v", err)
}
Expand Down