Skip to content

Commit d90eb82

Browse files
committed
Handle directory paths
1 parent a556214 commit d90eb82

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

pkg/github/repositories.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,12 @@ SHA MUST be provided for existing file updates.
424424

425425
if sha != "" {
426426
// User provided SHA - validate it's still current
427-
existingFile, _, respCheck, getErr := client.Repositories.GetContents(ctx, owner, repo, path, getOpts)
427+
existingFile, dirContent, respCheck, getErr := client.Repositories.GetContents(ctx, owner, repo, path, getOpts)
428428
if respCheck != nil {
429429
_ = respCheck.Body.Close()
430430
}
431-
if getErr != nil {
431+
switch {
432+
case getErr != nil:
432433
// 404 means file doesn't exist - proceed (new file creation)
433434
// Any other error (403, 500, network) should be surfaced
434435
if respCheck == nil || respCheck.StatusCode != http.StatusNotFound {
@@ -438,7 +439,11 @@ SHA MUST be provided for existing file updates.
438439
getErr,
439440
), nil, nil
440441
}
441-
} else if existingFile != nil {
442+
case dirContent != nil:
443+
return utils.NewToolResultError(fmt.Sprintf(
444+
"Path %s is a directory, not a file. This tool only works with files.",
445+
path)), nil, nil
446+
case existingFile != nil:
442447
currentSHA := existingFile.GetSHA()
443448
if currentSHA != sha {
444449
return utils.NewToolResultError(fmt.Sprintf(
@@ -449,11 +454,12 @@ SHA MUST be provided for existing file updates.
449454
}
450455
} else {
451456
// No SHA provided - check if file already exists
452-
existingFile, _, respCheck, getErr := client.Repositories.GetContents(ctx, owner, repo, path, getOpts)
457+
existingFile, dirContent, respCheck, getErr := client.Repositories.GetContents(ctx, owner, repo, path, getOpts)
453458
if respCheck != nil {
454459
_ = respCheck.Body.Close()
455460
}
456-
if getErr != nil {
461+
switch {
462+
case getErr != nil:
457463
// 404 means file doesn't exist - proceed with creation
458464
// Any other error (403, 500, network) should be surfaced
459465
if respCheck == nil || respCheck.StatusCode != http.StatusNotFound {
@@ -463,7 +469,11 @@ SHA MUST be provided for existing file updates.
463469
getErr,
464470
), nil, nil
465471
}
466-
} else if existingFile != nil {
472+
case dirContent != nil:
473+
return utils.NewToolResultError(fmt.Sprintf(
474+
"Path %s is a directory, not a file. This tool only works with files.",
475+
path)), nil, nil
476+
case existingFile != nil:
467477
// File exists but no SHA was provided - reject to prevent blind overwrites
468478
return utils.NewToolResultError(fmt.Sprintf(
469479
"File already exists at %s. You must provide the current file's SHA when updating. "+

0 commit comments

Comments
 (0)