Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Create MarkdownService, EmojisService, CodesOfConductService and MetaService #2937

Merged
merged 15 commits into from
Oct 9, 2023
Merged
81 changes: 81 additions & 0 deletions github/codesofconduct.go
Original file line number Diff line number Diff line change
@@ -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)
}

// 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) List(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.List instead
func (c *Client) ListCodesOfConduct(ctx context.Context) ([]*CodeOfConduct, *Response, error) {
return c.CodesOfConduct.List(ctx)
}

// 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) 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 {
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.Get instead
func (c *Client) GetCodeOfConduct(ctx context.Context, key string) (*CodeOfConduct, *Response, error) {
return c.CodesOfConduct.Get(ctx, key)
}
117 changes: 117 additions & 0 deletions github/codesofconduct_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// 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_List(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)
assertNilError(t, err)

want := []*CodeOfConduct{
{
Key: String("key"),
Name: String("name"),
URL: String("url"),
}}
if !cmp.Equal(want, cs) {
t.Errorf("returned %+v, want %+v", cs, want)
}

const methodName = "List"
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.CodesOfConduct.List(ctx)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestCodesOfConductService_Get(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")
assertNilError(t, err)

want := &CodeOfConduct{
Key: String("key"),
Name: String("name"),
URL: String("url"),
Body: String("body"),
}
if !cmp.Equal(want, coc) {
t.Errorf("returned %+v, want %+v", coc, want)
}

const methodName = "Get"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.CodesOfConduct.Get(ctx, "\n")
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.CodesOfConduct.Get(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)
}
37 changes: 37 additions & 0 deletions github/emojis.go
Original file line number Diff line number Diff line change
@@ -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

// List returns the emojis available to use on GitHub.
//
// GitHub API docs: https://docs.github.com/rest/emojis/emojis#get-emojis
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
}

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.List instead
func (c *Client) ListEmojis(ctx context.Context) (map[string]string, *Response, error) {
return c.Emojis.List(ctx)
}
45 changes: 45 additions & 0 deletions github/emojis_test.go
Original file line number Diff line number Diff line change
@@ -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_List(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("List returned error: %v", err)
}

want := map[string]string{"+1": "+1.png"}
if !cmp.Equal(want, emoji) {
t.Errorf("List returned %+v, want %+v", emoji, want)
}

const methodName = "List"
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Emojis.List(ctx)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}
4 changes: 2 additions & 2 deletions github/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (
"github.com/google/go-github/v55/github"
)

func ExampleClient_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(ctx, input, opt)
output, _, err := client.Markdown.Render(ctx, input, opt)
if err != nil {
fmt.Println(err)
}
Expand Down
8 changes: 8 additions & 0 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,11 @@ type Client struct {
Billing *BillingService
Checks *ChecksService
CodeScanning *CodeScanningService
CodesOfConduct *CodesOfConductService
Codespaces *CodespacesService
Dependabot *DependabotService
DependencyGraph *DependencyGraphService
Emojis *EmojisService
Enterprise *EnterpriseService
Gists *GistsService
Git *GitService
Expand All @@ -194,7 +196,9 @@ type Client struct {
IssueImport *IssueImportService
Issues *IssuesService
Licenses *LicensesService
Markdown *MarkdownService
Marketplace *MarketplaceService
Meta *MetaService
Migrations *MigrationService
Organizations *OrganizationsService
Projects *ProjectsService
Expand Down Expand Up @@ -401,8 +405,10 @@ func (c *Client) initialize() {
c.Checks = (*ChecksService)(&c.common)
c.CodeScanning = (*CodeScanningService)(&c.common)
c.Codespaces = (*CodespacesService)(&c.common)
c.CodesOfConduct = (*CodesOfConductService)(&c.common)
c.Dependabot = (*DependabotService)(&c.common)
c.DependencyGraph = (*DependencyGraphService)(&c.common)
c.Emojis = (*EmojisService)(&c.common)
c.Enterprise = (*EnterpriseService)(&c.common)
c.Gists = (*GistsService)(&c.common)
c.Git = (*GitService)(&c.common)
Expand All @@ -411,7 +417,9 @@ func (c *Client) initialize() {
c.IssueImport = (*IssueImportService)(&c.common)
c.Issues = (*IssuesService)(&c.common)
c.Licenses = (*LicensesService)(&c.common)
c.Markdown = (*MarkdownService)(&c.common)
c.Marketplace = &MarketplaceService{client: c}
c.Meta = (*MetaService)(&c.common)
c.Migrations = (*MigrationService)(&c.common)
c.Organizations = (*OrganizationsService)(&c.common)
c.Projects = (*ProjectsService)(&c.common)
Expand Down
Loading
Loading