Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major PR body changes #58

Merged
merged 1 commit into from
Sep 20, 2023
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/.lua
/.source
updated_subtrees.txt
added_subtrees.txt
TEMP.txt
11 changes: 1 addition & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ An easy-to-use Git (Sub)modules alternative to make the cloning process easier.\
- Git Submodules can sometimes be messy or confusing, which this tool aims to solve.
- Lets you choose the desired branch of the repository, unlike Git Submodules which automatically chooses the default branch.
- Easily take a look at the changes **directly in the PR** without extra effort. Git Submodules changes cannot be seen via the PR diff.
- Provide a customizable PR body when fetching, updating, and doing the same actions.
<!-- - Easily specify which files are ignored at the moment of updating the repositories. This is very useful if you want to modify a repository/submodule. -->

## Usage
Expand All @@ -29,14 +28,6 @@ Your `repos.lua` file should look similar to the following.
local config = {
labels_fetch = "repo-fetch,dependencies"
labels_update = "dependencies",
pr_body = {
update = "This PR updated all the subtrees." ..
"The updated subtrees are:" ..
"- Test 1." ..
"- Test 2.",
fetch = "This PR fetched all the subtrees.",
both = "This PR both updates subtrees and fetches new ones."
}
}

local repos = {
Expand Down Expand Up @@ -92,7 +83,7 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # This pulls changes before doing any changes
- uses: Panquesito7/submodules-alternative@v1.6.0
- uses: Panquesito7/submodules-alternative@v1.6.1
with:
repos_filename: repos # In case your file is named `repos.lua`, you can leave it as `repos`.
use_pr: true # Whether to create a pull request when updating/adding the repositories.
Expand Down
19 changes: 13 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,31 +127,38 @@ runs:
shell: bash
run: |
if [[ ${{ inputs.use_pr }} == true ]]; then
if [[ ${{ inputs.one_pr }} == true ]]; then
if [[ ${{ inputs.one_pr }} == true ]]; then # One PR for everything: fetching and updating.
git push origin ${{ inputs.branch_name }}:${{ inputs.branch_name }} || true
gh pr create --base ${GITHUB_REF##*/} --head ${{ inputs.branch_name }} --title '${{ inputs.commit_message }}' --body "$(lua -e 'local config = require("${{ inputs.repos_filename }}").config; if config.both then print(config.both) else print("Repositories were added or updated using the Submodules Alternative tool.") end')" || true
else
# The One PR option is not available when adding the repositories.

pr_body=$(bash ${{ github.action_path }}/markdown_pr_body.sh "all" "false"
gh pr create --base ${GITHUB_REF##*/} --head ${{ inputs.branch_name }} --title '${{ inputs.commit_message }}' --body "$pr_body" || true
else # The One PR option is not available when adding the repositories.
# The code below creates one PR for fetching all the subtrees.
if [[ ${{ inputs.add_repos }} == true ]]; then
git checkout ${{ inputs.branch_name }} || true
git push origin ${{ inputs.branch_name }}:${{ inputs.branch_name }} || true

pr=$(gh pr list --base ${GITHUB_REF##*/} --head $branch)
pr_number=$(echo $pr | cut -d ' ' -f 1)

pr_body=$(bash ${{ github.action_path }}/markdown_pr_body.sh "added" "false")

labels_fetch=$(lua -e 'local labels = require("${{ inputs.repos_filename }}").config; if labels.labels_fetch then print(labels.labels_fetch) end') || "repo-fetch,dependencies"
gh pr create --base ${GITHUB_REF##*/} --head ${{ inputs.branch_name }} --title '${{ inputs.commit_message }}' --body "$(lua -e 'local config = require("${{ inputs.repos_filename }}").config; if config.fetch then print(config.fetch) else print("Repositories were added or updated using the Submodules Alternative tool.") end')" || true

if [[ $(gh pr list --base ${GITHUB_REF##*/} --head ${{ inputs.branch_name }}) ]]; then
gh pr edit $pr_number --add-label $labels_fetch || true
fi
else # The `add_repos` option is disabled.
elif [[ ${{ inputs.update_repos }} == true ]]; and [[ ${{ inputs.add_repos }} == false ]]; then
# The One PR option is available when updating the repositories.
branches=$(lua -e 'local repos = require("${{ inputs.repos_filename }}").repos; dofile("${{ github.action_path }}/helper-functions.lua"); get_repo_branches(repos)')
labels_update=$(lua -e 'local labels = require("${{ inputs.repos_filename }}").config; if labels.labels_update then print(labels.labels_update) end') || "dependencies"

for branch in ${branches[@]}; do
repo_name=${branch%-update}
gh pr create --base ${GITHUB_REF##*/} --head $branch --title 'Bump `'$repo_name'` to its latest commit' --body "$(lua -e 'local config = require("${{ inputs.repos_filename }}").config; if config.update then print(config.update) else print('The subtree `'$repo_name'` to its latest commit') end')"
pr_body=$(bash ${{ github.action_path }}/markdown_pr_body.sh "updated" "true" $repo_name)

gh pr create --base ${GITHUB_REF##*/} --head $branch --title 'Bump `'$repo_name'` to its latest commit' --body "$pr_body" || true

if [[ $(gh pr list --base ${GITHUB_REF##*/} --head $branch) ]]; then
pr=$(gh pr list --base ${GITHUB_REF##*/} --head $branch)
Expand Down
8 changes: 0 additions & 8 deletions repos-template.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
local config = {
labels_fetch = "repos-fetch,dependencies", -- Labels that will be added to the PRs when the `fetch-repos.lua` script is run, separated by commas in the same string(!).
labels_update = "dependencies", -- Labels that will be added to the PRs when running the `update-repos.lua` script, separated by commas in the same string(!).
pr_body = { -- The body of the PRs that will be used. `both` is used when both actions are run in the same PR.
update = "This PR updated all the subtrees." ..
"The updated subtrees are:" ..
"- Test 1." ..
"- Test 2.",
fetch = "This PR fetched all the subtrees.",
both = "This PR both updates subtrees and fetches new ones."
}
}

local repos = {
Expand Down
9 changes: 0 additions & 9 deletions repos-test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@

local config = {
labels_update = "dependencies,enhancement",
pr_body = {
update =
"This is a PR that updated the subtrees.\n" ..
"Updated submodules are:" ..
"- Test 1." ..
"- Test2. ",
fetch = "This is a PR that fetched all the subtrees specified in `repos.lua`",
both = "This is a PR that both updated and fetched the subtrees."
}
}

local repos = {
Expand Down
119 changes: 119 additions & 0 deletions tools/markdown_pr_body.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Arguments:
# [0]: Taken by Bash, the name of the script
# [1]: The mode that will be used to generate the PR body. Available modes are: "all", "updated", "added"
# [2]:
# Is it separated or not?
# If it's separated, each subtree PR will use `pr_body_update_separate.txt` and put their subtree name replacing
# `{updated_subtree}`. Available options are: `true` or `false`
#
# [3]: The subtree name that will be used to replace `{updated_subtree}` in `pr_body_update_separate.txt`.

if [ "$1" == "all" ] || [ "$1" == "" ];
then
# Reads the files and stores the lines in arrays
mapfile -t updated_subtrees < updated_subtrees.txt
mapfile -t added_subtrees < added_subtrees.txt

# Adds backticks around each subtree name
updated_subtrees=("${updated_subtrees[@]/#/\\\`}")
added_subtrees=("${added_subtrees[@]/#/\\\`}")

# Convert them to Markdown lists
updated_subtrees_md=$(printf -- '- %s\\n' "${updated_subtrees[@]}")
added_subtrees_md=$(printf -- '- %s\\n' "${added_subtrees[@]}")

# Creates a temporary file for the PR body
cat pr_body.txt > TEMP.txt

if test -s updated_subtrees.txt;
then
sed -i -e "s|{updated_subtrees}|$updated_subtrees_md|g" TEMP.txt
else
sed -i -e '/The following subtrees were updated:/,/^$/d' TEMP.txt
sed -i -e '/{updated_subtrees}/d' TEMP.txt
fi

if test -s added_subtrees.txt;
then
sed -i -e "s|{added_subtrees}|$added_subtrees_md|g" TEMP.txt
else
sed -i -e '/New subtrees were added:/,/^$/d' TEMP.txt
sed -i -e '/{added_subtrees}/d' TEMP.txt
fi

# Do both files have no content?
if ! test -s updated_subtrees.txt && ! test -s added_subtrees.txt;
then
echo "Various subtrees were updated or added in this PR." > TEMP.txt
fi

# Prints the PR body
cat TEMP.txt

# Delete the temporary PR body file
rm TEMP.txt

elif [ "$1" == "updated" ];
then

# Reads the files and stores the lines in arrays
mapfile -t updated_subtrees < updated_subtrees.txt

# Adds backticks around each subtree name
updated_subtrees=("${updated_subtrees[@]/#/\\\`}")

# Convert them to Markdown lists
updated_subtrees_md=$(printf -- '- %s\\n' "${updated_subtrees[@]}")

# Creates a temporary file for the PR body
cat pr_body_update.txt > TEMP.txt

# Prints the PR body
cat TEMP.txt

# Delete the temporary PR body file
rm TEMP.txt

elif [ "$1" == "added" ];
then

# Reads the files and stores the lines in arrays
mapfile -t added_subtrees < added_subtrees.txt

# Adds backticks around each subtree name
added_subtrees=("${added_subtrees[@]/#/\\\`}")

# Convert them to Markdown lists
added_subtrees_md=$(printf -- '- %s\\n' "${added_subtrees[@]}")

# Creates a temporary file for the PR body
cat pr_body_fetch.txt > TEMP.txt

# Prints the PR body
cat TEMP.txt

# Delete the temporary PR body file
rm TEMP.txt

else
echo "Invalid mode. Available modes are: \"all\", \"updated\", \"added\""
exit 1
fi

if [ "$2" == "true" ];
then
# Creates a temporary file for the PR body
cat pr_body_update_separate.txt > TEMP.txt

# Add backticks around the subtree name
updated_subtree="\\\`$3\\\`"

# Replace the updated subtree name
sed -i -e "s|{updated_subtree}|$updated_subtree|g" TEMP.txt

# Prints the PR body
cat TEMP.txt

# Delete the temporary PR body file
rm TEMP.txt
fi
9 changes: 9 additions & 0 deletions tools/pr_body.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
The following subtrees were updated:

{updated_subtrees}
New subtrees were added:

{added_subtrees}
For more information on how the tool works, please check [our repository](https://github.com/Panquesito7/submodules-alternative).
"""
7 changes: 7 additions & 0 deletions tools/pr_body_fetch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
New subtrees were added in their latest commit:

{added_subtrees}

For more information on how the tool works, please check [our repository](https://github.com/Panquesito7/submodules-alternative).
"""
8 changes: 8 additions & 0 deletions tools/pr_body_update.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""
This PR updated the following subtrees:

{updated_subtrees}

These subtrees have been set to their latest commit. You can see the full changes in the diff.
For more information on how to use this tool and how it works, please check our [repository](https://github.com/Panquesito7/submodules-alternative).
"""
6 changes: 6 additions & 0 deletions tools/pr_body_update_separate.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
The subtree {updated_subtree} was updated to its latest commit.
See the diff. for all the changes that the updated subtree provided.

For more information on how to use this tool and how it works, please check our [repository](https://github.com/Panquesito7/submodules-alternative).
"""
17 changes: 17 additions & 0 deletions update-repos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ local function update_repos()
os.execute("git checkout --theirs " .. repos[i].dir .. repos[i].name)
os.execute("git add " .. repos[i].dir .. repos[i].name)

local updated_repos = io.open("updated_repos.txt", "w+")
if updated_repos then
updated_repos:write("")
updated_repos:close()
end

updated_repos = io.open("updated_repos.txt", "a+")
if updated_repos then
updated_repos:write(repos[i].name .. "\n")
updated_repos:close()
end

if one_pr == "false" then
local default_branch = io.popen("git remote show origin | grep \"HEAD branch\" | cut -d' ' -f5"):read("*a")

Expand Down Expand Up @@ -132,5 +144,10 @@ local function update_repos()
end
end

local updated_repos = io.open("updated_repos.txt", "a+")
if updated_repos then
updated_repos:write("\n")
end

-- Update all repositories.
update_repos()