Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 11, 2025

Add Discussion Support for add-comment Safe Output ✅

Summary

Successfully implemented comprehensive support for GitHub Discussions in the add-comment safe output feature. Workflows can now create comments on discussions in addition to issues and pull requests.

Completed Tasks

  • Update JSON schema to include discussions in add-comment description
  • Update Go code (add_comment.go) to generate "on" condition including github.event.discussion.number
  • Update JavaScript (add_comment.cjs) to handle discussion comments via GraphQL addDiscussionComment mutation
  • Update TypeScript type definitions for discussion support
  • Add test for discussion comment support
  • Update documentation
  • Manual testing via compiled workflows
  • All unit tests pass (100% success rate)
  • Code formatting validated
  • Refactored to use variadic buildOr with DisjunctionNode
  • Added discussion and discussion_comment events to schema
  • Updated dev.md to demonstrate discussion events with workflow_dispatch support

Recent Changes (Latest Commit)

  • Added workflow_dispatch support: dev.md now supports manual triggering with a discussion_number input parameter
  • Dynamic discussion targeting: The prompt now uses ${{ github.event.inputs.discussion_number || github.event.discussion.number }} to support both manual dispatch and automatic discussion events
  • Added github.event.discussion.number to allowed expressions: Updated constants to allow this expression in workflow markdown
  • Added target: "*": Allows the workflow to comment on any discussion when using workflow_dispatch

Key Changes

1. Schema (pkg/parser/schemas/main_workflow_schema.json)

  • Updated descriptions to mention discussions alongside issues and PRs
  • Modified target field to include discussion numbers
  • Added discussion event with types: created, edited, deleted, transferred, pinned, unpinned, labeled, unlabeled, locked, unlocked, category_changed, answered, unanswered
  • Added discussion_comment event with types: created, edited, deleted

2. Go Code (pkg/workflow/add_comment.go & pkg/workflow/expressions.go)

  • Refactored buildOr: Now accepts variadic arguments ...ConditionNode
  • Handles 0 arguments (returns nil), 1 argument (returns as-is), 2 arguments (simple OR), 3+ arguments (uses DisjunctionNode)
  • NEW: Uses DisjunctionNode for 3+ conditions to avoid deep nesting and improve readability
  • Uses single variadic buildOr to check for issue, PR, or discussion events
  • Added discussions: write permission to add_comment job

3. Constants (pkg/constants/constants.go)

  • NEW: Added github.event.discussion.number to AllowedExpressions list for expression safety validation

4. JavaScript (pkg/workflow/js/add_comment.cjs)

  • Added isDiscussionContext detection for discussion/discussion_comment events
  • Implemented GraphQL query to fetch discussion ID
  • Implemented addDiscussionComment mutation for creating discussion comments
  • Handles both issue_number and discussion_number fields in comment items
  • Normalized GraphQL response to match REST API structure

5. Type Definitions (pkg/workflow/js/types/safe-outputs.d.ts)

  • Added optional discussion_number field to AddCommentItem interface

6. MCP Server (pkg/workflow/js/safe_outputs_mcp_server.cjs)

  • Updated tool description and input schema to include discussions

7. Tests

  • Added pkg/workflow/add_comment_discussion_test.go verifying all discussion support components
  • Added TestBuildOr in expressions_test.go with comprehensive test cases for the variadic function
  • Updated test expectations to match DisjunctionNode flat rendering
  • All existing tests continue to pass

8. Documentation (docs/src/content/docs/reference/safe-outputs.md)

  • Updated all descriptions to mention discussions
  • Added discussion-specific example workflow
  • Documented discussions: write permission requirement

9. Example (dev.md)

  • Changed to use add-comment instead of create-issue
  • Added both discussion and discussion_comment events as triggers
  • NEW: Added workflow_dispatch with discussion_number input parameter
  • NEW: Added target: "*" to support manual discussion number specification
  • Updated instruction to dynamically reference discussion number from input or event context

Technical Implementation

Improved buildOr Function

