Skip to content

Commit dd43500

Browse files
Copilotpelikhan
andcommitted
Add GetWorkflowIDFromPath helper function
- Created GetWorkflowIDFromPath() helper function in compiler_yaml_helpers.go - Updated compiler_safe_outputs_consolidated.go to use the helper function - Removed unused filepath and strings imports from compiler_safe_outputs_consolidated.go - Added comprehensive test coverage for GetWorkflowIDFromPath() - Renamed workflowFilename parameter to workflowID for clarity in buildJobLevelSafeOutputEnvVars() Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
1 parent fb7c96f commit dd43500

3 files changed

Lines changed: 56 additions & 9 deletions

File tree

pkg/workflow/compiler_safe_outputs_consolidated.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package workflow
22

33
import (
44
"fmt"
5-
"path/filepath"
6-
"strings"
75

86
"github.com/githubnext/gh-aw/pkg/constants"
97
"github.com/githubnext/gh-aw/pkg/logger"
@@ -282,7 +280,7 @@ func (c *Compiler) buildConsolidatedSafeOutputsJob(data *WorkflowData, mainJobNa
282280

283281
// 9. Create Code Scanning Alert step
284282
if data.SafeOutputs.CreateCodeScanningAlerts != nil {
285-
workflowFilename := strings.TrimSuffix(filepath.Base(markdownPath), ".md")
283+
workflowFilename := GetWorkflowIDFromPath(markdownPath)
286284
stepConfig := c.buildCreateCodeScanningAlertStepConfig(data, mainJobName, threatDetectionEnabled, workflowFilename)
287285
stepYAML := c.buildConsolidatedSafeOutputStep(data, stepConfig)
288286
steps = append(steps, stepYAML...)
@@ -498,11 +496,11 @@ func (c *Compiler) buildConsolidatedSafeOutputsJob(data *WorkflowData, mainJobNa
498496
needs = append(needs, constants.ActivationJobName)
499497
}
500498

501-
// Extract workflow filename (without extension) for GH_AW_WORKFLOW_ID
502-
workflowFilename := strings.TrimSuffix(filepath.Base(markdownPath), ".md")
499+
// Extract workflow ID from markdown path for GH_AW_WORKFLOW_ID
500+
workflowID := GetWorkflowIDFromPath(markdownPath)
503501

504502
// Build job-level environment variables that are common to all safe output steps
505-
jobEnv := c.buildJobLevelSafeOutputEnvVars(data, workflowFilename)
503+
jobEnv := c.buildJobLevelSafeOutputEnvVars(data, workflowID)
506504

507505
job := &Job{
508506
Name: "safe_outputs",
@@ -587,12 +585,12 @@ func (c *Compiler) buildConsolidatedSafeOutputStep(data *WorkflowData, config Sa
587585

588586
// buildJobLevelSafeOutputEnvVars builds environment variables that should be set at the job level
589587
// for the consolidated safe_outputs job. These are variables that are common to all safe output steps.
590-
func (c *Compiler) buildJobLevelSafeOutputEnvVars(data *WorkflowData, workflowFilename string) map[string]string {
588+
func (c *Compiler) buildJobLevelSafeOutputEnvVars(data *WorkflowData, workflowID string) map[string]string {
591589
envVars := make(map[string]string)
592590

593-
// Set GH_AW_WORKFLOW_ID to the workflow filename (without extension)
591+
// Set GH_AW_WORKFLOW_ID to the workflow ID (filename without extension)
594592
// This is used for branch naming in create_pull_request and other operations
595-
envVars["GH_AW_WORKFLOW_ID"] = fmt.Sprintf("%q", workflowFilename)
593+
envVars["GH_AW_WORKFLOW_ID"] = fmt.Sprintf("%q", workflowID)
596594

597595
// Add workflow metadata that's common to all steps
598596
envVars["GH_AW_WORKFLOW_NAME"] = fmt.Sprintf("%q", data.Name)

pkg/workflow/compiler_yaml_helpers.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package workflow
22

33
import (
44
"fmt"
5+
"path/filepath"
56
"strings"
67

78
"github.com/githubnext/gh-aw/pkg/constants"
@@ -10,6 +11,13 @@ import (
1011

1112
var compilerYamlHelpersLog = logger.New("workflow:compiler_yaml_helpers")
1213

14+
// GetWorkflowIDFromPath extracts the workflow ID from a markdown file path.
15+
// The workflow ID is the filename without the .md extension.
16+
// Example: "/path/to/ai-moderator.md" -> "ai-moderator"
17+
func GetWorkflowIDFromPath(markdownPath string) string {
18+
return strings.TrimSuffix(filepath.Base(markdownPath), ".md")
19+
}
20+
1321
// convertStepToYAML converts a step map to YAML format.
1422
// This is a method wrapper around the package-level ConvertStepToYAML function.
1523
func (c *Compiler) convertStepToYAML(stepMap map[string]any) (string, error) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package workflow
2+
3+
import "testing"
4+
5+
func TestGetWorkflowIDFromPath(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
path string
9+
expected string
10+
}{
11+
{
12+
name: "simple filename",
13+
path: "ai-moderator.md",
14+
expected: "ai-moderator",
15+
},
16+
{
17+
name: "full path",
18+
path: "/home/user/workflows/test-workflow.md",
19+
expected: "test-workflow",
20+
},
21+
{
22+
name: "filename with multiple dots",
23+
path: "/path/to/workflow.test.md",
24+
expected: "workflow.test",
25+
},
26+
{
27+
name: "relative path",
28+
path: ".github/workflows/daily-fact.md",
29+
expected: "daily-fact",
30+
},
31+
}
32+
33+
for _, tt := range tests {
34+
t.Run(tt.name, func(t *testing.T) {
35+
result := GetWorkflowIDFromPath(tt.path)
36+
if result != tt.expected {
37+
t.Errorf("GetWorkflowIDFromPath(%q) = %q, want %q", tt.path, result, tt.expected)
38+
}
39+
})
40+
}
41+
}

0 commit comments

Comments
 (0)