Skip to content

Commit 8c9dee2

Browse files
committed
fix relative path
1 parent 5ff56c7 commit 8c9dee2

File tree

2 files changed

+161
-11
lines changed

2 files changed

+161
-11
lines changed

gitops/git/github_app/github_app.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func CreateCommit(baseBranch string, commitBranch string, gitopsPath string, fil
105105
log.Printf("Starting Create Commit: Base branch: %s\n", baseBranch)
106106
log.Printf("GitOps Path: %s\n", gitopsPath)
107107
log.Printf("Modified Files: %v\n", files)
108-
fileEntries, err := getFilesToCommit(files)
108+
fileEntries, err := getFilesToCommit(gitopsPath, files)
109109

110110
if err != nil {
111111
log.Fatalf("failed to get files to commit: %v", err)
@@ -121,33 +121,29 @@ func CreateCommit(baseBranch string, commitBranch string, gitopsPath string, fil
121121
createPR(ctx, gh, baseBranch, commitBranch, commitMsg, "")
122122
}
123123

124-
func getFilesToCommit(inputPaths []string) ([]FileEntry, error) {
124+
func getFilesToCommit(gitopsPath string, inputPaths []string) ([]FileEntry, error) {
125125
var allFileEntries []FileEntry
126126

127127
for _, inputPath := range inputPaths {
128128
inputPath = strings.TrimSuffix(inputPath, "/")
129-
absInputPath, err := filepath.Abs(inputPath)
130-
if err != nil {
131-
return nil, fmt.Errorf("failed to get absolute path for input: %v", err)
132-
}
129+
absInputPath := filepath.Join(gitopsPath, inputPath)
133130

134131
info, err := os.Stat(absInputPath)
135132
if err != nil {
136133
return nil, fmt.Errorf("failed to access input path %s: %v", absInputPath, err)
137134
}
138135

139-
baseName := filepath.Base(absInputPath)
140136
if info.IsDir() {
141137
err = filepath.Walk(absInputPath, func(path string, info os.FileInfo, err error) error {
142138
if err != nil {
143139
return err
144140
}
145141
if !info.IsDir() {
146-
relPath, err := filepath.Rel(absInputPath, path)
142+
// Get path relative to gitopsPath
143+
relPath, err := filepath.Rel(gitopsPath, path)
147144
if err != nil {
148145
return fmt.Errorf("failed to get relative path for %s: %v", path, err)
149146
}
150-
relPath = filepath.Join(baseName, relPath)
151147
allFileEntries = append(allFileEntries, FileEntry{
152148
RelativePath: relPath,
153149
FullPath: path,
@@ -159,8 +155,9 @@ func getFilesToCommit(inputPaths []string) ([]FileEntry, error) {
159155
return nil, fmt.Errorf("failed to walk input directory: %v", err)
160156
}
161157
} else {
158+
// For single files, use the inputPath directly as it's already relative to gitopsPath
162159
allFileEntries = append(allFileEntries, FileEntry{
163-
RelativePath: filepath.Base(absInputPath),
160+
RelativePath: inputPath,
164161
FullPath: absInputPath,
165162
})
166163
}
@@ -179,7 +176,7 @@ func createPR(ctx context.Context, gh *github.Client, baseBranch string, commitB
179176
Head: &commitBranch,
180177
Base: &baseBranch, // This is the target branch
181178
Body: &prDescription,
182-
MaintainerCanModify: github.Bool(true),
179+
MaintainerCanModify: github.Ptr(true),
183180
}
184181

185182
pr, _, err := gh.PullRequests.Create(ctx, *repoOwner, *repo, newPR)
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package github_app
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"reflect"
7+
"sort"
8+
"testing"
9+
)
10+
11+
func TestGetFilesToCommit(t *testing.T) {
12+
// Create a temporary directory structure for testing
13+
tmpDir, err := os.MkdirTemp("", "gitops-test")
14+
if err != nil {
15+
t.Fatalf("Failed to create temp dir: %v", err)
16+
}
17+
defer os.RemoveAll(tmpDir)
18+
19+
// Create test directory structure
20+
testDirs := []string{
21+
"buildkite-agents/ai-services/config",
22+
"buildkite-agents/airflow-dev/settings",
23+
"buildkite-agents/single-file-dir",
24+
}
25+
26+
testFiles := []string{
27+
"buildkite-agents/ai-services/config/file1.yaml",
28+
"buildkite-agents/ai-services/config/file2.yaml",
29+
"buildkite-agents/airflow-dev/settings/settings.json",
30+
"buildkite-agents/single-file-dir/single.txt",
31+
}
32+
33+
// Create directories
34+
for _, dir := range testDirs {
35+
err := os.MkdirAll(filepath.Join(tmpDir, dir), 0755)
36+
if err != nil {
37+
t.Fatalf("Failed to create directory %s: %v", dir, err)
38+
}
39+
}
40+
41+
// Create files with some content
42+
for _, file := range testFiles {
43+
err := os.WriteFile(
44+
filepath.Join(tmpDir, file),
45+
[]byte("test content"),
46+
0644,
47+
)
48+
if err != nil {
49+
t.Fatalf("Failed to create file %s: %v", file, err)
50+
}
51+
}
52+
53+
tests := []struct {
54+
name string
55+
inputPaths []string
56+
expectedFiles []string
57+
expectedError bool
58+
}{
59+
{
60+
name: "Single directory with multiple files",
61+
inputPaths: []string{
62+
"buildkite-agents/ai-services",
63+
},
64+
expectedFiles: []string{
65+
"buildkite-agents/ai-services/config/file1.yaml",
66+
"buildkite-agents/ai-services/config/file2.yaml",
67+
},
68+
expectedError: false,
69+
},
70+
{
71+
name: "Multiple directories",
72+
inputPaths: []string{
73+
"buildkite-agents/ai-services",
74+
"buildkite-agents/airflow-dev",
75+
},
76+
expectedFiles: []string{
77+
"buildkite-agents/ai-services/config/file1.yaml",
78+
"buildkite-agents/ai-services/config/file2.yaml",
79+
"buildkite-agents/airflow-dev/settings/settings.json",
80+
},
81+
expectedError: false,
82+
},
83+
{
84+
name: "Single file",
85+
inputPaths: []string{
86+
"buildkite-agents/single-file-dir/single.txt",
87+
},
88+
expectedFiles: []string{
89+
"buildkite-agents/single-file-dir/single.txt",
90+
},
91+
expectedError: false,
92+
},
93+
{
94+
name: "Mixed files and directories",
95+
inputPaths: []string{
96+
"buildkite-agents/ai-services",
97+
"buildkite-agents/single-file-dir/single.txt",
98+
},
99+
expectedFiles: []string{
100+
"buildkite-agents/ai-services/config/file1.yaml",
101+
"buildkite-agents/ai-services/config/file2.yaml",
102+
"buildkite-agents/single-file-dir/single.txt",
103+
},
104+
expectedError: false,
105+
},
106+
{
107+
name: "Non-existent path",
108+
inputPaths: []string{
109+
"buildkite-agents/non-existent",
110+
},
111+
expectedFiles: nil,
112+
expectedError: true,
113+
},
114+
}
115+
116+
for _, tt := range tests {
117+
t.Run(tt.name, func(t *testing.T) {
118+
fileEntries, err := getFilesToCommit(tmpDir, tt.inputPaths)
119+
120+
// Check error condition
121+
if (err != nil) != tt.expectedError {
122+
t.Errorf("getFilesToCommit() error = %v, expectedError %v", err, tt.expectedError)
123+
return
124+
}
125+
126+
if tt.expectedError {
127+
return
128+
}
129+
130+
// Get actual relative paths
131+
var actualFiles []string
132+
for _, entry := range fileEntries {
133+
actualFiles = append(actualFiles, entry.RelativePath)
134+
}
135+
136+
// Sort both slices for comparison
137+
sort.Strings(actualFiles)
138+
sort.Strings(tt.expectedFiles)
139+
140+
// Compare results
141+
if !reflect.DeepEqual(actualFiles, tt.expectedFiles) {
142+
t.Errorf("getFilesToCommit() got = %v, want %v", actualFiles, tt.expectedFiles)
143+
}
144+
145+
// Verify that FullPath exists for each entry
146+
for _, entry := range fileEntries {
147+
if _, err := os.Stat(entry.FullPath); os.IsNotExist(err) {
148+
t.Errorf("FullPath %s does not exist", entry.FullPath)
149+
}
150+
}
151+
})
152+
}
153+
}

0 commit comments

Comments
 (0)