Skip to content

Commit 98b01af

Browse files
authored
fix: correct YAML indentation for detection job permissions block (#23647)
1 parent bdaab38 commit 98b01af

2 files changed

Lines changed: 81 additions & 1 deletion

File tree

pkg/workflow/threat_detection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ func (c *Compiler) buildDetectionJob(data *WorkflowData) (*Job, error) {
656656
Needs: needs,
657657
If: jobCondition,
658658
RunsOn: c.indentYAMLLines(runsOn, " "),
659-
Permissions: c.indentYAMLLines(permissions, " "),
659+
Permissions: permissions,
660660
Steps: steps,
661661
Outputs: outputs,
662662
}

pkg/workflow/threat_detection_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,3 +1081,83 @@ func TestBuildDetectionEngineExecutionStepPropagatesAPITarget(t *testing.T) {
10811081
})
10821082
}
10831083
}
1084+
1085+
// TestDetectionJobPermissionsIndentation verifies that the detection job's permissions block
1086+
// is correctly indented in the rendered YAML output.
1087+
// Regression test for the indentation bug where c.indentYAMLLines was called on
1088+
// RenderToYAML() output which already uses 6-space indentation for permission values,
1089+
// resulting in 10-space indentation instead of the correct 6.
1090+
func TestDetectionJobPermissionsIndentation(t *testing.T) {
1091+
tests := []struct {
1092+
name string
1093+
data *WorkflowData
1094+
wantContains []string
1095+
wantNotContains []string
1096+
}{
1097+
{
1098+
name: "copilot-requests feature produces correctly indented permissions",
1099+
data: &WorkflowData{
1100+
Name: "test-workflow",
1101+
AI: "copilot",
1102+
SafeOutputs: &SafeOutputsConfig{
1103+
ThreatDetection: &ThreatDetectionConfig{},
1104+
},
1105+
Features: map[string]any{
1106+
string(constants.CopilotRequestsFeatureFlag): true,
1107+
},
1108+
},
1109+
// permission values must be indented by exactly 6 spaces (4 for job key + 2 for sub-key)
1110+
wantContains: []string{
1111+
" copilot-requests: write",
1112+
},
1113+
// Over-indented value (10 spaces) must not appear - this was the bug
1114+
wantNotContains: []string{
1115+
" copilot-requests: write",
1116+
},
1117+
},
1118+
{
1119+
name: "permissions block absent when copilot-requests feature disabled and no contents read needed",
1120+
data: &WorkflowData{
1121+
Name: "test-workflow",
1122+
AI: "copilot",
1123+
SafeOutputs: &SafeOutputsConfig{
1124+
ThreatDetection: &ThreatDetectionConfig{},
1125+
},
1126+
},
1127+
// copilot-requests should not be in the output when the feature is not enabled
1128+
wantContains: []string{},
1129+
wantNotContains: []string{"copilot-requests: write"},
1130+
},
1131+
}
1132+
1133+
for _, tt := range tests {
1134+
t.Run(tt.name, func(t *testing.T) {
1135+
compiler := NewCompiler()
1136+
1137+
job, err := compiler.buildDetectionJob(tt.data)
1138+
if err != nil {
1139+
t.Fatalf("buildDetectionJob() error: %v", err)
1140+
}
1141+
if job == nil {
1142+
t.Fatal("buildDetectionJob() returned nil job")
1143+
}
1144+
1145+
if err := compiler.jobManager.AddJob(job); err != nil {
1146+
t.Fatalf("AddJob() error: %v", err)
1147+
}
1148+
1149+
yamlOutput := compiler.jobManager.RenderToYAML()
1150+
1151+
for _, expected := range tt.wantContains {
1152+
if !strings.Contains(yamlOutput, expected) {
1153+
t.Errorf("YAML output should contain %q, but got:\n%s", expected, yamlOutput)
1154+
}
1155+
}
1156+
for _, unexpected := range tt.wantNotContains {
1157+
if strings.Contains(yamlOutput, unexpected) {
1158+
t.Errorf("YAML output should NOT contain %q, but got:\n%s", unexpected, yamlOutput)
1159+
}
1160+
}
1161+
})
1162+
}
1163+
}

0 commit comments

Comments
 (0)