Skip to content

Commit

Permalink
move services to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAbides committed Sep 21, 2023
1 parent 6f4b8a1 commit 2c0768b
Show file tree
Hide file tree
Showing 10 changed files with 732 additions and 656 deletions.
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)
}

// 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)
}
121 changes: 121 additions & 0 deletions github/codesofconduct_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
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

// 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)
}
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_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
})
}
67 changes: 67 additions & 0 deletions github/markdown.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 2c0768b

Please sign in to comment.