From d974196464490dda8c6ce78acb82cff114010c3c Mon Sep 17 00:00:00 2001 From: David Leal Date: Mon, 17 Jul 2023 12:02:37 -0600 Subject: [PATCH] Delete branches if they're already merged (#39) --- README.md | 10 +++++++++- action.yml | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a8e5d40..4422de2 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ jobs: - uses: actions/checkout@v3 with: fetch-depth: 0 # This pulls changes before doing any changes - - uses: Panquesito7/submodules-alternative@v1.5.4 + - uses: Panquesito7/submodules-alternative@v1.5.5 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. @@ -77,8 +77,16 @@ jobs: update_repos: true # When enabled, this will attempt to update all the repositories. squash_commits: false # Whether to squash all commits or not on every repository update/addition. Cannot be used if `one_pr` is disabled. one_pr: false # Creates one single PR for everything if enabled. Works only for `update_repos` if disabled. + delete_existing_branches: true # Deletes the branches that updated the subtrees. Note that this is done only on action run, not on immediate PR merge. ``` +If `delete_existing_branches` is enabled, it will attempt to delete the branches before running the scripts to ensure there are no merge conflicts.\ +However, this is not the most efficient way of deleting the branches. These are other alternatives I recommend: + +1. Automatically [delete branches](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/configuring-pull-request-merges/managing-the-automatic-deletion-of-branches) on PR merging (recommended). +2. Manually delete the branches (not recommended). +3. Use a GitHub Action like [Delete Merged Branch](https://github.com/SvanBoxel/delete-merged-branch). + You can also configure to run the workflow manually by using `workflow_dispatch` instead of `schedule`.\ For more information about Cron, you can check [CronHub](https://crontab.cronhub.io/). diff --git a/action.yml b/action.yml index 50eee2e..15662e1 100644 --- a/action.yml +++ b/action.yml @@ -41,6 +41,10 @@ inputs: description: "Creates one single PR for updating all the repositories. This does not apply to the `fetch-repos` script. `squash_commits` must be disabled!" required: false default: "false" + delete_existing_branches: + description: "Deletes the branches that updated the subtrees. Note that this is done only on action run, not on immediate PR merge." + required: false + default: "true" runs: using: "composite" steps: @@ -99,6 +103,25 @@ runs: wget https://raw.githubusercontent.com/Panquesito7/submodules-alternative/main/update-repos.lua fi fi + - name: Delete existing branches + shell: bash + run: | + if [[ ${{ inputs.update_repos }} == true ]] && [[ ${{ inputs.delete_existing_branches }} == true ]]; then + branches=$(lua -e 'local repos = require("${{ inputs.repos_filename }}").repos; dofile("helper-functions.lua"); get_repo_branches(repos)') + for branch in ${branches[@]}; do + if git ls-remote --heads origin $branch | grep -q $branch; then + if git diff --name-only origin/${GITHUB_REF##*/} $branch | grep -q $branch; then + git push origin -d $branch + fi + fi + done + + if git ls-remote --heads origin ${{ inputs.branch_name }} | grep -q ${{ inputs.branch_name }}; then + if git diff --name-only origin/${GITHUB_REF##*/} ${{ inputs.branch_name }} | grep -q ${{ inputs.branch_name }}; then + git push origin -d ${{ inputs.branch_name }} + fi + fi + fi - name: Clone repositories shell: bash run: |