Repository: google/go-github
Pull request #1090 (2019-01-04 18:49:31)

Added Interactions restrictions API for organizations
Issue : #1077 

gmlewis: Thank you, @gauntface for the review!
Merging.

Patch:
diff --git a/github/interactions_orgs.go b/github/interactions_orgs.go
new file mode 100644
index 00000000..af25f656
--- /dev/null
+++ b/github/interactions_orgs.go
@@ -0,0 +1,80 @@
+// Copyright 2019 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 (
+	"context"
+	"fmt"
+)
+
+// GetRestrictionsForOrg fetches the interaction restrictions for an organization.
+//
+// GitHub API docs: https://developer.github.com/v3/interactions/orgs/#get-interaction-restrictions-for-an-organization
+func (s *InteractionsService) GetRestrictionsForOrg(ctx context.Context, organization string) (*InteractionRestriction, *Response, error) {
+	u := fmt.Sprintf("orgs/%v/interaction-limits", organization)
+	req, err := s.client.NewRequest("GET", u, nil)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	// TODO: remove custom Accept header when this API fully launches.
+	req.Header.Set("Accept", mediaTypeInteractionRestrictionsPreview)
+
+	organizationInteractions := new(InteractionRestriction)
+
+	resp, err := s.client.Do(ctx, req, organizationInteractions)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return organizationInteractions, resp, nil
+}
+
+// UpdateRestrictionsForOrg adds or updates the interaction restrictions for an organization.
+//
+// limit specifies the group of GitHub users who can comment, open issues, or create pull requests
+// in public repositories for the given organization.
+// Possible values are: "existing_users", "contributors_only", "collaborators_only".
+//
+// GitHub API docs: https://developer.github.com/v3/interactions/orgs/#add-or-update-interaction-restrictions-for-an-organization
+func (s *InteractionsService) UpdateRestrictionsForOrg(ctx context.Context, organization, limit string) (*InteractionRestriction, *Response, error) {
+	u := fmt.Sprintf("orgs/%v/interaction-limits", organization)
+
+	interaction := &InteractionRestriction{Limit: String(limit)}
+
+	req, err := s.client.NewRequest("PUT", u, interaction)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	// TODO: remove custom Accept header when this API fully launches.
+	req.Header.Set("Accept", mediaTypeInteractionRestrictionsPreview)
+
+	organizationInteractions := new(InteractionRestriction)
+
+	resp, err := s.client.Do(ctx, req, organizationInteractions)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return organizationInteractions, resp, nil
+}
+
+// RemoveRestrictionsFromOrg removes the interaction restrictions for an organization.
+//
+// GitHub API docs: https://developer.github.com/v3/interactions/orgs/#remove-interaction-restrictions-for-an-organization
+func (s *InteractionsService) RemoveRestrictionsFromOrg(ctx context.Context, organization string) (*Response, error) {
+	u := fmt.Sprintf("orgs/%v/interaction-limits", organization)
+	req, err := s.client.NewRequest("DELETE", u, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	// TODO: remove custom Accept header when this API fully launches.
+	req.Header.Set("Accept", mediaTypeInteractionRestrictionsPreview)
+
+	return s.client.Do(ctx, req, nil)
+}
diff --git a/github/interactions_repos.go b/github/interactions_repos.go
index a91297cc..58234822 100644
--- a/github/interactions_repos.go
+++ b/github/interactions_repos.go
@@ -35,9 +35,16 @@ func (s *InteractionsService) GetRestrictionsForRepo(ctx context.Context, owner,
 
 // UpdateRestrictionsForRepo adds or updates the interaction restrictions for a repository.
 //
+// limit specifies the group of GitHub users who can comment, open issues, or create pull requests
+// for the given repository.
+// Possible values are: "existing_users", "contributors_only", "collaborators_only".
+//
 // GitHub API docs: https://developer.github.com/v3/interactions/repos/#add-or-update-interaction-restrictions-for-a-repository
-func (s *InteractionsService) UpdateRestrictionsForRepo(ctx context.Context, owner, repo string, interaction *InteractionRestriction) (*InteractionRestriction, *Response, error) {
+func (s *InteractionsService) UpdateRestrictionsForRepo(ctx context.Context, owner, repo, limit string) (*InteractionRestriction, *Response, error) {
 	u := fmt.Sprintf("repos/%v/%v/interaction-limits", owner, repo)
+
+	interaction := &InteractionRestriction{Limit: String(limit)}
+
 	req, err := s.client.NewRequest("PUT", u, interaction)
 	if err != nil {
 		return nil, nil, err


Test patch:
diff --git a/github/interactions_orgs_test.go b/github/interactions_orgs_test.go
new file mode 100644
index 00000000..88b7ff6f
--- /dev/null
+++ b/github/interactions_orgs_test.go
@@ -0,0 +1,80 @@
+// Copyright 2019 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 (
+	"context"
+	"encoding/json"
+	"fmt"
+	"net/http"
+	"reflect"
+	"testing"
+)
+
+func TestInteractionsService_GetRestrictionsForOrgs(t *testing.T) {
+	client, mux, _, teardown := setup()
+	defer teardown()
+
+	mux.HandleFunc("/orgs/o/interaction-limits", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "GET")
+		testHeader(t, r, "Accept", mediaTypeInteractionRestrictionsPreview)
+		fmt.Fprint(w, `{"origin":"organization"}`)
+	})
+
+	organizationInteractions, _, err := client.Interactions.GetRestrictionsForOrg(context.Background(), "o")
+	if err != nil {
+		t.Errorf("Interactions.GetRestrictionsForOrg returned error: %v", err)
+	}
+
+	want := &InteractionRestriction{Origin: String("organization")}
+	if !reflect.DeepEqual(organizationInteractions, want) {
+		t.Errorf("Interactions.GetRestrictionsForOrg returned %+v, want %+v", organizationInteractions, want)
+	}
+}
+
+func TestInteractionsService_UpdateRestrictionsForOrg(t *testing.T) {
+	client, mux, _, teardown := setup()
+	defer teardown()
+
+	input := &InteractionRestriction{Limit: String("existing_users")}
+
+	mux.HandleFunc("/orgs/o/interaction-limits", func(w http.ResponseWriter, r *http.Request) {
+		v := new(InteractionRestriction)
+		json.NewDecoder(r.Body).Decode(v)
+
+		testMethod(t, r, "PUT")
+		testHeader(t, r, "Accept", mediaTypeInteractionRestrictionsPreview)
+		if !reflect.DeepEqual(v, input) {
+			t.Errorf("Request body = %+v, want %+v", v, input)
+		}
+		fmt.Fprint(w, `{"origin":"organization"}`)
+	})
+
+	organizationInteractions, _, err := client.Interactions.UpdateRestrictionsForOrg(context.Background(), "o", input.GetLimit())
+	if err != nil {
+		t.Errorf("Interactions.UpdateRestrictionsForOrg returned error: %v", err)
+	}
+
+	want := &InteractionRestriction{Origin: String("organization")}
+	if !reflect.DeepEqual(organizationInteractions, want) {
+		t.Errorf("Interactions.UpdateRestrictionsForOrg returned %+v, want %+v", organizationInteractions, want)
+	}
+}
+
+func TestInteractionsService_RemoveRestrictionsFromOrg(t *testing.T) {
+	client, mux, _, teardown := setup()
+	defer teardown()
+
+	mux.HandleFunc("/orgs/o/interaction-limits", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "DELETE")
+		testHeader(t, r, "Accept", mediaTypeInteractionRestrictionsPreview)
+	})
+
+	_, err := client.Interactions.RemoveRestrictionsFromOrg(context.Background(), "o")
+	if err != nil {
+		t.Errorf("Interactions.RemoveRestrictionsFromOrg returned error: %v", err)
+	}
+}
diff --git a/github/interactions_repos_test.go b/github/interactions_repos_test.go
index 47ac89c6..80857ae0 100644
--- a/github/interactions_repos_test.go
+++ b/github/interactions_repos_test.go
@@ -53,7 +53,7 @@ func TestInteractionsService_UpdateRestrictionsForRepo(t *testing.T) {
 		fmt.Fprint(w, `{"origin":"repository"}`)
 	})
 
-	repoInteractions, _, err := client.Interactions.UpdateRestrictionsForRepo(context.Background(), "o", "r", input)
+	repoInteractions, _, err := client.Interactions.UpdateRestrictionsForRepo(context.Background(), "o", "r", input.GetLimit())
 	if err != nil {
 		t.Errorf("Interactions.UpdateRestrictionsForRepo returned error: %v", err)
 	}