Skip to content
Open
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
59 changes: 59 additions & 0 deletions github/activity_star.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,65 @@ func (s *ActivityService) ListStargazers(ctx context.Context, owner, repo string
return stargazers, resp, nil
}

// StargazerCount represents the number of users who have starred a repository.
type StargazerCount struct {
Count int `json:"count"`
}

// GetStargazerCount gets the number of people who have starred the specified repo.
//
// GitHub API docs: https://docs.github.com/rest/activity/starring?apiVersion=2022-11-28#get-stargazer-count
//
//meta:operation GET /repos/{owner}/{repo}/stargazers/count
func (s *ActivityService) GetStargazerCount(ctx context.Context, owner, repo string) (*StargazerCount, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/stargazers/count", owner, repo)
req, err := s.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return nil, nil, err
}

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

return count, resp, nil
}

// StargazerHistory represents the stars a repository received during one week.
type StargazerHistory struct {
Days []int `json:"days"`
Total int `json:"total"`
Week Timestamp `json:"week"`
}

// ListStargazerHistory lists the stars of the specified repo grouped by week.
//
// GitHub API docs: https://docs.github.com/rest/activity/starring?apiVersion=2022-11-28#get-repository-star-history
//
//meta:operation GET /repos/{owner}/{repo}/stargazers/history
func (s *ActivityService) ListStargazerHistory(ctx context.Context, owner, repo string, opts *ListOptions) ([]*StargazerHistory, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/stargazers/history", owner, repo)
u, err := addOptions(u, opts)
if err != nil {
return nil, nil, err
}

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

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

return history, resp, nil
}

// ActivityListStarredOptions specifies the optional parameters to the
// ActivityService.ListStarred method.
type ActivityListStarredOptions struct {
Expand Down
80 changes: 80 additions & 0 deletions github/activity_star_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,86 @@ func TestActivityService_ListStargazers(t *testing.T) {
})
}

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

mux.HandleFunc("/repos/o/r/stargazers/count", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"count":42}`)
})

ctx := t.Context()
count, _, err := client.Activity.GetStargazerCount(ctx, "o", "r")
if err != nil {
t.Errorf("Activity.GetStargazerCount returned error: %v", err)
}

want := &StargazerCount{Count: 42}
if !cmp.Equal(count, want) {
t.Errorf("Activity.GetStargazerCount returned %+v, want %+v", count, want)
}

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

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Activity.GetStargazerCount(ctx, "o", "r")
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

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

mux.HandleFunc("/repos/o/r/stargazers/history", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"page": "2",
"per_page": "30",
})

fmt.Fprint(w, `[{"week":`+referenceUnixTimeStr+`,"total":19,"days":[0,12,7,0,0,0,0]}]`)
})

ctx := t.Context()
opts := &ListOptions{Page: 2, PerPage: 30}
history, _, err := client.Activity.ListStargazerHistory(ctx, "o", "r", opts)
if err != nil {
t.Errorf("Activity.ListStargazerHistory returned error: %v", err)
}

want := []*StargazerHistory{{
Days: []int{0, 12, 7, 0, 0, 0, 0},
Total: 19,
Week: referenceTimestamp,
}}
if !cmp.Equal(history, want) {
t.Errorf("Activity.ListStargazerHistory returned %+v, want %+v", history, want)
}

const methodName = "ListStargazerHistory"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Activity.ListStargazerHistory(ctx, "\n", "\n", opts)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Activity.ListStargazerHistory(ctx, "o", "r", opts)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestActivityService_ListStarred_authenticatedUser(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.

35 changes: 35 additions & 0 deletions github/github-accessors_test.go

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

31 changes: 31 additions & 0 deletions github/github-iterators.go

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

72 changes: 72 additions & 0 deletions github/github-iterators_test.go

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

Loading