repos_community_health.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2017 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. package github
  6. import (
  7. "context"
  8. "fmt"
  9. "time"
  10. )
  11. // Metric represents the different fields for one file in community health files.
  12. type Metric struct {
  13. Name *string `json:"name"`
  14. Key *string `json:"key"`
  15. URL *string `json:"url"`
  16. HTMLURL *string `json:"html_url"`
  17. }
  18. // CommunityHealthFiles represents the different files in the community health metrics response.
  19. type CommunityHealthFiles struct {
  20. CodeOfConduct *Metric `json:"code_of_conduct"`
  21. Contributing *Metric `json:"contributing"`
  22. License *Metric `json:"license"`
  23. Readme *Metric `json:"readme"`
  24. }
  25. // CommunityHealthMetrics represents a response containing the community metrics of a repository.
  26. type CommunityHealthMetrics struct {
  27. HealthPercentage *int `json:"health_percentage"`
  28. Files *CommunityHealthFiles `json:"files"`
  29. UpdatedAt *time.Time `json:"updated_at"`
  30. }
  31. // GetCommunityHealthMetrics retrieves all the community health metrics for a repository.
  32. //
  33. // GitHub API docs: https://developer.github.com/v3/repos/community/#retrieve-community-health-metrics
  34. func (s *RepositoriesService) GetCommunityHealthMetrics(ctx context.Context, owner, repo string) (*CommunityHealthMetrics, *Response, error) {
  35. u := fmt.Sprintf("repos/%v/%v/community/profile", owner, repo)
  36. req, err := s.client.NewRequest("GET", u, nil)
  37. if err != nil {
  38. return nil, nil, err
  39. }
  40. // TODO: remove custom Accept header when this API fully launches.
  41. req.Header.Set("Accept", mediaTypeRepositoryCommunityHealthMetricsPreview)
  42. metrics := &CommunityHealthMetrics{}
  43. resp, err := s.client.Do(ctx, req, metrics)
  44. if err != nil {
  45. return nil, resp, err
  46. }
  47. return metrics, resp, nil
  48. }