-
Notifications
You must be signed in to change notification settings - Fork 368
Extract shared buildInputSchema helper to deduplicate input-schema generation
#25795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package workflow | ||
|
|
||
| // buildInputSchema converts GitHub Actions input definitions (workflow_dispatch, | ||
| // workflow_call, or dispatch_repository inputs) into JSON Schema properties and | ||
| // a required field list suitable for MCP tool inputSchema. | ||
| // | ||
| // descriptionFn is called to produce the fallback description when an input | ||
| // definition does not include its own "description" field. | ||
| // | ||
| // Supported input types: string (default), number, boolean, choice, environment. | ||
| // Choice inputs with options are mapped to a string enum. Unknown types default | ||
| // to string. | ||
| func buildInputSchema(inputs map[string]any, descriptionFn func(inputName string) string) (properties map[string]any, required []string) { | ||
| properties = make(map[string]any) | ||
| required = []string{} | ||
|
|
||
| for inputName, inputDef := range inputs { | ||
| inputDefMap, ok := inputDef.(map[string]any) | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| inputType := "string" | ||
| inputDescription := descriptionFn(inputName) | ||
| inputRequired := false | ||
|
|
||
| if desc, ok := inputDefMap["description"].(string); ok && desc != "" { | ||
| inputDescription = desc | ||
| } | ||
|
|
||
| if req, ok := inputDefMap["required"].(bool); ok { | ||
| inputRequired = req | ||
| } | ||
|
|
||
| // Map GitHub Actions input types to JSON Schema types. | ||
| if typeStr, ok := inputDefMap["type"].(string); ok { | ||
| switch typeStr { | ||
| case "number": | ||
| inputType = "number" | ||
| case "boolean": | ||
| inputType = "boolean" | ||
| case "choice": | ||
| inputType = "string" | ||
| if options, ok := inputDefMap["options"].([]any); ok && len(options) > 0 { | ||
| prop := map[string]any{ | ||
| "type": inputType, | ||
| "description": inputDescription, | ||
| "enum": options, | ||
| } | ||
| if defaultVal, ok := inputDefMap["default"]; ok { | ||
| prop["default"] = defaultVal | ||
| } | ||
| properties[inputName] = prop | ||
| if inputRequired { | ||
| required = append(required, inputName) | ||
| } | ||
| continue | ||
| } | ||
| case "environment": | ||
| inputType = "string" | ||
| } | ||
| } | ||
|
|
||
| prop := map[string]any{ | ||
| "type": inputType, | ||
| "description": inputDescription, | ||
| } | ||
| if defaultVal, ok := inputDefMap["default"]; ok { | ||
| prop["default"] = defaultVal | ||
| } | ||
| properties[inputName] = prop | ||
|
|
||
| if inputRequired { | ||
| required = append(required, inputName) | ||
| } | ||
| } | ||
|
|
||
| return properties, required | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,263 @@ | ||
| //go:build !integration | ||
|
|
||
| package workflow | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sort" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // defaultDescFn is a simple description function used in tests. | ||
| func defaultDescFn(inputName string) string { | ||
| return fmt.Sprintf("Input parameter '%s'", inputName) | ||
| } | ||
|
|
||
| // TestBuildInputSchemaStringType tests that the default type is string. | ||
| func TestBuildInputSchemaStringType(t *testing.T) { | ||
| inputs := map[string]any{ | ||
| "message": map[string]any{ | ||
| "type": "string", | ||
| "description": "A message", | ||
| }, | ||
| } | ||
|
|
||
| properties, required := buildInputSchema(inputs, defaultDescFn) | ||
|
|
||
| prop, ok := properties["message"].(map[string]any) | ||
| require.True(t, ok, "message property should exist") | ||
| assert.Equal(t, "string", prop["type"], "type should be string") | ||
| assert.Equal(t, "A message", prop["description"], "description should match") | ||
| assert.Empty(t, required, "required should be empty") | ||
| } | ||
|
|
||
| // TestBuildInputSchemaNumberType tests number type mapping. | ||
| func TestBuildInputSchemaNumberType(t *testing.T) { | ||
| inputs := map[string]any{ | ||
| "count": map[string]any{ | ||
| "type": "number", | ||
| "description": "A count", | ||
| }, | ||
| } | ||
|
|
||
| properties, _ := buildInputSchema(inputs, defaultDescFn) | ||
|
|
||
| prop, ok := properties["count"].(map[string]any) | ||
| require.True(t, ok, "count property should exist") | ||
| assert.Equal(t, "number", prop["type"], "type should be number") | ||
| } | ||
|
|
||
| // TestBuildInputSchemaBooleanType tests boolean type mapping. | ||
| func TestBuildInputSchemaBooleanType(t *testing.T) { | ||
| inputs := map[string]any{ | ||
| "dry_run": map[string]any{ | ||
| "type": "boolean", | ||
| }, | ||
| } | ||
|
|
||
| properties, _ := buildInputSchema(inputs, defaultDescFn) | ||
|
|
||
| prop, ok := properties["dry_run"].(map[string]any) | ||
| require.True(t, ok, "dry_run property should exist") | ||
| assert.Equal(t, "boolean", prop["type"], "type should be boolean") | ||
| } | ||
|
|
||
| // TestBuildInputSchemaChoiceType tests choice type with enum options. | ||
| func TestBuildInputSchemaChoiceType(t *testing.T) { | ||
| inputs := map[string]any{ | ||
| "environment": map[string]any{ | ||
| "type": "choice", | ||
| "description": "Target environment", | ||
| "options": []any{"staging", "production"}, | ||
| }, | ||
| } | ||
|
|
||
| properties, _ := buildInputSchema(inputs, defaultDescFn) | ||
|
|
||
| prop, ok := properties["environment"].(map[string]any) | ||
| require.True(t, ok, "environment property should exist") | ||
| assert.Equal(t, "string", prop["type"], "choice maps to string") | ||
| assert.Equal(t, []any{"staging", "production"}, prop["enum"], "enum values should match") | ||
| } | ||
|
|
||
| // TestBuildInputSchemaChoiceWithDefault tests that choice type preserves default. | ||
| func TestBuildInputSchemaChoiceWithDefault(t *testing.T) { | ||
| inputs := map[string]any{ | ||
| "environment": map[string]any{ | ||
| "type": "choice", | ||
| "description": "Target environment", | ||
| "options": []any{"staging", "production"}, | ||
| "default": "staging", | ||
| }, | ||
| } | ||
|
|
||
| properties, _ := buildInputSchema(inputs, defaultDescFn) | ||
|
|
||
| prop, ok := properties["environment"].(map[string]any) | ||
| require.True(t, ok, "environment property should exist") | ||
| assert.Equal(t, "string", prop["type"], "choice maps to string") | ||
| assert.Equal(t, []any{"staging", "production"}, prop["enum"], "enum values should match") | ||
| assert.Equal(t, "staging", prop["default"], "default should be preserved for choice") | ||
| } | ||
|
|
||
| // TestBuildInputSchemaEnvironmentType tests environment type mapping. | ||
| func TestBuildInputSchemaEnvironmentType(t *testing.T) { | ||
| inputs := map[string]any{ | ||
| "deploy_env": map[string]any{ | ||
| "type": "environment", | ||
| "description": "Deployment environment", | ||
| }, | ||
| } | ||
|
|
||
| properties, _ := buildInputSchema(inputs, defaultDescFn) | ||
|
|
||
| prop, ok := properties["deploy_env"].(map[string]any) | ||
| require.True(t, ok, "deploy_env property should exist") | ||
| assert.Equal(t, "string", prop["type"], "environment maps to string") | ||
| } | ||
|
|
||
| // TestBuildInputSchemaDefaultPropagation tests that default values are propagated. | ||
| func TestBuildInputSchemaDefaultPropagation(t *testing.T) { | ||
| inputs := map[string]any{ | ||
| "version": map[string]any{ | ||
| "type": "string", | ||
| "description": "Version", | ||
| "default": "latest", | ||
| }, | ||
| } | ||
|
|
||
| properties, _ := buildInputSchema(inputs, defaultDescFn) | ||
|
|
||
| prop, ok := properties["version"].(map[string]any) | ||
| require.True(t, ok, "version property should exist") | ||
| assert.Equal(t, "latest", prop["default"], "default value should be set") | ||
| } | ||
|
|
||
| // TestBuildInputSchemaRequiredInputs tests required field tracking. | ||
| func TestBuildInputSchemaRequiredInputs(t *testing.T) { | ||
| inputs := map[string]any{ | ||
| "name": map[string]any{ | ||
| "type": "string", | ||
| "required": true, | ||
| }, | ||
| "optional": map[string]any{ | ||
| "type": "string", | ||
| "required": false, | ||
| }, | ||
| } | ||
|
|
||
| _, required := buildInputSchema(inputs, defaultDescFn) | ||
|
|
||
| assert.Contains(t, required, "name", "name should be required") | ||
| assert.NotContains(t, required, "optional", "optional should not be required") | ||
| } | ||
|
|
||
| // TestBuildInputSchemaRequiredSorting tests that required list order is deterministic | ||
| // across multiple runs (callers typically sort the result). | ||
| func TestBuildInputSchemaRequiredSorting(t *testing.T) { | ||
| inputs := map[string]any{ | ||
| "z_param": map[string]any{"type": "string", "required": true}, | ||
| "a_param": map[string]any{"type": "string", "required": true}, | ||
| "m_param": map[string]any{"type": "string", "required": true}, | ||
| } | ||
|
|
||
| for i := range 10 { | ||
| _, required := buildInputSchema(inputs, defaultDescFn) | ||
| sort.Strings(required) | ||
| assert.Equal(t, []string{"a_param", "m_param", "z_param"}, required, | ||
| "required should be sortable to deterministic order (iteration %d)", i) | ||
| } | ||
| } | ||
|
|
||
| // TestBuildInputSchemaSkipsInvalidDefs tests that non-map input definitions are skipped. | ||
| func TestBuildInputSchemaSkipsInvalidDefs(t *testing.T) { | ||
| inputs := map[string]any{ | ||
| "valid": map[string]any{ | ||
| "type": "string", | ||
| "description": "Valid input", | ||
| }, | ||
| "invalid_string": "not a map", | ||
| "invalid_number": 42, | ||
| "invalid_nil": nil, | ||
| } | ||
|
|
||
| properties, required := buildInputSchema(inputs, defaultDescFn) | ||
|
|
||
| assert.Len(t, properties, 1, "Only valid input should produce a property") | ||
| assert.Contains(t, properties, "valid", "valid input should be present") | ||
| assert.Empty(t, required, "no required inputs") | ||
| } | ||
|
|
||
| // TestBuildInputSchemaEmptyInputs tests that empty inputs produce empty results. | ||
| func TestBuildInputSchemaEmptyInputs(t *testing.T) { | ||
| properties, required := buildInputSchema(make(map[string]any), defaultDescFn) | ||
|
|
||
| assert.Empty(t, properties, "properties should be empty") | ||
| assert.Empty(t, required, "required should be empty") | ||
| } | ||
|
|
||
| // TestBuildInputSchemaNilInputs tests that nil inputs produce empty results. | ||
| func TestBuildInputSchemaNilInputs(t *testing.T) { | ||
| properties, required := buildInputSchema(nil, defaultDescFn) | ||
|
|
||
| assert.Empty(t, properties, "properties should be empty") | ||
| assert.Empty(t, required, "required should be empty") | ||
| } | ||
|
|
||
| // TestBuildInputSchemaFallbackDescription tests the descriptionFn callback. | ||
| func TestBuildInputSchemaFallbackDescription(t *testing.T) { | ||
| inputs := map[string]any{ | ||
| "param": map[string]any{ | ||
| "type": "string", | ||
| // No description provided - should use fallback | ||
| }, | ||
| } | ||
|
|
||
| descFn := func(inputName string) string { | ||
| return fmt.Sprintf("Custom fallback for '%s'", inputName) | ||
| } | ||
|
|
||
| properties, _ := buildInputSchema(inputs, descFn) | ||
|
|
||
| prop, ok := properties["param"].(map[string]any) | ||
| require.True(t, ok, "param property should exist") | ||
| assert.Equal(t, "Custom fallback for 'param'", prop["description"], "should use fallback description") | ||
| } | ||
|
|
||
| // TestBuildInputSchemaChoiceWithoutOptions tests choice type without options falls back to string. | ||
| func TestBuildInputSchemaChoiceWithoutOptions(t *testing.T) { | ||
| inputs := map[string]any{ | ||
| "env": map[string]any{ | ||
| "type": "choice", | ||
| "description": "Environment", | ||
| // No options - should fall through to regular string property | ||
| }, | ||
| } | ||
|
|
||
| properties, _ := buildInputSchema(inputs, defaultDescFn) | ||
|
|
||
| prop, ok := properties["env"].(map[string]any) | ||
| require.True(t, ok, "env property should exist") | ||
| assert.Equal(t, "string", prop["type"], "choice without options maps to string") | ||
| _, hasEnum := prop["enum"] | ||
| assert.False(t, hasEnum, "should not have enum when no options") | ||
| } | ||
|
|
||
| // TestBuildInputSchemaUnknownType tests that unknown type defaults to string. | ||
| func TestBuildInputSchemaUnknownType(t *testing.T) { | ||
| inputs := map[string]any{ | ||
| "param": map[string]any{ | ||
| "type": "unknown_type", | ||
| "description": "Some param", | ||
| }, | ||
| } | ||
|
|
||
| properties, _ := buildInputSchema(inputs, defaultDescFn) | ||
|
|
||
| prop, ok := properties["param"].(map[string]any) | ||
| require.True(t, ok, "param property should exist") | ||
| assert.Equal(t, "string", prop["type"], "unknown type should default to string") | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
buildInputSchema unconditionally calls descriptionFn (line 24). If a future caller passes a nil callback, this will panic with a nil-pointer dereference. Consider handling a nil descriptionFn (e.g., default to an empty-string fallback) or explicitly documenting/panicking with a clearer message.