From 90a6bb35ecfffec23668eb8f90da4e03ca743652 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Wed, 20 Sep 2023 13:22:31 -0500 Subject: [PATCH 01/13] Create new services --- github/examples_test.go | 4 +- github/github.go | 8 +++ github/misc.go | 106 +++++++++++++++++++++++++++++++--------- github/misc_test.go | 44 ++++++++--------- 4 files changed, 115 insertions(+), 47 deletions(-) diff --git a/github/examples_test.go b/github/examples_test.go index a2147bf417..db82e4de0d 100644 --- a/github/examples_test.go +++ b/github/examples_test.go @@ -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) } diff --git a/github/github.go b/github/github.go index e9ce136b7b..7d352db75b 100644 --- a/github/github.go +++ b/github/github.go @@ -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 @@ -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 @@ -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) @@ -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) diff --git a/github/misc.go b/github/misc.go index a01b716fa2..703b19929a 100644 --- a/github/misc.go +++ b/github/misc.go @@ -37,10 +37,12 @@ type markdownRequest struct { Context *string `json:"context,omitempty"` } +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 != "" { @@ -51,13 +53,13 @@ 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 } @@ -65,9 +67,29 @@ func (c *Client) Markdown(ctx context.Context, text string, opts *MarkdownOption return buf.String(), resp, nil } +type EmojisService service + +// ListEmojis returns the emojis available to use on GitHub. +// +// 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 := 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. // -// GitHub API docs: https://docs.github.com/en/rest/emojis/ +// Deprecated: Use EmojisService.ListEmojis instead func (c *Client) ListEmojis(ctx context.Context) (map[string]string, *Response, error) { req, err := c.NewRequest("GET", "emojis", nil) if err != nil { @@ -95,11 +117,13 @@ func (c *CodeOfConduct) String() string { return Stringify(c) } +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 } @@ -108,7 +132,7 @@ 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 } @@ -116,12 +140,18 @@ func (c *Client) ListCodesOfConduct(ctx context.Context) ([]*CodeOfConduct, *Res 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 } @@ -130,7 +160,7 @@ 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 } @@ -138,6 +168,14 @@ func (c *Client) GetCodeOfConduct(ctx context.Context, key string) (*CodeOfCondu 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) +} + +type MetaService service + // APIMeta represents metadata about the GitHub API. type APIMeta struct { // An Array of IP addresses in CIDR format specifying the addresses @@ -190,15 +228,15 @@ 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 } @@ -206,21 +244,29 @@ func (c *Client) APIMeta(ctx context.Context) (*APIMeta, *Response, error) { 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 } @@ -228,20 +274,34 @@ func (c *Client) Octocat(ctx context.Context, message string) (string, *Response 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) +} diff --git a/github/misc_test.go b/github/misc_test.go index 45c9989381..c8c224961b 100644 --- a/github/misc_test.go +++ b/github/misc_test.go @@ -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() @@ -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", }) @@ -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", }) @@ -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() @@ -71,7 +71,7 @@ func TestListEmojis(t *testing.T) { }) ctx := context.Background() - emoji, _, err := client.ListEmojis(ctx) + emoji, _, err := client.Emojis.ListEmojis(ctx) if err != nil { t.Errorf("ListEmojis returned error: %v", err) } @@ -83,7 +83,7 @@ func TestListEmojis(t *testing.T) { const methodName = "ListEmojis" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.ListEmojis(ctx) + got, resp, err := client.Emojis.ListEmojis(ctx) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -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() @@ -106,7 +106,7 @@ func TestListCodesOfConduct(t *testing.T) { }) ctx := context.Background() - cs, _, err := client.ListCodesOfConduct(ctx) + cs, _, err := client.CodesOfConduct.ListCodesOfConduct(ctx) if err != nil { t.Errorf("ListCodesOfConduct returned error: %v", err) } @@ -123,7 +123,7 @@ func TestListCodesOfConduct(t *testing.T) { const methodName = "ListCodesOfConduct" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.ListCodesOfConduct(ctx) + got, resp, err := client.CodesOfConduct.ListCodesOfConduct(ctx) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -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() @@ -147,7 +147,7 @@ func TestGetCodeOfConduct(t *testing.T) { }) ctx := context.Background() - coc, _, err := client.GetCodeOfConduct(ctx, "k") + coc, _, err := client.CodesOfConduct.GetCodeOfConduct(ctx, "k") if err != nil { t.Errorf("ListCodesOfConduct returned error: %v", err) } @@ -164,12 +164,12 @@ func TestGetCodeOfConduct(t *testing.T) { const methodName = "GetCodeOfConduct" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.GetCodeOfConduct(ctx, "\n") + _, _, err = client.CodesOfConduct.GetCodeOfConduct(ctx, "\n") return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.GetCodeOfConduct(ctx, "k") + got, resp, err := client.CodesOfConduct.GetCodeOfConduct(ctx, "k") if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -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() @@ -220,7 +220,7 @@ func TestAPIMeta(t *testing.T) { }) ctx := context.Background() - meta, _, err := client.APIMeta(ctx) + meta, _, err := client.Meta.APIMeta(ctx) if err != nil { t.Errorf("APIMeta returned error: %v", err) } @@ -243,7 +243,7 @@ func TestAPIMeta(t *testing.T) { const methodName = "APIMeta" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.APIMeta(ctx) + got, resp, err := client.Meta.APIMeta(ctx) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -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() @@ -266,7 +266,7 @@ func TestOctocat(t *testing.T) { }) ctx := context.Background() - got, _, err := client.Octocat(ctx, input) + got, _, err := client.Meta.Octocat(ctx, input) if err != nil { t.Errorf("Octocat returned error: %v", err) } @@ -277,7 +277,7 @@ func TestOctocat(t *testing.T) { const methodName = "Octocat" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Octocat(ctx, input) + got, resp, err := client.Meta.Octocat(ctx, input) if got != "" { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -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() @@ -298,7 +298,7 @@ func TestZen(t *testing.T) { }) ctx := context.Background() - got, _, err := client.Zen(ctx) + got, _, err := client.Meta.Zen(ctx) if err != nil { t.Errorf("Zen returned error: %v", err) } @@ -309,7 +309,7 @@ func TestZen(t *testing.T) { const methodName = "Zen" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Zen(ctx) + got, resp, err := client.Meta.Zen(ctx) if got != "" { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } From 107ee30e3d8e028e381b66382e1d950c2a3086eb Mon Sep 17 00:00:00 2001 From: Will Roden Date: Wed, 20 Sep 2023 13:28:27 -0500 Subject: [PATCH 02/13] fix ListEmojis --- github/misc.go | 13 +------------ test/integration/misc_test.go | 4 ++-- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/github/misc.go b/github/misc.go index 703b19929a..f41f97540c 100644 --- a/github/misc.go +++ b/github/misc.go @@ -91,18 +91,7 @@ func (s *EmojisService) ListEmojis(ctx context.Context) (map[string]string, *Res // // Deprecated: Use EmojisService.ListEmojis instead func (c *Client) ListEmojis(ctx context.Context) (map[string]string, *Response, error) { - req, err := c.NewRequest("GET", "emojis", nil) - if err != nil { - return nil, nil, err - } - - var emoji map[string]string - resp, err := c.Do(ctx, req, &emoji) - if err != nil { - return nil, resp, err - } - - return emoji, resp, nil + return c.Emojis.ListEmojis(ctx) } // CodeOfConduct represents a code of conduct. diff --git a/test/integration/misc_test.go b/test/integration/misc_test.go index e0cee29bf0..d62363d45a 100644 --- a/test/integration/misc_test.go +++ b/test/integration/misc_test.go @@ -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) } @@ -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) } From fb26df48897013bd490e7dfb1a10b058e779c2ab Mon Sep 17 00:00:00 2001 From: Will Roden Date: Wed, 20 Sep 2023 13:48:58 -0500 Subject: [PATCH 03/13] move services to their own files --- github/codesofconduct.go | 75 +++++++ github/codesofconduct_test.go | 116 +++++++++++ github/emojis.go | 31 +++ github/emojis_test.go | 40 ++++ github/markdown.go | 61 ++++++ github/markdown_test.go | 75 +++++++ github/meta.go | 145 ++++++++++++++ github/meta_test.go | 155 +++++++++++++++ github/misc.go | 296 ---------------------------- github/misc_test.go | 356 ---------------------------------- 10 files changed, 698 insertions(+), 652 deletions(-) create mode 100644 github/codesofconduct.go create mode 100644 github/codesofconduct_test.go create mode 100644 github/emojis.go create mode 100644 github/emojis_test.go create mode 100644 github/markdown.go create mode 100644 github/markdown_test.go create mode 100644 github/meta.go create mode 100644 github/meta_test.go delete mode 100644 github/misc.go delete mode 100644 github/misc_test.go diff --git a/github/codesofconduct.go b/github/codesofconduct.go new file mode 100644 index 0000000000..a12f1bee1a --- /dev/null +++ b/github/codesofconduct.go @@ -0,0 +1,75 @@ +package github + +import ( + "context" + "fmt" +) + +type CodesOfConductService service + +// CodeOfConduct represents a code of conduct. +type CodeOfConduct struct { + Name *string `json:"name,omitempty"` + Key *string `json:"key,omitempty"` + URL *string `json:"url,omitempty"` + Body *string `json:"body,omitempty"` +} + +func (c *CodeOfConduct) String() string { + return Stringify(c) +} + +// ListCodesOfConduct returns all codes of conduct. +// +// 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 + } + + // TODO: remove custom Accept header when this API fully launches. + req.Header.Set("Accept", mediaTypeCodesOfConductPreview) + + var cs []*CodeOfConduct + 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. +// +// 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 := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + // TODO: remove custom Accept header when this API fully launches. + req.Header.Set("Accept", mediaTypeCodesOfConductPreview) + + coc := new(CodeOfConduct) + 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) +} diff --git a/github/codesofconduct_test.go b/github/codesofconduct_test.go new file mode 100644 index 0000000000..b00ef3b204 --- /dev/null +++ b/github/codesofconduct_test.go @@ -0,0 +1,116 @@ +package github + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestCodesOfConductService_ListCodesOfConduct(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/codes_of_conduct", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) + fmt.Fprint(w, `[{ + "key": "key", + "name": "name", + "url": "url"} + ]`) + }) + + ctx := context.Background() + cs, _, err := client.CodesOfConduct.ListCodesOfConduct(ctx) + if err != nil { + t.Errorf("ListCodesOfConduct returned error: %v", err) + } + + want := []*CodeOfConduct{ + { + Key: String("key"), + Name: String("name"), + URL: String("url"), + }} + if !cmp.Equal(want, cs) { + t.Errorf("ListCodesOfConduct returned %+v, want %+v", cs, want) + } + + const methodName = "ListCodesOfConduct" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.CodesOfConduct.ListCodesOfConduct(ctx) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestCodesOfConductService_GetCodeOfConduct(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/codes_of_conduct/k", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) + fmt.Fprint(w, `{ + "key": "key", + "name": "name", + "url": "url", + "body": "body"}`, + ) + }) + + ctx := context.Background() + coc, _, err := client.CodesOfConduct.GetCodeOfConduct(ctx, "k") + if err != nil { + t.Errorf("ListCodesOfConduct returned error: %v", err) + } + + want := &CodeOfConduct{ + Key: String("key"), + Name: String("name"), + URL: String("url"), + Body: String("body"), + } + if !cmp.Equal(want, coc) { + t.Errorf("GetCodeOfConductByKey returned %+v, want %+v", coc, want) + } + + const methodName = "GetCodeOfConduct" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.CodesOfConduct.GetCodeOfConduct(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.CodesOfConduct.GetCodeOfConduct(ctx, "k") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestCodeOfConduct_Marshal(t *testing.T) { + testJSONMarshal(t, &CodeOfConduct{}, "{}") + + a := &CodeOfConduct{ + Name: String("name"), + Key: String("key"), + URL: String("url"), + Body: String("body"), + } + + want := `{ + "name": "name", + "key": "key", + "url": "url", + "body": "body" + }` + + testJSONMarshal(t, a, want) +} diff --git a/github/emojis.go b/github/emojis.go new file mode 100644 index 0000000000..50a2e66092 --- /dev/null +++ b/github/emojis.go @@ -0,0 +1,31 @@ +package github + +import ( + "context" +) + +type EmojisService service + +// ListEmojis returns the emojis available to use on GitHub. +// +// 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 := s.client.Do(ctx, req, &emoji) + if err != nil { + return nil, resp, err + } + + return emoji, resp, nil +} + +// ListEmojis +// Deprecated: Use EmojisService.ListEmojis instead +func (c *Client) ListEmojis(ctx context.Context) (map[string]string, *Response, error) { + return c.Emojis.ListEmojis(ctx) +} diff --git a/github/emojis_test.go b/github/emojis_test.go new file mode 100644 index 0000000000..d3b3a4c452 --- /dev/null +++ b/github/emojis_test.go @@ -0,0 +1,40 @@ +package github + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestEmojisService_ListEmojis(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/emojis", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"+1": "+1.png"}`) + }) + + ctx := context.Background() + emoji, _, err := client.Emojis.ListEmojis(ctx) + if err != nil { + t.Errorf("ListEmojis returned error: %v", err) + } + + want := map[string]string{"+1": "+1.png"} + if !cmp.Equal(want, emoji) { + t.Errorf("ListEmojis returned %+v, want %+v", emoji, want) + } + + const methodName = "ListEmojis" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Emojis.ListEmojis(ctx) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} diff --git a/github/markdown.go b/github/markdown.go new file mode 100644 index 0000000000..a855a183e3 --- /dev/null +++ b/github/markdown.go @@ -0,0 +1,61 @@ +package github + +import ( + "bytes" + "context" +) + +type MarkdownService service + +// MarkdownOptions specifies optional parameters to the Markdown method. +type MarkdownOptions struct { + // Mode identifies the rendering mode. Possible values are: + // markdown - render a document as plain Markdown, just like + // README files are rendered. + // + // gfm - to render a document as user-content, e.g. like user + // comments or issues are rendered. In GFM mode, hard line breaks are + // always taken into account, and issue and user mentions are linked + // accordingly. + // + // Default is "markdown". + Mode string + + // Context identifies the repository context. Only taken into account + // when rendering as "gfm". + Context string +} + +type markdownRequest struct { + Text *string `json:"text,omitempty"` + Mode *string `json:"mode,omitempty"` + Context *string `json:"context,omitempty"` +} + +// Markdown renders an arbitrary Markdown document. +// +// 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 != "" { + request.Mode = String(opts.Mode) + } + if opts.Context != "" { + request.Context = String(opts.Context) + } + } + + req, err := s.client.NewRequest("POST", "markdown", request) + if err != nil { + return "", nil, err + } + + buf := new(bytes.Buffer) + resp, err := s.client.Do(ctx, req, buf) + if err != nil { + return "", resp, err + } + + return buf.String(), resp, nil +} diff --git a/github/markdown_test.go b/github/markdown_test.go new file mode 100644 index 0000000000..aae5e4e3f5 --- /dev/null +++ b/github/markdown_test.go @@ -0,0 +1,75 @@ +package github + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestMarkdownService_Markdown(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + input := &markdownRequest{ + Text: String("# text #"), + Mode: String("gfm"), + Context: String("google/go-github"), + } + mux.HandleFunc("/markdown", func(w http.ResponseWriter, r *http.Request) { + v := new(markdownRequest) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) + + testMethod(t, r, "POST") + if !cmp.Equal(v, input) { + t.Errorf("Request body = %+v, want %+v", v, input) + } + fmt.Fprint(w, `

