github.com/google/go-github/v70@v70.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 "testing" 13 "time" 14 15 "github.com/google/go-cmp/cmp" 16 ) 17 18 func TestRepositoriesService_GetCommunityHealthMetrics(t *testing.T) { 19 t.Parallel() 20 client, mux, _ := setup(t) 21 22 mux.HandleFunc("/repos/o/r/community/profile", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 fmt.Fprintf(w, `{ 25 "health_percentage": 100, 26 "description": "My first repository on GitHub!", 27 "documentation": null, 28 "files": { 29 "code_of_conduct": { 30 "name": "Contributor Covenant", 31 "key": "contributor_covenant", 32 "url": null, 33 "html_url": "https://github.com/octocat/Hello-World/blob/master/CODE_OF_CONDUCT.md" 34 }, 35 "code_of_conduct_file": { 36 "url": "https://api.github.com/repos/octocat/Hello-World/contents/CODE_OF_CONDUCT.md", 37 "html_url": "https://github.com/octocat/Hello-World/blob/master/CODE_OF_CONDUCT.md" 38 }, 39 "contributing": { 40 "url": "https://api.github.com/repos/octocat/Hello-World/contents/CONTRIBUTING", 41 "html_url": "https://github.com/octocat/Hello-World/blob/master/CONTRIBUTING" 42 }, 43 "issue_template": { 44 "url": "https://api.github.com/repos/octocat/Hello-World/contents/ISSUE_TEMPLATE", 45 "html_url": "https://github.com/octocat/Hello-World/blob/master/ISSUE_TEMPLATE" 46 }, 47 "pull_request_template": { 48 "url": "https://api.github.com/repos/octocat/Hello-World/contents/PULL_REQUEST_TEMPLATE", 49 "html_url": "https://github.com/octocat/Hello-World/blob/master/PULL_REQUEST_TEMPLATE" 50 }, 51 "license": { 52 "name": "MIT License", 53 "key": "mit", 54 "spdx_id": "MIT", 55 "url": "https://api.github.com/licenses/mit", 56 "html_url": "https://github.com/octocat/Hello-World/blob/master/LICENSE", 57 "node_id": "MDc6TGljZW5zZW1pdA==" 58 }, 59 "readme": { 60 "url": "https://api.github.com/repos/octocat/Hello-World/contents/README.md", 61 "html_url": "https://github.com/octocat/Hello-World/blob/master/README.md" 62 } 63 }, 64 "updated_at": "2017-02-28T00:00:00Z", 65 "content_reports_enabled": true 66 }`) 67 }) 68 69 ctx := context.Background() 70 got, _, err := client.Repositories.GetCommunityHealthMetrics(ctx, "o", "r") 71 if err != nil { 72 t.Errorf("Repositories.GetCommunityHealthMetrics returned error: %v", err) 73 } 74 75 updatedAt := time.Date(2017, time.February, 28, 0, 0, 0, 0, time.UTC) 76 want := &CommunityHealthMetrics{ 77 HealthPercentage: Ptr(100), 78 Description: Ptr("My first repository on GitHub!"), 79 UpdatedAt: &Timestamp{updatedAt}, 80 ContentReportsEnabled: Ptr(true), 81 Files: &CommunityHealthFiles{ 82 CodeOfConduct: &Metric{ 83 Name: Ptr("Contributor Covenant"), 84 Key: Ptr("contributor_covenant"), 85 HTMLURL: Ptr("https://github.com/octocat/Hello-World/blob/master/CODE_OF_CONDUCT.md"), 86 }, 87 CodeOfConductFile: &Metric{ 88 URL: Ptr("https://api.github.com/repos/octocat/Hello-World/contents/CODE_OF_CONDUCT.md"), 89 HTMLURL: Ptr("https://github.com/octocat/Hello-World/blob/master/CODE_OF_CONDUCT.md"), 90 }, 91 Contributing: &Metric{ 92 URL: Ptr("https://api.github.com/repos/octocat/Hello-World/contents/CONTRIBUTING"), 93 HTMLURL: Ptr("https://github.com/octocat/Hello-World/blob/master/CONTRIBUTING"), 94 }, 95 IssueTemplate: &Metric{ 96 URL: Ptr("https://api.github.com/repos/octocat/Hello-World/contents/ISSUE_TEMPLATE"), 97 HTMLURL: Ptr("https://github.com/octocat/Hello-World/blob/master/ISSUE_TEMPLATE"), 98 }, 99 PullRequestTemplate: &Metric{ 100 URL: Ptr("https://api.github.com/repos/octocat/Hello-World/contents/PULL_REQUEST_TEMPLATE"), 101 HTMLURL: Ptr("https://github.com/octocat/Hello-World/blob/master/PULL_REQUEST_TEMPLATE"), 102 }, 103 License: &Metric{ 104 Name: Ptr("MIT License"), 105 Key: Ptr("mit"), 106 SPDXID: Ptr("MIT"), 107 URL: Ptr("https://api.github.com/licenses/mit"), 108 HTMLURL: Ptr("https://github.com/octocat/Hello-World/blob/master/LICENSE"), 109 NodeID: Ptr("MDc6TGljZW5zZW1pdA=="), 110 }, 111 Readme: &Metric{ 112 URL: Ptr("https://api.github.com/repos/octocat/Hello-World/contents/README.md"), 113 HTMLURL: Ptr("https://github.com/octocat/Hello-World/blob/master/README.md"), 114 }, 115 }, 116 } 117 if !cmp.Equal(got, want) { 118 t.Errorf("Repositories.GetCommunityHealthMetrics:\ngot:\n%v\nwant:\n%v", Stringify(got), Stringify(want)) 119 } 120 121 const methodName = "GetCommunityHealthMetrics" 122 testBadOptions(t, methodName, func() (err error) { 123 _, _, err = client.Repositories.GetCommunityHealthMetrics(ctx, "\n", "\n") 124 return err 125 }) 126 127 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 128 got, resp, err := client.Repositories.GetCommunityHealthMetrics(ctx, "o", "r") 129 if got != nil { 130 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 131 } 132 return resp, err 133 }) 134 } 135 136 func TestMetric_Marshal(t *testing.T) { 137 t.Parallel() 138 testJSONMarshal(t, &Metric{}, "{}") 139 140 r := &Metric{ 141 Name: Ptr("name"), 142 Key: Ptr("key"), 143 SPDXID: Ptr("spdx_id"), 144 URL: Ptr("url"), 145 HTMLURL: Ptr("hurl"), 146 NodeID: Ptr("node_id"), 147 } 148 149 want := `{ 150 "name": "name", 151 "key": "key", 152 "spdx_id": "spdx_id", 153 "url": "url", 154 "html_url": "hurl", 155 "node_id": "node_id" 156 }` 157 158 testJSONMarshal(t, r, want) 159 } 160 161 func TestCommunityHealthFiles_Marshal(t *testing.T) { 162 t.Parallel() 163 testJSONMarshal(t, &CommunityHealthFiles{}, "{}") 164 165 r := &CommunityHealthFiles{ 166 CodeOfConduct: &Metric{ 167 Name: Ptr("name"), 168 Key: Ptr("key"), 169 URL: Ptr("url"), 170 HTMLURL: Ptr("hurl"), 171 }, 172 CodeOfConductFile: &Metric{ 173 Name: Ptr("name"), 174 Key: Ptr("key"), 175 URL: Ptr("url"), 176 HTMLURL: Ptr("hurl"), 177 }, 178 Contributing: &Metric{ 179 Name: Ptr("name"), 180 Key: Ptr("key"), 181 URL: Ptr("url"), 182 HTMLURL: Ptr("hurl"), 183 }, 184 IssueTemplate: &Metric{ 185 Name: Ptr("name"), 186 Key: Ptr("key"), 187 URL: Ptr("url"), 188 HTMLURL: Ptr("hurl"), 189 }, 190 PullRequestTemplate: &Metric{ 191 Name: Ptr("name"), 192 Key: Ptr("key"), 193 URL: Ptr("url"), 194 HTMLURL: Ptr("hurl"), 195 }, 196 License: &Metric{ 197 Name: Ptr("name"), 198 Key: Ptr("key"), 199 SPDXID: Ptr("spdx_id"), 200 URL: Ptr("url"), 201 HTMLURL: Ptr("hurl"), 202 NodeID: Ptr("node_id"), 203 }, 204 Readme: &Metric{ 205 Name: Ptr("name"), 206 Key: Ptr("key"), 207 URL: Ptr("url"), 208 HTMLURL: Ptr("hurl"), 209 }, 210 } 211 212 want := `{ 213 "code_of_conduct": { 214 "name": "name", 215 "key": "key", 216 "url": "url", 217 "html_url": "hurl" 218 }, 219 "code_of_conduct_file": { 220 "name": "name", 221 "key": "key", 222 "url": "url", 223 "html_url": "hurl" 224 }, 225 "contributing": { 226 "name": "name", 227 "key": "key", 228 "url": "url", 229 "html_url": "hurl" 230 }, 231 "issue_template": { 232 "name": "name", 233 "key": "key", 234 "url": "url", 235 "html_url": "hurl" 236 }, 237 "pull_request_template": { 238 "name": "name", 239 "key": "key", 240 "url": "url", 241 "html_url": "hurl" 242 }, 243 "license": { 244 "name": "name", 245 "key": "key", 246 "spdx_id": "spdx_id", 247 "url": "url", 248 "html_url": "hurl", 249 "node_id": "node_id" 250 }, 251 "readme": { 252 "name": "name", 253 "key": "key", 254 "url": "url", 255 "html_url": "hurl" 256 } 257 }` 258 259 testJSONMarshal(t, r, want) 260 } 261 262 func TestCommunityHealthMetrics_Marshal(t *testing.T) { 263 t.Parallel() 264 testJSONMarshal(t, &CommunityHealthMetrics{}, "{}") 265 266 r := &CommunityHealthMetrics{ 267 HealthPercentage: Ptr(1), 268 Description: Ptr("desc"), 269 Documentation: Ptr("docs"), 270 Files: &CommunityHealthFiles{ 271 CodeOfConduct: &Metric{ 272 Name: Ptr("name"), 273 Key: Ptr("key"), 274 URL: Ptr("url"), 275 HTMLURL: Ptr("hurl"), 276 }, 277 CodeOfConductFile: &Metric{ 278 Name: Ptr("name"), 279 Key: Ptr("key"), 280 URL: Ptr("url"), 281 HTMLURL: Ptr("hurl"), 282 }, 283 Contributing: &Metric{ 284 Name: Ptr("name"), 285 Key: Ptr("key"), 286 URL: Ptr("url"), 287 HTMLURL: Ptr("hurl"), 288 }, 289 IssueTemplate: &Metric{ 290 Name: Ptr("name"), 291 Key: Ptr("key"), 292 URL: Ptr("url"), 293 HTMLURL: Ptr("hurl"), 294 }, 295 PullRequestTemplate: &Metric{ 296 Name: Ptr("name"), 297 Key: Ptr("key"), 298 URL: Ptr("url"), 299 HTMLURL: Ptr("hurl"), 300 }, 301 License: &Metric{ 302 Name: Ptr("name"), 303 Key: Ptr("key"), 304 SPDXID: Ptr("spdx_id"), 305 URL: Ptr("url"), 306 HTMLURL: Ptr("hurl"), 307 NodeID: Ptr("node_id"), 308 }, 309 Readme: &Metric{ 310 Name: Ptr("name"), 311 Key: Ptr("key"), 312 URL: Ptr("url"), 313 HTMLURL: Ptr("hurl"), 314 }, 315 }, 316 UpdatedAt: &Timestamp{referenceTime}, 317 ContentReportsEnabled: Ptr(true), 318 } 319 320 want := `{ 321 "health_percentage": 1, 322 "description": "desc", 323 "documentation": "docs", 324 "files": { 325 "code_of_conduct": { 326 "name": "name", 327 "key": "key", 328 "url": "url", 329 "html_url": "hurl" 330 }, 331 "code_of_conduct_file": { 332 "name": "name", 333 "key": "key", 334 "url": "url", 335 "html_url": "hurl" 336 }, 337 "contributing": { 338 "name": "name", 339 "key": "key", 340 "url": "url", 341 "html_url": "hurl" 342 }, 343 "issue_template": { 344 "name": "name", 345 "key": "key", 346 "url": "url", 347 "html_url": "hurl" 348 }, 349 "pull_request_template": { 350 "name": "name", 351 "key": "key", 352 "url": "url", 353 "html_url": "hurl" 354 }, 355 "license": { 356 "name": "name", 357 "key": "key", 358 "spdx_id": "spdx_id", 359 "url": "url", 360 "html_url": "hurl", 361 "node_id": "node_id" 362 }, 363 "readme": { 364 "name": "name", 365 "key": "key", 366 "url": "url", 367 "html_url": "hurl" 368 } 369 }, 370 "updated_at": ` + referenceTimeStr + `, 371 "content_reports_enabled": true 372 }` 373 374 testJSONMarshal(t, r, want) 375 }