func buildOr(conditions ...ConditionNode) ConditionNode {
    if len(conditions) == 0 {
        return nil
    }
    if len(conditions) == 1 {
        return conditions[0]
    }
    if len(conditions) == 2 {
        return &OrNode{Left: conditions[0], Right: conditions[1]}
    }
    // Use DisjunctionNode for 3+ conditions to avoid deep nesting
    return &DisjunctionNode{Terms: conditions}
}

workflow_dispatch Configuration (dev.md)

on:
  discussion:
    types: [created]
  discussion_comment:
    types: [created]
  workflow_dispatch:
    inputs:
      discussion_number:
        description: "Discussion number to add comment to"
        required: true
        type: string

Dynamic Prompt

Write a delightful poem about the last 3 pull requests and add it as a comment to discussion #${{ github.event.inputs.discussion_number || github.event.discussion.number }}.

Benefits of DisjunctionNode

  • Cleaner rendering: a || b || c instead of (a) || ((b) || (c))
  • No deep nesting: Flat structure easier to read and maintain
  • Consistent with existing codebase: Leverages existing DisjunctionNode type

Event Detection
The workflow detects three context types:

  • Issues: issues, issue_comment
  • Pull Requests: pull_request, pull_request_review, pull_request_review_comment
  • Discussions: discussion, discussion_comment

API Strategy

  • REST API: Used for issues and PRs (github.rest.issues.createComment)
  • GraphQL API: Used for discussions (addDiscussionComment mutation)

Generated Condition (Improved)

if: >
  ((always()) && (contains(needs.agent.outputs.output_types, 'add-comment'))) &&
  (github.event.issue.number || github.event.pull_request.number || github.event.discussion.number)

Permissions

permissions:
  contents: read
  issues: write
  pull-requests: write
  discussions: write

Testing Results

✅ All unit tests pass (6.7s)
✅ Build successful
✅ Code formatted
✅ Variadic buildOr with DisjunctionNode tested with 7 test cases
✅ dev.md compiles successfully with workflow_dispatch and discussion events
✅ Discussion and discussion_comment events validated in schema
✅ Expression safety validation includes github.event.discussion.number

Usage Examples

Discussion Workflow with Manual Dispatch (from dev.md)

on:
  discussion:
    types: [created]
  discussion_comment:
    types: [created]
  workflow_dispatch:
    inputs:
      discussion_number:
        description: "Discussion number to add comment to"
        required: true
        type: string
permissions:
  discussions: write
safe-outputs:
  add-comment:
    target: "*"

Multi-Target Support
Agent can specify:

  • issue_number: 42 for issue comments
  • discussion_number: 42 for discussion comments
  • Use workflow_dispatch input to target specific discussion

Backward Compatibility

100% backward compatible

  • All existing issue/PR workflows work unchanged
  • No breaking changes to API or configuration
  • Discussion support is purely additive
  • Variadic buildOr handles legacy 2-argument calls
  • DisjunctionNode provides cleaner output without changing semantics
  • workflow_dispatch input is optional and doesn't affect automatic triggers
Original prompt

Add support for discussions in the "add-comment" safe output.

  • update schema
  • update generation of "on" condition involving discussions
  • use graphql to generate a new discussion comment

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 11, 2025 05:16
- Update schema to include discussions in add-comment description
- Add github.event.discussion.number to job condition in add_comment.go
- Add discussions: write permission to add_comment job
- Update add_comment.cjs to detect discussion context and use GraphQL mutation
- Update TypeScript types to include discussion_number field
- Update MCP server definition to include discussion support
- Add test for discussion support in add_comment_discussion_test.go

Co-authored-by: pelikhan <[email protected]>
- Update safe-outputs.md to include discussion context
- Add discussion example workflow
- Update descriptions to mention discussions alongside issues and PRs

Co-authored-by: pelikhan <[email protected]>
Copilot AI changed the title [WIP] Add support for discussions in add-comment safe output Add discussion support to add-comment safe output Oct 11, 2025
Copilot AI requested a review from pelikhan October 11, 2025 05:26
Copilot finished work on behalf of pelikhan October 11, 2025 05:26
Copy link
Contributor

@pelikhan pelikhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot update dev.md to use add-comment and trigger on a discussion comment. Write a poem to the comment.

