Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 100 additions & 6 deletions github/copilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,25 +712,53 @@ func (s *CopilotService) DisableOrganizationCodingAgentRepository(ctx context.Co
return s.client.Do(req, nil)
}

// CopilotOrganizationContentExclusionDetails lists all Copilot content exclusion
// rules for an organization, keyed by repository full name. Each value is the
// list of file paths excluded from Copilot for that repository.
type CopilotOrganizationContentExclusionDetails map[string][]string
// CopilotContentExclusionDetails lists Copilot content exclusion path rules,
// keyed by repository identifier. Each value is the list of file paths excluded
// from Copilot for that repository.
type CopilotContentExclusionDetails map[string][]string

// CopilotSetContentExclusionDetailsRequest represents the Copilot content exclusion
// rules to set, keyed by repository identifier.
type CopilotSetContentExclusionDetailsRequest map[string][]*CopilotContentExclusionRule

// CopilotContentExclusionRule represents a single Copilot content exclusion rule.
// Exactly one of Path, IfAnyMatch, or IfNoneMatch should be set.
type CopilotContentExclusionRule struct {
Path *string `json:"-"`
IfAnyMatch []string `json:"ifAnyMatch,omitempty"`
IfNoneMatch []string `json:"ifNoneMatch,omitempty"`
}

// MarshalJSON implements the json.Marshaler interface.
// A rule with Path set is encoded as a plain string.
func (r CopilotContentExclusionRule) MarshalJSON() ([]byte, error) {
if r.Path != nil {
return json.Marshal(*r.Path)
}
type alias CopilotContentExclusionRule
return json.Marshal(alias(r))
}

// CopilotSetContentExclusionResponse represents the response from setting
// Copilot content exclusion rules.
type CopilotSetContentExclusionResponse struct {
Message *string `json:"message,omitempty"`
}

// GetOrganizationContentExclusionDetails gets the Copilot content exclusion rules for an organization.
//
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-content-exclusion-management?apiVersion=2022-11-28#get-copilot-content-exclusion-rules-for-an-organization
//
//meta:operation GET /orgs/{org}/copilot/content_exclusion
func (s *CopilotService) GetOrganizationContentExclusionDetails(ctx context.Context, org string) (CopilotOrganizationContentExclusionDetails, *Response, error) {
func (s *CopilotService) GetOrganizationContentExclusionDetails(ctx context.Context, org string) (CopilotContentExclusionDetails, *Response, error) {
u := fmt.Sprintf("orgs/%v/copilot/content_exclusion", org)

req, err := s.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return nil, nil, err
}

details := CopilotOrganizationContentExclusionDetails{}
details := CopilotContentExclusionDetails{}
resp, err := s.client.Do(req, &details)
if err != nil {
return nil, resp, err
Expand All @@ -739,6 +767,72 @@ func (s *CopilotService) GetOrganizationContentExclusionDetails(ctx context.Cont
return details, resp, nil
}

// SetOrganizationContentExclusionDetails sets the Copilot content exclusion rules for an organization.
//
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-content-exclusion-management?apiVersion=2022-11-28#set-copilot-content-exclusion-rules-for-an-organization
//
//meta:operation PUT /orgs/{org}/copilot/content_exclusion
func (s *CopilotService) SetOrganizationContentExclusionDetails(ctx context.Context, org string, body CopilotSetContentExclusionDetailsRequest) (*CopilotSetContentExclusionResponse, *Response, error) {
u := fmt.Sprintf("orgs/%v/copilot/content_exclusion", org)

req, err := s.client.NewRequest(ctx, "PUT", u, body)
if err != nil {
return nil, nil, err
}

var result *CopilotSetContentExclusionResponse
resp, err := s.client.Do(req, &result)
if err != nil {
return nil, resp, err
}

return result, resp, nil
}

// GetEnterpriseContentExclusionDetails gets the Copilot content exclusion rules for an enterprise.
//
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/copilot/copilot-content-exclusion-management?apiVersion=2022-11-28#get-copilot-content-exclusion-rules-for-an-enterprise
//
//meta:operation GET /enterprises/{enterprise}/copilot/content_exclusion
func (s *CopilotService) GetEnterpriseContentExclusionDetails(ctx context.Context, enterprise string) (CopilotContentExclusionDetails, *Response, error) {
u := fmt.Sprintf("enterprises/%v/copilot/content_exclusion", enterprise)

req, err := s.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return nil, nil, err
}

details := CopilotContentExclusionDetails{}
resp, err := s.client.Do(req, &details)
if err != nil {
return nil, resp, err
}

return details, resp, nil
}

// SetEnterpriseContentExclusionDetails sets the Copilot content exclusion rules for an enterprise.
//
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/copilot/copilot-content-exclusion-management?apiVersion=2022-11-28#set-copilot-content-exclusion-rules-for-an-enterprise
//
//meta:operation PUT /enterprises/{enterprise}/copilot/content_exclusion
func (s *CopilotService) SetEnterpriseContentExclusionDetails(ctx context.Context, enterprise string, body CopilotSetContentExclusionDetailsRequest) (*CopilotSetContentExclusionResponse, *Response, error) {
u := fmt.Sprintf("enterprises/%v/copilot/content_exclusion", enterprise)

req, err := s.client.NewRequest(ctx, "PUT", u, body)
if err != nil {
return nil, nil, err
}

var result *CopilotSetContentExclusionResponse
resp, err := s.client.Do(req, &result)
if err != nil {
return nil, resp, err
}

return result, resp, nil
}

// AddCopilotTeams adds teams to the Copilot for Business subscription for an organization.
//
// GitHub API docs: https://docs.github.com/rest/copilot/copilot-user-management?apiVersion=2022-11-28#add-teams-to-the-copilot-subscription-for-an-organization
Expand Down
167 changes: 166 additions & 1 deletion github/copilot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,7 @@ func TestCopilotService_GetOrganizationContentExclusionDetails(t *testing.T) {
t.Errorf("Copilot.GetOrganizationContentExclusionDetails returned error: %v", err)
}

want := CopilotOrganizationContentExclusionDetails{
want := CopilotContentExclusionDetails{
"octo-repo": {"/src/some-dir/kernel.rs"},
"octo-repo-2": {"/docs/secret.md", "**/*.env"},
}
Expand All @@ -1270,6 +1270,171 @@ func TestCopilotService_GetOrganizationContentExclusionDetails(t *testing.T) {
})
}

