Skip to content

Fix: wire inputs.item_number fallback for label trigger shorthand workflows#19795

Merged
pelikhan merged 6 commits intomainfrom
copilot/fix-workflow-dispatch-item-number
Mar 6, 2026
Merged

Fix: wire inputs.item_number fallback for label trigger shorthand workflows#19795
pelikhan merged 6 commits intomainfrom
copilot/fix-workflow-dispatch-item-number

Conversation

Copy link
Contributor

Copilot AI commented Mar 6, 2026

Label trigger shorthand (on: pull_request labeled my-label) auto-generates a workflow_dispatch with inputs.item_number, but the compiler never wired that input as a fallback for entity number expressions. Manual dispatch runs received empty PR/issue/discussion numbers in the prompt, and concurrent dispatches for different items could cancel each other via the same concurrency group.

Changes

  • compiler_orchestrator_workflow.go — Added extractDispatchItemNumber(frontmatter map[string]any) bool that detects the label-shorthand signature by walking the structured in-memory frontmatter map (on → workflow_dispatch → inputs → item_number) produced during preprocessing — no YAML string re-parsing. Called from extractYAMLSections to populate the new WorkflowData.HasDispatchItemNumber field.

  • compiler_types.go — Added HasDispatchItemNumber bool to WorkflowData as a typed, first-class field set at parse time.

  • expression_extraction.go — Added applyWorkflowDispatchFallbacks(mappings []*ExpressionMapping, hasItemNumber bool) which patches the three canonical entity number expressions:

    github.event.pull_request.number → github.event.pull_request.number || inputs.item_number
    github.event.issue.number        → github.event.issue.number        || inputs.item_number
    github.event.discussion.number   → github.event.discussion.number   || inputs.item_number
    

    The EnvVar name (e.g. GH_AW_GITHUB_EVENT_PULL_REQUEST_NUMBER) is preserved so existing env-var references remain stable.

  • compiler_yaml.go — Calls applyWorkflowDispatchFallbacks after all expression mappings (including inline-mode ones) have been collected, ensuring the fallback is applied consistently in both normal and inlined-imports modes.

  • concurrency.go — Added entityConcurrencyKey helper to eliminate duplicated key-building. buildConcurrencyGroupKeys reads workflowData.HasDispatchItemNumber and inserts inputs.item_number into the fallback chain for PR/issue/discussion workflows, e.g.:

    ${{ github.event.pull_request.number || inputs.item_number || github.ref || github.run_id }}
    

    This prevents concurrent manual dispatches for different items from canceling each other.

  • Tests — Unit tests for extractDispatchItemNumber, applyWorkflowDispatchFallbacks, extractYAMLSections field population, and three new buildConcurrencyGroupKeys cases covering all three entity types with item_number. Parser integration tests in schedule_preprocessing_test.go verify the full pipeline from preprocessScheduleFields through extractDispatchItemNumber for all three entity types (pull_request, issues, discussion) and edge cases (comma-only labels error, 2-token fallthrough producing no item_number).


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…kflows

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix workflow_dispatch item_number for label trigger shorthand Fix: wire inputs.item_number fallback for label trigger shorthand workflows Mar 6, 2026
@pelikhan pelikhan marked this pull request as ready for review March 6, 2026 03:32
Copilot AI review requested due to automatic review settings March 6, 2026 03:32
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes manual workflow_dispatch runs for label-trigger shorthand workflows by wiring inputs.item_number as a fallback for entity-number expressions and by ensuring concurrency groups differ per dispatched item.

Changes:

  • Add detection for workflow_dispatch + inputs.item_number and patch canonical entity-number expressions to fall back to inputs.item_number.
  • Update concurrency key generation to include inputs.item_number in the fallback chain to prevent cross-item cancellation.
  • Add unit tests covering the new detection, fallback application, and concurrency behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
