Skip to content

Commit 3a432d6

Browse files
authored
Add GetCodeownersErrors to RepositoriesService (#2408)
Fixes: #2405 .
1 parent 645b457 commit 3a432d6

4 files changed

Lines changed: 174 additions & 0 deletions

File tree

github/github-accessors.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/repos_codeowners.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2022 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+
// CodeownersErrors represents a list of syntax errors detected in the CODEOWNERS file.
14+
type CodeownersErrors struct {
15+
Errors []*CodeownersError `json:"errors"`
16+
}
17+
18+
// CodeownersError represents a syntax error detected in the CODEOWNERS file.
19+
type CodeownersError struct {
20+
Line int `json:"line"`
21+
Column int `json:"column"`
22+
Kind string `json:"kind"`
23+
Source string `json:"source"`
24+
Suggestion *string `json:"suggestion,omitempty"`
25+
Message string `json:"message"`
26+
Path string `json:"path"`
27+
}
28+
29+
// GetCodeownersErrors lists any syntax errors that are detected in the CODEOWNERS file.
30+
//
31+
// GitHub API docs: https://docs.github.com/en/rest/repos/repos#list-codeowners-errors
32+
func (s *RepositoriesService) GetCodeownersErrors(ctx context.Context, owner, repo string) (*CodeownersErrors, *Response, error) {
33+
u := fmt.Sprintf("repos/%v/%v/codeowners/errors", owner, repo)
34+
req, err := s.client.NewRequest("GET", u, nil)
35+
if err != nil {
36+
return nil, nil, err
37+
}
38+
39+
codeownersErrors := &CodeownersErrors{}
40+
resp, err := s.client.Do(ctx, req, codeownersErrors)
41+
if err != nil {
42+
return nil, resp, err
43+
}
44+
45+
return codeownersErrors, resp, nil
46+
}

github/repos_codeowners_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright 2022 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 TestRepositoriesService_GetCodeownersErrors(t *testing.T) {
18+
client, mux, _, teardown := setup()
19+
defer teardown()
20+
21+
mux.HandleFunc("/repos/o/r/codeowners/errors", func(w http.ResponseWriter, r *http.Request) {
22+
testMethod(t, r, "GET")
23+
testHeader(t, r, "Accept", mediaTypeV3)
24+
fmt.Fprint(w, `{
25+
"errors": [
26+
{
27+
"line": 1,
28+
"column": 1,
29+
"kind": "Invalid pattern",
30+
"source": "***/*.rb @monalisa",
31+
"suggestion": "Did you mean **/*.rb?",
32+
"message": "Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^",
33+
"path": ".github/CODEOWNERS"
34+
}
35+
]
36+
}
37+
`)
38+
})
39+
40+
ctx := context.Background()
41+
codeownersErrors, _, err := client.Repositories.GetCodeownersErrors(ctx, "o", "r")
42+
if err != nil {
43+
t.Errorf("Repositories.GetCodeownersErrors returned error: %v", err)
44+
}
45+
46+
want := &CodeownersErrors{
47+
Errors: []*CodeownersError{
48+
{
49+
Line: 1,
50+
Column: 1,
51+
Kind: "Invalid pattern",
52+
Source: "***/*.rb @monalisa",
53+
Suggestion: String("Did you mean **/*.rb?"),
54+
Message: "Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^",
55+
Path: ".github/CODEOWNERS",
56+
},
57+
},
58+
}
59+
if !cmp.Equal(codeownersErrors, want) {
60+
t.Errorf("Repositories.GetCodeownersErrors returned %+v, want %+v", codeownersErrors, want)
61+
}
62+
63+
const methodName = "GetCodeownersErrors"
64+
testBadOptions(t, methodName, func() (err error) {
65+
_, _, err = client.Repositories.GetCodeownersErrors(ctx, "\n", "\n")
66+
return err
67+
})
68+
69+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
70+
got, resp, err := client.Repositories.GetCodeownersErrors(ctx, "o", "r")
71+
if got != nil {
72+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
73+
}
74+
return resp, err
75+
})
76+
}
77+
78+
func TestCodeownersErrors_Marshal(t *testing.T) {
79+
testJSONMarshal(t, &CodeownersErrors{}, "{}")
80+
81+
u := &CodeownersErrors{
82+
Errors: []*CodeownersError{
83+
{
84+
Line: 1,
85+
Column: 1,
86+
Kind: "Invalid pattern",
87+
Source: "***/*.rb @monalisa",
88+
Suggestion: String("Did you mean **/*.rb?"),
89+
Message: "Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^",
90+
Path: ".github/CODEOWNERS",
91+
},
92+
},
93+
}
94+
95+
want := `{
96+
"errors": [
97+
{
98+
"line": 1,
99+
"column": 1,
100+
"kind": "Invalid pattern",
101+
"source": "***/*.rb @monalisa",
102+
"suggestion": "Did you mean **/*.rb?",
103+
"message": "Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^",
104+
"path": ".github/CODEOWNERS"
105+
}
106+
]
107+
}
108+
`
109+
testJSONMarshal(t, u, want)
110+
}

0 commit comments

Comments
 (0)