Skip to content

Commit

Permalink
Add option to skip prerelease versions.
Browse files Browse the repository at this point in the history
Replace release sorting by tag with release sorting by creation date.
  • Loading branch information
BrOleg5 committed Jan 5, 2024
1 parent eb88def commit ab18e96
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
7 changes: 4 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@ CLI Help output::
keychain that holds the personal access or OAuth token
--releases include release information, not including assets or
binaries
--assets include assets alongside release information; only
applies if including releases
--latest-releases INCLUDE_LATEST_RELEASES
include certain number of the latest releases; only
include certain number of the latest releases;
only applies if including releases
--skip-prerelease skip prerelease and draft versions; only applies if including releases
--assets include assets alongside release information; only
applies if including releases
--exclude [REPOSITORY [REPOSITORY ...]]
names of repositories to exclude from backup.
Expand Down
26 changes: 18 additions & 8 deletions github_backup/github_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from urllib.parse import quote as urlquote
from urllib.parse import urlencode, urlparse
from urllib.request import HTTPRedirectHandler, Request, build_opener, urlopen
from operator import itemgetter
from datetime import datetime

try:
from . import __version__
Expand Down Expand Up @@ -377,19 +377,25 @@ def parse_args(args=None):
dest="include_releases",
help="include release information, not including assets or binaries",
)
parser.add_argument(
"--assets",
action="store_true",
dest="include_assets",
help="include assets alongside release information; only applies if including releases",
)
parser.add_argument(
"--latest-releases",
type=int,
default=0,
dest="include_latest_releases",
help="include certain number of the latest releases; only applies if including releases",
)
parser.add_argument(
"--skip-prerelease",
action="store_true",
dest="skip_prerelease",
help="skip prerelease and draft versions; only applies if including releases",
)
parser.add_argument(
"--assets",
action="store_true",
dest="include_assets",
help="include assets alongside release information; only applies if including releases",
)
parser.add_argument(
"--throttle-limit",
dest="throttle_limit",
Expand Down Expand Up @@ -1214,8 +1220,12 @@ def backup_releases(args, repo_cwd, repository, repos_template, include_assets=F
release_template = "{0}/{1}/releases".format(repos_template, repository_fullname)
releases = retrieve_data(args, release_template, query_args=query_args)

if args.skip_prerelease:
releases = [r for r in releases if r["prerelease"] == False and r["draft"] == False]

if args.include_latest_releases and args.include_latest_releases < len(releases):
releases = sorted(releases, key=itemgetter('tag_name'), reverse=True)
releases.sort(key=lambda item: datetime.strptime(item["created_at"], "%Y-%m-%dT%H:%M:%SZ"), \
reverse=True)
releases = releases[:args.include_latest_releases]
logger.info("Saving the latest {0} releases to disk".format(len(releases)))
else:
Expand Down

0 comments on commit ab18e96

Please sign in to comment.