Skip to content

Commit fb26df4

Browse files
committed
move services to their own files
1 parent 107ee30 commit fb26df4

10 files changed

Lines changed: 698 additions & 652 deletions

File tree

github/codesofconduct.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
)
7+
8+
type CodesOfConductService service
9+
10+
// CodeOfConduct represents a code of conduct.
11+
type CodeOfConduct struct {
12+
Name *string `json:"name,omitempty"`
13+
Key *string `json:"key,omitempty"`
14+
URL *string `json:"url,omitempty"`
15+
Body *string `json:"body,omitempty"`
16+
}
17+
18+
func (c *CodeOfConduct) String() string {
19+
return Stringify(c)
20+
}
21+
22+
// ListCodesOfConduct returns all codes of conduct.
23+
//
24+
// GitHub API docs: https://docs.github.com/rest/codes-of-conduct/codes-of-conduct#get-all-codes-of-conduct
25+
func (s *CodesOfConductService) ListCodesOfConduct(ctx context.Context) ([]*CodeOfConduct, *Response, error) {
26+
req, err := s.client.NewRequest("GET", "codes_of_conduct", nil)
27+
if err != nil {
28+
return nil, nil, err
29+
}
30+
31+
// TODO: remove custom Accept header when this API fully launches.
32+
req.Header.Set("Accept", mediaTypeCodesOfConductPreview)
33+
34+
var cs []*CodeOfConduct
35+
resp, err := s.client.Do(ctx, req, &cs)
36+
if err != nil {
37+
return nil, resp, err
38+
}
39+
40+
return cs, resp, nil
41+
}
42+
43+
// ListCodesOfConduct
44+
// Deprecated: Use CodesOfConductService.ListCodesOfConduct instead
45+
func (c *Client) ListCodesOfConduct(ctx context.Context) ([]*CodeOfConduct, *Response, error) {
46+
return c.CodesOfConduct.ListCodesOfConduct(ctx)
47+
}
48+
49+
// GetCodeOfConduct returns an individual code of conduct.
50+
//
51+
// GitHub API docs: https://docs.github.com/rest/codes-of-conduct/codes-of-conduct#get-a-code-of-conduct
52+
func (s *CodesOfConductService) GetCodeOfConduct(ctx context.Context, key string) (*CodeOfConduct, *Response, error) {
53+
u := fmt.Sprintf("codes_of_conduct/%s", key)
54+
req, err := s.client.NewRequest("GET", u, nil)
55+
if err != nil {
56+
return nil, nil, err
57+
}
58+
59+
// TODO: remove custom Accept header when this API fully launches.
60+
req.Header.Set("Accept", mediaTypeCodesOfConductPreview)
61+
62+
coc := new(CodeOfConduct)
63+
resp, err := s.client.Do(ctx, req, coc)
64+
if err != nil {
65+
return nil, resp, err
66+
}
67+
68+
return coc, resp, nil
69+
}
70+
71+
// GetCodeOfConduct
72+
// Deprecated: Use CodesOfConductService.GetCodeOfConduct instead
73+
func (c *Client) GetCodeOfConduct(ctx context.Context, key string) (*CodeOfConduct, *Response, error) {
74+
return c.CodesOfConduct.GetCodeOfConduct(ctx, key)
75+
}

github/codesofconduct_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/google/go-cmp/cmp"
10+
)
11+
12+
func TestCodesOfConductService_ListCodesOfConduct(t *testing.T) {
13+
client, mux, _, teardown := setup()
14+
defer teardown()
15+
16+
mux.HandleFunc("/codes_of_conduct", func(w http.ResponseWriter, r *http.Request) {
17+
testMethod(t, r, "GET")
18+
testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview)
19+
fmt.Fprint(w, `[{
20+
"key": "key",
21+
"name": "name",
22+
"url": "url"}
23+
]`)
24+
})
25+
26+
ctx := context.Background()
27+
cs, _, err := client.CodesOfConduct.ListCodesOfConduct(ctx)
28+
if err != nil {
29+
t.Errorf("ListCodesOfConduct returned error: %v", err)
30+
}
31+
32+
want := []*CodeOfConduct{
33+
{
34+
Key: String("key"),
35+
Name: String("name"),
36+
URL: String("url"),
37+
}}
38+
if !cmp.Equal(want, cs) {
39+
t.Errorf("ListCodesOfConduct returned %+v, want %+v", cs, want)
40+
}
41+
42+
const methodName = "ListCodesOfConduct"
43+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
44+
got, resp, err := client.CodesOfConduct.ListCodesOfConduct(ctx)
45+
if got != nil {
46+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
47+
}
48+
return resp, err
49+
})
50+
}
51+
52+
func TestCodesOfConductService_GetCodeOfConduct(t *testing.T) {
53+
client, mux, _, teardown := setup()
54+
defer teardown()
55+
56+
mux.HandleFunc("/codes_of_conduct/k", func(w http.ResponseWriter, r *http.Request) {
57+
testMethod(t, r, "GET")
58+
testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview)
59+
fmt.Fprint(w, `{
60+
"key": "key",
61+
"name": "name",
62+
"url": "url",
63+
"body": "body"}`,
64+
)
65+
})
66+
67+
ctx := context.Background()
68+
coc, _, err := client.CodesOfConduct.GetCodeOfConduct(ctx, "k")
69+
if err != nil {
70+
t.Errorf("ListCodesOfConduct returned error: %v", err)
71+
}
72+
73+
want := &CodeOfConduct{
74+
Key: String("key"),
75+
Name: String("name"),
76+
URL: String("url"),
77+
Body: String("body"),
78+
}
79+
if !cmp.Equal(want, coc) {
80+
t.Errorf("GetCodeOfConductByKey returned %+v, want %+v", coc, want)
81+
}
82+
83+
const methodName = "GetCodeOfConduct"
84+
testBadOptions(t, methodName, func() (err error) {
85+
_, _, err = client.CodesOfConduct.GetCodeOfConduct(ctx, "\n")
86+
return err
87+
})
88+
89+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
90+
got, resp, err := client.CodesOfConduct.GetCodeOfConduct(ctx, "k")
91+
if got != nil {
92+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
93+
}
94+
return resp, err
95+
})
96+
}
97+
98+
func TestCodeOfConduct_Marshal(t *testing.T) {
99+
testJSONMarshal(t, &CodeOfConduct{}, "{}")
100+
101+
a := &CodeOfConduct{
102+
Name: String("name"),
103+
Key: String("key"),
104+
URL: String("url"),
105+
Body: String("body"),
106+
}
107+
108+
want := `{
109+
"name": "name",
110+
"key": "key",
111+
"url": "url",
112+
"body": "body"
113+
}`
114+
115+
testJSONMarshal(t, a, want)
116+
}

