From 6d7c9eb06d79b8655c8d805eb9d5a3735f65e8ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Thu, 30 Jul 2020 18:38:49 +0200 Subject: [PATCH] Allow setting the default branch from the info settings tab --- gitsrht/app.py | 2 ++ gitsrht/blueprints/manage.py | 47 ++++++++++++++++++++++++++++ gitsrht/templates/settings_info.html | 29 +++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 gitsrht/blueprints/manage.py create mode 100644 gitsrht/templates/settings_info.html diff --git a/gitsrht/app.py b/gitsrht/app.py index 525e047..afafdc3 100644 --- a/gitsrht/app.py +++ b/gitsrht/app.py @@ -25,6 +25,7 @@ class GitApp(ScmSrhtFlask): from gitsrht.blueprints.api import plumbing, porcelain from gitsrht.blueprints.artifacts import artifacts from gitsrht.blueprints.email import mail + from gitsrht.blueprints.manage import manage from gitsrht.blueprints.repo import repo from gitsrht.blueprints.stats import stats from srht.graphql import gql_blueprint @@ -32,6 +33,7 @@ class GitApp(ScmSrhtFlask): self.register_blueprint(plumbing) self.register_blueprint(porcelain) self.register_blueprint(mail) + self.register_blueprint(manage) self.register_blueprint(repo) self.register_blueprint(stats) self.register_blueprint(webhooks_notify) diff --git a/gitsrht/blueprints/manage.py b/gitsrht/blueprints/manage.py new file mode 100644 index 0000000..f51cc99 --- /dev/null +++ b/gitsrht/blueprints/manage.py @@ -0,0 +1,47 @@ +import pygit2 +from flask import Blueprint, request, render_template +from flask import redirect, url_for +from gitsrht.git import Repository as GitRepository +from srht.database import db +from srht.oauth import loginrequired +from srht.validation import Validation +from scmsrht.access import check_access, UserAccess +from scmsrht.repos.redirect import BaseRedirectMixin +from scmsrht.repos.repository import RepoVisibility +from scmsrht.webhooks import UserWebhook + +manage = Blueprint('manage_git', __name__) + +@manage.route("///settings/info_git", methods=["POST"]) +@loginrequired +def settings_info_git_POST(owner_name, repo_name): + owner, repo = check_access(owner_name, repo_name, UserAccess.manage) + if isinstance(repo, BaseRedirectMixin): + repo = repo.new_repo + valid = Validation(request) + desc = valid.optional("description", default=repo.description) + visibility = valid.optional("visibility", + cls=RepoVisibility, + default=repo.visibility) + branch = valid.optional("default_branch_name") + with GitRepository(repo.path) as git_repo: + new_default_branch = None + if branch: + try: + new_default_branch = git_repo.branches.get(branch) + except pygit2.InvalidSpecError: + valid.error(f"Branch {branch} not found", field="default_branch_name") + if not valid.ok: + return render_template("settings_info.html", + owner=owner, repo=repo, **valid.kwargs) + if new_default_branch: + head_ref = git_repo.lookup_reference("HEAD") + head_ref.set_target(new_default_branch.name) + + repo.visibility = visibility + repo.description = desc + UserWebhook.deliver(UserWebhook.Events.repo_update, + repo.to_dict(), UserWebhook.Subscription.user_id == repo.owner_id) + db.session.commit() + return redirect(url_for("manage.settings_info", + owner_name=owner_name, repo_name=repo_name)) diff --git a/gitsrht/templates/settings_info.html b/gitsrht/templates/settings_info.html new file mode 100644 index 0000000..de2dee1 --- /dev/null +++ b/gitsrht/templates/settings_info.html @@ -0,0 +1,29 @@ +{% set info_action=url_for("manage_git.settings_info_git_POST", + owner_name=owner.canonical_name, repo_name=repo.name) %} +{% extends "bases/scmsettings_info.html" %} + +{% block extrafields %} +
+ + + {{valid.summary('default_branch_name')}} +
+{% endblock %} -- 2.38.4