text

`) + }) + + ctx := context.Background() + md, _, err := client.Markdown.Markdown(ctx, "# text #", &MarkdownOptions{ + Mode: "gfm", + Context: "google/go-github", + }) + if err != nil { + t.Errorf("Markdown returned error: %v", err) + } + + if want := "

text

"; want != md { + t.Errorf("Markdown returned %+v, want %+v", md, want) + } + + const methodName = "Markdown" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Markdown.Markdown(ctx, "# text #", &MarkdownOptions{ + Mode: "gfm", + Context: "google/go-github", + }) + if got != "" { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestMarkdownRequest_Marshal(t *testing.T) { + testJSONMarshal(t, &markdownRequest{}, "{}") + + a := &markdownRequest{ + Text: String("txt"), + Mode: String("mode"), + Context: String("ctx"), + } + + want := `{ + "text": "txt", + "mode": "mode", + "context": "ctx" + }` + + testJSONMarshal(t, a, want) +} diff --git a/github/meta.go b/github/meta.go new file mode 100644 index 0000000000..13a38fe671 --- /dev/null +++ b/github/meta.go @@ -0,0 +1,145 @@ +// Copyright 2014 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "bytes" + "context" + "fmt" + "net/url" +) + +type MetaService service + +// APIMeta represents metadata about the GitHub API. +type APIMeta struct { + // An Array of IP addresses in CIDR format specifying the addresses + // that incoming service hooks will originate from on GitHub.com. + Hooks []string `json:"hooks,omitempty"` + + // An Array of IP addresses in CIDR format specifying the Git servers + // for GitHub.com. + Git []string `json:"git,omitempty"` + + // Whether authentication with username and password is supported. + // (GitHub Enterprise instances using CAS or OAuth for authentication + // will return false. Features like Basic Authentication with a + // username and password, sudo mode, and two-factor authentication are + // not supported on these servers.) + VerifiablePasswordAuthentication *bool `json:"verifiable_password_authentication,omitempty"` + + // An array of IP addresses in CIDR format specifying the addresses + // which serve GitHub Pages websites. + Pages []string `json:"pages,omitempty"` + + // An Array of IP addresses specifying the addresses that source imports + // will originate from on GitHub.com. + Importer []string `json:"importer,omitempty"` + + // An array of IP addresses in CIDR format specifying the IP addresses + // GitHub Actions will originate from. + Actions []string `json:"actions,omitempty"` + + // An array of IP addresses in CIDR format specifying the IP addresses + // Dependabot will originate from. + Dependabot []string `json:"dependabot,omitempty"` + + // A map of algorithms to SSH key fingerprints. + SSHKeyFingerprints map[string]string `json:"ssh_key_fingerprints,omitempty"` + + // An array of SSH keys. + SSHKeys []string `json:"ssh_keys,omitempty"` + + // An array of IP addresses in CIDR format specifying the addresses + // which serve GitHub websites. + Web []string `json:"web,omitempty"` + + // An array of IP addresses in CIDR format specifying the addresses + // which serve GitHub APIs. + API []string `json:"api,omitempty"` +} + +// APIMeta returns information about GitHub.com, the service. Or, if you access +// this endpoint on your organization’s GitHub Enterprise installation, this +// endpoint provides information about that installation. +// +// 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 := 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. +// +// 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 := s.client.NewRequest("GET", u, nil) + if err != nil { + return "", nil, err + } + + buf := new(bytes.Buffer) + 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/ +// +// 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 := 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) +} diff --git a/github/meta_test.go b/github/meta_test.go new file mode 100644 index 0000000000..1fc1dd93af --- /dev/null +++ b/github/meta_test.go @@ -0,0 +1,155 @@ +// Copyright 2014 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestAPIMeta_Marshal(t *testing.T) { + testJSONMarshal(t, &APIMeta{}, "{}") + + a := &APIMeta{ + Hooks: []string{"h"}, + Git: []string{"g"}, + VerifiablePasswordAuthentication: Bool(true), + Pages: []string{"p"}, + Importer: []string{"i"}, + Actions: []string{"a"}, + Dependabot: []string{"d"}, + SSHKeyFingerprints: map[string]string{"a": "f"}, + SSHKeys: []string{"k"}, + API: []string{"a"}, + Web: []string{"w"}, + } + want := `{ + "hooks":["h"], + "git":["g"], + "verifiable_password_authentication":true, + "pages":["p"], + "importer":["i"], + "actions":["a"], + "dependabot":["d"], + "ssh_key_fingerprints":{"a":"f"}, + "ssh_keys":["k"], + "api":["a"], + "web":["w"] + }` + + testJSONMarshal(t, a, want) +} + +func TestMetaService_APIMeta(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/meta", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"web":["w"],"api":["a"],"hooks":["h"], "git":["g"], "pages":["p"], "importer":["i"], "actions":["a"], "dependabot":["d"], "verifiable_password_authentication": true}`) + }) + + ctx := context.Background() + meta, _, err := client.Meta.APIMeta(ctx) + if err != nil { + t.Errorf("APIMeta returned error: %v", err) + } + + want := &APIMeta{ + Hooks: []string{"h"}, + Git: []string{"g"}, + Pages: []string{"p"}, + Importer: []string{"i"}, + Actions: []string{"a"}, + Dependabot: []string{"d"}, + API: []string{"a"}, + Web: []string{"w"}, + + VerifiablePasswordAuthentication: Bool(true), + } + if !cmp.Equal(want, meta) { + t.Errorf("APIMeta returned %+v, want %+v", meta, want) + } + + const methodName = "APIMeta" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Meta.APIMeta(ctx) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestMetaService_Octocat(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + input := "input" + output := "sample text" + + mux.HandleFunc("/octocat", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{"s": input}) + w.Header().Set("Content-Type", "application/octocat-stream") + fmt.Fprint(w, output) + }) + + ctx := context.Background() + got, _, err := client.Meta.Octocat(ctx, input) + if err != nil { + t.Errorf("Octocat returned error: %v", err) + } + + if want := output; got != want { + t.Errorf("Octocat returned %+v, want %+v", got, want) + } + + const methodName = "Octocat" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Meta.Octocat(ctx, input) + if got != "" { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestMetaService_Zen(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + output := "sample text" + + mux.HandleFunc("/zen", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.Header().Set("Content-Type", "text/plain;charset=utf-8") + fmt.Fprint(w, output) + }) + + ctx := context.Background() + got, _, err := client.Meta.Zen(ctx) + if err != nil { + t.Errorf("Zen returned error: %v", err) + } + + if want := output; got != want { + t.Errorf("Zen returned %+v, want %+v", got, want) + } + + const methodName = "Zen" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Meta.Zen(ctx) + if got != "" { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} diff --git a/github/misc.go b/github/misc.go deleted file mode 100644 index f41f97540c..0000000000 --- a/github/misc.go +++ /dev/null @@ -1,296 +0,0 @@ -// Copyright 2014 The go-github AUTHORS. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package github - -import ( - "bytes" - "context" - "fmt" - "net/url" -) - -// MarkdownOptions specifies optional parameters to the Markdown method. -type MarkdownOptions struct { - // Mode identifies the rendering mode. Possible values are: - // markdown - render a document as plain Markdown, just like - // README files are rendered. - // - // gfm - to render a document as user-content, e.g. like user - // comments or issues are rendered. In GFM mode, hard line breaks are - // always taken into account, and issue and user mentions are linked - // accordingly. - // - // Default is "markdown". - Mode string - - // Context identifies the repository context. Only taken into account - // when rendering as "gfm". - Context string -} - -type markdownRequest struct { - Text *string `json:"text,omitempty"` - Mode *string `json:"mode,omitempty"` - Context *string `json:"context,omitempty"` -} - -type MarkdownService service - -// Markdown renders an arbitrary Markdown document. -// -// 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 != "" { - request.Mode = String(opts.Mode) - } - if opts.Context != "" { - request.Context = String(opts.Context) - } - } - - req, err := s.client.NewRequest("POST", "markdown", request) - if err != nil { - return "", nil, err - } - - buf := new(bytes.Buffer) - resp, err := s.client.Do(ctx, req, buf) - if err != nil { - return "", resp, err - } - - return buf.String(), resp, nil -} - -type EmojisService service - -// ListEmojis returns the emojis available to use on GitHub. -// -// 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 := 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"` - Key *string `json:"key,omitempty"` - URL *string `json:"url,omitempty"` - Body *string `json:"body,omitempty"` -} - -func (c *CodeOfConduct) String() string { - return Stringify(c) -} - -type CodesOfConductService service - -// ListCodesOfConduct returns all codes of conduct. -// -// 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 - } - - // TODO: remove custom Accept header when this API fully launches. - req.Header.Set("Accept", mediaTypeCodesOfConductPreview) - - var cs []*CodeOfConduct - 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. -// -// 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 := s.client.NewRequest("GET", u, nil) - if err != nil { - return nil, nil, err - } - - // TODO: remove custom Accept header when this API fully launches. - req.Header.Set("Accept", mediaTypeCodesOfConductPreview) - - coc := new(CodeOfConduct) - 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) -} - -type MetaService service - -// APIMeta represents metadata about the GitHub API. -type APIMeta struct { - // An Array of IP addresses in CIDR format specifying the addresses - // that incoming service hooks will originate from on GitHub.com. - Hooks []string `json:"hooks,omitempty"` - - // An Array of IP addresses in CIDR format specifying the Git servers - // for GitHub.com. - Git []string `json:"git,omitempty"` - - // Whether authentication with username and password is supported. - // (GitHub Enterprise instances using CAS or OAuth for authentication - // will return false. Features like Basic Authentication with a - // username and password, sudo mode, and two-factor authentication are - // not supported on these servers.) - VerifiablePasswordAuthentication *bool `json:"verifiable_password_authentication,omitempty"` - - // An array of IP addresses in CIDR format specifying the addresses - // which serve GitHub Pages websites. - Pages []string `json:"pages,omitempty"` - - // An Array of IP addresses specifying the addresses that source imports - // will originate from on GitHub.com. - Importer []string `json:"importer,omitempty"` - - // An array of IP addresses in CIDR format specifying the IP addresses - // GitHub Actions will originate from. - Actions []string `json:"actions,omitempty"` - - // An array of IP addresses in CIDR format specifying the IP addresses - // Dependabot will originate from. - Dependabot []string `json:"dependabot,omitempty"` - - // A map of algorithms to SSH key fingerprints. - SSHKeyFingerprints map[string]string `json:"ssh_key_fingerprints,omitempty"` - - // An array of SSH keys. - SSHKeys []string `json:"ssh_keys,omitempty"` - - // An array of IP addresses in CIDR format specifying the addresses - // which serve GitHub websites. - Web []string `json:"web,omitempty"` - - // An array of IP addresses in CIDR format specifying the addresses - // which serve GitHub APIs. - API []string `json:"api,omitempty"` -} - -// APIMeta returns information about GitHub.com, the service. Or, if you access -// this endpoint on your organization’s GitHub Enterprise installation, this -// endpoint provides information about that installation. -// -// 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 := 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. -// -// 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 := s.client.NewRequest("GET", u, nil) - if err != nil { - return "", nil, err - } - - buf := new(bytes.Buffer) - 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/ -// -// 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 := 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) -} diff --git a/github/misc_test.go b/github/misc_test.go deleted file mode 100644 index c8c224961b..0000000000 --- a/github/misc_test.go +++ /dev/null @@ -1,356 +0,0 @@ -// Copyright 2014 The go-github AUTHORS. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package github - -import ( - "context" - "encoding/json" - "fmt" - "net/http" - "testing" - - "github.com/google/go-cmp/cmp" -) - -func TestMarkdownService_Markdown(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - input := &markdownRequest{ - Text: String("# text #"), - Mode: String("gfm"), - Context: String("google/go-github"), - } - mux.HandleFunc("/markdown", func(w http.ResponseWriter, r *http.Request) { - v := new(markdownRequest) - assertNilError(t, json.NewDecoder(r.Body).Decode(v)) - - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } - fmt.Fprint(w, `

text

`) - }) - - ctx := context.Background() - md, _, err := client.Markdown.Markdown(ctx, "# text #", &MarkdownOptions{ - Mode: "gfm", - Context: "google/go-github", - }) - if err != nil { - t.Errorf("Markdown returned error: %v", err) - } - - if want := "

text

"; want != md { - t.Errorf("Markdown returned %+v, want %+v", md, want) - } - - const methodName = "Markdown" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Markdown.Markdown(ctx, "# text #", &MarkdownOptions{ - Mode: "gfm", - Context: "google/go-github", - }) - if got != "" { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestEmojisService_ListEmojis(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/emojis", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - fmt.Fprint(w, `{"+1": "+1.png"}`) - }) - - ctx := context.Background() - emoji, _, err := client.Emojis.ListEmojis(ctx) - if err != nil { - t.Errorf("ListEmojis returned error: %v", err) - } - - want := map[string]string{"+1": "+1.png"} - if !cmp.Equal(want, emoji) { - t.Errorf("ListEmojis returned %+v, want %+v", emoji, want) - } - - const methodName = "ListEmojis" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Emojis.ListEmojis(ctx) - if got != nil { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestCodesOfConductService_ListCodesOfConduct(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/codes_of_conduct", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) - fmt.Fprint(w, `[{ - "key": "key", - "name": "name", - "url": "url"} - ]`) - }) - - ctx := context.Background() - cs, _, err := client.CodesOfConduct.ListCodesOfConduct(ctx) - if err != nil { - t.Errorf("ListCodesOfConduct returned error: %v", err) - } - - want := []*CodeOfConduct{ - { - Key: String("key"), - Name: String("name"), - URL: String("url"), - }} - if !cmp.Equal(want, cs) { - t.Errorf("ListCodesOfConduct returned %+v, want %+v", cs, want) - } - - const methodName = "ListCodesOfConduct" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.CodesOfConduct.ListCodesOfConduct(ctx) - if got != nil { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestCodesOfConductService_GetCodeOfConduct(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/codes_of_conduct/k", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) - fmt.Fprint(w, `{ - "key": "key", - "name": "name", - "url": "url", - "body": "body"}`, - ) - }) - - ctx := context.Background() - coc, _, err := client.CodesOfConduct.GetCodeOfConduct(ctx, "k") - if err != nil { - t.Errorf("ListCodesOfConduct returned error: %v", err) - } - - want := &CodeOfConduct{ - Key: String("key"), - Name: String("name"), - URL: String("url"), - Body: String("body"), - } - if !cmp.Equal(want, coc) { - t.Errorf("GetCodeOfConductByKey returned %+v, want %+v", coc, want) - } - - const methodName = "GetCodeOfConduct" - testBadOptions(t, methodName, func() (err error) { - _, _, err = client.CodesOfConduct.GetCodeOfConduct(ctx, "\n") - return err - }) - - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.CodesOfConduct.GetCodeOfConduct(ctx, "k") - if got != nil { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestAPIMeta_Marshal(t *testing.T) { - testJSONMarshal(t, &APIMeta{}, "{}") - - a := &APIMeta{ - Hooks: []string{"h"}, - Git: []string{"g"}, - VerifiablePasswordAuthentication: Bool(true), - Pages: []string{"p"}, - Importer: []string{"i"}, - Actions: []string{"a"}, - Dependabot: []string{"d"}, - SSHKeyFingerprints: map[string]string{"a": "f"}, - SSHKeys: []string{"k"}, - API: []string{"a"}, - Web: []string{"w"}, - } - want := `{ - "hooks":["h"], - "git":["g"], - "verifiable_password_authentication":true, - "pages":["p"], - "importer":["i"], - "actions":["a"], - "dependabot":["d"], - "ssh_key_fingerprints":{"a":"f"}, - "ssh_keys":["k"], - "api":["a"], - "web":["w"] - }` - - testJSONMarshal(t, a, want) -} - -func TestMetaService_APIMeta(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/meta", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - fmt.Fprint(w, `{"web":["w"],"api":["a"],"hooks":["h"], "git":["g"], "pages":["p"], "importer":["i"], "actions":["a"], "dependabot":["d"], "verifiable_password_authentication": true}`) - }) - - ctx := context.Background() - meta, _, err := client.Meta.APIMeta(ctx) - if err != nil { - t.Errorf("APIMeta returned error: %v", err) - } - - want := &APIMeta{ - Hooks: []string{"h"}, - Git: []string{"g"}, - Pages: []string{"p"}, - Importer: []string{"i"}, - Actions: []string{"a"}, - Dependabot: []string{"d"}, - API: []string{"a"}, - Web: []string{"w"}, - - VerifiablePasswordAuthentication: Bool(true), - } - if !cmp.Equal(want, meta) { - t.Errorf("APIMeta returned %+v, want %+v", meta, want) - } - - const methodName = "APIMeta" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Meta.APIMeta(ctx) - if got != nil { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestMetaService_Octocat(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - input := "input" - output := "sample text" - - mux.HandleFunc("/octocat", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - testFormValues(t, r, values{"s": input}) - w.Header().Set("Content-Type", "application/octocat-stream") - fmt.Fprint(w, output) - }) - - ctx := context.Background() - got, _, err := client.Meta.Octocat(ctx, input) - if err != nil { - t.Errorf("Octocat returned error: %v", err) - } - - if want := output; got != want { - t.Errorf("Octocat returned %+v, want %+v", got, want) - } - - const methodName = "Octocat" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Meta.Octocat(ctx, input) - if got != "" { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestMetaService_Zen(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - output := "sample text" - - mux.HandleFunc("/zen", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - w.Header().Set("Content-Type", "text/plain;charset=utf-8") - fmt.Fprint(w, output) - }) - - ctx := context.Background() - got, _, err := client.Meta.Zen(ctx) - if err != nil { - t.Errorf("Zen returned error: %v", err) - } - - if want := output; got != want { - t.Errorf("Zen returned %+v, want %+v", got, want) - } - - const methodName = "Zen" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Meta.Zen(ctx) - if got != "" { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestMarkdownRequest_Marshal(t *testing.T) { - testJSONMarshal(t, &markdownRequest{}, "{}") - - a := &markdownRequest{ - Text: String("txt"), - Mode: String("mode"), - Context: String("ctx"), - } - - want := `{ - "text": "txt", - "mode": "mode", - "context": "ctx" - }` - - testJSONMarshal(t, a, want) -} - -func TestCodeOfConduct_Marshal(t *testing.T) { - testJSONMarshal(t, &CodeOfConduct{}, "{}") - - a := &CodeOfConduct{ - Name: String("name"), - Key: String("key"), - URL: String("url"), - Body: String("body"), - } - - want := `{ - "name": "name", - "key": "key", - "url": "url", - "body": "body" - }` - - testJSONMarshal(t, a, want) -} From 9306ab49996d65dc96f9a785659e67efe717f852 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Wed, 20 Sep 2023 13:50:04 -0500 Subject: [PATCH 04/13] Revert "move services to their own files" This reverts commit fb26df48897013bd490e7dfb1a10b058e779c2ab. --- github/codesofconduct.go | 75 ------- github/codesofconduct_test.go | 116 ----------- github/emojis.go | 31 --- github/emojis_test.go | 40 ---- github/markdown.go | 61 ------ github/markdown_test.go | 75 ------- github/meta.go | 145 -------------- github/meta_test.go | 155 --------------- github/misc.go | 296 ++++++++++++++++++++++++++++ github/misc_test.go | 356 ++++++++++++++++++++++++++++++++++ 10 files changed, 652 insertions(+), 698 deletions(-) delete mode 100644 github/codesofconduct.go delete mode 100644 github/codesofconduct_test.go delete mode 100644 github/emojis.go delete mode 100644 github/emojis_test.go delete mode 100644 github/markdown.go delete mode 100644 github/markdown_test.go delete mode 100644 github/meta.go delete mode 100644 github/meta_test.go create mode 100644 github/misc.go create mode 100644 github/misc_test.go diff --git a/github/codesofconduct.go b/github/codesofconduct.go deleted file mode 100644 index a12f1bee1a..0000000000 --- a/github/codesofconduct.go +++ /dev/null @@ -1,75 +0,0 @@ -package github - -import ( - "context" - "fmt" -) - -type CodesOfConductService service - -// CodeOfConduct represents a code of conduct. -type CodeOfConduct struct { - Name *string `json:"name,omitempty"` - Key *string `json:"key,omitempty"` - URL *string `json:"url,omitempty"` - Body *string `json:"body,omitempty"` -} - -func (c *CodeOfConduct) String() string { - return Stringify(c) -} - -// ListCodesOfConduct returns all codes of conduct. -// -// 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 - } - - // TODO: remove custom Accept header when this API fully launches. - req.Header.Set("Accept", mediaTypeCodesOfConductPreview) - - var cs []*CodeOfConduct - 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. -// -// 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 := s.client.NewRequest("GET", u, nil) - if err != nil { - return nil, nil, err - } - - // TODO: remove custom Accept header when this API fully launches. - req.Header.Set("Accept", mediaTypeCodesOfConductPreview) - - coc := new(CodeOfConduct) - 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) -} diff --git a/github/codesofconduct_test.go b/github/codesofconduct_test.go deleted file mode 100644 index b00ef3b204..0000000000 --- a/github/codesofconduct_test.go +++ /dev/null @@ -1,116 +0,0 @@ -package github - -import ( - "context" - "fmt" - "net/http" - "testing" - - "github.com/google/go-cmp/cmp" -) - -func TestCodesOfConductService_ListCodesOfConduct(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/codes_of_conduct", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) - fmt.Fprint(w, `[{ - "key": "key", - "name": "name", - "url": "url"} - ]`) - }) - - ctx := context.Background() - cs, _, err := client.CodesOfConduct.ListCodesOfConduct(ctx) - if err != nil { - t.Errorf("ListCodesOfConduct returned error: %v", err) - } - - want := []*CodeOfConduct{ - { - Key: String("key"), - Name: String("name"), - URL: String("url"), - }} - if !cmp.Equal(want, cs) { - t.Errorf("ListCodesOfConduct returned %+v, want %+v", cs, want) - } - - const methodName = "ListCodesOfConduct" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.CodesOfConduct.ListCodesOfConduct(ctx) - if got != nil { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestCodesOfConductService_GetCodeOfConduct(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/codes_of_conduct/k", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) - fmt.Fprint(w, `{ - "key": "key", - "name": "name", - "url": "url", - "body": "body"}`, - ) - }) - - ctx := context.Background() - coc, _, err := client.CodesOfConduct.GetCodeOfConduct(ctx, "k") - if err != nil { - t.Errorf("ListCodesOfConduct returned error: %v", err) - } - - want := &CodeOfConduct{ - Key: String("key"), - Name: String("name"), - URL: String("url"), - Body: String("body"), - } - if !cmp.Equal(want, coc) { - t.Errorf("GetCodeOfConductByKey returned %+v, want %+v", coc, want) - } - - const methodName = "GetCodeOfConduct" - testBadOptions(t, methodName, func() (err error) { - _, _, err = client.CodesOfConduct.GetCodeOfConduct(ctx, "\n") - return err - }) - - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.CodesOfConduct.GetCodeOfConduct(ctx, "k") - if got != nil { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestCodeOfConduct_Marshal(t *testing.T) { - testJSONMarshal(t, &CodeOfConduct{}, "{}") - - a := &CodeOfConduct{ - Name: String("name"), - Key: String("key"), - URL: String("url"), - Body: String("body"), - } - - want := `{ - "name": "name", - "key": "key", - "url": "url", - "body": "body" - }` - - testJSONMarshal(t, a, want) -} diff --git a/github/emojis.go b/github/emojis.go deleted file mode 100644 index 50a2e66092..0000000000 --- a/github/emojis.go +++ /dev/null @@ -1,31 +0,0 @@ -package github - -import ( - "context" -) - -type EmojisService service - -// ListEmojis returns the emojis available to use on GitHub. -// -// 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 := s.client.Do(ctx, req, &emoji) - if err != nil { - return nil, resp, err - } - - return emoji, resp, nil -} - -// ListEmojis -// Deprecated: Use EmojisService.ListEmojis instead -func (c *Client) ListEmojis(ctx context.Context) (map[string]string, *Response, error) { - return c.Emojis.ListEmojis(ctx) -} diff --git a/github/emojis_test.go b/github/emojis_test.go deleted file mode 100644 index d3b3a4c452..0000000000 --- a/github/emojis_test.go +++ /dev/null @@ -1,40 +0,0 @@ -package github - -import ( - "context" - "fmt" - "net/http" - "testing" - - "github.com/google/go-cmp/cmp" -) - -func TestEmojisService_ListEmojis(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/emojis", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - fmt.Fprint(w, `{"+1": "+1.png"}`) - }) - - ctx := context.Background() - emoji, _, err := client.Emojis.ListEmojis(ctx) - if err != nil { - t.Errorf("ListEmojis returned error: %v", err) - } - - want := map[string]string{"+1": "+1.png"} - if !cmp.Equal(want, emoji) { - t.Errorf("ListEmojis returned %+v, want %+v", emoji, want) - } - - const methodName = "ListEmojis" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Emojis.ListEmojis(ctx) - if got != nil { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} diff --git a/github/markdown.go b/github/markdown.go deleted file mode 100644 index a855a183e3..0000000000 --- a/github/markdown.go +++ /dev/null @@ -1,61 +0,0 @@ -package github - -import ( - "bytes" - "context" -) - -type MarkdownService service - -// MarkdownOptions specifies optional parameters to the Markdown method. -type MarkdownOptions struct { - // Mode identifies the rendering mode. Possible values are: - // markdown - render a document as plain Markdown, just like - // README files are rendered. - // - // gfm - to render a document as user-content, e.g. like user - // comments or issues are rendered. In GFM mode, hard line breaks are - // always taken into account, and issue and user mentions are linked - // accordingly. - // - // Default is "markdown". - Mode string - - // Context identifies the repository context. Only taken into account - // when rendering as "gfm". - Context string -} - -type markdownRequest struct { - Text *string `json:"text,omitempty"` - Mode *string `json:"mode,omitempty"` - Context *string `json:"context,omitempty"` -} - -// Markdown renders an arbitrary Markdown document. -// -// 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 != "" { - request.Mode = String(opts.Mode) - } - if opts.Context != "" { - request.Context = String(opts.Context) - } - } - - req, err := s.client.NewRequest("POST", "markdown", request) - if err != nil { - return "", nil, err - } - - buf := new(bytes.Buffer) - resp, err := s.client.Do(ctx, req, buf) - if err != nil { - return "", resp, err - } - - return buf.String(), resp, nil -} diff --git a/github/markdown_test.go b/github/markdown_test.go deleted file mode 100644 index aae5e4e3f5..0000000000 --- a/github/markdown_test.go +++ /dev/null @@ -1,75 +0,0 @@ -package github - -import ( - "context" - "encoding/json" - "fmt" - "net/http" - "testing" - - "github.com/google/go-cmp/cmp" -) - -func TestMarkdownService_Markdown(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - input := &markdownRequest{ - Text: String("# text #"), - Mode: String("gfm"), - Context: String("google/go-github"), - } - mux.HandleFunc("/markdown", func(w http.ResponseWriter, r *http.Request) { - v := new(markdownRequest) - assertNilError(t, json.NewDecoder(r.Body).Decode(v)) - - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } - fmt.Fprint(w, `

text

`) - }) - - ctx := context.Background() - md, _, err := client.Markdown.Markdown(ctx, "# text #", &MarkdownOptions{ - Mode: "gfm", - Context: "google/go-github", - }) - if err != nil { - t.Errorf("Markdown returned error: %v", err) - } - - if want := "

text

"; want != md { - t.Errorf("Markdown returned %+v, want %+v", md, want) - } - - const methodName = "Markdown" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Markdown.Markdown(ctx, "# text #", &MarkdownOptions{ - Mode: "gfm", - Context: "google/go-github", - }) - if got != "" { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestMarkdownRequest_Marshal(t *testing.T) { - testJSONMarshal(t, &markdownRequest{}, "{}") - - a := &markdownRequest{ - Text: String("txt"), - Mode: String("mode"), - Context: String("ctx"), - } - - want := `{ - "text": "txt", - "mode": "mode", - "context": "ctx" - }` - - testJSONMarshal(t, a, want) -} diff --git a/github/meta.go b/github/meta.go deleted file mode 100644 index 13a38fe671..0000000000 --- a/github/meta.go +++ /dev/null @@ -1,145 +0,0 @@ -// Copyright 2014 The go-github AUTHORS. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package github - -import ( - "bytes" - "context" - "fmt" - "net/url" -) - -type MetaService service - -// APIMeta represents metadata about the GitHub API. -type APIMeta struct { - // An Array of IP addresses in CIDR format specifying the addresses - // that incoming service hooks will originate from on GitHub.com. - Hooks []string `json:"hooks,omitempty"` - - // An Array of IP addresses in CIDR format specifying the Git servers - // for GitHub.com. - Git []string `json:"git,omitempty"` - - // Whether authentication with username and password is supported. - // (GitHub Enterprise instances using CAS or OAuth for authentication - // will return false. Features like Basic Authentication with a - // username and password, sudo mode, and two-factor authentication are - // not supported on these servers.) - VerifiablePasswordAuthentication *bool `json:"verifiable_password_authentication,omitempty"` - - // An array of IP addresses in CIDR format specifying the addresses - // which serve GitHub Pages websites. - Pages []string `json:"pages,omitempty"` - - // An Array of IP addresses specifying the addresses that source imports - // will originate from on GitHub.com. - Importer []string `json:"importer,omitempty"` - - // An array of IP addresses in CIDR format specifying the IP addresses - // GitHub Actions will originate from. - Actions []string `json:"actions,omitempty"` - - // An array of IP addresses in CIDR format specifying the IP addresses - // Dependabot will originate from. - Dependabot []string `json:"dependabot,omitempty"` - - // A map of algorithms to SSH key fingerprints. - SSHKeyFingerprints map[string]string `json:"ssh_key_fingerprints,omitempty"` - - // An array of SSH keys. - SSHKeys []string `json:"ssh_keys,omitempty"` - - // An array of IP addresses in CIDR format specifying the addresses - // which serve GitHub websites. - Web []string `json:"web,omitempty"` - - // An array of IP addresses in CIDR format specifying the addresses - // which serve GitHub APIs. - API []string `json:"api,omitempty"` -} - -// APIMeta returns information about GitHub.com, the service. Or, if you access -// this endpoint on your organization’s GitHub Enterprise installation, this -// endpoint provides information about that installation. -// -// 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 := 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. -// -// 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 := s.client.NewRequest("GET", u, nil) - if err != nil { - return "", nil, err - } - - buf := new(bytes.Buffer) - 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/ -// -// 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 := 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) -} diff --git a/github/meta_test.go b/github/meta_test.go deleted file mode 100644 index 1fc1dd93af..0000000000 --- a/github/meta_test.go +++ /dev/null @@ -1,155 +0,0 @@ -// Copyright 2014 The go-github AUTHORS. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package github - -import ( - "context" - "fmt" - "net/http" - "testing" - - "github.com/google/go-cmp/cmp" -) - -func TestAPIMeta_Marshal(t *testing.T) { - testJSONMarshal(t, &APIMeta{}, "{}") - - a := &APIMeta{ - Hooks: []string{"h"}, - Git: []string{"g"}, - VerifiablePasswordAuthentication: Bool(true), - Pages: []string{"p"}, - Importer: []string{"i"}, - Actions: []string{"a"}, - Dependabot: []string{"d"}, - SSHKeyFingerprints: map[string]string{"a": "f"}, - SSHKeys: []string{"k"}, - API: []string{"a"}, - Web: []string{"w"}, - } - want := `{ - "hooks":["h"], - "git":["g"], - "verifiable_password_authentication":true, - "pages":["p"], - "importer":["i"], - "actions":["a"], - "dependabot":["d"], - "ssh_key_fingerprints":{"a":"f"}, - "ssh_keys":["k"], - "api":["a"], - "web":["w"] - }` - - testJSONMarshal(t, a, want) -} - -func TestMetaService_APIMeta(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/meta", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - fmt.Fprint(w, `{"web":["w"],"api":["a"],"hooks":["h"], "git":["g"], "pages":["p"], "importer":["i"], "actions":["a"], "dependabot":["d"], "verifiable_password_authentication": true}`) - }) - - ctx := context.Background() - meta, _, err := client.Meta.APIMeta(ctx) - if err != nil { - t.Errorf("APIMeta returned error: %v", err) - } - - want := &APIMeta{ - Hooks: []string{"h"}, - Git: []string{"g"}, - Pages: []string{"p"}, - Importer: []string{"i"}, - Actions: []string{"a"}, - Dependabot: []string{"d"}, - API: []string{"a"}, - Web: []string{"w"}, - - VerifiablePasswordAuthentication: Bool(true), - } - if !cmp.Equal(want, meta) { - t.Errorf("APIMeta returned %+v, want %+v", meta, want) - } - - const methodName = "APIMeta" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Meta.APIMeta(ctx) - if got != nil { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestMetaService_Octocat(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - input := "input" - output := "sample text" - - mux.HandleFunc("/octocat", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - testFormValues(t, r, values{"s": input}) - w.Header().Set("Content-Type", "application/octocat-stream") - fmt.Fprint(w, output) - }) - - ctx := context.Background() - got, _, err := client.Meta.Octocat(ctx, input) - if err != nil { - t.Errorf("Octocat returned error: %v", err) - } - - if want := output; got != want { - t.Errorf("Octocat returned %+v, want %+v", got, want) - } - - const methodName = "Octocat" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Meta.Octocat(ctx, input) - if got != "" { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestMetaService_Zen(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - output := "sample text" - - mux.HandleFunc("/zen", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - w.Header().Set("Content-Type", "text/plain;charset=utf-8") - fmt.Fprint(w, output) - }) - - ctx := context.Background() - got, _, err := client.Meta.Zen(ctx) - if err != nil { - t.Errorf("Zen returned error: %v", err) - } - - if want := output; got != want { - t.Errorf("Zen returned %+v, want %+v", got, want) - } - - const methodName = "Zen" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Meta.Zen(ctx) - if got != "" { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} diff --git a/github/misc.go b/github/misc.go new file mode 100644 index 0000000000..f41f97540c --- /dev/null +++ b/github/misc.go @@ -0,0 +1,296 @@ +// Copyright 2014 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "bytes" + "context" + "fmt" + "net/url" +) + +// MarkdownOptions specifies optional parameters to the Markdown method. +type MarkdownOptions struct { + // Mode identifies the rendering mode. Possible values are: + // markdown - render a document as plain Markdown, just like + // README files are rendered. + // + // gfm - to render a document as user-content, e.g. like user + // comments or issues are rendered. In GFM mode, hard line breaks are + // always taken into account, and issue and user mentions are linked + // accordingly. + // + // Default is "markdown". + Mode string + + // Context identifies the repository context. Only taken into account + // when rendering as "gfm". + Context string +} + +type markdownRequest struct { + Text *string `json:"text,omitempty"` + Mode *string `json:"mode,omitempty"` + Context *string `json:"context,omitempty"` +} + +type MarkdownService service + +// Markdown renders an arbitrary Markdown document. +// +// 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 != "" { + request.Mode = String(opts.Mode) + } + if opts.Context != "" { + request.Context = String(opts.Context) + } + } + + req, err := s.client.NewRequest("POST", "markdown", request) + if err != nil { + return "", nil, err + } + + buf := new(bytes.Buffer) + resp, err := s.client.Do(ctx, req, buf) + if err != nil { + return "", resp, err + } + + return buf.String(), resp, nil +} + +type EmojisService service + +// ListEmojis returns the emojis available to use on GitHub. +// +// 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 := 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"` + Key *string `json:"key,omitempty"` + URL *string `json:"url,omitempty"` + Body *string `json:"body,omitempty"` +} + +func (c *CodeOfConduct) String() string { + return Stringify(c) +} + +type CodesOfConductService service + +// ListCodesOfConduct returns all codes of conduct. +// +// 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 + } + + // TODO: remove custom Accept header when this API fully launches. + req.Header.Set("Accept", mediaTypeCodesOfConductPreview) + + var cs []*CodeOfConduct + 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. +// +// 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 := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + // TODO: remove custom Accept header when this API fully launches. + req.Header.Set("Accept", mediaTypeCodesOfConductPreview) + + coc := new(CodeOfConduct) + 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) +} + +type MetaService service + +// APIMeta represents metadata about the GitHub API. +type APIMeta struct { + // An Array of IP addresses in CIDR format specifying the addresses + // that incoming service hooks will originate from on GitHub.com. + Hooks []string `json:"hooks,omitempty"` + + // An Array of IP addresses in CIDR format specifying the Git servers + // for GitHub.com. + Git []string `json:"git,omitempty"` + + // Whether authentication with username and password is supported. + // (GitHub Enterprise instances using CAS or OAuth for authentication + // will return false. Features like Basic Authentication with a + // username and password, sudo mode, and two-factor authentication are + // not supported on these servers.) + VerifiablePasswordAuthentication *bool `json:"verifiable_password_authentication,omitempty"` + + // An array of IP addresses in CIDR format specifying the addresses + // which serve GitHub Pages websites. + Pages []string `json:"pages,omitempty"` + + // An Array of IP addresses specifying the addresses that source imports + // will originate from on GitHub.com. + Importer []string `json:"importer,omitempty"` + + // An array of IP addresses in CIDR format specifying the IP addresses + // GitHub Actions will originate from. + Actions []string `json:"actions,omitempty"` + + // An array of IP addresses in CIDR format specifying the IP addresses + // Dependabot will originate from. + Dependabot []string `json:"dependabot,omitempty"` + + // A map of algorithms to SSH key fingerprints. + SSHKeyFingerprints map[string]string `json:"ssh_key_fingerprints,omitempty"` + + // An array of SSH keys. + SSHKeys []string `json:"ssh_keys,omitempty"` + + // An array of IP addresses in CIDR format specifying the addresses + // which serve GitHub websites. + Web []string `json:"web,omitempty"` + + // An array of IP addresses in CIDR format specifying the addresses + // which serve GitHub APIs. + API []string `json:"api,omitempty"` +} + +// APIMeta returns information about GitHub.com, the service. Or, if you access +// this endpoint on your organization’s GitHub Enterprise installation, this +// endpoint provides information about that installation. +// +// 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 := 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. +// +// 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 := s.client.NewRequest("GET", u, nil) + if err != nil { + return "", nil, err + } + + buf := new(bytes.Buffer) + 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/ +// +// 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 := 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) +} diff --git a/github/misc_test.go b/github/misc_test.go new file mode 100644 index 0000000000..c8c224961b --- /dev/null +++ b/github/misc_test.go @@ -0,0 +1,356 @@ +// Copyright 2014 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestMarkdownService_Markdown(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + input := &markdownRequest{ + Text: String("# text #"), + Mode: String("gfm"), + Context: String("google/go-github"), + } + mux.HandleFunc("/markdown", func(w http.ResponseWriter, r *http.Request) { + v := new(markdownRequest) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) + + testMethod(t, r, "POST") + if !cmp.Equal(v, input) { + t.Errorf("Request body = %+v, want %+v", v, input) + } + fmt.Fprint(w, `