github/emojis.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package github
2+
3+
import (
4+
"context"
5+
)
6+
7+
type EmojisService service
8+
9+
// ListEmojis returns the emojis available to use on GitHub.
10+
//
11+
// GitHub API docs: https://docs.github.com/rest/emojis/emojis#get-emojis
12+
func (s *EmojisService) ListEmojis(ctx context.Context) (map[string]string, *Response, error) {
13+
req, err := s.client.NewRequest("GET", "emojis", nil)
14+
if err != nil {
15+
return nil, nil, err
16+
}
17+
18+
var emoji map[string]string
19+
resp, err := s.client.Do(ctx, req, &emoji)
20+
if err != nil {
21+
return nil, resp, err
22+
}
23+
24+
return emoji, resp, nil
25+
}
26+
27+
// ListEmojis
28+
// Deprecated: Use EmojisService.ListEmojis instead
29+
func (c *Client) ListEmojis(ctx context.Context) (map[string]string, *Response, error) {
30+
return c.Emojis.ListEmojis(ctx)
31+
}

github/emojis_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/google/go-cmp/cmp"
10+
)
11+
12+
func TestEmojisService_ListEmojis(t *testing.T) {
13+
client, mux, _, teardown := setup()
14+
defer teardown()
15+
16+
mux.HandleFunc("/emojis", func(w http.ResponseWriter, r *http.Request) {
17+
testMethod(t, r, "GET")
18+
fmt.Fprint(w, `{"+1": "+1.png"}`)
19+
})
20+
21+
ctx := context.Background()
22+
emoji, _, err := client.Emojis.ListEmojis(ctx)
23+
if err != nil {
24+
t.Errorf("ListEmojis returned error: %v", err)
25+
}
26+
27+
want := map[string]string{"+1": "+1.png"}
28+
if !cmp.Equal(want, emoji) {
29+
t.Errorf("ListEmojis returned %+v, want %+v", emoji, want)
30+
}
31+
32+
const methodName = "ListEmojis"
33+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
34+
got, resp, err := client.Emojis.ListEmojis(ctx)
35+
if got != nil {
36+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
37+
}
38+
return resp, err
39+
})
40+
}

github/markdown.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package github
2+
3+
import (
4+
"bytes"
5+
"context"
6+
)
7+
8+
type MarkdownService service
9+
10+
// MarkdownOptions specifies optional parameters to the Markdown method.
11+
type MarkdownOptions struct {
12+
// Mode identifies the rendering mode. Possible values are:
13+
// markdown - render a document as plain Markdown, just like
14+
// README files are rendered.
15+
//
16+
// gfm - to render a document as user-content, e.g. like user
17+
// comments or issues are rendered. In GFM mode, hard line breaks are
18+
// always taken into account, and issue and user mentions are linked
19+
// accordingly.
20+
//
21+
// Default is "markdown".
22+
Mode string
23+
24+
// Context identifies the repository context. Only taken into account
25+
// when rendering as "gfm".
26+
Context string
27+
}
28+
29+
type markdownRequest struct {
30+
Text *string `json:"text,omitempty"`
31+
Mode *string `json:"mode,omitempty"`
32+
Context *string `json:"context,omitempty"`
33+
}
34+
35+
// Markdown renders an arbitrary Markdown document.
36+
//
37+
// GitHub API docs: https://docs.github.com/rest/markdown/markdown#render-a-markdown-document
38+
func (s *MarkdownService) Markdown(ctx context.Context, text string, opts *MarkdownOptions) (string, *Response, error) {
39+
request := &markdownRequest{Text: String(text)}
40+
if opts != nil {
41+
if opts.Mode != "" {
42+
request.Mode = String(opts.Mode)
43+
}
44+
if opts.Context != "" {
45+
request.Context = String(opts.Context)
46+
}
47+
}
48+
49+
req, err := s.client.NewRequest("POST", "markdown", request)
50+
if err != nil {
51+
return "", nil, err
52+
}
53+
54+
buf := new(bytes.Buffer)
55+
resp, err := s.client.Do(ctx, req, buf)
56+
if err != nil {
57+
return "", resp, err
58+
}
59+
60+
return buf.String(), resp, nil
61+
}

0 commit comments

Comments
 (0)