Skip to content

Commit 63437f3

Browse files
Add debug logging to 5 core files (#2584)
1 parent 1e744c7 commit 63437f3

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

pkg/cli/repo.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import (
55
"os/exec"
66
"strings"
77
"sync"
8+
9+
"github.com/githubnext/gh-aw/pkg/logger"
810
)
911

12+
var repoLog = logger.New("cli:repo")
13+
1014
// Global cache for current repository info
1115
var (
1216
getCurrentRepoSlugOnce sync.Once
@@ -25,7 +29,10 @@ func ClearCurrentRepoSlugCache() {
2529
// getCurrentRepoSlugUncached gets the current repository slug (owner/repo) using gh CLI (uncached)
2630
// Falls back to git remote parsing if gh CLI is not available
2731
func getCurrentRepoSlugUncached() (string, error) {
32+
repoLog.Print("Fetching current repository slug")
33+
2834
// Try gh CLI first (most reliable)
35+
repoLog.Print("Attempting to get repository slug via gh CLI")
2936
cmd := exec.Command("gh", "repo", "view", "--json", "owner,name", "--jq", ".owner.login + \"/\" + .name")
3037
output, err := cmd.Output()
3138
if err == nil {
@@ -34,19 +41,23 @@ func getCurrentRepoSlugUncached() (string, error) {
3441
// Validate format (should be owner/repo)
3542
parts := strings.Split(repoSlug, "/")
3643
if len(parts) == 2 && parts[0] != "" && parts[1] != "" {
44+
repoLog.Printf("Successfully got repository slug via gh CLI: %s", repoSlug)
3745
return repoSlug, nil
3846
}
3947
}
4048
}
4149

4250
// Fallback to git remote parsing if gh CLI is not available or fails
51+
repoLog.Print("gh CLI failed, falling back to git remote parsing")
4352
cmd = exec.Command("git", "remote", "get-url", "origin")
4453
output, err = cmd.Output()
4554
if err != nil {
55+
repoLog.Printf("Failed to get git remote URL: %v", err)
4656
return "", fmt.Errorf("failed to get current repository (gh CLI and git remote both failed): %w", err)
4757
}
4858

4959
remoteURL := strings.TrimSpace(string(output))
60+
repoLog.Printf("Parsing git remote URL: %s", remoteURL)
5061

5162
// Parse GitHub repository from remote URL
5263
// Handle both SSH and HTTPS formats
@@ -71,9 +82,11 @@ func getCurrentRepoSlugUncached() (string, error) {
7182
// Validate format (should be owner/repo)
7283
parts := strings.Split(repoPath, "/")
7384
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
85+
repoLog.Printf("Invalid repository format: %s", repoPath)
7486
return "", fmt.Errorf("invalid repository format: %s", repoPath)
7587
}
7688

89+
repoLog.Printf("Successfully parsed repository slug from git remote: %s", repoPath)
7790
return repoPath, nil
7891
}
7992

@@ -88,5 +101,6 @@ func GetCurrentRepoSlug() (string, error) {
88101
return "", currentRepoSlugError
89102
}
90103

104+
repoLog.Printf("Using cached repository slug: %s", currentRepoSlugResult)
91105
return currentRepoSlugResult, nil
92106
}

pkg/workflow/args.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ package workflow
33
import (
44
"encoding/json"
55
"strings"
6+
7+
"github.com/githubnext/gh-aw/pkg/logger"
68
)
79

10+
var argsLog = logger.New("workflow:args")
11+
812
// extractCustomArgs extracts custom args from tool configuration
913
// Handles both []any and []string formats
1014
func extractCustomArgs(toolConfig map[string]any) []string {
1115
if argsValue, exists := toolConfig["args"]; exists {
16+
argsLog.Print("Extracting custom args from tool configuration")
17+
1218
// Handle []any format
1319
if argsSlice, ok := argsValue.([]any); ok {
1420
customArgs := make([]string, 0, len(argsSlice))
@@ -17,10 +23,12 @@ func extractCustomArgs(toolConfig map[string]any) []string {
1723
customArgs = append(customArgs, argStr)
1824
}
1925
}
26+
argsLog.Printf("Extracted %d args from []any format", len(customArgs))
2027
return customArgs
2128
}
2229
// Handle []string format
2330
if argsSlice, ok := argsValue.([]string); ok {
31+
argsLog.Printf("Extracted %d args from []string format", len(argsSlice))
2432
return argsSlice
2533
}
2634
}
@@ -46,6 +54,8 @@ func getPlaywrightCustomArgs(playwrightTool any) []string {
4654
// writeArgsToYAML writes custom args to YAML with proper JSON quoting and escaping
4755
// indent specifies the indentation string for each argument line
4856
func writeArgsToYAML(yaml *strings.Builder, args []string, indent string) {
57+
argsLog.Printf("Writing %d args to YAML", len(args))
58+
4959
for _, arg := range args {
5060
yaml.WriteString(",\n")
5161
// Use json.Marshal to properly quote and escape the argument

pkg/workflow/domains.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import (
55
"encoding/json"
66
"fmt"
77
"strings"
8+
9+
"github.com/githubnext/gh-aw/pkg/logger"
810
)
911

12+
var domainsLog = logger.New("workflow:domains")
13+
1014
//go:embed data/ecosystem_domains.json
1115
var ecosystemDomainsJSON []byte
1216

@@ -24,9 +28,13 @@ var CopilotDefaultDomains = []string{
2428

2529
// init loads the ecosystem domains from the embedded JSON
2630
func init() {
31+
domainsLog.Print("Loading ecosystem domains from embedded JSON")
32+
2733
if err := json.Unmarshal(ecosystemDomainsJSON, &ecosystemDomains); err != nil {
2834
panic(fmt.Sprintf("failed to load ecosystem domains from JSON: %v", err))
2935
}
36+
37+
domainsLog.Printf("Loaded %d ecosystem categories", len(ecosystemDomains))
3038
}
3139

3240
// getEcosystemDomains returns the domains for a given ecosystem category
@@ -67,24 +75,30 @@ func getEcosystemDomains(category string) []string {
6775
// - "github-actions": GitHub Actions domains
6876
func GetAllowedDomains(network *NetworkPermissions) []string {
6977
if network == nil {
78+
domainsLog.Print("No network permissions specified, using defaults")
7079
return getEcosystemDomains("defaults") // Default allow-list for backwards compatibility
7180
}
7281
if network.Mode == "defaults" {
82+
domainsLog.Print("Network mode is defaults, using default ecosystem domains")
7383
return getEcosystemDomains("defaults") // Default allow-list for defaults mode
7484
}
7585

7686
// Handle empty allowed list (deny-all case)
7787
if len(network.Allowed) == 0 {
88+
domainsLog.Print("Empty allowed list, denying all network access")
7889
return []string{} // Return empty slice, not nil
7990
}
8091

92+
domainsLog.Printf("Processing %d allowed domains/ecosystems", len(network.Allowed))
93+
8194
// Process the allowed list, expanding ecosystem identifiers if present
8295
var expandedDomains []string
8396
for _, domain := range network.Allowed {
8497
// Try to get domains for this ecosystem category
8598
ecosystemDomains := getEcosystemDomains(domain)
8699
if len(ecosystemDomains) > 0 {
87100
// This was an ecosystem identifier, expand it
101+
domainsLog.Printf("Expanded ecosystem '%s' to %d domains", domain, len(ecosystemDomains))
88102
expandedDomains = append(expandedDomains, ecosystemDomains...)
89103
} else {
90104
// Add the domain as-is (regular domain name)

pkg/workflow/imports.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ import (
55
"fmt"
66
"strings"
77

8+
"github.com/githubnext/gh-aw/pkg/logger"
89
"github.com/githubnext/gh-aw/pkg/parser"
910
)
1011

12+
var importsLog = logger.New("workflow:imports")
13+
1114
// MergeTools merges two tools maps, combining allowed arrays when keys coincide
1215
// Handles newline-separated JSON objects from multiple imports/includes
1316
func (c *Compiler) MergeTools(topTools map[string]any, includedToolsJSON string) (map[string]any, error) {
17+
importsLog.Print("Merging tools from imports")
18+
1419
if includedToolsJSON == "" || includedToolsJSON == "{}" {
20+
importsLog.Print("No included tools to merge")
1521
return topTools, nil
1622
}
1723

@@ -22,6 +28,8 @@ func (c *Compiler) MergeTools(topTools map[string]any, includedToolsJSON string)
2228
result = make(map[string]any)
2329
}
2430

31+
importsLog.Printf("Processing %d tool definition lines", len(lines))
32+
2533
for _, line := range lines {
2634
line = strings.TrimSpace(line)
2735
if line == "" || line == "{}" {
@@ -47,7 +55,10 @@ func (c *Compiler) MergeTools(topTools map[string]any, includedToolsJSON string)
4755
// MergeMCPServers merges mcp-servers from imports with top-level mcp-servers
4856
// Takes object maps and merges them directly
4957
func (c *Compiler) MergeMCPServers(topMCPServers map[string]any, importedMCPServersJSON string) (map[string]any, error) {
58+
importsLog.Print("Merging MCP servers from imports")
59+
5060
if importedMCPServersJSON == "" || importedMCPServersJSON == "{}" {
61+
importsLog.Print("No imported MCP servers to merge")
5162
return topMCPServers, nil
5263
}
5364

@@ -59,6 +70,7 @@ func (c *Compiler) MergeMCPServers(topMCPServers map[string]any, importedMCPServ
5970

6071
// Split by newlines to handle multiple JSON objects from different imports
6172
lines := strings.Split(importedMCPServersJSON, "\n")
73+
importsLog.Printf("Processing %d MCP server definition lines", len(lines))
6274

6375
for _, line := range lines {
6476
line = strings.TrimSpace(line)
@@ -84,8 +96,11 @@ func (c *Compiler) MergeMCPServers(topMCPServers map[string]any, importedMCPServ
8496
// MergeNetworkPermissions merges network permissions from imports with top-level network permissions
8597
// Combines allowed domains from both sources into a single list
8698
func (c *Compiler) MergeNetworkPermissions(topNetwork *NetworkPermissions, importedNetworkJSON string) (*NetworkPermissions, error) {
99+
importsLog.Print("Merging network permissions from imports")
100+
87101
// If no imported network config, return top-level network as-is
88102
if importedNetworkJSON == "" || importedNetworkJSON == "{}" {
103+
importsLog.Print("No imported network permissions to merge")
89104
return topNetwork, nil
90105
}
91106

@@ -95,6 +110,7 @@ func (c *Compiler) MergeNetworkPermissions(topNetwork *NetworkPermissions, impor
95110
result.Mode = topNetwork.Mode
96111
result.Allowed = make([]string, len(topNetwork.Allowed))
97112
copy(result.Allowed, topNetwork.Allowed)
113+
importsLog.Printf("Starting with %d top-level allowed domains", len(topNetwork.Allowed))
98114
}
99115

100116
// Track domains to avoid duplicates
@@ -105,6 +121,7 @@ func (c *Compiler) MergeNetworkPermissions(topNetwork *NetworkPermissions, impor
105121

106122
// Split by newlines to handle multiple JSON objects from different imports
107123
lines := strings.Split(importedNetworkJSON, "\n")
124+
importsLog.Printf("Processing %d network permission lines", len(lines))
108125

109126
for _, line := range lines {
110127
line = strings.TrimSpace(line)

pkg/workflow/yaml.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ import (
44
"regexp"
55
"sort"
66

7+
"github.com/githubnext/gh-aw/pkg/logger"
78
"github.com/goccy/go-yaml"
89
)
910

11+
var yamlLog = logger.New("workflow:yaml")
12+
1013
// UnquoteYAMLKey removes quotes from a YAML key at the start of a line.
1114
// This is necessary because yaml.Marshal adds quotes around reserved words like "on".
1215
// The function only replaces the quoted key if it appears at the start of a line
1316
// (optionally preceded by whitespace) to avoid replacing quoted strings in values.
1417
func UnquoteYAMLKey(yamlStr string, key string) string {
18+
yamlLog.Printf("Unquoting YAML key: %s", key)
19+
1520
// Create a regex pattern that matches the quoted key at the start of a line
1621
// Pattern: (start of line or newline) + (optional whitespace) + quoted key + colon
1722
pattern := `(^|\n)([ \t]*)"` + regexp.QuoteMeta(key) + `":`
@@ -36,6 +41,8 @@ func UnquoteYAMLKey(yamlStr string, key string) string {
3641
// Priority fields are emitted first in the order specified, then remaining fields alphabetically.
3742
// This is used to ensure GitHub Actions workflow fields appear in a conventional order.
3843
func MarshalWithFieldOrder(data map[string]any, priorityFields []string) ([]byte, error) {
44+
yamlLog.Printf("Marshaling YAML with field order: %d priority fields", len(priorityFields))
45+
3946
orderedData := OrderMapFields(data, priorityFields)
4047
// Marshal the ordered data with proper options for GitHub Actions
4148
return yaml.MarshalWithOptions(orderedData,

0 commit comments

Comments
 (0)