text

`) + }) + + ctx := context.Background() + md, _, err := client.Markdown.Markdown(ctx, "# text #", &MarkdownOptions{ + Mode: "gfm", + Context: "google/go-github", + }) + if err != nil { + t.Errorf("Markdown returned error: %v", err) + } + + if want := "

text

"; want != md { + t.Errorf("Markdown returned %+v, want %+v", md, want) + } + + const methodName = "Markdown" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Markdown.Markdown(ctx, "# text #", &MarkdownOptions{ + Mode: "gfm", + Context: "google/go-github", + }) + if got != "" { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestEmojisService_ListEmojis(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/emojis", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"+1": "+1.png"}`) + }) + + ctx := context.Background() + emoji, _, err := client.Emojis.ListEmojis(ctx) + if err != nil { + t.Errorf("ListEmojis returned error: %v", err) + } + + want := map[string]string{"+1": "+1.png"} + if !cmp.Equal(want, emoji) { + t.Errorf("ListEmojis returned %+v, want %+v", emoji, want) + } + + const methodName = "ListEmojis" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Emojis.ListEmojis(ctx) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestCodesOfConductService_ListCodesOfConduct(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/codes_of_conduct", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) + fmt.Fprint(w, `[{ + "key": "key", + "name": "name", + "url": "url"} + ]`) + }) + + ctx := context.Background() + cs, _, err := client.CodesOfConduct.ListCodesOfConduct(ctx) + if err != nil { + t.Errorf("ListCodesOfConduct returned error: %v", err) + } + + want := []*CodeOfConduct{ + { + Key: String("key"), + Name: String("name"), + URL: String("url"), + }} + if !cmp.Equal(want, cs) { + t.Errorf("ListCodesOfConduct returned %+v, want %+v", cs, want) + } + + const methodName = "ListCodesOfConduct" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.CodesOfConduct.ListCodesOfConduct(ctx) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestCodesOfConductService_GetCodeOfConduct(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/codes_of_conduct/k", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) + fmt.Fprint(w, `{ + "key": "key", + "name": "name", + "url": "url", + "body": "body"}`, + ) + }) + + ctx := context.Background() + coc, _, err := client.CodesOfConduct.GetCodeOfConduct(ctx, "k") + if err != nil { + t.Errorf("ListCodesOfConduct returned error: %v", err) + } + + want := &CodeOfConduct{ + Key: String("key"), + Name: String("name"), + URL: String("url"), + Body: String("body"), + } + if !cmp.Equal(want, coc) { + t.Errorf("GetCodeOfConductByKey returned %+v, want %+v", coc, want) + } + + const methodName = "GetCodeOfConduct" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.CodesOfConduct.GetCodeOfConduct(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.CodesOfConduct.GetCodeOfConduct(ctx, "k") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestAPIMeta_Marshal(t *testing.T) { + testJSONMarshal(t, &APIMeta{}, "{}") + + a := &APIMeta{ + Hooks: []string{"h"}, + Git: []string{"g"}, + VerifiablePasswordAuthentication: Bool(true), + Pages: []string{"p"}, + Importer: []string{"i"}, + Actions: []string{"a"}, + Dependabot: []string{"d"}, + SSHKeyFingerprints: map[string]string{"a": "f"}, + SSHKeys: []string{"k"}, + API: []string{"a"}, + Web: []string{"w"}, + } + want := `{ + "hooks":["h"], + "git":["g"], + "verifiable_password_authentication":true, + "pages":["p"], + "importer":["i"], + "actions":["a"], + "dependabot":["d"], + "ssh_key_fingerprints":{"a":"f"}, + "ssh_keys":["k"], + "api":["a"], + "web":["w"] + }` + + testJSONMarshal(t, a, want) +} + +func TestMetaService_APIMeta(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/meta", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"web":["w"],"api":["a"],"hooks":["h"], "git":["g"], "pages":["p"], "importer":["i"], "actions":["a"], "dependabot":["d"], "verifiable_password_authentication": true}`) + }) + + ctx := context.Background() + meta, _, err := client.Meta.APIMeta(ctx) + if err != nil { + t.Errorf("APIMeta returned error: %v", err) + } + + want := &APIMeta{ + Hooks: []string{"h"}, + Git: []string{"g"}, + Pages: []string{"p"}, + Importer: []string{"i"}, + Actions: []string{"a"}, + Dependabot: []string{"d"}, + API: []string{"a"}, + Web: []string{"w"}, + + VerifiablePasswordAuthentication: Bool(true), + } + if !cmp.Equal(want, meta) { + t.Errorf("APIMeta returned %+v, want %+v", meta, want) + } + + const methodName = "APIMeta" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Meta.APIMeta(ctx) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestMetaService_Octocat(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + input := "input" + output := "sample text" + + mux.HandleFunc("/octocat", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{"s": input}) + w.Header().Set("Content-Type", "application/octocat-stream") + fmt.Fprint(w, output) + }) + + ctx := context.Background() + got, _, err := client.Meta.Octocat(ctx, input) + if err != nil { + t.Errorf("Octocat returned error: %v", err) + } + + if want := output; got != want { + t.Errorf("Octocat returned %+v, want %+v", got, want) + } + + const methodName = "Octocat" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Meta.Octocat(ctx, input) + if got != "" { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestMetaService_Zen(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + output := "sample text" + + mux.HandleFunc("/zen", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.Header().Set("Content-Type", "text/plain;charset=utf-8") + fmt.Fprint(w, output) + }) + + ctx := context.Background() + got, _, err := client.Meta.Zen(ctx) + if err != nil { + t.Errorf("Zen returned error: %v", err) + } + + if want := output; got != want { + t.Errorf("Zen returned %+v, want %+v", got, want) + } + + const methodName = "Zen" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Meta.Zen(ctx) + if got != "" { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestMarkdownRequest_Marshal(t *testing.T) { + testJSONMarshal(t, &markdownRequest{}, "{}") + + a := &markdownRequest{ + Text: String("txt"), + Mode: String("mode"), + Context: String("ctx"), + } + + want := `{ + "text": "txt", + "mode": "mode", + "context": "ctx" + }` + + testJSONMarshal(t, a, want) +} + +func TestCodeOfConduct_Marshal(t *testing.T) { + testJSONMarshal(t, &CodeOfConduct{}, "{}") + + a := &CodeOfConduct{ + Name: String("name"), + Key: String("key"), + URL: String("url"), + Body: String("body"), + } + + want := `{ + "name": "name", + "key": "key", + "url": "url", + "body": "body" + }` + + testJSONMarshal(t, a, want) +} From 3aa3e335df3b4742b6af423cc96aa8a0b70b0adb Mon Sep 17 00:00:00 2001 From: Will Roden Date: Wed, 20 Sep 2023 13:57:52 -0500 Subject: [PATCH 05/13] godoc --- github/misc.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/github/misc.go b/github/misc.go index f41f97540c..91b24780d4 100644 --- a/github/misc.go +++ b/github/misc.go @@ -37,6 +37,7 @@ 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. @@ -67,6 +68,7 @@ func (s *MarkdownService) Markdown(ctx context.Context, text string, opts *Markd 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. @@ -106,6 +108,7 @@ 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. @@ -163,6 +166,7 @@ func (c *Client) GetCodeOfConduct(ctx context.Context, key string) (*CodeOfCondu 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. From 6f4b8a108b103abd8b91e593d322d024737573e5 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Wed, 20 Sep 2023 14:14:57 -0500 Subject: [PATCH 06/13] call deprecated methods from test to increase coverage --- github/misc_test.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/github/misc_test.go b/github/misc_test.go index c8c224961b..71fd69915c 100644 --- a/github/misc_test.go +++ b/github/misc_test.go @@ -71,7 +71,7 @@ func TestEmojisService_ListEmojis(t *testing.T) { }) ctx := context.Background() - emoji, _, err := client.Emojis.ListEmojis(ctx) + emoji, _, err := client.ListEmojis(ctx) if err != nil { t.Errorf("ListEmojis returned error: %v", err) } @@ -83,7 +83,7 @@ func TestEmojisService_ListEmojis(t *testing.T) { const methodName = "ListEmojis" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Emojis.ListEmojis(ctx) + got, resp, err := client.ListEmojis(ctx) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -106,7 +106,7 @@ func TestCodesOfConductService_ListCodesOfConduct(t *testing.T) { }) ctx := context.Background() - cs, _, err := client.CodesOfConduct.ListCodesOfConduct(ctx) + cs, _, err := client.ListCodesOfConduct(ctx) if err != nil { t.Errorf("ListCodesOfConduct returned error: %v", err) } @@ -123,7 +123,7 @@ func TestCodesOfConductService_ListCodesOfConduct(t *testing.T) { const methodName = "ListCodesOfConduct" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.CodesOfConduct.ListCodesOfConduct(ctx) + got, resp, err := client.ListCodesOfConduct(ctx) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -147,7 +147,7 @@ func TestCodesOfConductService_GetCodeOfConduct(t *testing.T) { }) ctx := context.Background() - coc, _, err := client.CodesOfConduct.GetCodeOfConduct(ctx, "k") + coc, _, err := client.GetCodeOfConduct(ctx, "k") if err != nil { t.Errorf("ListCodesOfConduct returned error: %v", err) } @@ -164,12 +164,12 @@ func TestCodesOfConductService_GetCodeOfConduct(t *testing.T) { const methodName = "GetCodeOfConduct" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.CodesOfConduct.GetCodeOfConduct(ctx, "\n") + _, _, err = client.GetCodeOfConduct(ctx, "\n") return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.CodesOfConduct.GetCodeOfConduct(ctx, "k") + got, resp, err := client.GetCodeOfConduct(ctx, "k") if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -220,7 +220,7 @@ func TestMetaService_APIMeta(t *testing.T) { }) ctx := context.Background() - meta, _, err := client.Meta.APIMeta(ctx) + meta, _, err := client.APIMeta(ctx) if err != nil { t.Errorf("APIMeta returned error: %v", err) } @@ -243,7 +243,7 @@ func TestMetaService_APIMeta(t *testing.T) { const methodName = "APIMeta" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Meta.APIMeta(ctx) + got, resp, err := client.APIMeta(ctx) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -266,7 +266,7 @@ func TestMetaService_Octocat(t *testing.T) { }) ctx := context.Background() - got, _, err := client.Meta.Octocat(ctx, input) + got, _, err := client.Octocat(ctx, input) if err != nil { t.Errorf("Octocat returned error: %v", err) } @@ -277,7 +277,7 @@ func TestMetaService_Octocat(t *testing.T) { const methodName = "Octocat" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Meta.Octocat(ctx, input) + got, resp, err := client.Octocat(ctx, input) if got != "" { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -298,7 +298,7 @@ func TestMetaService_Zen(t *testing.T) { }) ctx := context.Background() - got, _, err := client.Meta.Zen(ctx) + got, _, err := client.Zen(ctx) if err != nil { t.Errorf("Zen returned error: %v", err) } @@ -309,7 +309,7 @@ func TestMetaService_Zen(t *testing.T) { const methodName = "Zen" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Meta.Zen(ctx) + got, resp, err := client.Zen(ctx) if got != "" { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } From 2c0768b7ecd5efbb55c37eb66dafb16cb3d8970c Mon Sep 17 00:00:00 2001 From: Will Roden Date: Thu, 21 Sep 2023 07:48:48 -0500 Subject: [PATCH 07/13] move services to separate files --- github/codesofconduct.go | 81 ++++++++ github/codesofconduct_test.go | 121 ++++++++++++ github/emojis.go | 37 ++++ github/emojis_test.go | 45 +++++ github/markdown.go | 67 +++++++ github/markdown_test.go | 80 ++++++++ github/meta.go | 146 ++++++++++++++ github/meta_test.go | 155 +++++++++++++++ github/misc.go | 300 ---------------------------- github/misc_test.go | 356 ---------------------------------- 10 files changed, 732 insertions(+), 656 deletions(-) create mode 100644 github/codesofconduct.go create mode 100644 github/codesofconduct_test.go create mode 100644 github/emojis.go create mode 100644 github/emojis_test.go create mode 100644 github/markdown.go create mode 100644 github/markdown_test.go create mode 100644 github/meta.go create mode 100644 github/meta_test.go delete mode 100644 github/misc.go delete mode 100644 github/misc_test.go diff --git a/github/codesofconduct.go b/github/codesofconduct.go new file mode 100644 index 0000000000..17a0a876ca --- /dev/null +++ b/github/codesofconduct.go @@ -0,0 +1,81 @@ +// Copyright 2023 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "context" + "fmt" +) + +// CodesOfConductService provides access to code-of-conduct-related functions in the GitHub API. +type CodesOfConductService service + +// CodeOfConduct represents a code of conduct. +type CodeOfConduct struct { + Name *string `json:"name,omitempty"` + Key *string `json:"key,omitempty"` + URL *string `json:"url,omitempty"` + Body *string `json:"body,omitempty"` +} + +func (c *CodeOfConduct) String() string { + return Stringify(c) +} + +// ListCodesOfConduct returns all codes of conduct. +// +// 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 + } + + // TODO: remove custom Accept header when this API fully launches. + req.Header.Set("Accept", mediaTypeCodesOfConductPreview) + + var cs []*CodeOfConduct + 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. +// +// 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 := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + // TODO: remove custom Accept header when this API fully launches. + req.Header.Set("Accept", mediaTypeCodesOfConductPreview) + + coc := new(CodeOfConduct) + 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) +} diff --git a/github/codesofconduct_test.go b/github/codesofconduct_test.go new file mode 100644 index 0000000000..01ccefc264 --- /dev/null +++ b/github/codesofconduct_test.go @@ -0,0 +1,121 @@ +// Copyright 2023 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestCodesOfConductService_ListCodesOfConduct(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/codes_of_conduct", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) + fmt.Fprint(w, `[{ + "key": "key", + "name": "name", + "url": "url"} + ]`) + }) + + ctx := context.Background() + cs, _, err := client.ListCodesOfConduct(ctx) + if err != nil { + t.Errorf("ListCodesOfConduct returned error: %v", err) + } + + want := []*CodeOfConduct{ + { + Key: String("key"), + Name: String("name"), + URL: String("url"), + }} + if !cmp.Equal(want, cs) { + t.Errorf("ListCodesOfConduct returned %+v, want %+v", cs, want) + } + + const methodName = "ListCodesOfConduct" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.ListCodesOfConduct(ctx) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestCodesOfConductService_GetCodeOfConduct(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/codes_of_conduct/k", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) + fmt.Fprint(w, `{ + "key": "key", + "name": "name", + "url": "url", + "body": "body"}`, + ) + }) + + ctx := context.Background() + coc, _, err := client.GetCodeOfConduct(ctx, "k") + if err != nil { + t.Errorf("ListCodesOfConduct returned error: %v", err) + } + + want := &CodeOfConduct{ + Key: String("key"), + Name: String("name"), + URL: String("url"), + Body: String("body"), + } + if !cmp.Equal(want, coc) { + t.Errorf("GetCodeOfConductByKey returned %+v, want %+v", coc, want) + } + + const methodName = "GetCodeOfConduct" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.GetCodeOfConduct(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.GetCodeOfConduct(ctx, "k") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestCodeOfConduct_Marshal(t *testing.T) { + testJSONMarshal(t, &CodeOfConduct{}, "{}") + + a := &CodeOfConduct{ + Name: String("name"), + Key: String("key"), + URL: String("url"), + Body: String("body"), + } + + want := `{ + "name": "name", + "key": "key", + "url": "url", + "body": "body" + }` + + testJSONMarshal(t, a, want) +} diff --git a/github/emojis.go b/github/emojis.go new file mode 100644 index 0000000000..b51f46c9b5 --- /dev/null +++ b/github/emojis.go @@ -0,0 +1,37 @@ +// Copyright 2023 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "context" +) + +// 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/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 := s.client.Do(ctx, req, &emoji) + if err != nil { + return nil, resp, err + } + + return emoji, resp, nil +} + +// ListEmojis +// Deprecated: Use EmojisService.ListEmojis instead +func (c *Client) ListEmojis(ctx context.Context) (map[string]string, *Response, error) { + return c.Emojis.ListEmojis(ctx) +} diff --git a/github/emojis_test.go b/github/emojis_test.go new file mode 100644 index 0000000000..d0d811b5c5 --- /dev/null +++ b/github/emojis_test.go @@ -0,0 +1,45 @@ +// Copyright 2023 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestEmojisService_ListEmojis(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/emojis", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"+1": "+1.png"}`) + }) + + ctx := context.Background() + emoji, _, err := client.ListEmojis(ctx) + if err != nil { + t.Errorf("ListEmojis returned error: %v", err) + } + + want := map[string]string{"+1": "+1.png"} + if !cmp.Equal(want, emoji) { + t.Errorf("ListEmojis returned %+v, want %+v", emoji, want) + } + + const methodName = "ListEmojis" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.ListEmojis(ctx) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} diff --git a/github/markdown.go b/github/markdown.go new file mode 100644 index 0000000000..871b1a7762 --- /dev/null +++ b/github/markdown.go @@ -0,0 +1,67 @@ +// Copyright 2023 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "bytes" + "context" +) + +// MarkdownService provides access to markdown-related functions in the GitHub API. +type MarkdownService service + +// MarkdownOptions specifies optional parameters to the Markdown method. +type MarkdownOptions struct { + // Mode identifies the rendering mode. Possible values are: + // markdown - render a document as plain Markdown, just like + // README files are rendered. + // + // gfm - to render a document as user-content, e.g. like user + // comments or issues are rendered. In GFM mode, hard line breaks are + // always taken into account, and issue and user mentions are linked + // accordingly. + // + // Default is "markdown". + Mode string + + // Context identifies the repository context. Only taken into account + // when rendering as "gfm". + Context string +} + +type markdownRequest struct { + Text *string `json:"text,omitempty"` + Mode *string `json:"mode,omitempty"` + Context *string `json:"context,omitempty"` +} + +// Markdown renders an arbitrary Markdown document. +// +// 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 != "" { + request.Mode = String(opts.Mode) + } + if opts.Context != "" { + request.Context = String(opts.Context) + } + } + + req, err := s.client.NewRequest("POST", "markdown", request) + if err != nil { + return "", nil, err + } + + buf := new(bytes.Buffer) + resp, err := s.client.Do(ctx, req, buf) + if err != nil { + return "", resp, err + } + + return buf.String(), resp, nil +} diff --git a/github/markdown_test.go b/github/markdown_test.go new file mode 100644 index 0000000000..3bbf9fb735 --- /dev/null +++ b/github/markdown_test.go @@ -0,0 +1,80 @@ +// Copyright 2023 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestMarkdownService_Markdown(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + input := &markdownRequest{ + Text: String("# text #"), + Mode: String("gfm"), + Context: String("google/go-github"), + } + mux.HandleFunc("/markdown", func(w http.ResponseWriter, r *http.Request) { + v := new(markdownRequest) + assertNilError(t, json.NewDecoder(r.Body).Decode(v)) + + testMethod(t, r, "POST") + if !cmp.Equal(v, input) { + t.Errorf("Request body = %+v, want %+v", v, input) + } + fmt.Fprint(w, `

