def new_issue(repo, username=None): """ Create a new issue """ repo = progit.lib.get_project(SESSION, repo, user=username) if repo is None: flask.abort(404, 'Project not found') status = progit.lib.get_issue_statuses(SESSION) form = progit.forms.IssueForm(status=status) if form.validate_on_submit(): title = form.title.data content = form.issue_content.data try: message = progit.lib.new_issue( SESSION, repo=repo, title=title, content=content, user=flask.g.fas_user.username, ticketfolder=APP.config['TICKETS_FOLDER'], ) SESSION.commit() flask.flash(message) return flask.redirect( flask.url_for('view_issues', username=username, repo=repo.name)) except progit.exceptions.ProgitException, err: flask.flash(str(err), 'error') except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error')
def new_issue(repo, username=None): """ Create a new issue """ repo = progit.lib.get_project(SESSION, repo, user=username) if repo is None: flask.abort(404, 'Project not found') status = progit.lib.get_issue_statuses(SESSION) form = progit.forms.IssueForm(status=status) if form.validate_on_submit(): title = form.title.data content = form.issue_content.data try: message = progit.lib.new_issue( SESSION, repo=repo, title=title, content=content, user=flask.g.fas_user.username, ticketfolder=APP.config['TICKETS_FOLDER'], ) SESSION.commit() flask.flash(message) return flask.redirect(flask.url_for( 'view_issues', username=username, repo=repo.name)) except progit.exceptions.ProgitException, err: flask.flash(str(err), 'error') except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error')
def new_project(): """ Form to create a new project. """ form = progit.forms.ProjectForm() if form.validate_on_submit(): name = form.name.data description = form.description.data try: message = progit.lib.new_project( SESSION, name=name, description=description, user=flask.g.fas_user.username, gitfolder=APP.config['GIT_FOLDER'], docfolder=APP.config['DOCS_FOLDER'], ticketfolder=APP.config['TICKETS_FOLDER'] ) SESSION.commit() generate_gitolite_acls() flask.flash(message) return flask.redirect(flask.url_for('view_repo', repo=name)) except progit.exceptions.ProgitException, err: flask.flash(str(err), 'error') except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error')
def user_settings(): """ Update the user settings. """ user = progit.lib.get_user(SESSION, flask.g.fas_user.username) form = progit.forms.UserSettingsForm() if form.validate_on_submit(): ssh_key = form.ssh_key.data try: message = progit.lib.update_user_ssh( SESSION, user=user, ssh_key=ssh_key, ) if message != 'Nothing to update': generate_gitolite_key(user.user, ssh_key) generate_authorized_key_file() SESSION.commit() flask.flash(message) return flask.redirect( flask.url_for('view_user', username=user.user)) except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error')
def add_comment_issue(repo, issueid, username=None): ''' Add a comment to an issue. ''' repo = progit.lib.get_project(SESSION, repo, user=username) if repo is None: flask.abort(404, 'Project not found') if not repo.issue_tracker: flask.abort(404, 'No issue tracker found for this project') issue = progit.lib.get_issue(SESSION, issueid) if issue is None or issue.project != repo: flask.abort(404, 'Issue not found') form = progit.forms.AddIssueCommentForm() if form.validate_on_submit(): comment = form.comment.data try: message = progit.lib.add_issue_comment( SESSION, issue=issue, comment=comment, user=flask.g.fas_user.username, ticketfolder=APP.config['TICKETS_FOLDER'], ) SESSION.commit() flask.flash(message) except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error')
def edit_issue(repo, issueid, username=None): """ Edit the specified issue """ repo = progit.lib.get_project(SESSION, repo, user=username) if repo is None: flask.abort(404, 'Project not found') if not repo.issue_tracker: flask.abort(404, 'No issue tracker found for this project') if not is_repo_admin(repo): flask.abort(403, 'You are not allowed to edit issues for this project') issue = progit.lib.get_issue(SESSION, issueid) if issue is None or issue.project != repo: flask.abort(404, 'Issue not found') status = progit.lib.get_issue_statuses(SESSION) form = progit.forms.IssueForm(status=status) if form.validate_on_submit(): title = form.title.data content = form.issue_content.data status = form.status.data try: message = progit.lib.edit_issue( SESSION, issue=issue, title=title, content=content, status=status, ticketfolder=APP.config['TICKETS_FOLDER'], ) SESSION.commit() flask.flash(message) url = flask.url_for('view_issue', username=username, repo=repo.name, issueid=issueid) return flask.redirect(url) except progit.exceptions.ProgitException, err: flask.flash(str(err), 'error') except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error')
def edit_issue(repo, issueid, username=None): """ Edit the specified issue """ repo = progit.lib.get_project(SESSION, repo, user=username) if repo is None: flask.abort(404, 'Project not found') if not repo.issue_tracker: flask.abort(404, 'No issue tracker found for this project') if not is_repo_admin(repo): flask.abort( 403, 'You are not allowed to edit issues for this project') issue = progit.lib.get_issue(SESSION, issueid) if issue is None or issue.project != repo: flask.abort(404, 'Issue not found') status = progit.lib.get_issue_statuses(SESSION) form = progit.forms.IssueForm(status=status) if form.validate_on_submit(): title = form.title.data content = form.issue_content.data status = form.status.data try: message = progit.lib.edit_issue( SESSION, issue=issue, title=title, content=content, status=status, ticketfolder=APP.config['TICKETS_FOLDER'], ) SESSION.commit() flask.flash(message) url = flask.url_for( 'view_issue', username=username, repo=repo.name, issueid=issueid) return flask.redirect(url) except progit.exceptions.ProgitException, err: flask.flash(str(err), 'error') except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error')
def pull_request_add_comment(repo, requestid, commit, row, username=None): """ Add a comment to a commit in a pull-request. """ repo = progit.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404, 'Project not found') request = progit.lib.get_pull_request(SESSION, project_id=repo.id, requestid=requestid) repo = request.repo_from if not request: flask.abort(404, 'Pull-request not found') form = progit.forms.AddPullRequestCommentForm() form.commit.data = commit form.requestid.data = requestid form.row.data = row if form.validate_on_submit(): comment = form.comment.data try: message = progit.lib.add_pull_request_comment( SESSION, request=request, commit=commit, row=row, comment=comment, user=flask.g.fas_user.username, ) SESSION.commit() flask.flash(message) except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error') return flask.redirect( flask.url_for('request_pull', username=username, repo=repo.name, requestid=requestid))
def view_issue(repo, issueid, username=None): """ List all issues associated to a repo """ repo = progit.lib.get_project(SESSION, repo, user=username) if repo is None: flask.abort(404, 'Project not found') if not repo.issue_tracker: flask.abort(404, 'No issue tracker found for this project') issue = progit.lib.get_issue(SESSION, issueid) if issue is None or issue.project != repo: flask.abort(404, 'Issue not found') status = progit.lib.get_issue_statuses(SESSION) form_comment = progit.forms.AddIssueCommentForm() form = None if authenticated() and is_repo_admin(repo): form = progit.forms.UpdateIssueStatusForm(status=status) if form.validate_on_submit(): try: message = progit.lib.edit_issue( SESSION, issue=issue, status=form.status.data, ticketfolder=APP.config['TICKETS_FOLDER'], ) SESSION.commit() flask.flash(message) url = flask.url_for('view_issues', username=username, repo=repo.name) return flask.redirect(url) except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error') elif flask.request.method == 'GET': form.status.data = issue.status
def view_issue(repo, issueid, username=None): """ List all issues associated to a repo """ repo = progit.lib.get_project(SESSION, repo, user=username) if repo is None: flask.abort(404, 'Project not found') if not repo.issue_tracker: flask.abort(404, 'No issue tracker found for this project') issue = progit.lib.get_issue(SESSION, issueid) if issue is None or issue.project != repo: flask.abort(404, 'Issue not found') status = progit.lib.get_issue_statuses(SESSION) form_comment = progit.forms.AddIssueCommentForm() form = None if authenticated() and is_repo_admin(repo): form = progit.forms.UpdateIssueStatusForm(status=status) if form.validate_on_submit(): try: message = progit.lib.edit_issue( SESSION, issue=issue, status=form.status.data, ticketfolder=APP.config['TICKETS_FOLDER'], ) SESSION.commit() flask.flash(message) url = flask.url_for( 'view_issues', username=username, repo=repo.name) return flask.redirect(url) except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error') elif flask.request.method == 'GET': form.status.data = issue.status
def pull_request_add_comment(repo, requestid, commit, row, username=None): """ Add a comment to a commit in a pull-request. """ repo = progit.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404, 'Project not found') request = progit.lib.get_pull_request( SESSION, project_id=repo.id, requestid=requestid) repo = request.repo_from if not request: flask.abort(404, 'Pull-request not found') form = progit.forms.AddPullRequestCommentForm() form.commit.data = commit form.requestid.data = requestid form.row.data = row if form.validate_on_submit(): comment = form.comment.data try: message = progit.lib.add_pull_request_comment( SESSION, request=request, commit=commit, row=row, comment=comment, user=flask.g.fas_user.username, ) SESSION.commit() flask.flash(message) except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error') return flask.redirect(flask.url_for( 'request_pull', username=username, repo=repo.name, requestid=requestid))
def view_settings(repo, username=None): """ Presents the settings of the project. """ repo = progit.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404, 'Project not found') if not is_repo_admin(repo): flask.abort( 403, 'You are not allowed to change the settings for this project') plugins = progit.plugins.get_plugin_names() form = progit.forms.ProjectSettingsForm() if form.validate_on_submit(): issue_tracker = form.issue_tracker.data project_docs = form.project_docs.data try: message = progit.lib.update_project_settings( SESSION, repo=repo, issue_tracker=issue_tracker, project_docs=project_docs, ) SESSION.commit() flask.flash(message) return flask.redirect(flask.url_for( 'view_repo', username=username, repo=repo.name)) except progit.exceptions.ProgitException, err: flask.flash(str(err), 'error') except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error')
def new_request_pull(repo, username=None, commitid=None): """ Request pulling the changes from the fork into the project. """ repo = progit.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404) if not is_repo_admin(repo): flask.abort( 403, 'You are not allowed to create pull-requests for this project') if repo.is_fork: repopath = os.path.join(APP.config['FORK_FOLDER'], repo.path) else: repopath = os.path.join(APP.config['GIT_FOLDER'], repo.path) repo_obj = pygit2.Repository(repopath) if repo.parent: parentname = os.path.join(APP.config['GIT_FOLDER'], repo.parent.path) else: parentname = os.path.join(APP.config['GIT_FOLDER'], repo.path) orig_repo = pygit2.Repository(parentname) frombranchname = flask.request.args.get('from_branch', 'master') frombranch = repo_obj.lookup_branch(frombranchname) if not frombranch: flask.flash('Branch %s does not exist' % frombranchname, 'error') frombranchname = 'master' branchname = flask.request.args.get('branch', 'master') branch = orig_repo.lookup_branch(branchname) if not branch: flask.flash('Branch %s does not exist' % branchname, 'error') branchname = 'master' if commitid is None: commitid = repo_obj.head.target if branchname: branch = repo_obj.lookup_branch(frombranchname) commitid = branch.get_object().hex diff_commits = [] diffs = [] if not repo_obj.is_empty and not orig_repo.is_empty: orig_commit = orig_repo[ orig_repo.lookup_branch(branchname).get_object().hex] master_commits = [ commit.oid.hex for commit in orig_repo.walk( orig_repo.lookup_branch(branchname).get_object().hex, pygit2.GIT_SORT_TIME) ] repo_commit = repo_obj[commitid] for commit in repo_obj.walk( repo_commit.oid.hex, pygit2.GIT_SORT_TIME): if commit.oid.hex in master_commits: break diff_commits.append(commit) diffs.append( repo_obj.diff( repo_obj.revparse_single(commit.parents[0].oid.hex), repo_obj.revparse_single(commit.oid.hex) ) ) elif orig_repo.is_empty: orig_commit = None repo_commit = repo_obj[repo_obj.head.target] diff = repo_commit.tree.diff_to_tree(swap=True) else: flask.flash( 'Fork is empty, there are no commits to request pulling', 'error') return flask.redirect(flask.url_for( 'view_repo', username=username, repo=repo.name)) html_diffs = [] for diff in diffs: html_diffs.append( highlight( diff.patch, DiffLexer(), HtmlFormatter( noclasses=True, style="tango",) ) ) form = progit.forms.RequestPullForm() if form.validate_on_submit(): try: if orig_commit: orig_commit = orig_commit.oid.hex parent = repo if repo.parent: parent = repo.parent message = progit.lib.new_pull_request( SESSION, repo=parent, repo_from=repo, branch=branchname, title=form.title.data, start_id=orig_commit, stop_id=repo_commit.oid.hex, user=flask.g.fas_user.username, ) SESSION.commit() flask.flash(message) if not parent.is_fork: url = flask.url_for( 'request_pulls', username=None, repo=parent.name) else: url = flask.url_for( 'request_pulls', username=parent.user, repo=parent.name) return flask.redirect(url) except progit.exceptions.ProgitException, err: flask.flash(str(err), 'error') except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error')
ticketfolder=APP.config['TICKETS_FOLDER'], user=flask.g.fas_user.username) SESSION.commit() generate_gitolite_acls() flask.flash(message) return flask.redirect( flask.url_for( 'view_repo', username=flask.g.fas_user.username, repo=repo.name) ) except progit.exceptions.ProgitException, err: flask.flash(str(err), 'error') except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error') return flask.redirect(flask.url_for('view_repo', repo=repo.name)) @APP.route('/<repo>/request-pull/new', methods=('GET', 'POST')) @APP.route('/<repo>/request-pull/new/<commitid>', methods=('GET', 'POST')) @APP.route('/fork/<username>/<repo>/request-pull/new', methods=('GET', 'POST')) @APP.route('/fork/<username>/<repo>/request-pull/new/<commitid>', methods=('GET', 'POST')) @cla_required def new_request_pull(repo, username=None, commitid=None):
forkfolder=APP.config['FORK_FOLDER'], docfolder=APP.config['DOCS_FOLDER'], ticketfolder=APP.config['TICKETS_FOLDER'], user=flask.g.fas_user.username) SESSION.commit() generate_gitolite_acls() flask.flash(message) return flask.redirect( flask.url_for('view_repo', username=flask.g.fas_user.username, repo=repo.name)) except progit.exceptions.ProgitException, err: flask.flash(str(err), 'error') except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error') return flask.redirect(flask.url_for('view_repo', repo=repo.name)) @APP.route('/<repo>/request-pull/new', methods=('GET', 'POST')) @APP.route('/<repo>/request-pull/new/<commitid>', methods=('GET', 'POST')) @APP.route('/fork/<username>/<repo>/request-pull/new', methods=('GET', 'POST')) @APP.route('/fork/<username>/<repo>/request-pull/new/<commitid>', methods=('GET', 'POST')) @cla_required def new_request_pull(repo, username=None, commitid=None): """ Request pulling the changes from the fork into the project. """ repo = progit.lib.get_project(SESSION, repo, user=username)
def view_plugin_page(repo, plugin, full, username=None): """ Presents the settings of the project. """ repo = progit.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404, 'Project not found') if not is_repo_admin(repo): flask.abort( 403, 'You are not allowed to change the settings for this project') plugin = get_plugin(plugin) fields = [] new = True dbobj = plugin.db_object() if hasattr(repo, plugin.backref): dbobj = getattr(repo, plugin.backref) # There should always be only one, but let's double check if dbobj and len(dbobj) > 0: dbobj = dbobj[0] new = False else: dbobj = plugin.db_object() form = plugin.form(obj=dbobj) for field in plugin.form_fields: fields.append(getattr(form, field)) if form.validate_on_submit(): form.populate_obj(obj=dbobj) if new: dbobj.project_id = repo.id SESSION.add(dbobj) try: SESSION.flush() except SQLAlchemyError, err: SESSION.rollback() APP.logger.debug('Could not add plugin %s', plugin.name) APP.logger.exception(err) flask.flash( 'Could not add plugin %s, please contact an admin' % plugin.name) return flask.render_template( 'plugin.html', select='settings', full=full, repo=repo, username=username, plugin=plugin, form=form, fields=fields, ) if form.active.data: # Set up the main script if necessary plugin.set_up(repo) # Install the plugin itself plugin.install(repo, dbobj) flask.flash('Hook activated') else: plugin.remove(repo) flask.flash('Hook inactived') SESSION.commit()
def new_request_pull(repo, username=None, commitid=None): """ Request pulling the changes from the fork into the project. """ repo = progit.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404) if not is_repo_admin(repo): flask.abort( 403, 'You are not allowed to create pull-requests for this project') if repo.is_fork: repopath = os.path.join(APP.config['FORK_FOLDER'], repo.path) else: repopath = os.path.join(APP.config['GIT_FOLDER'], repo.path) repo_obj = pygit2.Repository(repopath) if repo.parent: parentname = os.path.join(APP.config['GIT_FOLDER'], repo.parent.path) else: parentname = os.path.join(APP.config['GIT_FOLDER'], repo.path) orig_repo = pygit2.Repository(parentname) frombranchname = flask.request.args.get('from_branch', 'master') frombranch = repo_obj.lookup_branch(frombranchname) if not frombranch: flask.flash('Branch %s does not exist' % frombranchname, 'error') frombranchname = 'master' branchname = flask.request.args.get('branch', 'master') branch = orig_repo.lookup_branch(branchname) if not branch: flask.flash('Branch %s does not exist' % branchname, 'error') branchname = 'master' if commitid is None: commitid = repo_obj.head.target if branchname: branch = repo_obj.lookup_branch(frombranchname) commitid = branch.get_object().hex diff_commits = [] diffs = [] if not repo_obj.is_empty and not orig_repo.is_empty: orig_commit = orig_repo[orig_repo.lookup_branch( branchname).get_object().hex] master_commits = [ commit.oid.hex for commit in orig_repo.walk( orig_repo.lookup_branch(branchname).get_object().hex, pygit2.GIT_SORT_TIME) ] repo_commit = repo_obj[commitid] for commit in repo_obj.walk(repo_commit.oid.hex, pygit2.GIT_SORT_TIME): if commit.oid.hex in master_commits: break diff_commits.append(commit) diffs.append( repo_obj.diff( repo_obj.revparse_single(commit.parents[0].oid.hex), repo_obj.revparse_single(commit.oid.hex))) elif orig_repo.is_empty: orig_commit = None repo_commit = repo_obj[repo_obj.head.target] diff = repo_commit.tree.diff_to_tree(swap=True) else: flask.flash('Fork is empty, there are no commits to request pulling', 'error') return flask.redirect( flask.url_for('view_repo', username=username, repo=repo.name)) html_diffs = [] for diff in diffs: html_diffs.append( highlight(diff.patch, DiffLexer(), HtmlFormatter( noclasses=True, style="tango", ))) form = progit.forms.RequestPullForm() if form.validate_on_submit(): try: if orig_commit: orig_commit = orig_commit.oid.hex parent = repo if repo.parent: parent = repo.parent message = progit.lib.new_pull_request( SESSION, repo=parent, repo_from=repo, branch=branchname, title=form.title.data, start_id=orig_commit, stop_id=repo_commit.oid.hex, user=flask.g.fas_user.username, ) SESSION.commit() flask.flash(message) if not parent.is_fork: url = flask.url_for('request_pulls', username=None, repo=parent.name) else: url = flask.url_for('request_pulls', username=parent.user, repo=parent.name) return flask.redirect(url) except progit.exceptions.ProgitException, err: flask.flash(str(err), 'error') except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error')