Skip to content
Merged
Show file tree
Hide file tree
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
85 changes: 32 additions & 53 deletions lua/gh-co/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ local FS = {}

FS.cachedCodeownersFilePath = nil

local function hasGithubDirectory(name, kind)
return name == '.github' and kind == 'directory'
end

local function hasDocsDirectory(name, kind)
return name == 'docs' and kind == 'directory'
end

local function hasCodeownersFile(name, kind)
return name == 'CODEOWNERS' and kind == 'file'
local function hasCodeownersFile(path)
local dirContents = vim.fs.dir(path)
for name, kind in dirContents do
if name == 'CODEOWNERS' and kind == 'file' then
return true
end
end
return false
end

local function getRootDirectoryName(currentPath)
local dir = vim.fs.dirname(
vim.fs.find(
{ ".github", "docs" },
{ ".github", "docs", "CODEOWNERS" },
{
path = currentPath,
upward = true
Expand Down Expand Up @@ -64,54 +62,35 @@ FS.getCodeownersFilePath = function()
Not able to detect project root directory. Maybe you should run nvim from the root of the project.
]])

local githubDirName = nil
local docsDirName = nil
local hasGithubDir = false
local hasDocsDir = false
local hasRootCodeowners = false
for name, kind in rootDirContents do
if hasGithubDirectory(name, kind) then
githubDirName = name
end

if hasDocsDirectory(name, kind) then
docsDirName = name
end
end

local codeownerDirName = rootDirName .. "/" .. githubDirName or docsDirName
local codeownerDirContents = vim.fs.dir(codeownerDirName)

assert(
codeownerDirContents,
"Directory " .. codeownerDirName .. " does not seem to exist."
)

local codeownerFileNameWithinDefaultFolder = nil
for name, kind in codeownerDirContents do
if hasCodeownersFile(name, kind) then
codeownerFileNameWithinDefaultFolder = name
break
if kind == 'directory' then
if name == '.github' then
hasGithubDir = true
elseif name == 'docs' then
hasDocsDir = true
end
elseif kind == 'file' and name == 'CODEOWNERS' then
hasRootCodeowners = true
end
end

local codeownerFilePath = nil

-- handles case when project is storing CODEOWNERS file in root directory
if codeownerFileNameWithinDefaultFolder == nil then
for name, kind in vim.fs.dir(rootDirName) do
if hasCodeownersFile(name, kind) then
codeownerFilePath = rootDirName .. "/" .. name
FS.cachedCodeownersFilePath = codeownerFilePath
break
end
end

return codeownerFilePath
else
codeownerFilePath = codeownerDirName .. "/" .. codeownerFileNameWithinDefaultFolder

FS.cachedCodeownersFilePath = codeownerFilePath

return codeownerFilePath
-- picking the best matching CODEOWNERS file according to priority
-- luacheck: ignore 631
-- https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners#codeowners-file-location
if hasGithubDir and hasCodeownersFile(rootDirName .. '/.github') then
codeownerFilePath = rootDirName .. '/.github/CODEOWNERS'
elseif hasRootCodeowners then
codeownerFilePath = rootDirName .. '/CODEOWNERS'
elseif hasDocsDir and hasCodeownersFile(rootDirName .. '/docs') then
codeownerFilePath = rootDirName .. '/docs/CODEOWNERS'
end

FS.cachedCodeownersFilePath = codeownerFilePath
return codeownerFilePath
end

FS.openCodeownersFile = function()
Expand Down
Loading
Loading