Skip to content

#569:Partial Fix: Fix for Users Blocking #600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Apr 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
74b3db5
#514: added inviter to Invitation for orgs and teams invite
varadarajana Jan 21, 2017
4a4a270
#514: go fmt
varadarajana Jan 21, 2017
3f2f969
#514: removed prints
varadarajana Jan 21, 2017
1d7107a
#514: changed time data types and included them in test
varadarajana Jan 21, 2017
f5b2eeb
#514 Added Org List Pending Invitations
varadarajana Jan 21, 2017
112d718
#514 removed typos in error handling
varadarajana Jan 21, 2017
2a95ca6
#514: Modified to pointer time.Time in Invitation
varadarajana Jan 28, 2017
6305685
#514: Set up Naming conventions for multi words, pointer to bool
varadarajana Jan 29, 2017
1840a00
#514: multi word variable format
varadarajana Jan 29, 2017
5c4cab8
#514 : used User instead of new struct
varadarajana Jan 30, 2017
e732a04
#514 : removed Inviter struct
varadarajana Jan 30, 2017
7e8a9ee
Merge remote-tracking branch 'upstream/master'
varadarajana Jan 31, 2017
1bd4328
#514: Added quotes to list values in comment
varadarajana Jan 31, 2017
7883eb5
#569:user block: first cut
varadarajana Mar 25, 2017
8206fe8
Merge branch 'master' of https://github.com/varadarajana/go-github
varadarajana Mar 25, 2017
546b2aa
#569: merge issue
varadarajana Mar 26, 2017
11768d6
#569 : Changes to media type for user blocking APIs
varadarajana Mar 26, 2017
b5b6801
Merge remote-tracking branch 'upstream/master'
varadarajana Mar 29, 2017
6868470
#569 : changed method name to IsBlocking
varadarajana Mar 29, 2017
5cd446d
#567 : Code review comments for GIT API URL
varadarajana Mar 30, 2017
f2a91e1
Rephrase documentation to be more direct.
dmitshur Apr 8, 2017
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
3 changes: 3 additions & 0 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ const (

// https://developer.github.com/changes/2016-12-14-reviews-api/
mediaTypePullRequestReviewsPreview = "application/vnd.github.black-cat-preview+json"

// https://developer.github.com/changes/2017-02-28-user-blocking-apis-and-webhook/
mediaTypeBlockUsersPreview = "application/vnd.github.giant-sentry-fist-preview+json"
)

// A Client manages communication with the GitHub API.
Expand Down
91 changes: 91 additions & 0 deletions github/users_blocking.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2017 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"
)

// ListBlockedUsers lists all the blocked users by the authenticated user.
//
// GitHub API docs: https://developer.github.com/v3/users/blocking/#list-blocked-users
func (s *UsersService) ListBlockedUsers(ctx context.Context, opt *ListOptions) ([]*User, *Response, error) {
u := "user/blocks"
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}

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

Copy link
Member

Choose a reason for hiding this comment

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

According to the the GitHub API docs:

image

I think you're missing that preview header.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shurcooL Thank you, I missed the preview header, now it is added. Can you please review?

// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeBlockUsersPreview)

var blockedUsers []*User
resp, err := s.client.Do(ctx, req, &blockedUsers)
if err != nil {
return nil, resp, err
}

return blockedUsers, resp, nil
}

// IsBlocked reports whether specified user is blocked by the authenticated user.
//
// GitHub API docs: https://developer.github.com/v3/users/blocking/#check-whether-youve-blocked-a-user
func (s *UsersService) IsBlocked(ctx context.Context, user string) (bool, *Response, error) {
u := fmt.Sprintf("user/blocks/%v", user)

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

// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeBlockUsersPreview)

resp, err := s.client.Do(ctx, req, nil)
isBlocked, err := parseBoolResponse(err)
return isBlocked, resp, err
}

// BlockUser blocks specified user for the authenticated user.
//
// GitHub API docs: https://developer.github.com/v3/users/blocking/#block-a-user
func (s *UsersService) BlockUser(ctx context.Context, user string) (*Response, error) {
u := fmt.Sprintf("user/blocks/%v", user)

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

// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeBlockUsersPreview)

return s.client.Do(ctx, req, nil)
}

// UnblockUser unblocks specified user for the authenticated user.
//
// GitHub API docs: https://developer.github.com/v3/users/blocking/#unblock-a-user
func (s *UsersService) UnblockUser(ctx context.Context, user string) (*Response, error) {
u := fmt.Sprintf("user/blocks/%v", user)

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", mediaTypeBlockUsersPreview)

return s.client.Do(ctx, req, nil)
}
90 changes: 90 additions & 0 deletions github/users_blocking_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2017 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"
"net/http"
"reflect"
"testing"
)

func TestUsersService_ListBlockedUsers(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/user/blocks", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeBlockUsersPreview)
testFormValues(t, r, values{"page": "2"})
fmt.Fprint(w, `[{
"login": "octocat"
}]`)
})

opt := &ListOptions{Page: 2}
blockedUsers, _, err := client.Users.ListBlockedUsers(context.Background(), opt)
if err != nil {
t.Errorf("Users.ListBlockedUsers returned error: %v", err)
}

want := []*User{{Login: String("octocat")}}
if !reflect.DeepEqual(blockedUsers, want) {
t.Errorf("Users.ListBlockedUsers returned %+v, want %+v", blockedUsers, want)
}
}

func TestUsersService_IsBlocked(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/user/blocks/u", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeBlockUsersPreview)
w.WriteHeader(http.StatusNoContent)
})

isBlocked, _, err := client.Users.IsBlocked(context.Background(), "u")
if err != nil {
t.Errorf("Users.IsBlocked returned error: %v", err)
}
if want := true; isBlocked != want {
t.Errorf("Users.IsBlocked returned %+v, want %+v", isBlocked, want)
}
}

func TestUsersService_BlockUser(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/user/blocks/u", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
testHeader(t, r, "Accept", mediaTypeBlockUsersPreview)
w.WriteHeader(http.StatusNoContent)
})

_, err := client.Users.BlockUser(context.Background(), "u")
if err != nil {
t.Errorf("Users.BlockUser returned error: %v", err)
}
}

func TestUsersService_UnblockUser(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/user/blocks/u", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
testHeader(t, r, "Accept", mediaTypeBlockUsersPreview)
w.WriteHeader(http.StatusNoContent)
})

_, err := client.Users.UnblockUser(context.Background(), "u")
if err != nil {
t.Errorf("Users.UnblockUser returned error: %v", err)
}
}