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	License       *Metric `json:"license"`
27	Readme        *Metric `json:"readme"`
28}
29
30// CommunityHealthMetrics represents a response containing the community metrics of a repository.
31type CommunityHealthMetrics struct {
32	HealthPercentage *int                  `json:"health_percentage"`
33	Files            *CommunityHealthFiles `json:"files"`
34	UpdatedAt        *time.Time            `json:"updated_at"`
35}
36
37// GetCommunityHealthMetrics retrieves all the community health  metrics for a  repository.
38//
39// GitHub API docs: https://developer.github.com/v3/repos/community/#retrieve-community-health-metrics
40func (s *RepositoriesService) GetCommunityHealthMetrics(ctx context.Context, owner, repo string) (*CommunityHealthMetrics, *Response, error) {
41	u := fmt.Sprintf("repos/%v/%v/community/profile", owner, repo)
42	req, err := s.client.NewRequest("GET", u, nil)
43	if err != nil {
44		return nil, nil, err
45	}
46
47	// TODO: remove custom Accept header when this API fully launches.
48	req.Header.Set("Accept", mediaTypeRepositoryCommunityHealthMetricsPreview)
49
50	metrics := &CommunityHealthMetrics{}
51	resp, err := s.client.Do(ctx, req, metrics)
52	if err != nil {
53		return nil, resp, err
54	}
55
56	return metrics, resp, nil
57}
58