From 5212a8752a1d807d293988f06d1c073dbd6f23c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Thu, 25 Feb 2021 20:44:26 +0100 Subject: [PATCH] Fix /~o/r/send-email/end 500ing if it couldn't find the specified branch --- gitsrht/blueprints/email.py | 48 ++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/gitsrht/blueprints/email.py b/gitsrht/blueprints/email.py index 6a2b886..158b457 100644 --- a/gitsrht/blueprints/email.py +++ b/gitsrht/blueprints/email.py @@ -29,6 +29,28 @@ smtp_password = cfg("mail", "smtp-password", default=None) smtp_from = cfg("mail", "smtp-from", default=None) outgoing_domain = cfg("git.sr.ht", "outgoing-domain") +def render_send_email_start(owner, repo, git_repo, selected_branch, + ncommits=8, **kwargs): + branches = [( + branch, + git_repo.branches[branch], + git_repo.get(git_repo.branches[branch].target) + ) for branch + in git_repo.raw_listall_branches(pygit2.GIT_BRANCH_LOCAL)] + branches = sorted(branches, + key=lambda b: (b[0] == selected_branch, commit_time(b[2])), + reverse=True) + + commits = dict() + for branch in branches[:2]: + commits[branch[0]] = get_log(git_repo, + branch[2], commits_per_page=ncommits) + + return render_template("send-email.html", + view="send-email", owner=owner, repo=repo, + selected_branch=selected_branch, branches=branches, + commits=commits, **kwargs) + @mail.route("///send-email") @loginrequired def send_email_start(owner, repo): @@ -41,25 +63,8 @@ def send_email_start(owner, repo): ncommits = 8 selected_branch = request.args.get("branch", default=None) - branches = [( - branch, - git_repo.branches[branch], - git_repo.get(git_repo.branches[branch].target) - ) for branch - in git_repo.raw_listall_branches(pygit2.GIT_BRANCH_LOCAL)] - branches = sorted(branches, - key=lambda b: (b[0] == selected_branch, commit_time(b[2])), - reverse=True) - - commits = dict() - for branch in branches[:2]: - commits[branch[0]] = get_log(git_repo, - branch[2], commits_per_page=ncommits) - - return render_template("send-email.html", - view="send-email", owner=owner, repo=repo, - selected_branch=selected_branch, branches=branches, - commits=commits) + return render_send_email_start(owner, repo, git_repo, selected_branch, + ncommits) @mail.route("///send-email/end", methods=["POST"]) @loginrequired @@ -68,7 +73,12 @@ def send_email_end(owner, repo): with GitRepository(repo.path) as git_repo: valid = Validation(request) branch = valid.require("branch") + if not branch in git_repo.branches: + valid.error(f"Branch {branch} not found", field="branch") commit = valid.require(f"commit-{branch}") + if not valid.ok: + return render_send_email_start(owner, repo, git_repo, branch, + **valid.kwargs) branch = git_repo.branches[branch] tip = git_repo.get(branch.target) -- 2.38.4