-
Notifications
You must be signed in to change notification settings - Fork 251
Make discussion categories case-insensitive #14820
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
274 changes: 274 additions & 0 deletions
274
actions/setup/js/create_discussion_category_normalization.test.cjs
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,274 @@ | ||
| // @ts-check | ||
| import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; | ||
| import { createRequire } from "module"; | ||
|
|
||
| const require = createRequire(import.meta.url); | ||
| const { main: createDiscussionMain } = require("./create_discussion.cjs"); | ||
|
|
||
| describe("create_discussion category normalization", () => { | ||
| let mockGithub; | ||
| let mockCore; | ||
| let mockContext; | ||
| let mockExec; | ||
| let originalEnv; | ||
|
|
||
| beforeEach(() => { | ||
| // Save original environment | ||
| originalEnv = { ...process.env }; | ||
|
|
||
| // Mock GitHub API with discussion categories | ||
| mockGithub = { | ||
| rest: {}, | ||
| graphql: vi.fn().mockImplementation((query, variables) => { | ||
| // Handle repository query (fetch categories) | ||
| if (query.includes("discussionCategories")) { | ||
| return Promise.resolve({ | ||
| repository: { | ||
| id: "R_test123", | ||
| discussionCategories: { | ||
| nodes: [ | ||
| { | ||
| id: "DIC_kwDOGFsHUM4BsUn1", | ||
| name: "General", | ||
| slug: "general", | ||
| description: "General discussions", | ||
| }, | ||
| { | ||
| id: "DIC_kwDOGFsHUM4BsUn2", | ||
| name: "Audits", | ||
| slug: "audits", | ||
| description: "Audit reports", | ||
| }, | ||
| { | ||
| id: "DIC_kwDOGFsHUM4BsUn3", | ||
| name: "Research", | ||
| slug: "research", | ||
| description: "Research discussions", | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
| // Handle create discussion mutation | ||
| if (query.includes("createDiscussion")) { | ||
| return Promise.resolve({ | ||
| createDiscussion: { | ||
| discussion: { | ||
| id: "D_test456", | ||
| number: 42, | ||
| title: variables.title, | ||
| url: "https://github.com/test-owner/test-repo/discussions/42", | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
| return Promise.reject(new Error("Unknown GraphQL query")); | ||
| }), | ||
| }; | ||
|
|
||
| // Mock Core | ||
| mockCore = { | ||
| info: vi.fn(), | ||
| warning: vi.fn(), | ||
| error: vi.fn(), | ||
| setOutput: vi.fn(), | ||
| }; | ||
|
|
||
| // Mock Context | ||
| mockContext = { | ||
| repo: { owner: "test-owner", repo: "test-repo" }, | ||
| runId: 12345, | ||
| payload: { | ||
| repository: { | ||
| html_url: "https://github.com/test-owner/test-repo", | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| // Mock Exec | ||
| mockExec = { | ||
| exec: vi.fn().mockResolvedValue(0), | ||
| }; | ||
|
|
||
| // Set globals | ||
| global.github = mockGithub; | ||
| global.core = mockCore; | ||
| global.context = mockContext; | ||
| global.exec = mockExec; | ||
|
|
||
| // Set required environment variables | ||
| process.env.GH_AW_WORKFLOW_NAME = "Test Workflow"; | ||
| process.env.GH_AW_WORKFLOW_ID = "test-workflow"; | ||
| process.env.GH_AW_WORKFLOW_SOURCE_URL = "https://github.com/owner/repo/blob/main/workflow.md"; | ||
| process.env.GITHUB_SERVER_URL = "https://github.com"; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| // Restore environment | ||
| process.env = originalEnv; | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("should match category name case-insensitively (lowercase config, capitalized repo)", async () => { | ||
| const handler = await createDiscussionMain({ | ||
| max: 5, | ||
| category: "audits", // lowercase config | ||
| }); | ||
|
|
||
| const result = await handler( | ||
| { | ||
| title: "Test Discussion", | ||
| body: "This is a test discussion.", | ||
| }, | ||
| {} | ||
| ); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| expect(result.number).toBe(42); | ||
|
|
||
| // Verify the correct category ID was used (Audits with capital A) | ||
| const createMutationCall = mockGithub.graphql.mock.calls.find(call => call[0].includes("createDiscussion")); | ||
| expect(createMutationCall).toBeDefined(); | ||
| expect(createMutationCall[1].categoryId).toBe("DIC_kwDOGFsHUM4BsUn2"); // Audits category | ||
| }); | ||
|
|
||
| it("should match category name case-insensitively (capitalized config, capitalized repo)", async () => { | ||
| const handler = await createDiscussionMain({ | ||
| max: 5, | ||
| category: "Audits", // Capitalized config (user error) | ||
| }); | ||
|
|
||
| const result = await handler( | ||
| { | ||
| title: "Test Discussion", | ||
| body: "This is a test discussion.", | ||
| }, | ||
| {} | ||
| ); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| expect(result.number).toBe(42); | ||
|
|
||
| // Verify the correct category ID was used | ||
| const createMutationCall = mockGithub.graphql.mock.calls.find(call => call[0].includes("createDiscussion")); | ||
| expect(createMutationCall).toBeDefined(); | ||
| expect(createMutationCall[1].categoryId).toBe("DIC_kwDOGFsHUM4BsUn2"); // Audits category | ||
| }); | ||
|
|
||
| it("should match category name case-insensitively (mixed case config)", async () => { | ||
| const handler = await createDiscussionMain({ | ||
| max: 5, | ||
| category: "AuDiTs", // Mixed case (should still match) | ||
| }); | ||
|
|
||
| const result = await handler( | ||
| { | ||
| title: "Test Discussion", | ||
| body: "This is a test discussion.", | ||
| }, | ||
| {} | ||
| ); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| expect(result.number).toBe(42); | ||
|
|
||
| // Verify the correct category ID was used | ||
| const createMutationCall = mockGithub.graphql.mock.calls.find(call => call[0].includes("createDiscussion")); | ||
| expect(createMutationCall).toBeDefined(); | ||
| expect(createMutationCall[1].categoryId).toBe("DIC_kwDOGFsHUM4BsUn2"); // Audits category | ||
| }); | ||
|
|
||
| it("should match category slug case-insensitively", async () => { | ||
| const handler = await createDiscussionMain({ | ||
| max: 5, | ||
| category: "RESEARCH", // Uppercase slug | ||
| }); | ||
|
|
||
| const result = await handler( | ||
| { | ||
| title: "Test Discussion", | ||
| body: "This is a test discussion.", | ||
| }, | ||
| {} | ||
| ); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| expect(result.number).toBe(42); | ||
|
|
||
| // Verify the correct category ID was used (Research) | ||
| const createMutationCall = mockGithub.graphql.mock.calls.find(call => call[0].includes("createDiscussion")); | ||
| expect(createMutationCall).toBeDefined(); | ||
| expect(createMutationCall[1].categoryId).toBe("DIC_kwDOGFsHUM4BsUn3"); // Research category | ||
| }); | ||
|
|
||
| it("should preserve category IDs (exact match, case-sensitive)", async () => { | ||
| const handler = await createDiscussionMain({ | ||
| max: 5, | ||
| category: "DIC_kwDOGFsHUM4BsUn3", // Direct category ID | ||
| }); | ||
|
|
||
| const result = await handler( | ||
| { | ||
| title: "Test Discussion", | ||
| body: "This is a test discussion.", | ||
| }, | ||
| {} | ||
| ); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| expect(result.number).toBe(42); | ||
|
|
||
| // Verify the exact category ID was used | ||
| const createMutationCall = mockGithub.graphql.mock.calls.find(call => call[0].includes("createDiscussion")); | ||
| expect(createMutationCall).toBeDefined(); | ||
| expect(createMutationCall[1].categoryId).toBe("DIC_kwDOGFsHUM4BsUn3"); | ||
| }); | ||
|
|
||
| it("should use item category over config category (case-insensitive)", async () => { | ||
| const handler = await createDiscussionMain({ | ||
| max: 5, | ||
| category: "general", // Config says general | ||
| }); | ||
|
|
||
| const result = await handler( | ||
| { | ||
| title: "Test Discussion", | ||
| body: "This is a test discussion.", | ||
| category: "AUDITS", // Item overrides with uppercase | ||
| }, | ||
| {} | ||
| ); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| expect(result.number).toBe(42); | ||
|
|
||
| // Verify Audits category was used (from item, not config) | ||
| const createMutationCall = mockGithub.graphql.mock.calls.find(call => call[0].includes("createDiscussion")); | ||
| expect(createMutationCall).toBeDefined(); | ||
| expect(createMutationCall[1].categoryId).toBe("DIC_kwDOGFsHUM4BsUn2"); // Audits category | ||
| }); | ||
|
|
||
| it("should fallback to first category when no match found", async () => { | ||
| const handler = await createDiscussionMain({ | ||
| max: 5, | ||
| category: "NonExistentCategory", | ||
| }); | ||
|
|
||
| const result = await handler( | ||
| { | ||
| title: "Test Discussion", | ||
| body: "This is a test discussion.", | ||
| }, | ||
| {} | ||
| ); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| expect(result.number).toBe(42); | ||
|
|
||
| // Verify fallback to first category (General) | ||
| const createMutationCall = mockGithub.graphql.mock.calls.find(call => call[0].includes("createDiscussion")); | ||
| expect(createMutationCall).toBeDefined(); | ||
| expect(createMutationCall[1].categoryId).toBe("DIC_kwDOGFsHUM4BsUn1"); // General (first) | ||
| }); | ||
| }); |
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -92,10 +92,8 @@ func (c *Compiler) parseDiscussionsConfig(outputMap map[string]any) *CreateDiscu | |||||
| return nil // Invalid configuration, return nil to cause validation error | ||||||
| } | ||||||
|
|
||||||
| // Validate category naming convention (lowercase, preferably plural) | ||||||
| if validateDiscussionCategory(config.Category, discussionLog, c.markdownPath) { | ||||||
| return nil // Invalid configuration, return nil to cause validation error | ||||||
| } | ||||||
| // Normalize and validate category naming convention | ||||||
|
||||||
| // Normalize and validate category naming convention | |
| // Normalize category naming convention for consistent handling and logging |
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.
resolveCategoryIdnow callscategoryToMatch.toLowerCase(), which will throw aTypeErrorifitem.category(orconfig.category) is not a string (agent output can be non-string). Consider coercing to string (e.g.,String(categoryToMatch)) or guarding withtypeof categoryToMatch === "string"before lowercasing so invalid types fall back gracefully instead of crashing the handler.See below for a potential fix: