-
Notifications
You must be signed in to change notification settings - Fork 432
[code-scanning-fix] Fix workflow-graphql-static-concat: extract GraphQL query to named constant #41357
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
[code-scanning-fix] Fix workflow-graphql-static-concat: extract GraphQL query to named constant #41357
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -54,6 +54,15 @@ import ( | |||||||||||||
|
|
||||||||||||||
| var repositoryFeaturesLog = newValidationLogger("repository_features") | ||||||||||||||
|
|
||||||||||||||
| // checkRepositoryHasDiscussionsQuery is a hardcoded static GraphQL query template used to check | ||||||||||||||
| // if discussions are enabled for a repository. Declared as a named constant to make clear | ||||||||||||||
| // it is not user-controlled input (CWE-89 / workflow-graphql-static-concat). | ||||||||||||||
| const checkRepositoryHasDiscussionsQuery = `query($owner: String!, $name: String!) { | ||||||||||||||
| repository(owner: $owner, name: $name) { | ||||||||||||||
| hasDiscussionsEnabled | ||||||||||||||
| } | ||||||||||||||
| }` | ||||||||||||||
|
|
||||||||||||||
| // RepositoryFeatures holds cached information about repository capabilities | ||||||||||||||
| type RepositoryFeatures struct { | ||||||||||||||
| HasDiscussions bool | ||||||||||||||
|
|
@@ -252,22 +261,15 @@ func checkRepositoryHasDiscussions(repo string, verbose bool) (bool, error) { | |||||||||||||
|
|
||||||||||||||
| // checkRepositoryHasDiscussionsUncached checks if a repository has discussions enabled (no caching) | ||||||||||||||
| func checkRepositoryHasDiscussionsUncached(repo string) (bool, error) { | ||||||||||||||
| // Use GitHub GraphQL API to check if discussions are enabled | ||||||||||||||
| // The hasDiscussionsEnabled field is the canonical way to check this | ||||||||||||||
| query := `query($owner: String!, $name: String!) { | ||||||||||||||
| repository(owner: $owner, name: $name) { | ||||||||||||||
| hasDiscussionsEnabled | ||||||||||||||
| } | ||||||||||||||
| }` | ||||||||||||||
|
|
||||||||||||||
| // Split repo into owner and name | ||||||||||||||
| parts := strings.SplitN(repo, "/", 2) | ||||||||||||||
| if len(parts) != 2 || parts[0] == "" || parts[1] == "" { | ||||||||||||||
| return false, fmt.Errorf("invalid repository format: %s. Expected format: owner/repo. Example: github/gh-aw", repo) | ||||||||||||||
| } | ||||||||||||||
| owner, name := parts[0], parts[1] | ||||||||||||||
|
|
||||||||||||||
| // Execute GraphQL query using gh CLI | ||||||||||||||
| // Execute GraphQL query using gh CLI. | ||||||||||||||
| // checkRepositoryHasDiscussionsQuery is a package-level constant — not user-controlled. | ||||||||||||||
| type GraphQLResponse struct { | ||||||||||||||
| Data struct { | ||||||||||||||
| Repository struct { | ||||||||||||||
|
|
@@ -276,7 +278,7 @@ func checkRepositoryHasDiscussionsUncached(repo string) (bool, error) { | |||||||||||||
| } `json:"data"` | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| stdOut, _, err := gh.Exec("api", "graphql", "-f", "query="+query, | ||||||||||||||
| stdOut, _, err := gh.Exec("api", "graphql", "-f", "query="+checkRepositoryHasDiscussionsQuery, | ||||||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/zoom-out] 💡 Affected lines in project_command.go
Even with |
||||||||||||||
| "-f", "owner="+owner, "-f", "name="+name) | ||||||||||||||
|
Comment on lines
+281
to
282
|
||||||||||||||
| if err != nil { | ||||||||||||||
| return false, fmt.Errorf("failed to query discussions status: %w", err) | ||||||||||||||
|
|
||||||||||||||
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.
[/improve-codebase-architecture] This call-site comment repeats what the package-level
constdoc comment (lines 57–59) already explains — consider removing it to keep the call site clean.💡 Detail
The
constdeclaration already documents:The name
checkRepositoryHasDiscussionsQueryitself carries enough signal at the call site. Removing the inline comment reduces redundancy without losing any safety intent.