Skip to content

Commit 5d3c7cb

Browse files
authored
Support Preview Hovercard API (#973)
Fixes #875. Closes #890.
1 parent 93e3f3a commit 5d3c7cb

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

github/github-accessors.go

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github.go

+3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ const (
111111
// https://developer.github.com/changes/2018-02-07-team-discussions-api/
112112
mediaTypeTeamDiscussionsPreview = "application/vnd.github.echo-preview+json"
113113

114+
// https://developer.github.com/changes/2018-03-21-hovercard-api-preview/
115+
mediaTypeHovercardPreview = "application/vnd.github.hagar-preview+json"
116+
114117
// https://developer.github.com/changes/2018-01-10-lock-reason-api-preview/
115118
mediaTypeLockReasonPreview = "application/vnd.github.sailor-v-preview+json"
116119

github/users.go

+50
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,56 @@ func (s *UsersService) Edit(ctx context.Context, user *User) (*User, *Response,
135135
return uResp, resp, nil
136136
}
137137

138+
// HovercardOptions specifies optional parameters to the UsersService.GetHovercard
139+
// method.
140+
type HovercardOptions struct {
141+
// SubjectType specifies the additional information to be received about the hovercard.
142+
// Possible values are: organization, repository, issue, pull_request. (Required when using subject_id.)
143+
SubjectType string `url:"subject_type"`
144+
145+
// SubjectID specifies the ID for the SubjectType. (Required when using subject_type.)
146+
SubjectID string `url:"subject_id"`
147+
}
148+
149+
// Hovercard represents hovercard information about a user.
150+
type Hovercard struct {
151+
Contexts []*UserContext `json:"contexts,omitempty"`
152+
}
153+
154+
// UserContext represents the contextual information about user.
155+
type UserContext struct {
156+
Message *string `json:"message,omitempty"`
157+
Octicon *string `json:"octicon,omitempty"`
158+
}
159+
160+
// GetHovercard fetches contextual information about user. It requires authentication
161+
// via Basic Auth or via OAuth with the repo scope.
162+
//
163+
// GitHub API docs: https://developer.github.com/v3/users/#get-contextual-information-about-a-user
164+
func (s *UsersService) GetHovercard(ctx context.Context, user string, opt *HovercardOptions) (*Hovercard, *Response, error) {
165+
u := fmt.Sprintf("users/%v/hovercard", user)
166+
u, err := addOptions(u, opt)
167+
if err != nil {
168+
return nil, nil, err
169+
}
170+
171+
req, err := s.client.NewRequest("GET", u, nil)
172+
if err != nil {
173+
return nil, nil, err
174+
}
175+
176+
// TODO: remove custom Accept header when this API fully launches.
177+
req.Header.Set("Accept", mediaTypeHovercardPreview)
178+
179+
hc := new(Hovercard)
180+
resp, err := s.client.Do(ctx, req, hc)
181+
if err != nil {
182+
return nil, resp, err
183+
}
184+
185+
return hc, resp, nil
186+
}
187+
138188
// UserListOptions specifies optional parameters to the UsersService.ListAll
139189
// method.
140190
type UserListOptions struct {

github/users_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,29 @@ func TestUsersService_Edit(t *testing.T) {
153153
}
154154
}
155155

156+
func TestUsersService_GetHovercard(t *testing.T) {
157+
client, mux, _, teardown := setup()
158+
defer teardown()
159+
160+
mux.HandleFunc("/users/u/hovercard", func(w http.ResponseWriter, r *http.Request) {
161+
testMethod(t, r, "GET")
162+
testHeader(t, r, "Accept", mediaTypeHovercardPreview)
163+
testFormValues(t, r, values{"subject_type": "repository", "subject_id": "20180408"})
164+
fmt.Fprint(w, `{"contexts": [{"message":"Owns this repository", "octicon": "repo"}]}`)
165+
})
166+
167+
opt := &HovercardOptions{SubjectType: "repository", SubjectID: "20180408"}
168+
hovercard, _, err := client.Users.GetHovercard(context.Background(), "u", opt)
169+
if err != nil {
170+
t.Errorf("Users.GetHovercard returned error: %v", err)
171+
}
172+
173+
want := &Hovercard{Contexts: []*UserContext{{Message: String("Owns this repository"), Octicon: String("repo")}}}
174+
if !reflect.DeepEqual(hovercard, want) {
175+
t.Errorf("Users.GetHovercard returned %+v, want %+v", hovercard, want)
176+
}
177+
}
178+
156179
func TestUsersService_ListAll(t *testing.T) {
157180
client, mux, _, teardown := setup()
158181
defer teardown()

0 commit comments

Comments
 (0)