From 10e4d1d3125d7b17baea74eff94e0402e47dfedf Mon Sep 17 00:00:00 2001 From: cbrgm Date: Fri, 8 Jul 2022 20:59:11 +0200 Subject: [PATCH 1/6] feat: add GetCodeownersErrors to RepositoriesService --- github/repos_codeowners.go | 41 +++++++++++++ github/repos_codeowners_test.go | 104 ++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 github/repos_codeowners.go create mode 100644 github/repos_codeowners_test.go diff --git a/github/repos_codeowners.go b/github/repos_codeowners.go new file mode 100644 index 00000000000..271f1588805 --- /dev/null +++ b/github/repos_codeowners.go @@ -0,0 +1,41 @@ +package github + +import ( + "context" + "fmt" +) + +// CodeownersErrors represents a list of syntax errors detected in the CODEOWNERS file. +type CodeownersErrors struct { + Errors []*CodeownersError `json:"errors"` +} + +// CodeownersError represents a syntax error detected in the CODEOWNERS file. +type CodeownersError struct { + Line *int `json:"line"` + Column *int `json:"column"` + Kind *string `json:"kind"` + Source *string `json:"source"` + Suggestion *string `json:"suggestion"` + Message *string `json:"message"` + Path *string `json:"path"` +} + +// GetCodeownersErrors lists any syntax errors that are detected in the CODEOWNERS file. +// +// GitHub API docs: https://docs.github.com/en/rest/repos/repos#list-codeowners-errors +func (s *RepositoriesService) GetCodeownersErrors(ctx context.Context, owner, repo string) (*CodeownersErrors, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/codeowners/errors", owner, repo) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + codeownersErrors := &CodeownersErrors{} + resp, err := s.client.Do(ctx, req, codeownersErrors) + if err != nil { + return nil, resp, err + } + + return codeownersErrors, resp, nil +} diff --git a/github/repos_codeowners_test.go b/github/repos_codeowners_test.go new file mode 100644 index 00000000000..cbb2bb5a9a2 --- /dev/null +++ b/github/repos_codeowners_test.go @@ -0,0 +1,104 @@ +package github + +import ( + "context" + "fmt" + "github.com/google/go-cmp/cmp" + "net/http" + "testing" +) + +func TestRepositoriesService_GetCodeownersErrors(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/codeowners/errors", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testHeader(t, r, "Accept", mediaTypeV3) + fmt.Fprint(w, `{ + "errors": [ + { + "line": 1, + "column": 1, + "kind": "Invalid pattern", + "source": "***/*.rb @monalisa", + "suggestion": "Did you mean **/*.rb?", + "message": "Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^", + "path": ".github/CODEOWNERS" + } + ] + } + `) + }) + + ctx := context.Background() + codeownersErrors, _, err := client.Repositories.GetCodeownersErrors(ctx, "o", "r") + if err != nil { + t.Errorf("Repositories.GetCodeownersErrors returned error: %v", err) + } + + want := &CodeownersErrors{ + Errors: []*CodeownersError{ + { + Line: Int(1), + Column: Int(1), + Kind: String("Invalid pattern"), + Source: String("***/*.rb @monalisa"), + Suggestion: String("Did you mean **/*.rb?"), + Message: String("Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^"), + Path: String(".github/CODEOWNERS"), + }, + }, + } + if !cmp.Equal(codeownersErrors, want) { + t.Errorf("Repositories.GetCodeownersErrors returned %+v, want %+v", codeownersErrors, want) + } + + const methodName = "GetCodeownersErrors" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Repositories.GetCodeownersErrors(ctx, "\n", "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Repositories.GetCodeownersErrors(ctx, "o", "r") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestCodeownersErrors_Marshal(t *testing.T) { + testJSONMarshal(t, &CodeownersErrors{}, "{}") + + u := &CodeownersErrors{ + Errors: []*CodeownersError{ + { + Line: Int(1), + Column: Int(1), + Kind: String("Invalid pattern"), + Source: String("***/*.rb @monalisa"), + Suggestion: String("Did you mean **/*.rb?"), + Message: String("Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^"), + Path: String(".github/CODEOWNERS"), + }, + }, + } + + want := `{ + "errors": [ + { + "line": 1, + "column": 1, + "kind": "Invalid pattern", + "source": "***/*.rb @monalisa", + "suggestion": "Did you mean **/*.rb?", + "message": "Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^", + "path": ".github/CODEOWNERS" + } + ] + } +` + testJSONMarshal(t, u, want) +} From 91859550589188c4a14edc8c9598f889a2648aef Mon Sep 17 00:00:00 2001 From: cbrgm Date: Fri, 8 Jul 2022 21:11:08 +0200 Subject: [PATCH 2/6] fix: generate github-accessors --- github/github-accessors.go | 56 ++++++++++++++++++++++++++ github/github-accessors_test.go | 70 +++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) diff --git a/github/github-accessors.go b/github/github-accessors.go index da146bcbf23..37d0122b072 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -2286,6 +2286,62 @@ func (c *CodeOfConduct) GetURL() string { return *c.URL } +// GetColumn returns the Column field if it's non-nil, zero value otherwise. +func (c *CodeownersError) GetColumn() int { + if c == nil || c.Column == nil { + return 0 + } + return *c.Column +} + +// GetKind returns the Kind field if it's non-nil, zero value otherwise. +func (c *CodeownersError) GetKind() string { + if c == nil || c.Kind == nil { + return "" + } + return *c.Kind +} + +// GetLine returns the Line field if it's non-nil, zero value otherwise. +func (c *CodeownersError) GetLine() int { + if c == nil || c.Line == nil { + return 0 + } + return *c.Line +} + +// GetMessage returns the Message field if it's non-nil, zero value otherwise. +func (c *CodeownersError) GetMessage() string { + if c == nil || c.Message == nil { + return "" + } + return *c.Message +} + +// GetPath returns the Path field if it's non-nil, zero value otherwise. +func (c *CodeownersError) GetPath() string { + if c == nil || c.Path == nil { + return "" + } + return *c.Path +} + +// GetSource returns the Source field if it's non-nil, zero value otherwise. +func (c *CodeownersError) GetSource() string { + if c == nil || c.Source == nil { + return "" + } + return *c.Source +} + +// GetSuggestion returns the Suggestion field if it's non-nil, zero value otherwise. +func (c *CodeownersError) GetSuggestion() string { + if c == nil || c.Suggestion == nil { + return "" + } + return *c.Suggestion +} + // GetHTMLURL returns the HTMLURL field if it's non-nil, zero value otherwise. func (c *CodeResult) GetHTMLURL() string { if c == nil || c.HTMLURL == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 5d30ec960a3..0c4f2bd625c 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -2699,6 +2699,76 @@ func TestCodeOfConduct_GetURL(tt *testing.T) { c.GetURL() } +func TestCodeownersError_GetColumn(tt *testing.T) { + var zeroValue int + c := &CodeownersError{Column: &zeroValue} + c.GetColumn() + c = &CodeownersError{} + c.GetColumn() + c = nil + c.GetColumn() +} + +func TestCodeownersError_GetKind(tt *testing.T) { + var zeroValue string + c := &CodeownersError{Kind: &zeroValue} + c.GetKind() + c = &CodeownersError{} + c.GetKind() + c = nil + c.GetKind() +} + +func TestCodeownersError_GetLine(tt *testing.T) { + var zeroValue int + c := &CodeownersError{Line: &zeroValue} + c.GetLine() + c = &CodeownersError{} + c.GetLine() + c = nil + c.GetLine() +} + +func TestCodeownersError_GetMessage(tt *testing.T) { + var zeroValue string + c := &CodeownersError{Message: &zeroValue} + c.GetMessage() + c = &CodeownersError{} + c.GetMessage() + c = nil + c.GetMessage() +} + +func TestCodeownersError_GetPath(tt *testing.T) { + var zeroValue string + c := &CodeownersError{Path: &zeroValue} + c.GetPath() + c = &CodeownersError{} + c.GetPath() + c = nil + c.GetPath() +} + +func TestCodeownersError_GetSource(tt *testing.T) { + var zeroValue string + c := &CodeownersError{Source: &zeroValue} + c.GetSource() + c = &CodeownersError{} + c.GetSource() + c = nil + c.GetSource() +} + +func TestCodeownersError_GetSuggestion(tt *testing.T) { + var zeroValue string + c := &CodeownersError{Suggestion: &zeroValue} + c.GetSuggestion() + c = &CodeownersError{} + c.GetSuggestion() + c = nil + c.GetSuggestion() +} + func TestCodeResult_GetHTMLURL(tt *testing.T) { var zeroValue string c := &CodeResult{HTMLURL: &zeroValue} From 3e5e5f8b095fe5a1956b1ad2ee4cfd04daab3a33 Mon Sep 17 00:00:00 2001 From: cbrgm Date: Fri, 8 Jul 2022 21:15:21 +0200 Subject: [PATCH 3/6] fix: make goimports linter happy and sort imports --- github/repos_codeowners_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/github/repos_codeowners_test.go b/github/repos_codeowners_test.go index cbb2bb5a9a2..ac1cac971c3 100644 --- a/github/repos_codeowners_test.go +++ b/github/repos_codeowners_test.go @@ -3,9 +3,10 @@ package github import ( "context" "fmt" - "github.com/google/go-cmp/cmp" "net/http" "testing" + + "github.com/google/go-cmp/cmp" ) func TestRepositoriesService_GetCodeownersErrors(t *testing.T) { From 595a0239e6bf4c8b5bbe6f2af64a70e13553e6ff Mon Sep 17 00:00:00 2001 From: cbrgm Date: Sat, 9 Jul 2022 21:49:42 +0200 Subject: [PATCH 4/6] docs: add copyright headers --- github/repos_codeowners.go | 5 +++++ github/repos_codeowners_test.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/github/repos_codeowners.go b/github/repos_codeowners.go index 271f1588805..803c8632884 100644 --- a/github/repos_codeowners.go +++ b/github/repos_codeowners.go @@ -1,3 +1,8 @@ +// Copyright 2022 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 ( diff --git a/github/repos_codeowners_test.go b/github/repos_codeowners_test.go index ac1cac971c3..9e39b45bb15 100644 --- a/github/repos_codeowners_test.go +++ b/github/repos_codeowners_test.go @@ -1,3 +1,8 @@ +// Copyright 2022 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 ( From e57822755fbc9d0f569616fcb128111961700465 Mon Sep 17 00:00:00 2001 From: cbrgm Date: Sat, 9 Jul 2022 21:53:04 +0200 Subject: [PATCH 5/6] refactor: use values instead of pointers --- github/repos_codeowners.go | 14 +++++++------- github/repos_codeowners_test.go | 24 ++++++++++++------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/github/repos_codeowners.go b/github/repos_codeowners.go index 803c8632884..835d56e164c 100644 --- a/github/repos_codeowners.go +++ b/github/repos_codeowners.go @@ -17,13 +17,13 @@ type CodeownersErrors struct { // CodeownersError represents a syntax error detected in the CODEOWNERS file. type CodeownersError struct { - Line *int `json:"line"` - Column *int `json:"column"` - Kind *string `json:"kind"` - Source *string `json:"source"` - Suggestion *string `json:"suggestion"` - Message *string `json:"message"` - Path *string `json:"path"` + Line int `json:"line"` + Column int `json:"column"` + Kind string `json:"kind"` + Source string `json:"source"` + Suggestion *string `json:"suggestion,omitempty"` + Message string `json:"message"` + Path string `json:"path"` } // GetCodeownersErrors lists any syntax errors that are detected in the CODEOWNERS file. diff --git a/github/repos_codeowners_test.go b/github/repos_codeowners_test.go index 9e39b45bb15..62c58c5b192 100644 --- a/github/repos_codeowners_test.go +++ b/github/repos_codeowners_test.go @@ -46,13 +46,13 @@ func TestRepositoriesService_GetCodeownersErrors(t *testing.T) { want := &CodeownersErrors{ Errors: []*CodeownersError{ { - Line: Int(1), - Column: Int(1), - Kind: String("Invalid pattern"), - Source: String("***/*.rb @monalisa"), + Line: 1, + Column: 1, + Kind: "Invalid pattern", + Source: "***/*.rb @monalisa", Suggestion: String("Did you mean **/*.rb?"), - Message: String("Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^"), - Path: String(".github/CODEOWNERS"), + Message: "Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^", + Path: ".github/CODEOWNERS", }, }, } @@ -81,13 +81,13 @@ func TestCodeownersErrors_Marshal(t *testing.T) { u := &CodeownersErrors{ Errors: []*CodeownersError{ { - Line: Int(1), - Column: Int(1), - Kind: String("Invalid pattern"), - Source: String("***/*.rb @monalisa"), + Line: 1, + Column: 1, + Kind: "Invalid pattern", + Source: "***/*.rb @monalisa", Suggestion: String("Did you mean **/*.rb?"), - Message: String("Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^"), - Path: String(".github/CODEOWNERS"), + Message: "Invalid pattern on line 3: Did you mean **/*.rb?\n\n ***/*.rb @monalisa\n ^", + Path: ".github/CODEOWNERS", }, }, } From 0d5cc3a969b2f5a663dc9a04fafddd2cd4749ab9 Mon Sep 17 00:00:00 2001 From: cbrgm Date: Sat, 9 Jul 2022 21:53:50 +0200 Subject: [PATCH 6/6] refactor: re-generate github-accessors --- github/github-accessors.go | 48 -------------------------- github/github-accessors_test.go | 60 --------------------------------- 2 files changed, 108 deletions(-) diff --git a/github/github-accessors.go b/github/github-accessors.go index 37d0122b072..5db061766a8 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -2286,54 +2286,6 @@ func (c *CodeOfConduct) GetURL() string { return *c.URL } -// GetColumn returns the Column field if it's non-nil, zero value otherwise. -func (c *CodeownersError) GetColumn() int { - if c == nil || c.Column == nil { - return 0 - } - return *c.Column -} - -// GetKind returns the Kind field if it's non-nil, zero value otherwise. -func (c *CodeownersError) GetKind() string { - if c == nil || c.Kind == nil { - return "" - } - return *c.Kind -} - -// GetLine returns the Line field if it's non-nil, zero value otherwise. -func (c *CodeownersError) GetLine() int { - if c == nil || c.Line == nil { - return 0 - } - return *c.Line -} - -// GetMessage returns the Message field if it's non-nil, zero value otherwise. -func (c *CodeownersError) GetMessage() string { - if c == nil || c.Message == nil { - return "" - } - return *c.Message -} - -// GetPath returns the Path field if it's non-nil, zero value otherwise. -func (c *CodeownersError) GetPath() string { - if c == nil || c.Path == nil { - return "" - } - return *c.Path -} - -// GetSource returns the Source field if it's non-nil, zero value otherwise. -func (c *CodeownersError) GetSource() string { - if c == nil || c.Source == nil { - return "" - } - return *c.Source -} - // GetSuggestion returns the Suggestion field if it's non-nil, zero value otherwise. func (c *CodeownersError) GetSuggestion() string { if c == nil || c.Suggestion == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 0c4f2bd625c..f476c30be69 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -2699,66 +2699,6 @@ func TestCodeOfConduct_GetURL(tt *testing.T) { c.GetURL() } -func TestCodeownersError_GetColumn(tt *testing.T) { - var zeroValue int - c := &CodeownersError{Column: &zeroValue} - c.GetColumn() - c = &CodeownersError{} - c.GetColumn() - c = nil - c.GetColumn() -} - -func TestCodeownersError_GetKind(tt *testing.T) { - var zeroValue string - c := &CodeownersError{Kind: &zeroValue} - c.GetKind() - c = &CodeownersError{} - c.GetKind() - c = nil - c.GetKind() -} - -func TestCodeownersError_GetLine(tt *testing.T) { - var zeroValue int - c := &CodeownersError{Line: &zeroValue} - c.GetLine() - c = &CodeownersError{} - c.GetLine() - c = nil - c.GetLine() -} - -func TestCodeownersError_GetMessage(tt *testing.T) { - var zeroValue string - c := &CodeownersError{Message: &zeroValue} - c.GetMessage() - c = &CodeownersError{} - c.GetMessage() - c = nil - c.GetMessage() -} - -func TestCodeownersError_GetPath(tt *testing.T) { - var zeroValue string - c := &CodeownersError{Path: &zeroValue} - c.GetPath() - c = &CodeownersError{} - c.GetPath() - c = nil - c.GetPath() -} - -func TestCodeownersError_GetSource(tt *testing.T) { - var zeroValue string - c := &CodeownersError{Source: &zeroValue} - c.GetSource() - c = &CodeownersError{} - c.GetSource() - c = nil - c.GetSource() -} - func TestCodeownersError_GetSuggestion(tt *testing.T) { var zeroValue string c := &CodeownersError{Suggestion: &zeroValue}