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