func TestCopilotContentExclusionRule_Marshal(t *testing.T) {
t.Parallel()

tests := []struct {
name string
rule CopilotContentExclusionRule
want string
}{
{
name: "path",
rule: CopilotContentExclusionRule{Path: new("/src/some-dir/kernel.rs")},
want: `"/src/some-dir/kernel.rs"`,
},
{
name: "ifAnyMatch",
rule: CopilotContentExclusionRule{IfAnyMatch: []string{"*.env"}},
want: `{"ifAnyMatch":["*.env"]}`,
},
{
name: "ifNoneMatch",
rule: CopilotContentExclusionRule{IfNoneMatch: []string{"*.go", "*.md"}},
want: `{"ifNoneMatch":["*.go","*.md"]}`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testJSONMarshalOnly(t, tt.rule, tt.want)
})
}
}
Comment on lines +1273 to +1304

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please write table-driven tests?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 93f28b8 — converted to a table-driven test with subtests. Thanks!


func TestCopilotService_SetOrganizationContentExclusionDetails(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

body := CopilotSetContentExclusionDetailsRequest{
"octo-repo": {
{Path: new("/src/some-dir/kernel.rs")},
{IfAnyMatch: []string{"*.env"}},
},
}

mux.HandleFunc("/orgs/o/copilot/content_exclusion", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
testJSONBody(t, r, body)
fmt.Fprint(w, `{"message": "Content exclusion rules updated successfully."}`)
})

ctx := t.Context()
got, _, err := client.Copilot.SetOrganizationContentExclusionDetails(ctx, "o", body)
if err != nil {
t.Errorf("Copilot.SetOrganizationContentExclusionDetails returned error: %v", err)
}

want := &CopilotSetContentExclusionResponse{
Message: new("Content exclusion rules updated successfully."),
}
if !cmp.Equal(got, want) {
t.Errorf("Copilot.SetOrganizationContentExclusionDetails returned %+v, want %+v", got, want)
}

const methodName = "SetOrganizationContentExclusionDetails"

testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Copilot.SetOrganizationContentExclusionDetails(ctx, "\n", body)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Copilot.SetOrganizationContentExclusionDetails(ctx, "o", body)
if got != nil {
t.Errorf("Copilot.SetOrganizationContentExclusionDetails returned %+v, want nil", got)
}
return resp, err
})
}

func TestCopilotService_GetEnterpriseContentExclusionDetails(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

mux.HandleFunc("/enterprises/e/copilot/content_exclusion", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{
"[email protected]:*/copilot": ["/__tests__/**"],
"octo-org/octo-repo": ["/src/some-dir/kernel.rs"]
}`)
})

ctx := t.Context()
got, _, err := client.Copilot.GetEnterpriseContentExclusionDetails(ctx, "e")
if err != nil {
t.Errorf("Copilot.GetEnterpriseContentExclusionDetails returned error: %v", err)
}

want := CopilotContentExclusionDetails{
"[email protected]:*/copilot": {"/__tests__/**"},
"octo-org/octo-repo": {"/src/some-dir/kernel.rs"},
}
if !cmp.Equal(got, want) {
t.Errorf("Copilot.GetEnterpriseContentExclusionDetails returned %+v, want %+v", got, want)
}

const methodName = "GetEnterpriseContentExclusionDetails"

testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Copilot.GetEnterpriseContentExclusionDetails(ctx, "\n")
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Copilot.GetEnterpriseContentExclusionDetails(ctx, "e")
if got != nil {
t.Errorf("Copilot.GetEnterpriseContentExclusionDetails returned %+v, want nil", got)
}
return resp, err
})
}

func TestCopilotService_SetEnterpriseContentExclusionDetails(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

body := CopilotSetContentExclusionDetailsRequest{
"[email protected]:*/copilot": {{Path: new("/__tests__/**")}},
"octo-org/octo-repo": {{IfNoneMatch: []string{"*.go"}}},
}

mux.HandleFunc("/enterprises/e/copilot/content_exclusion", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
testJSONBody(t, r, body)
fmt.Fprint(w, `{"message": "Content exclusion rules updated successfully."}`)
})

ctx := t.Context()
got, _, err := client.Copilot.SetEnterpriseContentExclusionDetails(ctx, "e", body)
if err != nil {
t.Errorf("Copilot.SetEnterpriseContentExclusionDetails returned error: %v", err)
}

want := &CopilotSetContentExclusionResponse{
Message: new("Content exclusion rules updated successfully."),
}
if !cmp.Equal(got, want) {
t.Errorf("Copilot.SetEnterpriseContentExclusionDetails returned %+v, want %+v", got, want)
}

const methodName = "SetEnterpriseContentExclusionDetails"

testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Copilot.SetEnterpriseContentExclusionDetails(ctx, "\n", body)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Copilot.SetEnterpriseContentExclusionDetails(ctx, "e", body)
if got != nil {
t.Errorf("Copilot.SetEnterpriseContentExclusionDetails returned %+v, want nil", got)
}
return resp, err
})
}

func TestCopilotService_AddCopilotTeams(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)
Expand Down
32 changes: 32 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading