Skip to content

Commit

Permalink
Add support for deleting old tags as well
Browse files Browse the repository at this point in the history
It might also be beneficial to get rid of old tags so add a new
`delete_tags` option for that which defaults to `false` to preserve
the existing behavior.
  • Loading branch information
hwoarang committed Jun 3, 2020
1 parent 75a26c7 commit 3fe627f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Delete Old Branches Action

## Introduction
This simple GitHub Action will delete branches that haven't received a commit recently. The time since last commit is configurable
This simple GitHub Action will delete branches and optionally tags that haven't received a commit recently. The time since last commit is configurable

## Disclaimer
**Always** run the GitHub action in dry-run mode to ensure that it will do the right thing before you actually let it do it. Also make sure that you have a full copy of the repository (`git clone --mirror ...`) in case something goes bad
Expand All @@ -29,6 +29,7 @@ jobs:
repo_token: ${{ github.token }}
date: '3 months ago'
dry_run: true
delete_tags: false
```
Once you are happy switch, `dry_run` to `false` so the action actually does the job

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ inputs:
description: 'Run in dry-run mode so no branches are deleted'
required: false
default: true
delete_tags:
description: 'Also look for tags to delete'
required: false
default: false

runs:
using: 'docker'
Expand Down
17 changes: 13 additions & 4 deletions delete-old-branches
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ REPO="${GITHUB_REPOSITORY}"
DATE=${INPUT_DATE}
GITHUB_TOKEN=${INPUT_REPO_TOKEN}
DRY_RUN=${INPUT_DRY_RUN:-true}
DELETE_TAGS=${INPUT_DELETE_TAGS:-false}

branch_protected() {
local br=${1}
Expand All @@ -26,13 +27,14 @@ branch_protected() {
esac
}

delete_branch() {
local br=${1}
delete_branch_or_tag() {
local br=${1} ref="heads"
[[ "${DELETE_TAGS}" == true ]] && ref="tags"

echo "Deleting: ${br}"
if [[ "${DRY_RUN}" == false ]]; then
status=$(curl -X DELETE -o /dev/null -s -H "Authorization: Bearer ${GITHUB_TOKEN}" \
-w "%{http_code}" "${BASE_URI}/repos/${REPO}/git/refs/heads/${br}")
-w "%{http_code}" "${BASE_URI}/repos/${REPO}/git/refs/${ref}/${br}")

case ${status} in
204) ;;
Expand All @@ -49,9 +51,16 @@ main() {
for br in $(git ls-remote -q --heads | sed "s@^.*heads/@@"); do
if [[ -z "$(git log --oneline -1 --since="${DATE}" origin/"${br}")" ]]; then
branch_protected "${br}" && echo "branch: ${br} is likely protected. Won't delete it" && continue
delete_branch "${br}"
delete_branch_or_tag "${br}"
fi
done
if [[ "${DELETE_TAGS}" == true ]]; then
for br in $(git ls-remote -q --tags | sed "s@^.*tags/@@"); do
if [[ -z "$(git log --oneline -1 --since="${DATE}" "${br}")" ]]; then
delete_branch_or_tag "${br}"
fi
done
fi
}

main "$@"

0 comments on commit 3fe627f

Please sign in to comment.