Skip to content

Commit

Permalink
Add clean subcommand
Browse files Browse the repository at this point in the history
Extend CLI functionality with a `clean` subcommand to delete
pre-releases between two stable releases. Update README with usage
examples, modify command validation, and add logic to find and delete
pre-releases.
  • Loading branch information
Amygos committed Oct 30, 2024
1 parent 02cdb41 commit 7f8d3a2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
15 changes: 14 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ gh extension install NethServer/gh-ns8-release-module
## Usage

```bash
gh ns8-release-module [create|check|comment] [options]
gh ns8-release-module [create|check|comment|clean] [options]
```

### Commands

- `create`: Creates a new release.
- `check`: Check the status of the `main` branch
- `comment`: Adds a comment to the release issues
- `clean`: Removes pre-releases between stable releases

### Options

Expand Down Expand Up @@ -88,6 +89,18 @@ Add a comment to the release issues:
gh ns8-release-module comment --repo NethServer/ns8-module --release-name <release-name>
```

Remove pre-releases between stable releases:

```bash
gh ns8-release-module clean --repo NethServer/ns8-module --release-name <stable-release>
```

Remove pre-releases from latest stable release:

```bash
gh ns8-release-module clean --repo NethServer/ns8-module
```

## Troubleshooting

If you encounter any issues while using the `gh-ns8-release-module` extension, consider the following troubleshooting steps:
Expand Down
28 changes: 28 additions & 0 deletions functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,31 @@ function create_comment() {
fi
done
}

# Function to clean pre-releases between stable releases
function clean_releases() {
local repo=$1
local stable_release=$2
local pre_releases=()

# Get the previous stable release
previous_release=$(find_previous_release $repo $stable_release)

pre_releases=$(gh release list --repo $repo --limit 1000 --json isPrerelease,tagName,createdAt | \
jq --arg start_tag "$previous_release" --arg end_tag "$stable_release" -r '
def between($start; $end):
map(select(.tagName == $start).createdAt) as $start_date |
map(select(.tagName == $end).createdAt) as $end_date |
map(select(.isPrerelease and .createdAt > $start_date[0] and .createdAt <= $end_date[0])) |
sort_by(.createdAt) |
map(.tagName) |
join(" ");
between($start_tag; $end_tag)
')

if [ -n "$pre_releases" ]; then
for pre_release in $pre_releases; do
gh release delete $pre_release --repo $repo --yes
done
fi
}
26 changes: 20 additions & 6 deletions gh-ns8-release-module
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ source "$(dirname "$(readlink -f "$0")")/functions.sh"

# Function to display usage instructions
function usage() {
echo "Usage: $0 [create|check|comment] --repo <repo-name> [options]"
echo "Usage: $0 [create|check|comment|clean] --repo <repo-name> [options]"
echo ""
echo "Options:"
echo " --repo <repo-name> The GitHub NethServer 8 module repository (e.g., owner/ns8-module)."
Expand All @@ -22,12 +22,12 @@ function usage() {
exit 1
}

# The first argument must be `create` or `comment`
if [[ "$1" =~ ^(create|check|comment)$ ]]; then
# The first argument must be `create`, `check`, `comment`, or `clean`
if [[ "$1" =~ ^(create|check|comment|clean)$ ]]; then
subcommand=$1
shift
else
echo 'The first argument must be `create`, `check` or `comment`'
echo 'The first argument must be `create`, `check`, `comment`, or `clean`'
usage
fi

Expand Down Expand Up @@ -105,7 +105,7 @@ else
fi

# If the argument `--testing` is not provided then the `--release-name` argument must be present
if [ $testing_arg == 0 ] && [ -z "$release_name_arg" ] && [ $subcommand != "check" ]; then
if [ $testing_arg == 0 ] && [ -z "$release_name_arg" ] && [ $subcommand != "check" ] && [ $subcommand != "clean" ]; then
echo "Please provide the release name using the --release-name flag"
exit 1
fi
Expand All @@ -120,7 +120,7 @@ if [ ! -z "$release_name_arg" ]; then
fi


# Check if the subcommand is `create` or `comment`
# Check if the subcommand is `create`, `check`, `comment`, or `clean`
if [ "$subcommand" == "create" ]; then
# Create a new release

Expand Down Expand Up @@ -182,4 +182,18 @@ elif [ "$subcommand" == "comment" ]; then
fi

create_comment $repo_arg $release_name_arg

elif [ "$subcommand" == "clean" ]; then

# If no stable release is provided, get the latest stable release
if [ -z "$release_name_arg" ]; then
release_name_arg=$(gh release list --repo $repo_arg --exclude-pre-releases --json tagName --limit 1 --order desc --jq '.[0].tagName')
if [ -z "$release_name_arg" ]; then
echo "No stable release found in the repository."
exit 1
fi
fi

clean_releases $repo_arg $release_name_arg

fi

0 comments on commit 7f8d3a2

Please sign in to comment.