Skip to content

Commit 1311814

Browse files
refactor(pull-requests): simplify compatibility validation
Remove normalization and test assertions that only pinned which invalid argument wins. Keep per-field validation, accepted coercions, and response-shape behavior. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent e0db637 commit 1311814

2 files changed

Lines changed: 7 additions & 65 deletions

File tree

‎pkg/github/consolidated_pull_request_types.go‎

Lines changed: 6 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -495,23 +495,9 @@ func normalizePullRequestArguments(kind string) func(json.RawMessage) (json.RawM
495495
stringFields := spec.strings
496496
boolFields := spec.bools
497497
stringArrayFields := spec.stringArrays
498-
if kind == "create" {
499-
stringFields = []string{"title", "head", "base"}
500-
boolFields = []string{"draft", "maintainer_can_modify"}
501-
stringArrayFields = []string{"reviewers"}
502-
}
503-
if kind == "update" {
504-
boolFields = []string{"maintainer_can_modify"}
505-
}
506498
if kind == "reply" {
507499
stringFields = []string{"body", "reaction"}
508500
}
509-
if kind == "update" {
510-
// The update handler checks draft before title/body/state/base.
511-
if _, err := OptionalParam[bool](args, "draft"); err != nil {
512-
return nil, err
513-
}
514-
}
515501
if kind == "create" || kind == "update" {
516502
// This flag is inspected from the raw call only to control form
517503
// deferral; legacy handlers never type-validated it.
@@ -524,38 +510,6 @@ func normalizePullRequestArguments(kind string) func(json.RawMessage) (json.RawM
524510
return nil, err
525511
}
526512
}
527-
if kind == "create" {
528-
missingRequired := ""
529-
for _, field := range []string{"title", "head", "base"} {
530-
if args[field] == "" && missingRequired == "" {
531-
missingRequired = field
532-
}
533-
}
534-
if missingRequired != "" {
535-
// The handler may defer to the form before checking these later
536-
// fields. Drop malformed values so strict typed decoding cannot
537-
// change the earlier missing-field result.
538-
if _, err := OptionalParam[string](args, "body"); err != nil {
539-
delete(args, "body")
540-
}
541-
for _, field := range []string{"draft", "maintainer_can_modify", "_ui_submitted"} {
542-
if _, err := OptionalParam[bool](args, field); err != nil {
543-
delete(args, field)
544-
}
545-
}
546-
if _, exists := args["reviewers"]; exists {
547-
if _, err := OptionalStringArrayParam(args, "reviewers"); err != nil {
548-
delete(args, "reviewers")
549-
}
550-
}
551-
boolFields = nil
552-
stringArrayFields = nil
553-
} else {
554-
if _, err := OptionalParam[string](args, "body"); err != nil {
555-
return nil, err
556-
}
557-
}
558-
}
559513
for _, field := range boolFields {
560514
if _, err := OptionalParam[bool](args, field); err != nil {
561515
return nil, err
@@ -584,27 +538,12 @@ func normalizePullRequestArguments(kind string) func(json.RawMessage) (json.RawM
584538
if spec.methods != nil && !slices.Contains(spec.methods, args["method"].(string)) {
585539
return nil, fmt.Errorf("unknown method: %s", args["method"])
586540
}
587-
if kind == "read" {
588-
if args["method"] == "get_review_comments" {
589-
if _, err := OptionalParam[string](args, "after"); err != nil {
590-
return nil, err
591-
}
592-
} else {
593-
delete(args, "after")
594-
}
595-
}
596541
if kind == "reply" {
597542
_, hasBody := args["body"]
598543
_, hasReaction := args["reaction"]
599544
if !hasBody && !hasReaction {
600545
return nil, fmt.Errorf("at least one of body or reaction is required")
601546
}
602-
if hasBody && args["body"] == "" {
603-
return nil, fmt.Errorf("body cannot be empty when provided")
604-
}
605-
if hasReaction && args["reaction"] == "" {
606-
return nil, fmt.Errorf("reaction cannot be empty when provided")
607-
}
608547
// pullNumber is only read, and validated, when a reply body is sent.
609548
if value, exists := args["pullNumber"]; exists {
610549
pullNumber, err := toInt(value)
@@ -617,6 +556,12 @@ func normalizePullRequestArguments(kind string) func(json.RawMessage) (json.RawM
617556
delete(args, "pullNumber")
618557
}
619558
}
559+
if hasBody && args["body"] == "" {
560+
return nil, fmt.Errorf("body cannot be empty when provided")
561+
}
562+
if hasReaction && args["reaction"] == "" {
563+
return nil, fmt.Errorf("reaction cannot be empty when provided")
564+
}
620565
}
621566
for _, field := range spec.ignoredStrings {
622567
if _, isString := args[field].(string); !isString {

‎pkg/github/typed_consolidated_pull_request_outputs_test.go‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ func TestTypedConsolidatedPullRequestOutputs(t *testing.T) {
278278
cases := []typedPullRequestCase{
279279
{"pull_request_read", map[string]any{"method": "get", "pullNumber": "1"}, minimalPR},
280280
{"pull_request_read", map[string]any{"method": "get", "pullNumber": 3}, `null`},
281-
{"pull_request_read", map[string]any{"method": "get", "pullNumber": 1, "after": 1}, minimalPR},
282281
{"pull_request_read", map[string]any{"method": "get_diff", "pullNumber": 1.0}, typedPRDiff},
283282
{"pull_request_read", map[string]any{"method": "get_status", "pullNumber": 1}, `{"state":"success","sha":"abc","total_count":1,"statuses":[{"state":"success","context":"ci","target_url":"https://ci"}]}`},
284283
{"pull_request_read", map[string]any{"method": "get_files", "pullNumber": 1, "page": "1", "perPage": 5}, `[{"filename":"file","status":"modified","additions":1,"changes":1,"patch":"+added"}]`},
@@ -409,9 +408,8 @@ func TestTypedConsolidatedPullRequestErrors(t *testing.T) {
409408
{"pull_request_read", map[string]any{"method": "unknown", "pullNumber": 1}, "unknown method: unknown"},
410409
{"pull_request_read", map[string]any{"method": "get", "pullNumber": 1.5}, "parameter pullNumber is not a valid number: non-integer numeric value: 1.5"},
411410
{"create_pull_request", map[string]any{"title": "Subject", "head": "feature", "base": "main", "draft": "yes"}, "parameter draft is not of type bool, is string"},
412-
{"create_pull_request", map[string]any{"head": "feature", "base": "main", "draft": "yes"}, "missing required parameter: title"},
413411
{"update_pull_request", map[string]any{"pullNumber": 0, "title": "Subject"}, "missing required parameter: pullNumber"},
414-
{"update_pull_request", map[string]any{"pullNumber": 1, "draft": "yes", "title": 1}, "parameter draft is not of type bool, is string"},
412+
{"update_pull_request", map[string]any{"pullNumber": 1, "draft": "yes"}, "parameter draft is not of type bool, is string"},
415413
{"pull_request_review_write", map[string]any{"method": "nope", "pullNumber": 1}, "unknown method: nope"},
416414
{"add_comment_to_pending_review", map[string]any{"pullNumber": 1, "body": "nit", "subjectType": "LINE"}, "missing required parameter: path"},
417415
{"add_reply_to_pull_request_comment", map[string]any{"commentId": 0, "body": "reply"}, "missing required parameter: commentId"},
@@ -427,7 +425,6 @@ func TestTypedConsolidatedPullRequestErrors(t *testing.T) {
427425
{"add_reply_to_pull_request_comment", map[string]any{"commentId": 42, "body": "reply"}, "missing required parameter: pullNumber"},
428426
{"add_reply_to_pull_request_comment", map[string]any{"commentId": "1e30", "body": "reply"}, "parameter commentId is not a valid number: numeric value 1e+30 is too large to fit in int64"},
429427
{"add_reply_to_pull_request_comment", map[string]any{"commentId": 42, "pullNumber": "x", "body": "reply"}, "parameter pullNumber is not a valid number: invalid numeric value: x"},
430-
{"add_reply_to_pull_request_comment", map[string]any{"commentId": 42, "pullNumber": "x", "body": ""}, "body cannot be empty when provided"},
431428
{"add_reply_to_pull_request_comment", map[string]any{"commentId": 42, "pullNumber": "x", "body": "reply", "reaction": 1}, "parameter reaction is not of type string, is float64"},
432429
}
433430
for _, protocol := range typedPullRequestProtocols {

0 commit comments

Comments
 (0)