github.com/google/go-github/v33@v33.0.0/github/repos_community_health_test.go (about)

     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  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  	"net/http"
    12  	"reflect"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  func TestRepositoriesService_GetCommunityHealthMetrics(t *testing.T) {
    18  	client, mux, _, teardown := setup()
    19  	defer teardown()
    20  
    21  	mux.HandleFunc("/repos/o/r/community/profile", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "GET")
    23  		testHeader(t, r, "Accept", mediaTypeRepositoryCommunityHealthMetricsPreview)
    24  		fmt.Fprintf(w, `{
    25  				"health_percentage": 100,
    26  				"files": {
    27  					"code_of_conduct": {
    28  						"name": "Contributor Covenant",
    29  						"key": "contributor_covenant",
    30  						"url": null,
    31  						"html_url": "https://github.com/octocat/Hello-World/blob/master/CODE_OF_CONDUCT.md"
    32  					},
    33  					"contributing": {
    34  						"url": "https://api.github.com/repos/octocat/Hello-World/contents/CONTRIBUTING",
    35  						"html_url": "https://github.com/octocat/Hello-World/blob/master/CONTRIBUTING"
    36  					},
    37  					"license": {
    38  						"name": "MIT License",
    39  						"key": "mit",
    40  						"url": "https://api.github.com/licenses/mit",
    41  						"html_url": "https://github.com/octocat/Hello-World/blob/master/LICENSE"
    42  					},
    43  					"readme": {
    44  						"url": "https://api.github.com/repos/octocat/Hello-World/contents/README.md",
    45  						"html_url": "https://github.com/octocat/Hello-World/blob/master/README.md"
    46  					}
    47  				},
    48  				"updated_at": "2017-02-28T00:00:00Z"
    49  			}`)
    50  	})
    51  
    52  	got, _, err := client.Repositories.GetCommunityHealthMetrics(context.Background(), "o", "r")
    53  	if err != nil {
    54  		t.Errorf("Repositories.GetCommunityHealthMetrics returned error: %v", err)
    55  	}
    56  
    57  	updatedAt := time.Date(2017, time.February, 28, 0, 0, 0, 0, time.UTC)
    58  	want := &CommunityHealthMetrics{
    59  		HealthPercentage: Int(100),
    60  		UpdatedAt:        &updatedAt,
    61  		Files: &CommunityHealthFiles{
    62  			CodeOfConduct: &Metric{
    63  				Name:    String("Contributor Covenant"),
    64  				Key:     String("contributor_covenant"),
    65  				HTMLURL: String("https://github.com/octocat/Hello-World/blob/master/CODE_OF_CONDUCT.md"),
    66  			},
    67  			Contributing: &Metric{
    68  				URL:     String("https://api.github.com/repos/octocat/Hello-World/contents/CONTRIBUTING"),
    69  				HTMLURL: String("https://github.com/octocat/Hello-World/blob/master/CONTRIBUTING"),
    70  			},
    71  			License: &Metric{
    72  				Name:    String("MIT License"),
    73  				Key:     String("mit"),
    74  				URL:     String("https://api.github.com/licenses/mit"),
    75  				HTMLURL: String("https://github.com/octocat/Hello-World/blob/master/LICENSE"),
    76  			},
    77  			Readme: &Metric{
    78  				URL:     String("https://api.github.com/repos/octocat/Hello-World/contents/README.md"),
    79  				HTMLURL: String("https://github.com/octocat/Hello-World/blob/master/README.md"),
    80  			},
    81  		},
    82  	}
    83  	if !reflect.DeepEqual(got, want) {
    84  		t.Errorf("Repositories.GetCommunityHealthMetrics:\ngot:\n%v\nwant:\n%v", Stringify(got), Stringify(want))
    85  	}
    86  }