github.com/google/go-github/v49@v49.1.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 client, mux, _, teardown := setup() 20 defer teardown() 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: Int(100), 78 Description: String("My first repository on GitHub!"), 79 UpdatedAt: &updatedAt, 80 ContentReportsEnabled: Bool(true), 81 Files: &CommunityHealthFiles{ 82 CodeOfConduct: &Metric{ 83 Name: String("Contributor Covenant"), 84 Key: String("contributor_covenant"), 85 HTMLURL: String("https://github.com/octocat/Hello-World/blob/master/CODE_OF_CONDUCT.md"), 86 }, 87 CodeOfConductFile: &Metric{ 88 URL: String("https://api.github.com/repos/octocat/Hello-World/contents/CODE_OF_CONDUCT.md"), 89 HTMLURL: String("https://github.com/octocat/Hello-World/blob/master/CODE_OF_CONDUCT.md"), 90 }, 91 Contributing: &Metric{ 92 URL: String("https://api.github.com/repos/octocat/Hello-World/contents/CONTRIBUTING"), 93 HTMLURL: String("https://github.com/octocat/Hello-World/blob/master/CONTRIBUTING"), 94 }, 95 IssueTemplate: &Metric{ 96 URL: String("https://api.github.com/repos/octocat/Hello-World/contents/ISSUE_TEMPLATE"), 97 HTMLURL: String("https://github.com/octocat/Hello-World/blob/master/ISSUE_TEMPLATE"), 98 }, 99 PullRequestTemplate: &Metric{ 100 URL: String("https://api.github.com/repos/octocat/Hello-World/contents/PULL_REQUEST_TEMPLATE"), 101 HTMLURL: String("https://github.com/octocat/Hello-World/blob/master/PULL_REQUEST_TEMPLATE"), 102 }, 103 License: &Metric{ 104 Name: String("MIT License"), 105 Key: String("mit"), 106 SPDXID: String("MIT"), 107 URL: String("https://api.github.com/licenses/mit"), 108 HTMLURL: String("https://github.com/octocat/Hello-World/blob/master/LICENSE"), 109 NodeID: String("MDc6TGljZW5zZW1pdA=="), 110 }, 111 Readme: &Metric{ 112 URL: String("https://api.github.com/repos/octocat/Hello-World/contents/README.md"), 113 HTMLURL: String("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 testJSONMarshal(t, &Metric{}, "{}") 138 139 r := &Metric{ 140 Name: String("name"), 141 Key: String("key"), 142 SPDXID: String("spdx_id"), 143 URL: String("url"), 144 HTMLURL: String("hurl"), 145 NodeID: String("node_id"), 146 } 147 148 want := `{ 149 "name": "name", 150 "key": "key", 151 "spdx_id": "spdx_id", 152 "url": "url", 153 "html_url": "hurl", 154 "node_id": "node_id" 155 }` 156 157 testJSONMarshal(t, r, want) 158 } 159 160 func TestCommunityHealthFiles_Marshal(t *testing.T) { 161 testJSONMarshal(t, &CommunityHealthFiles{}, "{}") 162 163 r := &CommunityHealthFiles{ 164 CodeOfConduct: &Metric{ 165 Name: String("name"), 166 Key: String("key"), 167 URL: String("url"), 168 HTMLURL: String("hurl"), 169 }, 170 CodeOfConductFile: &Metric{ 171 Name: String("name"), 172 Key: String("key"), 173 URL: String("url"), 174 HTMLURL: String("hurl"), 175 }, 176 Contributing: &Metric{ 177 Name: String("name"), 178 Key: String("key"), 179 URL: String("url"), 180 HTMLURL: String("hurl"), 181 }, 182 IssueTemplate: &Metric{ 183 Name: String("name"), 184 Key: String("key"), 185 URL: String("url"), 186 HTMLURL: String("hurl"), 187 }, 188 PullRequestTemplate: &Metric{ 189 Name: String("name"), 190 Key: String("key"), 191 URL: String("url"), 192 HTMLURL: String("hurl"), 193 }, 194 License: &Metric{ 195 Name: String("name"), 196 Key: String("key"), 197 SPDXID: String("spdx_id"), 198 URL: String("url"), 199 HTMLURL: String("hurl"), 200 NodeID: String("node_id"), 201 }, 202 Readme: &Metric{ 203 Name: String("name"), 204 Key: String("key"), 205 URL: String("url"), 206 HTMLURL: String("hurl"), 207 }, 208 } 209 210 want := `{ 211 "code_of_conduct": { 212 "name": "name", 213 "key": "key", 214 "url": "url", 215 "html_url": "hurl" 216 }, 217 "code_of_conduct_file": { 218 "name": "name", 219 "key": "key", 220 "url": "url", 221 "html_url": "hurl" 222 }, 223 "contributing": { 224 "name": "name", 225 "key": "key", 226 "url": "url", 227 "html_url": "hurl" 228 }, 229 "issue_template": { 230 "name": "name", 231 "key": "key", 232 "url": "url", 233 "html_url": "hurl" 234 }, 235 "pull_request_template": { 236 "name": "name", 237 "key": "key", 238 "url": "url", 239 "html_url": "hurl" 240 }, 241 "license": { 242 "name": "name", 243 "key": "key", 244 "spdx_id": "spdx_id", 245 "url": "url", 246 "html_url": "hurl", 247 "node_id": "node_id" 248 }, 249 "readme": { 250 "name": "name", 251 "key": "key", 252 "url": "url", 253 "html_url": "hurl" 254 } 255 }` 256 257 testJSONMarshal(t, r, want) 258 } 259 260 func TestCommunityHealthMetrics_Marshal(t *testing.T) { 261 testJSONMarshal(t, &CommunityHealthMetrics{}, "{}") 262 263 r := &CommunityHealthMetrics{ 264 HealthPercentage: Int(1), 265 Description: String("desc"), 266 Documentation: String("docs"), 267 Files: &CommunityHealthFiles{ 268 CodeOfConduct: &Metric{ 269 Name: String("name"), 270 Key: String("key"), 271 URL: String("url"), 272 HTMLURL: String("hurl"), 273 }, 274 CodeOfConductFile: &Metric{ 275 Name: String("name"), 276 Key: String("key"), 277 URL: String("url"), 278 HTMLURL: String("hurl"), 279 }, 280 Contributing: &Metric{ 281 Name: String("name"), 282 Key: String("key"), 283 URL: String("url"), 284 HTMLURL: String("hurl"), 285 }, 286 IssueTemplate: &Metric{ 287 Name: String("name"), 288 Key: String("key"), 289 URL: String("url"), 290 HTMLURL: String("hurl"), 291 }, 292 PullRequestTemplate: &Metric{ 293 Name: String("name"), 294 Key: String("key"), 295 URL: String("url"), 296 HTMLURL: String("hurl"), 297 }, 298 License: &Metric{ 299 Name: String("name"), 300 Key: String("key"), 301 SPDXID: String("spdx_id"), 302 URL: String("url"), 303 HTMLURL: String("hurl"), 304 NodeID: String("node_id"), 305 }, 306 Readme: &Metric{ 307 Name: String("name"), 308 Key: String("key"), 309 URL: String("url"), 310 HTMLURL: String("hurl"), 311 }, 312 }, 313 UpdatedAt: &referenceTime, 314 ContentReportsEnabled: Bool(true), 315 } 316 317 want := `{ 318 "health_percentage": 1, 319 "description": "desc", 320 "documentation": "docs", 321 "files": { 322 "code_of_conduct": { 323 "name": "name", 324 "key": "key", 325 "url": "url", 326 "html_url": "hurl" 327 }, 328 "code_of_conduct_file": { 329 "name": "name", 330 "key": "key", 331 "url": "url", 332 "html_url": "hurl" 333 }, 334 "contributing": { 335 "name": "name", 336 "key": "key", 337 "url": "url", 338 "html_url": "hurl" 339 }, 340 "issue_template": { 341 "name": "name", 342 "key": "key", 343 "url": "url", 344 "html_url": "hurl" 345 }, 346 "pull_request_template": { 347 "name": "name", 348 "key": "key", 349 "url": "url", 350 "html_url": "hurl" 351 }, 352 "license": { 353 "name": "name", 354 "key": "key", 355 "spdx_id": "spdx_id", 356 "url": "url", 357 "html_url": "hurl", 358 "node_id": "node_id" 359 }, 360 "readme": { 361 "name": "name", 362 "key": "key", 363 "url": "url", 364 "html_url": "hurl" 365 } 366 }, 367 "updated_at": ` + referenceTimeStr + `, 368 "content_reports_enabled": true 369 }` 370 371 testJSONMarshal(t, r, want) 372 }