From 66b0919d86dcea79ebb70237591b4329267fd544 Mon Sep 17 00:00:00 2001 From: Ryan Mast Date: Sat, 4 Sep 2021 09:24:08 -0700 Subject: [PATCH 1/2] Update get community profile to match docs --- github/github-accessors.go | 48 ++++++++++++++ github/github-accessors_test.go | 57 ++++++++++++++++ github/repos_community_health.go | 14 ++-- github/repos_community_health_test.go | 93 ++++++++++++++++++++++++--- 4 files changed, 199 insertions(+), 13 deletions(-) diff --git a/github/github-accessors.go b/github/github-accessors.go index b0169cbb027..8422d6a7975 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -2548,6 +2548,14 @@ func (c *CommunityHealthFiles) GetCodeOfConduct() *Metric { return c.CodeOfConduct } +// GetCodeOfConductFile returns the CodeOfConductFile field. +func (c *CommunityHealthFiles) GetCodeOfConductFile() *Metric { + if c == nil { + return nil + } + return c.CodeOfConductFile +} + // GetContributing returns the Contributing field. func (c *CommunityHealthFiles) GetContributing() *Metric { if c == nil { @@ -2588,6 +2596,30 @@ func (c *CommunityHealthFiles) GetReadme() *Metric { return c.Readme } +// GetContentReportsEnabled returns the ContentReportsEnabled field if it's non-nil, zero value otherwise. +func (c *CommunityHealthMetrics) GetContentReportsEnabled() bool { + if c == nil || c.ContentReportsEnabled == nil { + return false + } + return *c.ContentReportsEnabled +} + +// GetDescription returns the Description field if it's non-nil, zero value otherwise. +func (c *CommunityHealthMetrics) GetDescription() string { + if c == nil || c.Description == nil { + return "" + } + return *c.Description +} + +// GetDocumentation returns the Documentation field if it's non-nil, zero value otherwise. +func (c *CommunityHealthMetrics) GetDocumentation() string { + if c == nil || c.Documentation == nil { + return "" + } + return *c.Documentation +} + // GetFiles returns the Files field. func (c *CommunityHealthMetrics) GetFiles() *CommunityHealthFiles { if c == nil { @@ -7788,6 +7820,22 @@ func (m *Metric) GetName() string { return *m.Name } +// GetNodeID returns the NodeID field if it's non-nil, zero value otherwise. +func (m *Metric) GetNodeID() string { + if m == nil || m.NodeID == nil { + return "" + } + return *m.NodeID +} + +// GetSPDXID returns the SPDXID field if it's non-nil, zero value otherwise. +func (m *Metric) GetSPDXID() string { + if m == nil || m.SPDXID == nil { + return "" + } + return *m.SPDXID +} + // GetURL returns the URL field if it's non-nil, zero value otherwise. func (m *Metric) GetURL() string { if m == nil || m.URL == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index c6633945085..df604c449e2 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -2991,6 +2991,13 @@ func TestCommunityHealthFiles_GetCodeOfConduct(tt *testing.T) { c.GetCodeOfConduct() } +func TestCommunityHealthFiles_GetCodeOfConductFile(tt *testing.T) { + c := &CommunityHealthFiles{} + c.GetCodeOfConductFile() + c = nil + c.GetCodeOfConductFile() +} + func TestCommunityHealthFiles_GetContributing(tt *testing.T) { c := &CommunityHealthFiles{} c.GetContributing() @@ -3026,6 +3033,36 @@ func TestCommunityHealthFiles_GetReadme(tt *testing.T) { c.GetReadme() } +func TestCommunityHealthMetrics_GetContentReportsEnabled(tt *testing.T) { + var zeroValue bool + c := &CommunityHealthMetrics{ContentReportsEnabled: &zeroValue} + c.GetContentReportsEnabled() + c = &CommunityHealthMetrics{} + c.GetContentReportsEnabled() + c = nil + c.GetContentReportsEnabled() +} + +func TestCommunityHealthMetrics_GetDescription(tt *testing.T) { + var zeroValue string + c := &CommunityHealthMetrics{Description: &zeroValue} + c.GetDescription() + c = &CommunityHealthMetrics{} + c.GetDescription() + c = nil + c.GetDescription() +} + +func TestCommunityHealthMetrics_GetDocumentation(tt *testing.T) { + var zeroValue string + c := &CommunityHealthMetrics{Documentation: &zeroValue} + c.GetDocumentation() + c = &CommunityHealthMetrics{} + c.GetDocumentation() + c = nil + c.GetDocumentation() +} + func TestCommunityHealthMetrics_GetFiles(tt *testing.T) { c := &CommunityHealthMetrics{} c.GetFiles() @@ -9127,6 +9164,26 @@ func TestMetric_GetName(tt *testing.T) { m.GetName() } +func TestMetric_GetNodeID(tt *testing.T) { + var zeroValue string + m := &Metric{NodeID: &zeroValue} + m.GetNodeID() + m = &Metric{} + m.GetNodeID() + m = nil + m.GetNodeID() +} + +func TestMetric_GetSPDXID(tt *testing.T) { + var zeroValue string + m := &Metric{SPDXID: &zeroValue} + m.GetSPDXID() + m = &Metric{} + m.GetSPDXID() + m = nil + m.GetSPDXID() +} + func TestMetric_GetURL(tt *testing.T) { var zeroValue string m := &Metric{URL: &zeroValue} diff --git a/github/repos_community_health.go b/github/repos_community_health.go index aeb4277a40f..5719e4be644 100644 --- a/github/repos_community_health.go +++ b/github/repos_community_health.go @@ -15,13 +15,16 @@ import ( type Metric struct { Name *string `json:"name"` Key *string `json:"key"` + SPDXID *string `json:"spdx_id"` URL *string `json:"url"` HTMLURL *string `json:"html_url"` + NodeID *string `json:"node_id"` } // CommunityHealthFiles represents the different files in the community health metrics response. type CommunityHealthFiles struct { CodeOfConduct *Metric `json:"code_of_conduct"` + CodeOfConductFile *Metric `json:"code_of_conduct_file"` Contributing *Metric `json:"contributing"` IssueTemplate *Metric `json:"issue_template"` PullRequestTemplate *Metric `json:"pull_request_template"` @@ -31,14 +34,17 @@ type CommunityHealthFiles struct { // CommunityHealthMetrics represents a response containing the community metrics of a repository. type CommunityHealthMetrics struct { - HealthPercentage *int `json:"health_percentage"` - Files *CommunityHealthFiles `json:"files"` - UpdatedAt *time.Time `json:"updated_at"` + HealthPercentage *int `json:"health_percentage"` + Description *string `json:"description"` + Documentation *string `json:"documentation"` + Files *CommunityHealthFiles `json:"files"` + UpdatedAt *time.Time `json:"updated_at"` + ContentReportsEnabled *bool `json:"content_reports_enabled"` } // GetCommunityHealthMetrics retrieves all the community health metrics for a repository. // -// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-community-profile-metrics +// GitHub API docs: https://docs.github.com/en/rest/reference/repos#get-community-profile-metrics func (s *RepositoriesService) GetCommunityHealthMetrics(ctx context.Context, owner, repo string) (*CommunityHealthMetrics, *Response, error) { u := fmt.Sprintf("repos/%v/%v/community/profile", owner, repo) req, err := s.client.NewRequest("GET", u, nil) diff --git a/github/repos_community_health_test.go b/github/repos_community_health_test.go index 24a62493c4e..457dd36cc8a 100644 --- a/github/repos_community_health_test.go +++ b/github/repos_community_health_test.go @@ -24,6 +24,8 @@ func TestRepositoriesService_GetCommunityHealthMetrics(t *testing.T) { testHeader(t, r, "Accept", mediaTypeRepositoryCommunityHealthMetricsPreview) fmt.Fprintf(w, `{ "health_percentage": 100, + "description": "My first repository on GitHub!", + "documentation": null, "files": { "code_of_conduct": { "name": "Contributor Covenant", @@ -31,22 +33,37 @@ func TestRepositoriesService_GetCommunityHealthMetrics(t *testing.T) { "url": null, "html_url": "https://github.com/octocat/Hello-World/blob/master/CODE_OF_CONDUCT.md" }, + "code_of_conduct_file": { + "url": "https://api.github.com/repos/octocat/Hello-World/contents/CODE_OF_CONDUCT.md", + "html_url": "https://github.com/octocat/Hello-World/blob/master/CODE_OF_CONDUCT.md" + }, "contributing": { "url": "https://api.github.com/repos/octocat/Hello-World/contents/CONTRIBUTING", "html_url": "https://github.com/octocat/Hello-World/blob/master/CONTRIBUTING" }, + "issue_template": { + "url": "https://api.github.com/repos/octocat/Hello-World/contents/ISSUE_TEMPLATE", + "html_url": "https://github.com/octocat/Hello-World/blob/master/ISSUE_TEMPLATE" + }, + "pull_request_template": { + "url": "https://api.github.com/repos/octocat/Hello-World/contents/PULL_REQUEST_TEMPLATE", + "html_url": "https://github.com/octocat/Hello-World/blob/master/PULL_REQUEST_TEMPLATE" + }, "license": { "name": "MIT License", "key": "mit", + "spdx_id": "MIT", "url": "https://api.github.com/licenses/mit", - "html_url": "https://github.com/octocat/Hello-World/blob/master/LICENSE" + "html_url": "https://github.com/octocat/Hello-World/blob/master/LICENSE", + "node_id": "MDc6TGljZW5zZW1pdA==" }, "readme": { "url": "https://api.github.com/repos/octocat/Hello-World/contents/README.md", "html_url": "https://github.com/octocat/Hello-World/blob/master/README.md" } }, - "updated_at": "2017-02-28T00:00:00Z" + "updated_at": "2017-02-28T00:00:00Z", + "content_reports_enabled": true }`) }) @@ -58,23 +75,39 @@ func TestRepositoriesService_GetCommunityHealthMetrics(t *testing.T) { updatedAt := time.Date(2017, time.February, 28, 0, 0, 0, 0, time.UTC) want := &CommunityHealthMetrics{ - HealthPercentage: Int(100), - UpdatedAt: &updatedAt, + HealthPercentage: Int(100), + Description: String("My first repository on GitHub!"), + UpdatedAt: &updatedAt, + ContentReportsEnabled: Bool(true), Files: &CommunityHealthFiles{ CodeOfConduct: &Metric{ Name: String("Contributor Covenant"), Key: String("contributor_covenant"), HTMLURL: String("https://github.com/octocat/Hello-World/blob/master/CODE_OF_CONDUCT.md"), }, + CodeOfConductFile: &Metric{ + URL: String("https://api.github.com/repos/octocat/Hello-World/contents/CODE_OF_CONDUCT.md"), + HTMLURL: String("https://github.com/octocat/Hello-World/blob/master/CODE_OF_CONDUCT.md"), + }, Contributing: &Metric{ URL: String("https://api.github.com/repos/octocat/Hello-World/contents/CONTRIBUTING"), HTMLURL: String("https://github.com/octocat/Hello-World/blob/master/CONTRIBUTING"), }, + IssueTemplate: &Metric{ + URL: String("https://api.github.com/repos/octocat/Hello-World/contents/ISSUE_TEMPLATE"), + HTMLURL: String("https://github.com/octocat/Hello-World/blob/master/ISSUE_TEMPLATE"), + }, + PullRequestTemplate: &Metric{ + URL: String("https://api.github.com/repos/octocat/Hello-World/contents/PULL_REQUEST_TEMPLATE"), + HTMLURL: String("https://github.com/octocat/Hello-World/blob/master/PULL_REQUEST_TEMPLATE"), + }, License: &Metric{ Name: String("MIT License"), Key: String("mit"), + SPDXID: String("MIT"), URL: String("https://api.github.com/licenses/mit"), HTMLURL: String("https://github.com/octocat/Hello-World/blob/master/LICENSE"), + NodeID: String("MDc6TGljZW5zZW1pdA=="), }, Readme: &Metric{ URL: String("https://api.github.com/repos/octocat/Hello-World/contents/README.md"), @@ -107,15 +140,19 @@ func TestMetric_Marshal(t *testing.T) { r := &Metric{ Name: String("name"), Key: String("key"), + SPDXID: String("spdx_id"), URL: String("url"), HTMLURL: String("hurl"), + NodeID: String("node_id"), } want := `{ "name": "name", "key": "key", + "spdx_id": "spdx_id", "url": "url", - "html_url": "hurl" + "html_url": "hurl", + "node_id": "node_id" }` testJSONMarshal(t, r, want) @@ -131,6 +168,12 @@ func TestCommunityHealthFiles_Marshal(t *testing.T) { URL: String("url"), HTMLURL: String("hurl"), }, + CodeOfConductFile: &Metric{ + Name: String("name"), + Key: String("key"), + URL: String("url"), + HTMLURL: String("hurl"), + }, Contributing: &Metric{ Name: String("name"), Key: String("key"), @@ -152,8 +195,10 @@ func TestCommunityHealthFiles_Marshal(t *testing.T) { License: &Metric{ Name: String("name"), Key: String("key"), + SPDXID: String("spdx_id"), URL: String("url"), HTMLURL: String("hurl"), + NodeID: String("node_id"), }, Readme: &Metric{ Name: String("name"), @@ -170,6 +215,12 @@ func TestCommunityHealthFiles_Marshal(t *testing.T) { "url": "url", "html_url": "hurl" }, + "code_of_conduct_file": { + "name": "name", + "key": "key", + "url": "url", + "html_url": "hurl" + }, "contributing": { "name": "name", "key": "key", @@ -191,8 +242,10 @@ func TestCommunityHealthFiles_Marshal(t *testing.T) { "license": { "name": "name", "key": "key", + "spdx_id": "spdx_id", "url": "url", - "html_url": "hurl" + "html_url": "hurl", + "node_id": "node_id" }, "readme": { "name": "name", @@ -210,6 +263,8 @@ func TestCommunityHealthMetrics_Marshal(t *testing.T) { r := &CommunityHealthMetrics{ HealthPercentage: Int(1), + Description: String("desc"), + Documentation: String("docs"), Files: &CommunityHealthFiles{ CodeOfConduct: &Metric{ Name: String("name"), @@ -217,6 +272,12 @@ func TestCommunityHealthMetrics_Marshal(t *testing.T) { URL: String("url"), HTMLURL: String("hurl"), }, + CodeOfConductFile: &Metric{ + Name: String("name"), + Key: String("key"), + URL: String("url"), + HTMLURL: String("hurl"), + }, Contributing: &Metric{ Name: String("name"), Key: String("key"), @@ -238,8 +299,10 @@ func TestCommunityHealthMetrics_Marshal(t *testing.T) { License: &Metric{ Name: String("name"), Key: String("key"), + SPDXID: String("spdx_id"), URL: String("url"), HTMLURL: String("hurl"), + NodeID: String("node_id"), }, Readme: &Metric{ Name: String("name"), @@ -248,11 +311,14 @@ func TestCommunityHealthMetrics_Marshal(t *testing.T) { HTMLURL: String("hurl"), }, }, - UpdatedAt: &referenceTime, + UpdatedAt: &referenceTime, + ContentReportsEnabled: Bool(true), } want := `{ "health_percentage": 1, + "description": "desc", + "documentation": "docs", "files": { "code_of_conduct": { "name": "name", @@ -260,6 +326,12 @@ func TestCommunityHealthMetrics_Marshal(t *testing.T) { "url": "url", "html_url": "hurl" }, + "code_of_conduct_file": { + "name": "name", + "key": "key", + "url": "url", + "html_url": "hurl" + }, "contributing": { "name": "name", "key": "key", @@ -281,8 +353,10 @@ func TestCommunityHealthMetrics_Marshal(t *testing.T) { "license": { "name": "name", "key": "key", + "spdx_id": "spdx_id", "url": "url", - "html_url": "hurl" + "html_url": "hurl", + "node_id": "node_id" }, "readme": { "name": "name", @@ -291,7 +365,8 @@ func TestCommunityHealthMetrics_Marshal(t *testing.T) { "html_url": "hurl" } }, - "updated_at": ` + referenceTimeStr + ` + "updated_at": ` + referenceTimeStr + `, + "content_reports_enabled": true }` testJSONMarshal(t, r, want) From da6259674d082771a40102a496b43354861eb5cb Mon Sep 17 00:00:00 2001 From: Ryan Mast Date: Sat, 4 Sep 2021 09:26:53 -0700 Subject: [PATCH 2/2] Remove community health preview header --- github/github.go | 3 --- github/repos_community_health.go | 3 --- github/repos_community_health_test.go | 1 - 3 files changed, 7 deletions(-) diff --git a/github/github.go b/github/github.go index b83e4306c96..a2727ccf4f3 100644 --- a/github/github.go +++ b/github/github.go @@ -76,9 +76,6 @@ const ( // https://developer.github.com/changes/2017-02-28-user-blocking-apis-and-webhook/ mediaTypeBlockUsersPreview = "application/vnd.github.giant-sentry-fist-preview+json" - // https://developer.github.com/changes/2017-02-09-community-health/ - mediaTypeRepositoryCommunityHealthMetricsPreview = "application/vnd.github.black-panther-preview+json" - // https://developer.github.com/changes/2017-05-23-coc-api/ mediaTypeCodesOfConductPreview = "application/vnd.github.scarlet-witch-preview+json" diff --git a/github/repos_community_health.go b/github/repos_community_health.go index 5719e4be644..92e4d082ce6 100644 --- a/github/repos_community_health.go +++ b/github/repos_community_health.go @@ -52,9 +52,6 @@ func (s *RepositoriesService) GetCommunityHealthMetrics(ctx context.Context, own return nil, nil, err } - // TODO: remove custom Accept header when this API fully launches. - req.Header.Set("Accept", mediaTypeRepositoryCommunityHealthMetricsPreview) - metrics := &CommunityHealthMetrics{} resp, err := s.client.Do(ctx, req, metrics) if err != nil { diff --git a/github/repos_community_health_test.go b/github/repos_community_health_test.go index 457dd36cc8a..0031cdb7d83 100644 --- a/github/repos_community_health_test.go +++ b/github/repos_community_health_test.go @@ -21,7 +21,6 @@ func TestRepositoriesService_GetCommunityHealthMetrics(t *testing.T) { mux.HandleFunc("/repos/o/r/community/profile", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") - testHeader(t, r, "Accept", mediaTypeRepositoryCommunityHealthMetricsPreview) fmt.Fprintf(w, `{ "health_percentage": 100, "description": "My first repository on GitHub!",