Extract workflow metadata env setup to shared helper#3580
Merged
Conversation
Extract duplicated workflow metadata environment variable setup logic from four safe-output job builders into a new shared helper function `buildWorkflowMetadataEnvVars`. This eliminates ~13 lines of duplicate code in each of: - pkg/workflow/add_comment.go - pkg/workflow/create_issue.go - pkg/workflow/create_pr_review_comment.go - pkg/workflow/create_discussion.go Benefits: - Single point of truth for workflow name/source env vars - Consistent behavior across all safe-output job builders - Fixed inconsistency: create_discussion.go now also includes source URL - Easier to maintain and modify in the future Added comprehensive tests for the new helper function covering: - Workflow name only - Workflow name and source with ref - Workflow name and source without ref - Empty workflow name - Invalid source format handling Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Remove duplicate safe output workflow setup code
Extract workflow metadata env setup to shared helper
Nov 11, 2025
pelikhan
approved these changes
Nov 11, 2025
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR extracts duplicated workflow metadata environment variable setup logic from four safe-output job builders into a shared helper function. The refactoring eliminates 28 lines of duplicate code and fixes a bug where create_discussion.go was missing source URL handling.
Key Changes:
- Introduced
buildWorkflowMetadataEnvVars()helper function to centralizeGH_AW_WORKFLOW_NAME,GH_AW_WORKFLOW_SOURCE, andGH_AW_WORKFLOW_SOURCE_URLenvironment variable generation - Refactored all four safe-output job builders to use the new helper
- Fixed missing source/URL handling in
create_discussion.go(previously only set workflow name, not source/URL)
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
pkg/workflow/safe_outputs.go |
Added buildWorkflowMetadataEnvVars() helper function to generate workflow metadata environment variables |
pkg/workflow/safe_output_helpers_test.go |
Added comprehensive test coverage for the new helper with 5 test cases covering various scenarios |
pkg/workflow/create_pr_review_comment.go |
Replaced duplicated env var setup (11 lines) with helper function call |
pkg/workflow/create_issue.go |
Replaced duplicated env var setup (11 lines) with helper function call |
pkg/workflow/create_discussion.go |
Replaced incomplete env var setup with helper function call, fixing missing source/URL handling |
pkg/workflow/add_comment.go |
Replaced duplicated env var setup (11 lines) with helper function call |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
|
@copilot lint |
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Contributor
Author
Done! Applied code formatting with Changes in commit 3abf3c3:
All tests pass ✅ |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Refactor Safe Output Job Environment Setup ✅
This PR successfully addresses the duplicate code issue identified in #19245987270 by extracting shared workflow metadata setup logic into a reusable helper function.
Implementation Summary
Changes Made
New Helper Function
Created
buildWorkflowMetadataEnvVarsinpkg/workflow/safe_outputs.go:Purpose:
buildSourceURLUpdated Files
pkg/workflow/add_comment.go- Removed 11 lines of duplicate codepkg/workflow/create_issue.go- Removed 11 lines of duplicate codepkg/workflow/create_pr_review_comment.go- Removed 11 lines of duplicate codepkg/workflow/create_discussion.go- Fixed bug: Now includes source URL (was missing)Code Changes
Before (repeated 4 times):
After (4 instances):
Tests Added
Added
TestBuildWorkflowMetadataEnvVarswith 5 comprehensive test cases:Impact Analysis
Benefits
Code Quality:
create_discussion.gonow includes source URLMaintainability:
Test Coverage:
Verification Results
✅ Unit Tests
✅ Linting
✅ Integration Test
Manually verified with workflow containing all four safe-output types. All jobs correctly include:
✅ Build
$ make build ✓ Templates synced successfully go build ... -o gh-aw ./cmd/gh-aw Build successful!Addresses Issue Requirements
From the original issue (#19245987270):
✅ Recommendation 1: Extract shared helper for workflow metadata env vars
buildWorkflowMetadataEnvVarsfunction✅ Benefits Delivered:
✅ Impact:
create_discussion.goOriginal prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.