Skip to content

Commit

Permalink
Move AddEnabledReposInOrg to SetEnabledReposInOrg
Browse files Browse the repository at this point in the history
Switch from Add to Set in method name since the behaviour is to set the
list of enabled repos absolutely.

As a replacement added the real AddEnabledReposInOrg by implementing:
https://docs.github.com/en/rest/reference/actions#enable-a-selected-repository-for-github-actions-in-an-organization
which adds just a single entry to the list.
  • Loading branch information
FloThinksPi committed Jul 14, 2021
1 parent 6a10404 commit 8351b36
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
23 changes: 21 additions & 2 deletions github/actions_runners.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ func (s *ActionsService) ListEnabledReposInOrg(ctx context.Context, owner string
return repos, resp, nil
}

// AddEnabledReposInOrg adds a list of repositories to the list of enabled repos for GitHub Actions in an organization.
// SetEnabledReposInOrg replaces the list of selected repositories that are enabled for GitHub Actions in an organization..
//
// GitHub API docs: https://docs.github.com/en/rest/reference/actions#set-selected-repositories-enabled-for-github-actions-in-an-organization
func (s *ActionsService) AddEnabledReposInOrg(ctx context.Context, owner string, repositoryIDs []int64) (*Response, error) {
func (s *ActionsService) SetEnabledReposInOrg(ctx context.Context, owner string, repositoryIDs []int64) (*Response, error) {
u := fmt.Sprintf("orgs/%v/actions/permissions/repositories", owner)

req, err := s.client.NewRequest("PUT", u, struct {
Expand All @@ -285,6 +285,25 @@ func (s *ActionsService) AddEnabledReposInOrg(ctx context.Context, owner string,
return resp, nil
}

// AddEnabledReposInOrg adds a repository to the list of selected repositories that are enabled for GitHub Actions in an organization.
//
// GitHub API docs: https://docs.github.com/en/rest/reference/actions#enable-a-selected-repository-for-github-actions-in-an-organization
func (s *ActionsService) AddEnabledReposInOrg(ctx context.Context, owner string, repositoryID int64) (*Response, error) {
u := fmt.Sprintf("orgs/%v/actions/permissions/repositories/%v", owner, repositoryID)

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

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

return resp, nil
}

// RemoveEnabledRepoInOrg removes a single repository from the list of enabled repos for GitHub Actions in an organization.
//
// GitHub API docs: https://docs.github.com/en/rest/reference/actions#disable-a-selected-repository-for-github-actions-in-an-organization
Expand Down
37 changes: 32 additions & 5 deletions github/actions_runners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,32 +401,59 @@ func TestActionsService_ListEnabledReposInOrg(t *testing.T) {
})
}

func TestActionsService_AddEnabledReposInOrg(t *testing.T) {
func TestActionsService_SetEnabledReposInOrg(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/orgs/o/actions/permissions/repositories", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
testHeader(t, r, "Content-Type", "application/json")
testBody(t, r, `{"repository_id":[123,1234]}`+"\n")
testBody(t, r, `{"selected_repository_ids":[123,1234]}`+"\n")
w.WriteHeader(http.StatusNoContent)
})

ctx := context.Background()
_, err := client.Actions.SetEnabledReposInOrg(ctx, "o", []int64{123, 1234})
if err != nil {
t.Errorf("Actions.SetEnabledReposInOrg returned error: %v", err)
}

const methodName = "SetEnabledReposInOrg"

testBadOptions(t, methodName, func() (err error) {
_, err = client.Actions.SetEnabledReposInOrg(ctx, "\n", []int64{123, 1234})
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Actions.SetEnabledReposInOrg(ctx, "o", []int64{123, 1234})
})
}

func TestActionsService_AddEnabledReposInOrg(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/orgs/o/actions/permissions/repositories/123", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
w.WriteHeader(http.StatusNoContent)
})

ctx := context.Background()
_, err := client.Actions.AddEnabledReposInOrg(ctx, "o", []int64{123, 1234})
_, err := client.Actions.AddEnabledReposInOrg(ctx, "o", 123)
if err != nil {
t.Errorf("Actions.AddEnabledReposInOrg returned error: %v", err)
}

const methodName = "AddEnabledReposInOrg"

testBadOptions(t, methodName, func() (err error) {
_, err = client.Actions.AddEnabledReposInOrg(ctx, "\n", []int64{123, 1234})
_, err = client.Actions.AddEnabledReposInOrg(ctx, "\n", 123)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Actions.AddEnabledReposInOrg(ctx, "o", []int64{123, 1234})
return client.Actions.AddEnabledReposInOrg(ctx, "o", 123)
})
}

Expand Down

0 comments on commit 8351b36

Please sign in to comment.