Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gitlab_release_notes/generate.py: Add --quiet cmdline option. #6

Merged
merged 7 commits into from
Oct 6, 2023
40 changes: 30 additions & 10 deletions gitlab_release_notes/generate.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import datetime
import gitlab
import os.path
import sys
from .version import __version__

def generate_release_notes(project_id, endstr = ' <br>', **config):
def generate_release_notes(project_id, endstr = ' <br>', since=None, quiet=False, **config):
"""
Generate the release notes of a gitlab project from the last release

Expand Down Expand Up @@ -32,33 +33,43 @@ def generate_release_notes(project_id, endstr = ' <br>', **config):
gl = gitlab.Gitlab(**config)
project = gl.projects.get(project_id)

if not project.mergerequests.list(state='merged'):
raise ValueError(f"There is not merged merge request for project {project_id} {project.name}")
if not project.mergerequests.list(get_all=False,state='merged'):
raise ValueError(f"There is no merged merge request for project {project_id} {project.name}")

if not project.releases.list():
log = f"Changelog of {project.name}:{endstr}"
log = ""

if since:
log_pending = f"Changelog of {project.name} since {since}:{endstr}"
last_date = since
elif not project.releases.list(get_all=False):
log_pending = f"Changelog of {project.name}:{endstr}"
last_date = '0000-01-01T00:00:00Z'
else:
last_release = project.releases.list()[0]
log = f"Changelog since release {last_release.name} of {project.name}:{endstr}"
last_release = project.releases.list(get_all=False)[0]
log_pending = f"Changelog since release {last_release.name} of {project.name}:{endstr}"
last_date = last_release.released_at

page = 1
list_mrs = project.mergerequests.list(state='merged',
get_all=False,
order_by='updated_at',
updated_after=last_date,
page=page)
if not list_mrs:
log += f"There is no merged merge request after {last_date}"
if not quiet:
log += log_pending
log += f"There is no merged merge request after {last_date}{endstr}"
return log

log += log_pending
while list_mrs:
for mr in list_mrs:
line = f" * {mr.title} (@{mr.author['username']}){endstr}"
log += line

page += 1
list_mrs = project.mergerequests.list(state='merged',
get_all=False,
order_by='updated_at',
updated_after=last_date,
page=page
Expand All @@ -80,15 +91,24 @@ def main():
parser.add_argument("--private_token", type=str, required=False, default=None)
parser.add_argument('--version', action='version', version=__version__)
parser.add_argument('--html', action='store_true')
parser.add_argument('--since', type=datetime.date.fromisoformat, required=False, default=None)
parser.add_argument('--quiet', action='store_true')

args = parser.parse_args()

if args.html:
endstr = ' <br>'
else:
endstr = '\n'
notes = generate_release_notes(args.project_id, url=args.url, endstr=endstr, private_token=args.private_token)
print(notes)
notes = generate_release_notes(args.project_id,
url=args.url,
endstr=endstr,
since=args.since,
quiet=args.quiet,
private_token=args.private_token,
)
if notes:
print(notes)

if __name__ == "__main__":
main()
Loading