| 310 | } |
| 311 | |
| 312 | func TestDataUnmarshalJSON(t *testing.T) { |
| 313 | testCases := []struct { |
| 314 | name string |
| 315 | input string |
| 316 | wantAuthor *Author |
| 317 | wantErr bool |
| 318 | }{ |
| 319 | { |
| 320 | name: "string author (v1.0-v1.2 backwards compat)", |
| 321 | input: `{"platform":"github","type":"pull_request","number":"1","url":"https://github.com/o/r/pull/1","author":"dependabot[bot]"}`, |
| 322 | wantAuthor: &Author{Login: "dependabot[bot]", Type: "unknown"}, |
| 323 | }, |
| 324 | { |
| 325 | name: "object author (v1.3)", |
| 326 | input: `{"platform":"github","type":"pull_request","number":"1","url":"https://github.com/o/r/pull/1","author":{"login":"dependabot[bot]","type":"Bot"}}`, |
| 327 | wantAuthor: &Author{Login: "dependabot[bot]", Type: "Bot"}, |
| 328 | }, |
| 329 | { |
| 330 | name: "no author field", |
| 331 | input: `{"platform":"github","type":"pull_request","number":"1","url":"https://github.com/o/r/pull/1"}`, |
| 332 | wantAuthor: nil, |
| 333 | }, |
| 334 | { |
| 335 | name: "null author field", |
| 336 | input: `{"platform":"github","type":"pull_request","number":"1","url":"https://github.com/o/r/pull/1","author":null}`, |
| 337 | wantAuthor: nil, |
| 338 | }, |
| 339 | { |
| 340 | name: "invalid author type", |
| 341 | input: `{"platform":"github","type":"pull_request","number":"1","url":"https://github.com/o/r/pull/1","author":123}`, |
| 342 | wantErr: true, |
| 343 | }, |
| 344 | } |
| 345 | |
| 346 | for _, tc := range testCases { |
| 347 | t.Run(tc.name, func(t *testing.T) { |
| 348 | var data Data |
| 349 | err := json.Unmarshal([]byte(tc.input), &data) |
| 350 | if tc.wantErr { |
| 351 | assert.Error(t, err) |
| 352 | return |
| 353 | } |
| 354 | require.NoError(t, err) |
| 355 | assert.Equal(t, tc.wantAuthor, data.Author) |
| 356 | }) |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | func TestValidatePRInfoV1_3(t *testing.T) { |
| 361 | testCases := []struct { |