Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions internal/utils/ghutils/ghutils.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package ghutils

import (
"errors"
"io/fs"
"os"
"path/filepath"

"github.com/aviator-co/av/internal/git"
)

func HasCodeowners(repo *git.Repo) bool {
if stat, _ := os.Stat(filepath.Join(repo.Dir(), ".github/CODEOWNERS")); stat != nil {
_, err := os.Stat(filepath.Join(repo.Dir(), ".github/CODEOWNERS"))
Comment on lines 12 to +13

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent potential nil pointer dereferences, we should add a defensive check to ensure repo is not nil before calling repo.Dir(). Additionally, we can pass the path segments as separate arguments to filepath.Join for better platform-agnostic path construction.

Suggested change
func HasCodeowners(repo *git.Repo) bool {
if stat, _ := os.Stat(filepath.Join(repo.Dir(), ".github/CODEOWNERS")); stat != nil {
_, err := os.Stat(filepath.Join(repo.Dir(), ".github/CODEOWNERS"))
func HasCodeowners(repo *git.Repo) bool {
if repo == nil {
return false
}
_, err := os.Stat(filepath.Join(repo.Dir(), ".github", "CODEOWNERS"))

if err == nil {
return true
}
return false
if errors.Is(err, fs.ErrNotExist) {
return false
}
// For permission/IO errors, assume the file exists to be safe.
return true
}