Skip to content

Commit

Permalink
Create and checkout branch
Browse files Browse the repository at this point in the history
Before branch creation, the script will attempt to switch to the main branch of the current repository.
  • Loading branch information
Panquesito7 committed Jul 20, 2023
1 parent 882501a commit ec72bec
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion fetch-repos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ local function clone_repos()
helper_functions.check_variables(repos, i)

-- Get the default branch.
branch = helper_functions.get_def_branch(repos, i) or ""
branch = helper_functions.get_def_branch(repos[i]) or ""

if branch == nil then
goto continue
Expand Down
29 changes: 14 additions & 15 deletions helper-functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ end
-- fallback/specified branch in the repositories file.
--- @param repos table The table containing all of the repositories.
--- @param i number The index of the repository to check.
local function get_def_branch(repos, i)
local function get_def_branch(repo)
-- Get the current VCS that is being used.
local branch
local vcs = repos[i].url:match("https://(%w+).%w+")
local owner, repo = repos[i].url:match(vcs .. ".%w+/(.+)/(.+)")
local vcs = repo.url:match("https://(%w+).%w+")
local owner, repo = repo.url:match(vcs .. ".%w+/(.+)/(.+)")

-- Remove `.git` from `repo` if available.
repo = repo:gsub(".git", "")
Expand All @@ -62,21 +62,21 @@ local function get_def_branch(repos, i)
local handle

-- Is branch already defined? Do not waste API requests.
if repos[i].def_branch ~= nil then
return repos[i].def_branch
if repo.def_branch ~= nil then
return repo.def_branch
end -- Continue otherwise.

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

print("Found branch for `" .. repo .. "` using GitHub API.")
else
branch = repos[i].def_branch
branch = repo.def_branch
end
else
print("Error: Could not obtain the default branch name from the given URL.")
Expand All @@ -87,13 +87,13 @@ local function get_def_branch(repos, i)
handle = io.popen("wget -q -O - \"\"https://gitlab.com/api/v4/projects/" .. owner .. "%2F" .. repo .. "\"\" | jq -r '.default_branch'")
if handle then
-- Print message and update branch only if default branch not set.
if repos[i].def_branch == nil then
if repo.def_branch == nil then
branch = handle:read("*a")
handle:close()

print("Found branch for `" .. repo .. "` using GitLab API.")
else
branch = repos[i].def_branch
branch = repo.def_branch
end
else
print("Error: Could not obtain the default branch name from the given URL.")
Expand All @@ -104,13 +104,13 @@ local function get_def_branch(repos, i)
handle = io.popen("wget -q -O - \"\"https://api.bitbucket.org/2.0/repositories/" .. owner .. "/" .. repo .. "\"\" | jq -r '.mainbranch.name'")
if handle then
-- Print message and update branch only if default branch not set.
if repos[i].def_branch == nil then
if repo.def_branch == nil then
branch = handle:read("*a")
handle:close()

print("Found branch for `" .. repo .. "` using BitBucket API.")
else
branch = repos[i].def_branch
branch = repo.def_branch
end
else
print("Error: Could not obtain the default branch name from the given URL.")
Expand All @@ -120,8 +120,8 @@ local function get_def_branch(repos, i)
-- Fallback.
else
print("The default branch could not be found for `" .. repo .. "`. Using provided default branch instead.")
if repos[i].def_branch ~= nil then
branch = repos[i].def_branch
if repo.def_branch ~= nil then
branch = repo.def_branch
print("Found provided default branch for `" .. repo .. "`.")
else
print("Could not find provided default branch for `" .. repo .. "`. Skipping.")
Expand All @@ -135,8 +135,7 @@ local function get_def_branch(repos, i)
return branch
end

--- @brief Gets the branches used
--- when updating the repositories.
--- @brief Gets the branches used when updating the repositories.
--- This is used in the `action.yml` file.
--- @param repos table The table containing all of the repositories.
--- @return nil
Expand Down
15 changes: 13 additions & 2 deletions update-repos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ local function update_repos()
end

-- Attempt to get the default branch.
local branch = helper_functions.get_def_branch(repos, i)
local branch = helper_functions.get_def_branch(repos[i]) or ""

if branch == nil then
goto continue
Expand All @@ -83,7 +83,18 @@ local function update_repos()
os.execute("git add " .. repos[i].dir .. repos[i].name)

if one_pr == "false" then
os.execute("git branch " .. repos[i].name .. "-update")
local default_branch = io.popen("git remote show origin | grep \"HEAD branch\" | cut -d' ' -f5"):read("*a")

if default_branch then
default_branch = default_branch:gsub("\n", "")
os.execute("git checkout " .. default_branch)
else
print("Error: Could not get the default branch of the current repository.")
goto continue
end

-- Create a new branch.
os.execute("git checkout -b " .. repos[i].name .. "-update")
end

if squash_commits == "false" then
Expand Down

0 comments on commit ec72bec

Please sign in to comment.