| 1 |
// Copyright 2016 The go-github AUTHORS. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
package github |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"fmt" |
| 11 |
) |
| 12 |
|
| 13 |
// TrafficReferrer represent information about traffic from a referrer . |
| 14 |
type TrafficReferrer struct { |
| 15 |
Referrer *string `json:"referrer,omitempty"` |
| 16 |
Count *int `json:"count,omitempty"` |
| 17 |
Uniques *int `json:"uniques,omitempty"` |
| 18 |
} |
| 19 |
|
| 20 |
// TrafficPath represent information about the traffic on a path of the repo. |
| 21 |
type TrafficPath struct { |
| 22 |
Path *string `json:"path,omitempty"` |
| 23 |
Title *string `json:"title,omitempty"` |
| 24 |
Count *int `json:"count,omitempty"` |
| 25 |
Uniques *int `json:"uniques,omitempty"` |
| 26 |
} |
| 27 |
|
| 28 |
// TrafficData represent information about a specific timestamp in views or clones list. |
| 29 |
type TrafficData struct { |
| 30 |
Timestamp *Timestamp `json:"timestamp,omitempty"` |
| 31 |
Count *int `json:"count,omitempty"` |
| 32 |
Uniques *int `json:"uniques,omitempty"` |
| 33 |
} |
| 34 |
|
| 35 |
// TrafficViews represent information about the number of views in the last 14 days. |
| 36 |
type TrafficViews struct { |
| 37 |
Views []*TrafficData `json:"views,omitempty"` |
| 38 |
Count *int `json:"count,omitempty"` |
| 39 |
Uniques *int `json:"uniques,omitempty"` |
| 40 |
} |
| 41 |
|
| 42 |
// TrafficClones represent information about the number of clones in the last 14 days. |
| 43 |
type TrafficClones struct { |
| 44 |
Clones []*TrafficData `json:"clones,omitempty"` |
| 45 |
Count *int `json:"count,omitempty"` |
| 46 |
Uniques *int `json:"uniques,omitempty"` |
| 47 |
} |
| 48 |
|
| 49 |
// TrafficBreakdownOptions specifies the parameters to methods that support breakdown per day or week. |
| 50 |
// Can be one of: day, week. Default: day. |
| 51 |
type TrafficBreakdownOptions struct { |
| 52 |
Per string `url:"per,omitempty"` |
| 53 |
} |
| 54 |
|
| 55 |
// ListTrafficReferrers list the top 10 referrers over the last 14 days. |
| 56 |
// |
| 57 |
// GitHub API docs: https://docs.github.com/rest/metrics/traffic#get-top-referral-sources |
| 58 |
// |
| 59 |
//meta:operation GET /repos/{owner}/{repo}/traffic/popular/referrers |
| 60 |
func (s *RepositoriesService) ListTrafficReferrers(ctx context.Context, owner, repo string) ([]*TrafficReferrer, *Response, error) { |
| 61 |
u := fmt.Sprintf("repos/%v/%v/traffic/popular/referrers", owner, repo) |
| 62 |
|
| 63 |
req, err := s.client.NewRequest("GET", u, nil) |
| 64 |
if err != nil { |
| 65 |
return nil, nil, err |
| 66 |
} |
| 67 |
|
| 68 |
var trafficReferrers []*TrafficReferrer |
| 69 |
resp, err := s.client.Do(ctx, req, &trafficReferrers) |
| 70 |
if err != nil { |
| 71 |
return nil, resp, err |
| 72 |
} |
| 73 |
|
| 74 |
return trafficReferrers, resp, nil |
| 75 |
} |
| 76 |
|
| 77 |
// ListTrafficPaths list the top 10 popular content over the last 14 days. |
| 78 |
// |
| 79 |
// GitHub API docs: https://docs.github.com/rest/metrics/traffic#get-top-referral-paths |
| 80 |
// |
| 81 |
//meta:operation GET /repos/{owner}/{repo}/traffic/popular/paths |
| 82 |
func (s *RepositoriesService) ListTrafficPaths(ctx context.Context, owner, repo string) ([]*TrafficPath, *Response, error) { |
| 83 |
u := fmt.Sprintf("repos/%v/%v/traffic/popular/paths", owner, repo) |
| 84 |
|
| 85 |
req, err := s.client.NewRequest("GET", u, nil) |
| 86 |
if err != nil { |
| 87 |
return nil, nil, err |
| 88 |
} |
| 89 |
|
| 90 |
var paths []*TrafficPath |
| 91 |
resp, err := s.client.Do(ctx, req, &paths) |
| 92 |
if err != nil { |
| 93 |
return nil, resp, err |
| 94 |
} |
| 95 |
|
| 96 |
return paths, resp, nil |
| 97 |
} |
| 98 |
|
| 99 |
// ListTrafficViews get total number of views for the last 14 days and breaks it down either per day or week. |
| 100 |
// |
| 101 |
// GitHub API docs: https://docs.github.com/rest/metrics/traffic#get-page-views |
| 102 |
// |
| 103 |
//meta:operation GET /repos/{owner}/{repo}/traffic/views |
| 104 |
func (s *RepositoriesService) ListTrafficViews(ctx context.Context, owner, repo string, opts *TrafficBreakdownOptions) (*TrafficViews, *Response, error) { |
| 105 |
u := fmt.Sprintf("repos/%v/%v/traffic/views", owner, repo) |
| 106 |
u, err := addOptions(u, opts) |
| 107 |
if err != nil { |
| 108 |
return nil, nil, err |
| 109 |
} |
| 110 |
|
| 111 |
req, err := s.client.NewRequest("GET", u, nil) |
| 112 |
if err != nil { |
| 113 |
return nil, nil, err |
| 114 |
} |
| 115 |
|
| 116 |
trafficViews := new(TrafficViews) |
| 117 |
resp, err := s.client.Do(ctx, req, &trafficViews) |
| 118 |
if err != nil { |
| 119 |
return nil, resp, err |
| 120 |
} |
| 121 |
|
| 122 |
return trafficViews, resp, nil |
| 123 |
} |
| 124 |
|
| 125 |
// ListTrafficClones get total number of clones for the last 14 days and breaks it down either per day or week for the last 14 days. |
| 126 |
// |
| 127 |
// GitHub API docs: https://docs.github.com/rest/metrics/traffic#get-repository-clones |
| 128 |
// |
| 129 |
//meta:operation GET /repos/{owner}/{repo}/traffic/clones |
| 130 |
func (s *RepositoriesService) ListTrafficClones(ctx context.Context, owner, repo string, opts *TrafficBreakdownOptions) (*TrafficClones, *Response, error) { |
| 131 |
u := fmt.Sprintf("repos/%v/%v/traffic/clones", owner, repo) |
| 132 |
u, err := addOptions(u, opts) |
| 133 |
if err != nil { |
| 134 |
return nil, nil, err |
| 135 |
} |
| 136 |
|
| 137 |
req, err := s.client.NewRequest("GET", u, nil) |
| 138 |
if err != nil { |
| 139 |
return nil, nil, err |
| 140 |
} |
| 141 |
|
| 142 |
trafficClones := new(TrafficClones) |
| 143 |
resp, err := s.client.Do(ctx, req, &trafficClones) |
| 144 |
if err != nil { |
| 145 |
return nil, resp, err |
| 146 |
} |
| 147 |
|
| 148 |
return trafficClones, resp, nil |
| 149 |
} |