Skip to content

Commit

Permalink
Fix LuaCheck warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Panquesito7 committed Jul 20, 2023
1 parent 126a005 commit a54e9c8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion fetch-repos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ local function clone_repos()
end

-- Make sure all of the variables are set.
helper_functions.check_variables(repos, i)
helper_functions.check_variables(repos[i])

-- Get the default branch.
branch = helper_functions.get_def_branch(repos[i]) or ""
Expand Down
40 changes: 20 additions & 20 deletions helper-functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@
--- @param repo table The table containing all of the repositories.
--- @param i number The index of the repository to check.
--- @return nil
local function check_variables(repo, i)
if repo[i].name == nil then
print("Error: `name` is not set for repository " .. i)
local function check_variables(repo)
if repo.name == nil then
print("Error: `name` is not set for repository `" .. repo.name .. "`.")
os.exit(1)
end

if repo[i].url == nil then
print("Error: `url` is not set for repository " .. i)
if repo.url == nil then
print("Error: `url` is not set for repository `" .. repo.name .. "`.")
os.exit(1)
end

if repo[i].dir == nil then
print("Error: `dir` is not set for repository " .. i)
if repo.dir == nil then
print("Error: `dir` is not set for repository `" .. repo.name .. "`.")
os.exit(1)
end

if repo[i].def_branch == nil then
print("Warning: default branch not specified for `" .. repo[i].name .. "`. Attempting to obtain the default branch using the API.")
if repo.def_branch == nil then
print("Warning: default branch not specified for `" .. repo.name .. "`. Attempting to obtain the default branch using the API.")
end
end

Expand All @@ -52,10 +52,10 @@ local function get_def_branch(repo)
-- Get the current VCS that is being used.
local branch
local vcs = repo.url:match("https://(%w+).%w+")
local owner, repo = repo.url:match(vcs .. ".%w+/(.+)/(.+)")
local owner, repo_url = repo.url:match(vcs .. ".%w+/(.+)/(.+)")

-- Remove `.git` from `repo` if available.
repo = repo:gsub(".git", "")
repo_url = repo_url:gsub(".git", "")

-- Attempt to obtain the default branch name from the given URL.
-- Currently supports: GitLab, GitHub, and BitBucket.
Expand All @@ -67,14 +67,14 @@ local function get_def_branch(repo)
end -- Continue otherwise.

if vcs == "github" then
handle = io.popen("wget -q -O - \"\"https://api.github.com/repos/" .. owner .. "/" .. repo .. "\"\" | jq -r '.default_branch'")
handle = io.popen("wget -q -O - \"\"https://api.github.com/repos/" .. owner .. "/" .. repo_url .. "\"\" | jq -r '.default_branch'")
if handle then
-- Print message and update branch only if default branch not set.
if repo.def_branch == nil then
branch = handle:read("*a")
handle:close()

print("Found branch for `" .. repo .. "` using GitHub API.")
print("Found branch for `" .. repo_url .. "` using GitHub API.")
else
branch = repo.def_branch
end
Expand All @@ -84,14 +84,14 @@ local function get_def_branch(repo)
return nil
end
elseif vcs == "gitlab" then
handle = io.popen("wget -q -O - \"\"https://gitlab.com/api/v4/projects/" .. owner .. "%2F" .. repo .. "\"\" | jq -r '.default_branch'")
handle = io.popen("wget -q -O - \"\"https://gitlab.com/api/v4/projects/" .. owner .. "%2F" .. repo_url .. "\"\" | jq -r '.default_branch'")
if handle then
-- Print message and update branch only if default branch not set.
if repo.def_branch == nil then
branch = handle:read("*a")
handle:close()

print("Found branch for `" .. repo .. "` using GitLab API.")
print("Found branch for `" .. repo_url .. "` using GitLab API.")
else
branch = repo.def_branch
end
Expand All @@ -101,14 +101,14 @@ local function get_def_branch(repo)
return nil
end
elseif vcs == "bitbucket" then
handle = io.popen("wget -q -O - \"\"https://api.bitbucket.org/2.0/repositories/" .. owner .. "/" .. repo .. "\"\" | jq -r '.mainbranch.name'")
handle = io.popen("wget -q -O - \"\"https://api.bitbucket.org/2.0/repositories/" .. owner .. "/" .. repo_url .. "\"\" | jq -r '.mainbranch.name'")
if handle then
-- Print message and update branch only if default branch not set.
if repo.def_branch == nil then
branch = handle:read("*a")
handle:close()

print("Found branch for `" .. repo .. "` using BitBucket API.")
print("Found branch for `" .. repo_url .. "` using BitBucket API.")
else
branch = repo.def_branch
end
Expand All @@ -119,12 +119,12 @@ local function get_def_branch(repo)
end
-- Fallback.
else
print("The default branch could not be found for `" .. repo .. "`. Using provided default branch instead.")
print("The default branch could not be found for `" .. repo_url .. "`. Using provided default branch instead.")
if repo.def_branch ~= nil then
branch = repo.def_branch
print("Found provided default branch for `" .. repo .. "`.")
print("Found provided default branch for `" .. repo_url .. "`.")
else
print("Could not find provided default branch for `" .. repo .. "`. Skipping.")
print("Could not find provided default branch for `" .. repo_url .. "`. Skipping.")
return nil
end
end
Expand Down
2 changes: 1 addition & 1 deletion update-repos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ end
local function update_repos()
for i = 1, #repos do
-- Make sure all of the variables are set.
helper_functions.check_variables(repos, i)
helper_functions.check_variables(repos[i])

-- Make sure the repository is cloned already.
if os.execute("test -d " .. repos[i].dir .. repos[i].name) == nil then
Expand Down

0 comments on commit a54e9c8

Please sign in to comment.