text

`) + }) + + ctx := context.Background() + md, _, err := client.Markdown.Markdown(ctx, "# text #", &MarkdownOptions{ + Mode: "gfm", + Context: "google/go-github", + }) + if err != nil { + t.Errorf("Markdown returned error: %v", err) + } + + if want := "

text

"; want != md { + t.Errorf("Markdown returned %+v, want %+v", md, want) + } + + const methodName = "Markdown" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Markdown.Markdown(ctx, "# text #", &MarkdownOptions{ + Mode: "gfm", + Context: "google/go-github", + }) + if got != "" { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestMarkdownRequest_Marshal(t *testing.T) { + testJSONMarshal(t, &markdownRequest{}, "{}") + + a := &markdownRequest{ + Text: String("txt"), + Mode: String("mode"), + Context: String("ctx"), + } + + want := `{ + "text": "txt", + "mode": "mode", + "context": "ctx" + }` + + testJSONMarshal(t, a, want) +} diff --git a/github/meta.go b/github/meta.go new file mode 100644 index 0000000000..138febaedd --- /dev/null +++ b/github/meta.go @@ -0,0 +1,146 @@ +// Copyright 2014 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "bytes" + "context" + "fmt" + "net/url" +) + +// 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 + // that incoming service hooks will originate from on GitHub.com. + Hooks []string `json:"hooks,omitempty"` + + // An Array of IP addresses in CIDR format specifying the Git servers + // for GitHub.com. + Git []string `json:"git,omitempty"` + + // Whether authentication with username and password is supported. + // (GitHub Enterprise instances using CAS or OAuth for authentication + // will return false. Features like Basic Authentication with a + // username and password, sudo mode, and two-factor authentication are + // not supported on these servers.) + VerifiablePasswordAuthentication *bool `json:"verifiable_password_authentication,omitempty"` + + // An array of IP addresses in CIDR format specifying the addresses + // which serve GitHub Pages websites. + Pages []string `json:"pages,omitempty"` + + // An Array of IP addresses specifying the addresses that source imports + // will originate from on GitHub.com. + Importer []string `json:"importer,omitempty"` + + // An array of IP addresses in CIDR format specifying the IP addresses + // GitHub Actions will originate from. + Actions []string `json:"actions,omitempty"` + + // An array of IP addresses in CIDR format specifying the IP addresses + // Dependabot will originate from. + Dependabot []string `json:"dependabot,omitempty"` + + // A map of algorithms to SSH key fingerprints. + SSHKeyFingerprints map[string]string `json:"ssh_key_fingerprints,omitempty"` + + // An array of SSH keys. + SSHKeys []string `json:"ssh_keys,omitempty"` + + // An array of IP addresses in CIDR format specifying the addresses + // which serve GitHub websites. + Web []string `json:"web,omitempty"` + + // An array of IP addresses in CIDR format specifying the addresses + // which serve GitHub APIs. + API []string `json:"api,omitempty"` +} + +// APIMeta returns information about GitHub.com, the service. Or, if you access +// this endpoint on your organization’s GitHub Enterprise installation, this +// endpoint provides information about that installation. +// +// 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 := 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. +// +// 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 := s.client.NewRequest("GET", u, nil) + if err != nil { + return "", nil, err + } + + buf := new(bytes.Buffer) + 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/ +// +// 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 := 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) +} diff --git a/github/meta_test.go b/github/meta_test.go new file mode 100644 index 0000000000..1df6d858f4 --- /dev/null +++ b/github/meta_test.go @@ -0,0 +1,155 @@ +// Copyright 2014 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestAPIMeta_Marshal(t *testing.T) { + testJSONMarshal(t, &APIMeta{}, "{}") + + a := &APIMeta{ + Hooks: []string{"h"}, + Git: []string{"g"}, + VerifiablePasswordAuthentication: Bool(true), + Pages: []string{"p"}, + Importer: []string{"i"}, + Actions: []string{"a"}, + Dependabot: []string{"d"}, + SSHKeyFingerprints: map[string]string{"a": "f"}, + SSHKeys: []string{"k"}, + API: []string{"a"}, + Web: []string{"w"}, + } + want := `{ + "hooks":["h"], + "git":["g"], + "verifiable_password_authentication":true, + "pages":["p"], + "importer":["i"], + "actions":["a"], + "dependabot":["d"], + "ssh_key_fingerprints":{"a":"f"}, + "ssh_keys":["k"], + "api":["a"], + "web":["w"] + }` + + testJSONMarshal(t, a, want) +} + +func TestMetaService_APIMeta(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/meta", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"web":["w"],"api":["a"],"hooks":["h"], "git":["g"], "pages":["p"], "importer":["i"], "actions":["a"], "dependabot":["d"], "verifiable_password_authentication": true}`) + }) + + ctx := context.Background() + meta, _, err := client.APIMeta(ctx) + if err != nil { + t.Errorf("APIMeta returned error: %v", err) + } + + want := &APIMeta{ + Hooks: []string{"h"}, + Git: []string{"g"}, + Pages: []string{"p"}, + Importer: []string{"i"}, + Actions: []string{"a"}, + Dependabot: []string{"d"}, + API: []string{"a"}, + Web: []string{"w"}, + + VerifiablePasswordAuthentication: Bool(true), + } + if !cmp.Equal(want, meta) { + t.Errorf("APIMeta returned %+v, want %+v", meta, want) + } + + const methodName = "APIMeta" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.APIMeta(ctx) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestMetaService_Octocat(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + input := "input" + output := "sample text" + + mux.HandleFunc("/octocat", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{"s": input}) + w.Header().Set("Content-Type", "application/octocat-stream") + fmt.Fprint(w, output) + }) + + ctx := context.Background() + got, _, err := client.Octocat(ctx, input) + if err != nil { + t.Errorf("Octocat returned error: %v", err) + } + + if want := output; got != want { + t.Errorf("Octocat returned %+v, want %+v", got, want) + } + + const methodName = "Octocat" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Octocat(ctx, input) + if got != "" { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestMetaService_Zen(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + output := "sample text" + + mux.HandleFunc("/zen", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.Header().Set("Content-Type", "text/plain;charset=utf-8") + fmt.Fprint(w, output) + }) + + ctx := context.Background() + got, _, err := client.Zen(ctx) + if err != nil { + t.Errorf("Zen returned error: %v", err) + } + + if want := output; got != want { + t.Errorf("Zen returned %+v, want %+v", got, want) + } + + const methodName = "Zen" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Zen(ctx) + if got != "" { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} diff --git a/github/misc.go b/github/misc.go deleted file mode 100644 index 91b24780d4..0000000000 --- a/github/misc.go +++ /dev/null @@ -1,300 +0,0 @@ -// Copyright 2014 The go-github AUTHORS. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package github - -import ( - "bytes" - "context" - "fmt" - "net/url" -) - -// MarkdownOptions specifies optional parameters to the Markdown method. -type MarkdownOptions struct { - // Mode identifies the rendering mode. Possible values are: - // markdown - render a document as plain Markdown, just like - // README files are rendered. - // - // gfm - to render a document as user-content, e.g. like user - // comments or issues are rendered. In GFM mode, hard line breaks are - // always taken into account, and issue and user mentions are linked - // accordingly. - // - // Default is "markdown". - Mode string - - // Context identifies the repository context. Only taken into account - // when rendering as "gfm". - Context string -} - -type markdownRequest struct { - Text *string `json:"text,omitempty"` - Mode *string `json:"mode,omitempty"` - 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/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 != "" { - request.Mode = String(opts.Mode) - } - if opts.Context != "" { - request.Context = String(opts.Context) - } - } - - req, err := s.client.NewRequest("POST", "markdown", request) - if err != nil { - return "", nil, err - } - - buf := new(bytes.Buffer) - 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/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 := 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"` - Key *string `json:"key,omitempty"` - URL *string `json:"url,omitempty"` - Body *string `json:"body,omitempty"` -} - -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/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 - } - - // TODO: remove custom Accept header when this API fully launches. - req.Header.Set("Accept", mediaTypeCodesOfConductPreview) - - var cs []*CodeOfConduct - 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. -// -// 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 := s.client.NewRequest("GET", u, nil) - if err != nil { - return nil, nil, err - } - - // TODO: remove custom Accept header when this API fully launches. - req.Header.Set("Accept", mediaTypeCodesOfConductPreview) - - coc := new(CodeOfConduct) - 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 - // that incoming service hooks will originate from on GitHub.com. - Hooks []string `json:"hooks,omitempty"` - - // An Array of IP addresses in CIDR format specifying the Git servers - // for GitHub.com. - Git []string `json:"git,omitempty"` - - // Whether authentication with username and password is supported. - // (GitHub Enterprise instances using CAS or OAuth for authentication - // will return false. Features like Basic Authentication with a - // username and password, sudo mode, and two-factor authentication are - // not supported on these servers.) - VerifiablePasswordAuthentication *bool `json:"verifiable_password_authentication,omitempty"` - - // An array of IP addresses in CIDR format specifying the addresses - // which serve GitHub Pages websites. - Pages []string `json:"pages,omitempty"` - - // An Array of IP addresses specifying the addresses that source imports - // will originate from on GitHub.com. - Importer []string `json:"importer,omitempty"` - - // An array of IP addresses in CIDR format specifying the IP addresses - // GitHub Actions will originate from. - Actions []string `json:"actions,omitempty"` - - // An array of IP addresses in CIDR format specifying the IP addresses - // Dependabot will originate from. - Dependabot []string `json:"dependabot,omitempty"` - - // A map of algorithms to SSH key fingerprints. - SSHKeyFingerprints map[string]string `json:"ssh_key_fingerprints,omitempty"` - - // An array of SSH keys. - SSHKeys []string `json:"ssh_keys,omitempty"` - - // An array of IP addresses in CIDR format specifying the addresses - // which serve GitHub websites. - Web []string `json:"web,omitempty"` - - // An array of IP addresses in CIDR format specifying the addresses - // which serve GitHub APIs. - API []string `json:"api,omitempty"` -} - -// APIMeta returns information about GitHub.com, the service. Or, if you access -// this endpoint on your organization’s GitHub Enterprise installation, this -// endpoint provides information about that installation. -// -// 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 := 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. -// -// 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 := s.client.NewRequest("GET", u, nil) - if err != nil { - return "", nil, err - } - - buf := new(bytes.Buffer) - 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/ -// -// 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 := 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) -} diff --git a/github/misc_test.go b/github/misc_test.go deleted file mode 100644 index 71fd69915c..0000000000 --- a/github/misc_test.go +++ /dev/null @@ -1,356 +0,0 @@ -// Copyright 2014 The go-github AUTHORS. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package github - -import ( - "context" - "encoding/json" - "fmt" - "net/http" - "testing" - - "github.com/google/go-cmp/cmp" -) - -func TestMarkdownService_Markdown(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - input := &markdownRequest{ - Text: String("# text #"), - Mode: String("gfm"), - Context: String("google/go-github"), - } - mux.HandleFunc("/markdown", func(w http.ResponseWriter, r *http.Request) { - v := new(markdownRequest) - assertNilError(t, json.NewDecoder(r.Body).Decode(v)) - - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } - fmt.Fprint(w, `

text

`) - }) - - ctx := context.Background() - md, _, err := client.Markdown.Markdown(ctx, "# text #", &MarkdownOptions{ - Mode: "gfm", - Context: "google/go-github", - }) - if err != nil { - t.Errorf("Markdown returned error: %v", err) - } - - if want := "

text

"; want != md { - t.Errorf("Markdown returned %+v, want %+v", md, want) - } - - const methodName = "Markdown" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Markdown.Markdown(ctx, "# text #", &MarkdownOptions{ - Mode: "gfm", - Context: "google/go-github", - }) - if got != "" { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestEmojisService_ListEmojis(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/emojis", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - fmt.Fprint(w, `{"+1": "+1.png"}`) - }) - - ctx := context.Background() - emoji, _, err := client.ListEmojis(ctx) - if err != nil { - t.Errorf("ListEmojis returned error: %v", err) - } - - want := map[string]string{"+1": "+1.png"} - if !cmp.Equal(want, emoji) { - t.Errorf("ListEmojis returned %+v, want %+v", emoji, want) - } - - const methodName = "ListEmojis" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.ListEmojis(ctx) - if got != nil { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestCodesOfConductService_ListCodesOfConduct(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/codes_of_conduct", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) - fmt.Fprint(w, `[{ - "key": "key", - "name": "name", - "url": "url"} - ]`) - }) - - ctx := context.Background() - cs, _, err := client.ListCodesOfConduct(ctx) - if err != nil { - t.Errorf("ListCodesOfConduct returned error: %v", err) - } - - want := []*CodeOfConduct{ - { - Key: String("key"), - Name: String("name"), - URL: String("url"), - }} - if !cmp.Equal(want, cs) { - t.Errorf("ListCodesOfConduct returned %+v, want %+v", cs, want) - } - - const methodName = "ListCodesOfConduct" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.ListCodesOfConduct(ctx) - if got != nil { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestCodesOfConductService_GetCodeOfConduct(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/codes_of_conduct/k", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) - fmt.Fprint(w, `{ - "key": "key", - "name": "name", - "url": "url", - "body": "body"}`, - ) - }) - - ctx := context.Background() - coc, _, err := client.GetCodeOfConduct(ctx, "k") - if err != nil { - t.Errorf("ListCodesOfConduct returned error: %v", err) - } - - want := &CodeOfConduct{ - Key: String("key"), - Name: String("name"), - URL: String("url"), - Body: String("body"), - } - if !cmp.Equal(want, coc) { - t.Errorf("GetCodeOfConductByKey returned %+v, want %+v", coc, want) - } - - const methodName = "GetCodeOfConduct" - testBadOptions(t, methodName, func() (err error) { - _, _, err = client.GetCodeOfConduct(ctx, "\n") - return err - }) - - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.GetCodeOfConduct(ctx, "k") - if got != nil { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestAPIMeta_Marshal(t *testing.T) { - testJSONMarshal(t, &APIMeta{}, "{}") - - a := &APIMeta{ - Hooks: []string{"h"}, - Git: []string{"g"}, - VerifiablePasswordAuthentication: Bool(true), - Pages: []string{"p"}, - Importer: []string{"i"}, - Actions: []string{"a"}, - Dependabot: []string{"d"}, - SSHKeyFingerprints: map[string]string{"a": "f"}, - SSHKeys: []string{"k"}, - API: []string{"a"}, - Web: []string{"w"}, - } - want := `{ - "hooks":["h"], - "git":["g"], - "verifiable_password_authentication":true, - "pages":["p"], - "importer":["i"], - "actions":["a"], - "dependabot":["d"], - "ssh_key_fingerprints":{"a":"f"}, - "ssh_keys":["k"], - "api":["a"], - "web":["w"] - }` - - testJSONMarshal(t, a, want) -} - -func TestMetaService_APIMeta(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/meta", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - fmt.Fprint(w, `{"web":["w"],"api":["a"],"hooks":["h"], "git":["g"], "pages":["p"], "importer":["i"], "actions":["a"], "dependabot":["d"], "verifiable_password_authentication": true}`) - }) - - ctx := context.Background() - meta, _, err := client.APIMeta(ctx) - if err != nil { - t.Errorf("APIMeta returned error: %v", err) - } - - want := &APIMeta{ - Hooks: []string{"h"}, - Git: []string{"g"}, - Pages: []string{"p"}, - Importer: []string{"i"}, - Actions: []string{"a"}, - Dependabot: []string{"d"}, - API: []string{"a"}, - Web: []string{"w"}, - - VerifiablePasswordAuthentication: Bool(true), - } - if !cmp.Equal(want, meta) { - t.Errorf("APIMeta returned %+v, want %+v", meta, want) - } - - const methodName = "APIMeta" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.APIMeta(ctx) - if got != nil { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestMetaService_Octocat(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - input := "input" - output := "sample text" - - mux.HandleFunc("/octocat", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - testFormValues(t, r, values{"s": input}) - w.Header().Set("Content-Type", "application/octocat-stream") - fmt.Fprint(w, output) - }) - - ctx := context.Background() - got, _, err := client.Octocat(ctx, input) - if err != nil { - t.Errorf("Octocat returned error: %v", err) - } - - if want := output; got != want { - t.Errorf("Octocat returned %+v, want %+v", got, want) - } - - const methodName = "Octocat" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Octocat(ctx, input) - if got != "" { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestMetaService_Zen(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - output := "sample text" - - mux.HandleFunc("/zen", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - w.Header().Set("Content-Type", "text/plain;charset=utf-8") - fmt.Fprint(w, output) - }) - - ctx := context.Background() - got, _, err := client.Zen(ctx) - if err != nil { - t.Errorf("Zen returned error: %v", err) - } - - if want := output; got != want { - t.Errorf("Zen returned %+v, want %+v", got, want) - } - - const methodName = "Zen" - testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Zen(ctx) - if got != "" { - t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) - } - return resp, err - }) -} - -func TestMarkdownRequest_Marshal(t *testing.T) { - testJSONMarshal(t, &markdownRequest{}, "{}") - - a := &markdownRequest{ - Text: String("txt"), - Mode: String("mode"), - Context: String("ctx"), - } - - want := `{ - "text": "txt", - "mode": "mode", - "context": "ctx" - }` - - testJSONMarshal(t, a, want) -} - -func TestCodeOfConduct_Marshal(t *testing.T) { - testJSONMarshal(t, &CodeOfConduct{}, "{}") - - a := &CodeOfConduct{ - Name: String("name"), - Key: String("key"), - URL: String("url"), - Body: String("body"), - } - - want := `{ - "name": "name", - "key": "key", - "url": "url", - "body": "body" - }` - - testJSONMarshal(t, a, want) -} From f10a72eb91e35af67a7f0bbc2c554a5ac2a89e1b Mon Sep 17 00:00:00 2001 From: WillAbides <233500+WillAbides@users.noreply.github.com> Date: Thu, 21 Sep 2023 09:08:43 -0500 Subject: [PATCH 08/13] Update github/meta.go Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/meta.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/meta.go b/github/meta.go index 138febaedd..f58112184a 100644 --- a/github/meta.go +++ b/github/meta.go @@ -121,7 +121,7 @@ func (c *Client) Octocat(ctx context.Context, message string) (string, *Response // Zen returns a random line from The Zen of GitHub. // -// see also: http://warpspire.com/posts/taste/ +// See also: http://warpspire.com/posts/taste/ // // 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) { From 8442e31ed8e8358a4eb986ad0f4a3d471f3b8c16 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Thu, 21 Sep 2023 09:24:28 -0500 Subject: [PATCH 09/13] dedup names --- github/codesofconduct.go | 16 ++++++++-------- github/codesofconduct_test.go | 26 +++++++++++--------------- github/emojis.go | 8 ++++---- github/emojis_test.go | 10 +++++----- test/integration/misc_test.go | 8 ++++---- 5 files changed, 32 insertions(+), 36 deletions(-) diff --git a/github/codesofconduct.go b/github/codesofconduct.go index 17a0a876ca..4318ba56d2 100644 --- a/github/codesofconduct.go +++ b/github/codesofconduct.go @@ -25,10 +25,10 @@ func (c *CodeOfConduct) String() string { return Stringify(c) } -// ListCodesOfConduct returns all codes of conduct. +// List returns all codes of conduct. // // 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) { +func (s *CodesOfConductService) List(ctx context.Context) ([]*CodeOfConduct, *Response, error) { req, err := s.client.NewRequest("GET", "codes_of_conduct", nil) if err != nil { return nil, nil, err @@ -47,15 +47,15 @@ func (s *CodesOfConductService) ListCodesOfConduct(ctx context.Context) ([]*Code } // ListCodesOfConduct -// Deprecated: Use CodesOfConductService.ListCodesOfConduct instead +// Deprecated: Use CodesOfConductService.List instead func (c *Client) ListCodesOfConduct(ctx context.Context) ([]*CodeOfConduct, *Response, error) { - return c.CodesOfConduct.ListCodesOfConduct(ctx) + return c.CodesOfConduct.List(ctx) } -// GetCodeOfConduct returns an individual code of conduct. +// Get returns an individual code of conduct. // // 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) { +func (s *CodesOfConductService) Get(ctx context.Context, key string) (*CodeOfConduct, *Response, error) { u := fmt.Sprintf("codes_of_conduct/%s", key) req, err := s.client.NewRequest("GET", u, nil) if err != nil { @@ -75,7 +75,7 @@ func (s *CodesOfConductService) GetCodeOfConduct(ctx context.Context, key string } // GetCodeOfConduct -// Deprecated: Use CodesOfConductService.GetCodeOfConduct instead +// Deprecated: Use CodesOfConductService.Get instead func (c *Client) GetCodeOfConduct(ctx context.Context, key string) (*CodeOfConduct, *Response, error) { - return c.CodesOfConduct.GetCodeOfConduct(ctx, key) + return c.CodesOfConduct.Get(ctx, key) } diff --git a/github/codesofconduct_test.go b/github/codesofconduct_test.go index 01ccefc264..71ef31f7af 100644 --- a/github/codesofconduct_test.go +++ b/github/codesofconduct_test.go @@ -14,7 +14,7 @@ import ( "github.com/google/go-cmp/cmp" ) -func TestCodesOfConductService_ListCodesOfConduct(t *testing.T) { +func TestCodesOfConductService_List(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -30,9 +30,7 @@ func TestCodesOfConductService_ListCodesOfConduct(t *testing.T) { ctx := context.Background() cs, _, err := client.ListCodesOfConduct(ctx) - if err != nil { - t.Errorf("ListCodesOfConduct returned error: %v", err) - } + assertNilError(t, err) want := []*CodeOfConduct{ { @@ -41,12 +39,12 @@ func TestCodesOfConductService_ListCodesOfConduct(t *testing.T) { URL: String("url"), }} if !cmp.Equal(want, cs) { - t.Errorf("ListCodesOfConduct returned %+v, want %+v", cs, want) + t.Errorf("returned %+v, want %+v", cs, want) } - const methodName = "ListCodesOfConduct" + const methodName = "List" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.ListCodesOfConduct(ctx) + got, resp, err := client.CodesOfConduct.List(ctx) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -54,7 +52,7 @@ func TestCodesOfConductService_ListCodesOfConduct(t *testing.T) { }) } -func TestCodesOfConductService_GetCodeOfConduct(t *testing.T) { +func TestCodesOfConductService_Get(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -71,9 +69,7 @@ func TestCodesOfConductService_GetCodeOfConduct(t *testing.T) { ctx := context.Background() coc, _, err := client.GetCodeOfConduct(ctx, "k") - if err != nil { - t.Errorf("ListCodesOfConduct returned error: %v", err) - } + assertNilError(t, err) want := &CodeOfConduct{ Key: String("key"), @@ -82,17 +78,17 @@ func TestCodesOfConductService_GetCodeOfConduct(t *testing.T) { Body: String("body"), } if !cmp.Equal(want, coc) { - t.Errorf("GetCodeOfConductByKey returned %+v, want %+v", coc, want) + t.Errorf("returned %+v, want %+v", coc, want) } - const methodName = "GetCodeOfConduct" + const methodName = "Get" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.GetCodeOfConduct(ctx, "\n") + _, _, err = client.CodesOfConduct.Get(ctx, "\n") return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.GetCodeOfConduct(ctx, "k") + got, resp, err := client.CodesOfConduct.Get(ctx, "k") if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } diff --git a/github/emojis.go b/github/emojis.go index b51f46c9b5..15b57130b3 100644 --- a/github/emojis.go +++ b/github/emojis.go @@ -12,10 +12,10 @@ import ( // EmojisService provides access to emoji-related functions in the GitHub API. type EmojisService service -// ListEmojis returns the emojis available to use on GitHub. +// List returns the emojis available to use on GitHub. // // GitHub API docs: https://docs.github.com/rest/emojis/emojis#get-emojis -func (s *EmojisService) ListEmojis(ctx context.Context) (map[string]string, *Response, error) { +func (s *EmojisService) List(ctx context.Context) (map[string]string, *Response, error) { req, err := s.client.NewRequest("GET", "emojis", nil) if err != nil { return nil, nil, err @@ -31,7 +31,7 @@ func (s *EmojisService) ListEmojis(ctx context.Context) (map[string]string, *Res } // ListEmojis -// Deprecated: Use EmojisService.ListEmojis instead +// Deprecated: Use EmojisService.List instead func (c *Client) ListEmojis(ctx context.Context) (map[string]string, *Response, error) { - return c.Emojis.ListEmojis(ctx) + return c.Emojis.List(ctx) } diff --git a/github/emojis_test.go b/github/emojis_test.go index d0d811b5c5..79c890e36d 100644 --- a/github/emojis_test.go +++ b/github/emojis_test.go @@ -14,7 +14,7 @@ import ( "github.com/google/go-cmp/cmp" ) -func TestEmojisService_ListEmojis(t *testing.T) { +func TestEmojisService_List(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -26,17 +26,17 @@ func TestEmojisService_ListEmojis(t *testing.T) { ctx := context.Background() emoji, _, err := client.ListEmojis(ctx) if err != nil { - t.Errorf("ListEmojis returned error: %v", err) + t.Errorf("List returned error: %v", err) } want := map[string]string{"+1": "+1.png"} if !cmp.Equal(want, emoji) { - t.Errorf("ListEmojis returned %+v, want %+v", emoji, want) + t.Errorf("List returned %+v, want %+v", emoji, want) } - const methodName = "ListEmojis" + const methodName = "List" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.ListEmojis(ctx) + got, resp, err := client.Emojis.List(ctx) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } diff --git a/test/integration/misc_test.go b/test/integration/misc_test.go index d62363d45a..1fcc6dfab5 100644 --- a/test/integration/misc_test.go +++ b/test/integration/misc_test.go @@ -15,17 +15,17 @@ import ( ) func TestEmojis(t *testing.T) { - emoji, _, err := client.Emojis.ListEmojis(context.Background()) + emoji, _, err := client.Emojis.List(context.Background()) if err != nil { - t.Fatalf("ListEmojis returned error: %v", err) + t.Fatalf("List returned error: %v", err) } if len(emoji) == 0 { - t.Errorf("ListEmojis returned no emojis") + t.Errorf("List returned no emojis") } if _, ok := emoji["+1"]; !ok { - t.Errorf("ListEmojis missing '+1' emoji") + t.Errorf("List missing '+1' emoji") } } From c86f81ee2021c231003e7695260d943f5f0678a2 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Thu, 21 Sep 2023 09:32:14 -0500 Subject: [PATCH 10/13] rename Markdown to Render --- github/examples_test.go | 4 ++-- github/markdown.go | 12 ++++++------ github/markdown_test.go | 20 ++++++++++---------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/github/examples_test.go b/github/examples_test.go index db82e4de0d..4aca86218f 100644 --- a/github/examples_test.go +++ b/github/examples_test.go @@ -15,14 +15,14 @@ import ( "github.com/google/go-github/v55/github" ) -func ExampleMarkdownService_Markdown() { +func ExampleMarkdownService_Render() { 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.Markdown(ctx, input, opt) + output, _, err := client.Markdown.Render(ctx, input, opt) if err != nil { fmt.Println(err) } diff --git a/github/markdown.go b/github/markdown.go index 871b1a7762..48b445b3d8 100644 --- a/github/markdown.go +++ b/github/markdown.go @@ -13,10 +13,10 @@ import ( // MarkdownService provides access to markdown-related functions in the GitHub API. type MarkdownService service -// MarkdownOptions specifies optional parameters to the Markdown method. +// MarkdownOptions specifies optional parameters to the Render method. type MarkdownOptions struct { // Mode identifies the rendering mode. Possible values are: - // markdown - render a document as plain Markdown, just like + // markdown - render a document as plain Render, just like // README files are rendered. // // gfm - to render a document as user-content, e.g. like user @@ -32,17 +32,17 @@ type MarkdownOptions struct { Context string } -type markdownRequest struct { +type markdownRenderRequest struct { Text *string `json:"text,omitempty"` Mode *string `json:"mode,omitempty"` Context *string `json:"context,omitempty"` } -// Markdown renders an arbitrary Markdown document. +// Render renders an arbitrary Render document. // // 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)} +func (s *MarkdownService) Render(ctx context.Context, text string, opts *MarkdownOptions) (string, *Response, error) { + request := &markdownRenderRequest{Text: String(text)} if opts != nil { if opts.Mode != "" { request.Mode = String(opts.Mode) diff --git a/github/markdown_test.go b/github/markdown_test.go index 3bbf9fb735..2b6e7eee48 100644 --- a/github/markdown_test.go +++ b/github/markdown_test.go @@ -19,13 +19,13 @@ func TestMarkdownService_Markdown(t *testing.T) { client, mux, _, teardown := setup() defer teardown() - input := &markdownRequest{ + input := &markdownRenderRequest{ Text: String("# text #"), Mode: String("gfm"), Context: String("google/go-github"), } mux.HandleFunc("/markdown", func(w http.ResponseWriter, r *http.Request) { - v := new(markdownRequest) + v := new(markdownRenderRequest) assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") @@ -36,21 +36,21 @@ func TestMarkdownService_Markdown(t *testing.T) { }) ctx := context.Background() - md, _, err := client.Markdown.Markdown(ctx, "# text #", &MarkdownOptions{ + md, _, err := client.Markdown.Render(ctx, "# text #", &MarkdownOptions{ Mode: "gfm", Context: "google/go-github", }) if err != nil { - t.Errorf("Markdown returned error: %v", err) + t.Errorf("Render returned error: %v", err) } if want := "

text

"; want != md { - t.Errorf("Markdown returned %+v, want %+v", md, want) + t.Errorf("Render returned %+v, want %+v", md, want) } - const methodName = "Markdown" + const methodName = "Render" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Markdown.Markdown(ctx, "# text #", &MarkdownOptions{ + got, resp, err := client.Markdown.Render(ctx, "# text #", &MarkdownOptions{ Mode: "gfm", Context: "google/go-github", }) @@ -61,10 +61,10 @@ func TestMarkdownService_Markdown(t *testing.T) { }) } -func TestMarkdownRequest_Marshal(t *testing.T) { - testJSONMarshal(t, &markdownRequest{}, "{}") +func TestMarkdownRenderRequest_Marshal(t *testing.T) { + testJSONMarshal(t, &markdownRenderRequest{}, "{}") - a := &markdownRequest{ + a := &markdownRenderRequest{ Text: String("txt"), Mode: String("mode"), Context: String("ctx"), From 843273dc89c97ba7e4d42ed7b584e4072dc8f11c Mon Sep 17 00:00:00 2001 From: Will Roden Date: Thu, 21 Sep 2023 15:27:15 -0500 Subject: [PATCH 11/13] Rename MetaService.APIMeta to MetaService.Get --- github/meta.go | 8 ++++---- github/meta_test.go | 6 +++--- test/integration/misc_test.go | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/github/meta.go b/github/meta.go index f58112184a..a4d9bac77b 100644 --- a/github/meta.go +++ b/github/meta.go @@ -63,12 +63,12 @@ type APIMeta struct { API []string `json:"api,omitempty"` } -// APIMeta returns information about GitHub.com, the service. Or, if you access +// Get returns information about GitHub.com, the service. Or, if you access // this endpoint on your organization’s GitHub Enterprise installation, this // endpoint provides information about that installation. // // GitHub API docs: https://docs.github.com/rest/meta/meta#get-github-meta-information -func (s *MetaService) APIMeta(ctx context.Context) (*APIMeta, *Response, error) { +func (s *MetaService) Get(ctx context.Context) (*APIMeta, *Response, error) { req, err := s.client.NewRequest("GET", "meta", nil) if err != nil { return nil, nil, err @@ -84,9 +84,9 @@ func (s *MetaService) APIMeta(ctx context.Context) (*APIMeta, *Response, error) } // APIMeta -// Deprecated: Use MetaService.APIMeta instead. +// Deprecated: Use MetaService.Get instead. func (c *Client) APIMeta(ctx context.Context) (*APIMeta, *Response, error) { - return c.Meta.APIMeta(ctx) + return c.Meta.Get(ctx) } // Octocat returns an ASCII art octocat with the specified message in a speech diff --git a/github/meta_test.go b/github/meta_test.go index 1df6d858f4..1547dd1d22 100644 --- a/github/meta_test.go +++ b/github/meta_test.go @@ -59,7 +59,7 @@ func TestMetaService_APIMeta(t *testing.T) { ctx := context.Background() meta, _, err := client.APIMeta(ctx) if err != nil { - t.Errorf("APIMeta returned error: %v", err) + t.Errorf("Get returned error: %v", err) } want := &APIMeta{ @@ -75,10 +75,10 @@ func TestMetaService_APIMeta(t *testing.T) { VerifiablePasswordAuthentication: Bool(true), } if !cmp.Equal(want, meta) { - t.Errorf("APIMeta returned %+v, want %+v", meta, want) + t.Errorf("Get returned %+v, want %+v", meta, want) } - const methodName = "APIMeta" + const methodName = "Get" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { got, resp, err := client.APIMeta(ctx) if got != nil { diff --git a/test/integration/misc_test.go b/test/integration/misc_test.go index 1fcc6dfab5..6ffb163fdc 100644 --- a/test/integration/misc_test.go +++ b/test/integration/misc_test.go @@ -30,17 +30,17 @@ func TestEmojis(t *testing.T) { } func TestAPIMeta(t *testing.T) { - meta, _, err := client.Meta.APIMeta(context.Background()) + meta, _, err := client.Meta.Get(context.Background()) if err != nil { - t.Fatalf("APIMeta returned error: %v", err) + t.Fatalf("Get returned error: %v", err) } if len(meta.Hooks) == 0 { - t.Errorf("APIMeta returned no hook addresses") + t.Errorf("Get returned no hook addresses") } if len(meta.Git) == 0 { - t.Errorf("APIMeta returned no git addresses") + t.Errorf("Get returned no git addresses") } if !*meta.VerifiablePasswordAuthentication { From cefc7792c8fde20ccc30eb9145a08e03f0662b8c Mon Sep 17 00:00:00 2001 From: WillAbides <233500+WillAbides@users.noreply.github.com> Date: Sat, 7 Oct 2023 15:35:15 -0500 Subject: [PATCH 12/13] Apply suggestions from code review Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/meta_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/github/meta_test.go b/github/meta_test.go index 1547dd1d22..fdcd5da276 100644 --- a/github/meta_test.go +++ b/github/meta_test.go @@ -47,7 +47,7 @@ func TestAPIMeta_Marshal(t *testing.T) { testJSONMarshal(t, a, want) } -func TestMetaService_APIMeta(t *testing.T) { +func TestMetaService_Get(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -57,7 +57,7 @@ func TestMetaService_APIMeta(t *testing.T) { }) ctx := context.Background() - meta, _, err := client.APIMeta(ctx) + meta, _, err := client.MetaService.Get(ctx) if err != nil { t.Errorf("Get returned error: %v", err) } @@ -80,7 +80,7 @@ func TestMetaService_APIMeta(t *testing.T) { const methodName = "Get" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.APIMeta(ctx) + got, resp, err := client.MetaService.Get(ctx) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -103,7 +103,7 @@ func TestMetaService_Octocat(t *testing.T) { }) ctx := context.Background() - got, _, err := client.Octocat(ctx, input) + got, _, err := client.MetaService.Octocat(ctx, input) if err != nil { t.Errorf("Octocat returned error: %v", err) } @@ -114,7 +114,7 @@ func TestMetaService_Octocat(t *testing.T) { const methodName = "Octocat" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Octocat(ctx, input) + got, resp, err := client.MetaService.Octocat(ctx, input) if got != "" { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -135,7 +135,7 @@ func TestMetaService_Zen(t *testing.T) { }) ctx := context.Background() - got, _, err := client.Zen(ctx) + got, _, err := client.MetaService.Zen(ctx) if err != nil { t.Errorf("Zen returned error: %v", err) } @@ -146,7 +146,7 @@ func TestMetaService_Zen(t *testing.T) { const methodName = "Zen" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Zen(ctx) + got, resp, err := client.MetaService.Zen(ctx) if got != "" { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } From 118ec2ff14ba04b7603a58d460fa8ec5ae5da9e1 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Sat, 7 Oct 2023 15:41:55 -0500 Subject: [PATCH 13/13] Fix service name --- github/meta_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/github/meta_test.go b/github/meta_test.go index fdcd5da276..c7b01e298e 100644 --- a/github/meta_test.go +++ b/github/meta_test.go @@ -57,7 +57,7 @@ func TestMetaService_Get(t *testing.T) { }) ctx := context.Background() - meta, _, err := client.MetaService.Get(ctx) + meta, _, err := client.Meta.Get(ctx) if err != nil { t.Errorf("Get returned error: %v", err) } @@ -80,7 +80,7 @@ func TestMetaService_Get(t *testing.T) { const methodName = "Get" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.MetaService.Get(ctx) + got, resp, err := client.Meta.Get(ctx) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -103,7 +103,7 @@ func TestMetaService_Octocat(t *testing.T) { }) ctx := context.Background() - got, _, err := client.MetaService.Octocat(ctx, input) + got, _, err := client.Meta.Octocat(ctx, input) if err != nil { t.Errorf("Octocat returned error: %v", err) } @@ -114,7 +114,7 @@ func TestMetaService_Octocat(t *testing.T) { const methodName = "Octocat" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.MetaService.Octocat(ctx, input) + got, resp, err := client.Meta.Octocat(ctx, input) if got != "" { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -135,7 +135,7 @@ func TestMetaService_Zen(t *testing.T) { }) ctx := context.Background() - got, _, err := client.MetaService.Zen(ctx) + got, _, err := client.Meta.Zen(ctx) if err != nil { t.Errorf("Zen returned error: %v", err) } @@ -146,7 +146,7 @@ func TestMetaService_Zen(t *testing.T) { const methodName = "Zen" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.MetaService.Zen(ctx) + got, resp, err := client.Meta.Zen(ctx) if got != "" { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) }