pkg/workflow/expression_extraction.go Adds hasWorkflowDispatchItemNumber and applyWorkflowDispatchFallbacks to inject inputs.item_number fallbacks into extracted expressions.
pkg/workflow/compiler_yaml.go Calls applyWorkflowDispatchFallbacks during prompt generation so expression mappings get updated before substitution.
pkg/workflow/concurrency.go Extends concurrency group key expressions to include inputs.item_number where appropriate.
pkg/workflow/expression_extraction_test.go Adds unit tests for detection and fallback patching of entity-number expressions.
pkg/workflow/concurrency_test.go Adds test cases verifying concurrency keys include inputs.item_number for PR/issue/discussion label shorthand workflows.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +256 to +260
// hasWorkflowDispatchItemNumber returns true when the rendered "on:" YAML contains
// a workflow_dispatch trigger that exposes an item_number input. This is the
// signature produced by the label trigger shorthand (e.g. "on: pull_request labeled my-label").
func hasWorkflowDispatchItemNumber(on string) bool {
return strings.Contains(on, "workflow_dispatch") && strings.Contains(on, "item_number")
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

hasWorkflowDispatchItemNumber is likely to produce false positives because it matches any substring "item_number" (e.g., an input named my_item_number, a description mentioning item_number, etc.). That can cause later code to emit expressions referencing inputs.item_number even when that input is not actually defined, which may break validation (e.g., actionlint) and won’t fix the dispatch-number issue.

Consider tightening detection to match YAML keys (e.g., require workflow_dispatch: and item_number: and ideally inputs:) using a regex with word boundaries / line anchors, or parse the on: YAML into a map and check on.workflow_dispatch.inputs.item_number explicitly.

Suggested change
// hasWorkflowDispatchItemNumber returns true when the rendered "on:" YAML contains
// a workflow_dispatch trigger that exposes an item_number input. This is the
// signature produced by the label trigger shorthand (e.g. "on: pull_request labeled my-label").
func hasWorkflowDispatchItemNumber(on string) bool {
return strings.Contains(on, "workflow_dispatch") && strings.Contains(on, "item_number")
// workflowDispatchItemNumberRegex matches a workflow_dispatch trigger that defines
// an inputs.item_number key in the rendered "on:" YAML. It looks for:
//
// workflow_dispatch:
// inputs:
// item_number:
//
// with arbitrary but consistent indentation.
var workflowDispatchItemNumberRegex = regexp.MustCompile(
`(?m)^\s*workflow_dispatch:\s*(?:\n[ \t]+.*)*?\n[ \t]+inputs:\s*(?:\n[ \t]+.*)*?\n[ \t]+item_number:\s*`,
)
// hasWorkflowDispatchItemNumber returns true when the rendered "on:" YAML contains
// a workflow_dispatch trigger that exposes an item_number input. This is the
// signature produced by the label trigger shorthand (e.g. "on: pull_request labeled my-label").
func hasWorkflowDispatchItemNumber(on string) bool {
return workflowDispatchItemNumberRegex.MatchString(on)

Copilot uses AI. Check for mistakes.
Comment on lines +359 to +363
// Enhance entity number expressions with || inputs.item_number fallback when the
// workflow has a workflow_dispatch trigger with item_number (generated by the label
// trigger shorthand). Without this, manual dispatch runs receive an empty entity number.
applyWorkflowDispatchFallbacks(expressionMappings, data.On)

Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

applyWorkflowDispatchFallbacks is invoked before the inline-mode expression extraction (Step 2). When c.inlinePrompt or data.InlinedImports is true, the main markdown expressions are extracted and appended to expressionMappings after this call, so the newly added mappings (including github.event.*.number) will not receive the || inputs.item_number fallback.

To make the fallback effective in inline/inlined-import modes, call applyWorkflowDispatchFallbacks after all expression mappings have been collected (e.g., after Step 2 appends inlineExprMappings), or call it again after appending those mappings.

Copilot uses AI. Check for mistakes.
Comment on lines +199 to +205
} else if isPullRequestWorkflow(workflowData.On) && isIssueWorkflow(workflowData.On) {
// Mixed workflows with both issue and PR triggers
keys = append(keys, "${{ github.event.issue.number || github.event.pull_request.number || github.run_id }}")
if hasItemNumber {
keys = append(keys, "${{ github.event.issue.number || github.event.pull_request.number || inputs.item_number || github.run_id }}")
} else {
keys = append(keys, "${{ github.event.issue.number || github.event.pull_request.number || github.run_id }}")
}
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

The hasItemNumber branching introduces a lot of duplicated keys = append(keys, "${{ ... }}") strings across the trigger cases, which makes this function harder to maintain and increases the risk of future inconsistencies.

Consider building the base expression for each trigger case once, then conditionally inserting || inputs.item_number (e.g., via a small helper that takes the primary chain and injects the fallback when hasItemNumber is true).

Copilot uses AI. Check for mistakes.
@pelikhan
Copy link
Contributor

pelikhan commented Mar 6, 2026

@copilot make sure the feature is integrated in the overall parser of schedule. It should be parsed and return a structure data structure that indicates that itemNumber is used; instaed of parsing things again here.

…tured frontmatter parsing

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 6, 2026

@copilot make sure the feature is integrated in the overall parser of schedule. It should be parsed and return a structure data structure that indicates that itemNumber is used; instaed of parsing thi...

Done in 9424ecc. Instead of re-parsing the rendered on: YAML string:

  • Added extractDispatchItemNumber(frontmatter map[string]any) bool in compiler_orchestrator_workflow.go that walks the structured frontmatter map (on → workflow_dispatch → inputs → item_number) directly
  • Added HasDispatchItemNumber bool to WorkflowData, populated by extractYAMLSections via extractDispatchItemNumber
  • All consumers (applyWorkflowDispatchFallbacks, buildConcurrencyGroupKeys) now read data.HasDispatchItemNumber / workflowData.HasDispatchItemNumber — no YAML string re-parsing anywhere

@pelikhan
Copy link
Contributor

pelikhan commented Mar 6, 2026

@copilot review and add tests for parser

…ispatchItemNumber pipeline

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 6, 2026

@copilot review and add tests for parser

Added in 1745712. TestLabelTriggerShorthandPreprocessing and TestLabelTriggerShorthandPreprocessingErrors in schedule_preprocessing_test.go now cover the full parser pipeline:

  • For each entity type (pull_request, issues, discussion) and the pull-request alias: verify that preprocessScheduleFields expands the shorthand string into a frontmatter map with the correct workflow_dispatch.inputs.item_number structure, and that extractDispatchItemNumber returns true on the result.
  • Error path: comma-only label names → error "label trigger shorthand requires at least one label name".
  • Boundary: "pull_request labeled" (2 tokens, falls through to a plain activity-type trigger) → workflow_dispatch without item_numberextractDispatchItemNumber returns false.

@pelikhan pelikhan merged commit ce49917 into main Mar 6, 2026
@pelikhan pelikhan deleted the copilot/fix-workflow-dispatch-item-number branch March 6, 2026 07:57
github-actions bot pushed a commit that referenced this pull request Mar 6, 2026
Add two new glossary terms based on changes merged 2026-03-06:
- GH_AW_AGENT_TOKEN: magic secret for assign-to-agent PAT fallback (PR #19796)
- Label Trigger Shorthand: compact label trigger syntax with item_number (PR #19795)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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