Skip to content

Commit f8c16b8

Browse files
authored
Create MarkdownService, EmojisService, CodesOfConductService and MetaService (#2937)
Fixes: #2936.
1 parent 74db58f commit f8c16b8

File tree

13 files changed

+746
-613
lines changed

13 files changed

+746
-613
lines changed

github/codesofconduct.go

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

github/codesofconduct_test.go

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

github/emojis.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2023 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
)
11+
12+
// EmojisService provides access to emoji-related functions in the GitHub API.
13+
type EmojisService service
14+
15+
// List returns the emojis available to use on GitHub.
16+
//
17+
// GitHub API docs: https://docs.github.com/rest/emojis/emojis#get-emojis
18+
func (s *EmojisService) List(ctx context.Context) (map[string]string, *Response, error) {
19+
req, err := s.client.NewRequest("GET", "emojis", nil)
20+
if err != nil {
21+
return nil, nil, err
22+
}
23+
24+
var emoji map[string]string
25+
resp, err := s.client.Do(ctx, req, &emoji)
26+
if err != nil {
27+
return nil, resp, err
28+
}
29+
30+
return emoji, resp, nil
31+
}
32+
33+
// ListEmojis
34+
// Deprecated: Use EmojisService.List instead
35+
func (c *Client) ListEmojis(ctx context.Context) (map[string]string, *Response, error) {
36+
return c.Emojis.List(ctx)
37+
}

github/emojis_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2023 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
"fmt"
11+
"net/http"
12+
"testing"
13+
14+
"github.com/google/go-cmp/cmp"
15+
)
16+
17+
func TestEmojisService_List(t *testing.T) {
18+
client, mux, _, teardown := setup()
19+
defer teardown()
20+
21+
mux.HandleFunc("/emojis", func(w http.ResponseWriter, r *http.Request) {
22+
testMethod(t, r, "GET")
23+
fmt.Fprint(w, `{"+1": "+1.png"}`)
24+
})
25+
26+
ctx := context.Background()
27+
emoji, _, err := client.ListEmojis(ctx)
28+
if err != nil {
29+
t.Errorf("List returned error: %v", err)
30+
}
31+
32+
want := map[string]string{"+1": "+1.png"}
33+
if !cmp.Equal(want, emoji) {
34+
t.Errorf("List returned %+v, want %+v", emoji, want)
35+
}
36+
37+
const methodName = "List"
38+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
39+
got, resp, err := client.Emojis.List(ctx)
40+
if got != nil {
41+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
42+
}
43+
return resp, err
44+
})
45+
}

github/examples_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import (
1515
"github.com/google/go-github/v55/github"
1616
)
1717

18-
func ExampleClient_Markdown() {
18+
func ExampleMarkdownService_Render() {
1919
client := github.NewClient(nil)
2020

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

2424
ctx := context.Background()
25-
output, _, err := client.Markdown(ctx, input, opt)
25+
output, _, err := client.Markdown.Render(ctx, input, opt)
2626
if err != nil {
2727
fmt.Println(err)
2828
}

github/github.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,11 @@ type Client struct {
183183
Billing *BillingService
184184
Checks *ChecksService
185185
CodeScanning *CodeScanningService
186+
CodesOfConduct *CodesOfConductService
186187
Codespaces *CodespacesService
187188
Dependabot *DependabotService
188189
DependencyGraph *DependencyGraphService
190+
Emojis *EmojisService
189191
Enterprise *EnterpriseService
190192
Gists *GistsService
191193
Git *GitService
@@ -194,7 +196,9 @@ type Client struct {
194196
IssueImport *IssueImportService
195197
Issues *IssuesService
196198
Licenses *LicensesService
199+
Markdown *MarkdownService
197200
Marketplace *MarketplaceService
201+
Meta *MetaService
198202
Migrations *MigrationService
199203
Organizations *OrganizationsService
200204
Projects *ProjectsService
@@ -401,8 +405,10 @@ func (c *Client) initialize() {
401405
c.Checks = (*ChecksService)(&c.common)
402406
c.CodeScanning = (*CodeScanningService)(&c.common)
403407
c.Codespaces = (*CodespacesService)(&c.common)
408+
c.CodesOfConduct = (*CodesOfConductService)(&c.common)
404409
c.Dependabot = (*DependabotService)(&c.common)
405410
c.DependencyGraph = (*DependencyGraphService)(&c.common)
411+
c.Emojis = (*EmojisService)(&c.common)
406412
c.Enterprise = (*EnterpriseService)(&c.common)
407413
c.Gists = (*GistsService)(&c.common)
408414
c.Git = (*GitService)(&c.common)
@@ -411,7 +417,9 @@ func (c *Client) initialize() {
411417
c.IssueImport = (*IssueImportService)(&c.common)
412418
c.Issues = (*IssuesService)(&c.common)
413419
c.Licenses = (*LicensesService)(&c.common)
420+
c.Markdown = (*MarkdownService)(&c.common)
414421
c.Marketplace = &MarketplaceService{client: c}
422+
c.Meta = (*MetaService)(&c.common)
415423
c.Migrations = (*MigrationService)(&c.common)
416424
c.Organizations = (*OrganizationsService)(&c.common)
417425
c.Projects = (*ProjectsService)(&c.common)

0 commit comments

Comments
 (0)