- Add buildDisjunction function to handle multiple OR conditions elegantly
- Update add_comment.go to use buildDisjunction instead of nested buildOr
- Update dev.md to use add-comment safe output with discussion support
- Change dev.md to write a poem about PRs as a comment

Co-authored-by: pelikhan <[email protected]>
Copy link
Contributor Author

Copilot AI commented Oct 11, 2025

@copilot update dev.md to use add-comment and trigger on a discussion comment. Write a poem to the comment.

Updated dev.md to:

  • Use add-comment safe output instead of create-issue
  • Added target: "*" to allow commenting on any discussion
  • Changed the instruction to write a delightful poem about the last 3 PRs and add it as a comment to discussion rejig docs #1
  • Added discussions: write permission

The workflow now demonstrates the new discussion comment support!

Commit: 6e447b5

Copilot finished work on behalf of pelikhan October 11, 2025 05:40
Copilot AI requested a review from pelikhan October 11, 2025 05:40
@@ -1,24 +1,21 @@
---
on:
workflow_dispatch:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot add discussion created and discussion comment created events.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Added discussion and discussion_comment events to the schema with their respective event types:

discussion: created, edited, deleted, transferred, pinned, unpinned, labeled, unlabeled, locked, unlocked, category_changed, answered, unanswered

discussion_comment: created, edited, deleted

Updated dev.md to trigger on both discussion and discussion_comment created events. The workflow now demonstrates commenting on discussions when they are created or when comments are added.

Commit: a0e2d76

- Merged buildOr and buildDisjunction into single variadic buildOr function
- Added comprehensive tests for variadic buildOr function
- Added discussion and discussion_comment events to schema
- Updated dev.md to trigger on discussion and discussion_comment created events
- All tests pass successfully

Co-authored-by: pelikhan <[email protected]>
Copilot finished work on behalf of pelikhan October 11, 2025 06:00
Copilot AI requested a review from pelikhan October 11, 2025 06:00
- Updated buildOr to return DisjunctionNode for 3+ conditions instead of nested OrNode structures
- DisjunctionNode renders as flat OR chain (a || b || c) avoiding deep nesting
- Updated test expectations to match new flat rendering
- All tests pass successfully

Co-authored-by: pelikhan <[email protected]>
Copilot AI requested a review from pelikhan October 11, 2025 06:08
Copilot finished work on behalf of pelikhan October 11, 2025 06:08
@pelikhan
Copy link
Contributor

@copilot update dev.md to support workflow dispatch with a discussion number id.

The input is used in the prompt to tell the agent where to add the comment

- Added workflow_dispatch trigger with discussion_number input
- Added target: "*" to add-comment to allow specifying discussion number
- Updated prompt to use github.event.inputs.discussion_number or github.event.discussion.number
- Added github.event.discussion.number to allowed expressions in constants
- Workflow now supports manual dispatch with custom discussion number
- All tests pass successfully

Co-authored-by: pelikhan <[email protected]>
Copy link
Contributor Author

Copilot AI commented Oct 11, 2025

@copilot update dev.md to support workflow dispatch with a discussion number id.

The input is used in the prompt to tell the agent where to add the comment

Done! Updated dev.md to support workflow_dispatch with a discussion_number input:

Changes:

  • Added workflow_dispatch trigger with discussion_number input parameter (type: string, required: true)
  • Added target: "*" to add-comment configuration to allow specifying discussion number
  • Updated prompt to use ${{ github.event.inputs.discussion_number || github.event.discussion.number }}
    • Uses input value when manually dispatched
    • Falls back to event discussion number when triggered by discussion events
  • Added github.event.discussion.number to allowed expressions in constants

Usage:
When manually dispatching, users can specify any discussion number. The agent will use that number in the prompt to know where to add the comment.

Commit: 8f2a4b9

Copilot finished work on behalf of pelikhan October 11, 2025 15:01
@dsyme
Copy link
Contributor

dsyme commented Oct 11, 2025

Done

@dsyme dsyme closed this Oct 11, 2025
@pelikhan pelikhan deleted the copilot/add-discussion-support branch October 17, 2025 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants