-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Extract aliased project mutation primitive #2923
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
+450
−0
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,132 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "reflect" | ||
| "sync" | ||
|
|
||
| "github.com/shurcooL/githubv4" | ||
| ) | ||
|
|
||
| const batchMutationWireChunkSize = 20 | ||
|
|
||
| type batchMutationKind int | ||
|
|
||
| const ( | ||
| batchMutationUpdate batchMutationKind = iota | ||
| batchMutationClear | ||
| ) | ||
|
|
||
| func (k batchMutationKind) fieldName() string { | ||
| if k == batchMutationClear { | ||
| return "clearProjectV2ItemFieldValue" | ||
| } | ||
| return "updateProjectV2ItemFieldValue" | ||
| } | ||
|
|
||
| type projectV2ItemMutationResult struct { | ||
| ProjectV2Item struct { | ||
| ID string | ||
| FullDatabaseID string `graphql:"fullDatabaseId"` | ||
| } `graphql:"projectV2Item"` | ||
| } | ||
|
|
||
| type reflectedMutationTypeKey struct { | ||
| kind batchMutationKind | ||
| size int | ||
| } | ||
|
|
||
| var reflectedMutationTypeCache sync.Map | ||
|
|
||
| // Reflected types are cached only by operation and chunk size to bound | ||
| // reflect.StructOf's runtime cache; positional names and tags keep request data | ||
| // out of type identities. The pinned Client.Mutate binds its third argument to | ||
| // $input, so item0 uses $input and later aliases use $input1, $input2, ... | ||
| // supplied through the variables map. | ||
| func buildAliasedMutationType(kind batchMutationKind, size int) reflect.Type { | ||
| key := reflectedMutationTypeKey{kind: kind, size: size} | ||
| if cached, ok := reflectedMutationTypeCache.Load(key); ok { | ||
| return cached.(reflect.Type) | ||
| } | ||
|
|
||
| resultType := reflect.TypeFor[projectV2ItemMutationResult]() | ||
| fields := make([]reflect.StructField, size) | ||
| for i := range size { | ||
| varName := "input" | ||
| if i > 0 { | ||
| varName = fmt.Sprintf("input%d", i) | ||
| } | ||
| fields[i] = reflect.StructField{ | ||
| Name: fmt.Sprintf("Item%d", i), | ||
| Type: resultType, | ||
| Tag: reflect.StructTag(fmt.Sprintf(`graphql:"item%d: %s(input: $%s)"`, i, kind.fieldName(), varName)), | ||
| } | ||
| } | ||
|
|
||
| t := reflect.StructOf(fields) | ||
| actual, _ := reflectedMutationTypeCache.LoadOrStore(key, t) | ||
| return actual.(reflect.Type) | ||
| } | ||
|
|
||
| type mutationAliasOutcome struct { | ||
| // Populated confirms this alias returned a project item, even when the | ||
| // response also contains GraphQL errors. | ||
| Populated bool | ||
| NodeID string | ||
| FullDatabaseID string | ||
| } | ||
|
|
||
| // The pinned client decodes partial data before returning GraphQL errors but | ||
| // discards errors[].path. Populated aliases confirm writes; unpopulated aliases | ||
| // remain unknown and must not be retried individually. | ||
| func executeAliasedMutation(ctx context.Context, gqlClient *githubv4.Client, kind batchMutationKind, inputs []githubv4.Input) ([]mutationAliasOutcome, error) { | ||
| if len(inputs) == 0 { | ||
| return nil, nil | ||
| } | ||
| if len(inputs) > batchMutationWireChunkSize { | ||
| return nil, fmt.Errorf("internal error: chunk of %d exceeds wire chunk size %d", len(inputs), batchMutationWireChunkSize) | ||
| } | ||
|
|
||
| mutationType := buildAliasedMutationType(kind, len(inputs)) | ||
| mutationPtr := reflect.New(mutationType) | ||
|
|
||
| var variables map[string]any | ||
| if len(inputs) > 1 { | ||
| variables = make(map[string]any, len(inputs)-1) | ||
| for i := 1; i < len(inputs); i++ { | ||
| variables[fmt.Sprintf("input%d", i)] = inputs[i] | ||
| } | ||
| } | ||
|
|
||
| mutateErr := gqlClient.Mutate(ctx, mutationPtr.Interface(), inputs[0], variables) | ||
|
|
||
| outcomes := make([]mutationAliasOutcome, len(inputs)) | ||
| elem := mutationPtr.Elem() | ||
| for i := range inputs { | ||
| result, ok := elem.Field(i).Interface().(projectV2ItemMutationResult) | ||
| if !ok || result.ProjectV2Item.ID == "" { | ||
| continue | ||
| } | ||
| outcomes[i] = mutationAliasOutcome{ | ||
| Populated: true, | ||
| NodeID: result.ProjectV2Item.ID, | ||
| FullDatabaseID: result.ProjectV2Item.FullDatabaseID, | ||
| } | ||
| } | ||
| return outcomes, mutateErr | ||
| } | ||
|
|
||
| // The pinned client's GraphQL response error type is unexported; transport and | ||
| // decoding failures must remain distinguishable. | ||
| func isGraphQLResponseError(err error) bool { | ||
| for err != nil { | ||
| errType := reflect.TypeOf(err) | ||
| if errType.PkgPath() == "github.com/shurcooL/graphql" && errType.Name() == "errors" { | ||
| return true | ||
| } | ||
| err = errors.Unwrap(err) | ||
| } | ||
| return false | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.