Skip to content

Commit

Permalink
Merge branch 'support-delete-based-on-age'
Browse files Browse the repository at this point in the history
* support-delete-based-on-age:
  gh_release: Add older-than switch to limit deletion by age
  • Loading branch information
jcfr committed Mar 29, 2020
2 parents 4e6ae60 + 88f70a4 commit deb6fef
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Features (CLI and Python API)

* `delete`:

* Add support for ``--release-type``. Fix issue [#50](https://github.com/j0057/github-release/pull/50). Contributed by [@Flamefire](https://github.com/Flamefire).
* Add support for ``--release-type`` and ``--older-than``. Fix issue [#50](https://github.com/j0057/github-release/pull/50). Contributed by [@Flamefire](https://github.com/Flamefire).


Issues (CLI and Python API)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ It understands the following commands:
```bash
--keep-pattern KEEP_PATTERN
--type [all, draft, prerelease, release]
--older-than HOURS
--dry-run
--verbose
--help
Expand Down Expand Up @@ -476,4 +477,4 @@ It is covered by the Apache License, Version 2.0:
http://www.apache.org/licenses/LICENSE-2.0

The license file was added at revision 0393859 on 2017-02-12, but you may
consider that the license applies to all prior revisions as well.
consider that the license applies to all prior revisions as well.
29 changes: 28 additions & 1 deletion github_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import print_function

from datetime import tzinfo, timedelta, datetime
import fnmatch
import glob
import json
Expand All @@ -27,6 +28,21 @@
_github_api_url = None


class _UTC(tzinfo):
"""UTC"""

ZERO = timedelta(0)

def utcoffset(self, dt):
return self.ZERO

def tzname(self, dt):
return "UTC"

def dst(self, dt):
return self.ZERO


class _NoopProgressReporter(object):
reportProgress = False

Expand Down Expand Up @@ -528,6 +544,7 @@ def gh_release_edit(repo_name, current_tag_name,
@click.argument("pattern")
@click.option("--keep-pattern")
@click.option("--release-type", type=click.Choice(['all', 'draft', 'prerelease', 'release']), default='all')
@click.option("--older-than", type=int, default=0)
@click.option("--dry-run", is_flag=True, default=False)
@click.option("--verbose", is_flag=True, default=False)
@click.pass_obj
Expand All @@ -537,7 +554,7 @@ def _cli_release_delete(*args, **kwargs):


@_check_for_credentials
def gh_release_delete(repo_name, pattern, keep_pattern=None, release_type='all',
def gh_release_delete(repo_name, pattern, keep_pattern=None, release_type='all', older_than=0,
dry_run=False, verbose=False):
releases = get_releases(repo_name)
candidates = []
Expand All @@ -556,6 +573,16 @@ def gh_release_delete(repo_name, pattern, keep_pattern=None, release_type='all',
print('skipping release {0}: type {1} is not {2}'.format(
release['tag_name'], get_release_type(release), release_type))
continue
# Assumes Zulu time.
# See https://stackoverflow.com/questions/127803/how-to-parse-an-iso-8601-formatted-date
utc = _UTC()
rel_date = datetime.strptime(release['created_at'], "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=utc)
rel_age = int((datetime.now(utc) - rel_date).total_seconds() / 60 / 60) # In hours
if older_than > rel_age:
if verbose:
print('skipping release {0}: created less than {1} hours ago ({2}hrs)'.format(
release['tag_name'], older_than, rel_age))
continue
candidates.append(release)
for release in candidates:
print('deleting release {0}'.format(release['tag_name']))
Expand Down

0 comments on commit deb6fef

Please sign in to comment.