Blame dev-data.py

skrzepto 779aa7
#!/usr/bin/env python
Alex Gleason 6656f7
# -*- coding: utf-8 -*-
Alex Gleason 6656f7
skrzepto 779aa7
""" Populate the pagure db with some dev data. """
skrzepto 779aa7
Pierre-Yves Chibon 67d1cc
from __future__ import print_function, unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon b130e5
import argparse
skrzepto 779aa7
import os
Alex Gleason 633b65
import tempfile
Alex Gleason 633b65
import pygit2
Alex Gleason 633b65
import shutil
jingjing b0e64c
import six
Pierre-Yves Chibon b130e5
Pierre-Yves Chibon b130e5
from sqlalchemy import create_engine, MetaData
Pierre-Yves Chibon b130e5
skrzepto 779aa7
import pagure
skrzepto 779aa7
import tests
Pierre-Yves Chibon 930073
import pagure.lib.model
Pierre-Yves Chibon 930073
import pagure.lib.query
Alex Gleason 6656f7
from pagure.lib.login import generate_hashed_value
Alex Gleason 6656f7
from pagure.lib.model import create_default_status
Alex Gleason 633b65
from pagure.lib.repo import PagureRepo
Alex Gleason 633b65
skrzepto 779aa7
Pierre-Yves Chibon 73d120
"""
skrzepto 779aa7
Usage:
skrzepto 779aa7
python dev-data.py --init
skrzepto 779aa7
python dev-data.py --clean
skrzepto 779aa7
python dev-data.py --populate
skrzepto 779aa7
python dev-data.py --all
Pierre-Yves Chibon 73d120
"""
skrzepto 779aa7
Aurélien Bompard 831553
_config = pagure.config.reload_config()
Aurélien Bompard 831553
skrzepto 779aa7
Pierre-Yves Chibon 067853
def empty_dev_db(session):
Pierre-Yves Chibon 73d120
    print("")
Pierre-Yves Chibon 73d120
    print("WARNING: Deleting all data from", _config["DB_URL"])
Pierre-Yves Chibon 067853
    response = os.environ.get("FORCE_DELETE")
Pierre-Yves Chibon 067853
    if not response:
Pierre-Yves Chibon 73d120
        response = six.moves.input("Do you want to continue? (yes/no)    ")
Pierre-Yves Chibon 73d120
    if response.lower().startswith("y"):
Pierre-Yves Chibon 067853
        tables = reversed(pagure.lib.model_base.BASE.metadata.sorted_tables)
Pierre-Yves Chibon 067853
        for tbl in tables:
Pierre-Yves Chibon 067853
            session.execute(tbl.delete())
Alex Gleason 6656f7
    else:
Alex Gleason 6656f7
        exit("Aborting.")
skrzepto 779aa7
skrzepto 779aa7
skrzepto 779aa7
def insert_data(session, username, user_email):
Pierre-Yves Chibon 73d120
    _config["EMAIL_SEND"] = False
Pierre-Yves Chibon 73d120
    _config["TESTING"] = True
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # tags
Pierre-Yves Chibon 73d120
    item = pagure.lib.model.Tag(tag="tag1")
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # Users
skrzepto 779aa7
    # Create a couple of users
Alex Gleason 633b65
    pingou = item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
        user="pingou",
Pierre-Yves Chibon 73d120
        fullname="PY C",
Pierre-Yves Chibon 73d120
        password=generate_hashed_value("testing123"),
Alex Gleason 6656f7
        token=None,
Pierre-Yves Chibon 73d120
        default_email="bar@pingou.com",
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
Pierre-Yves Chibon 73d120
    print(
Pierre-Yves Chibon 73d120
        "User created: {} <{}>, {}".format(
Pierre-Yves Chibon 73d120
            item.user, item.default_email, "testing123"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
    )
skrzepto 779aa7
Alex Gleason 633b65
    foo = item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
        user="foo",
Pierre-Yves Chibon 73d120
        fullname="foo bar",
Pierre-Yves Chibon 73d120
        password=generate_hashed_value("testing123"),
Alex Gleason 6656f7
        token=None,
Pierre-Yves Chibon 73d120
        default_email="foo@bar.com",
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
Pierre-Yves Chibon 73d120
    print(
Pierre-Yves Chibon 73d120
        "User created: {} <{}>, {}".format(
Pierre-Yves Chibon 73d120
            item.user, item.default_email, "testing123"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
    )
skrzepto 779aa7
Alex Gleason 633b65
    you = item = pagure.lib.model.User(
skrzepto 779aa7
        user=username,
skrzepto 779aa7
        fullname=username,
Pierre-Yves Chibon 73d120
        password=generate_hashed_value("testing123"),
Alex Gleason 6656f7
        token=None,
skrzepto 779aa7
        default_email=user_email,
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
Pierre-Yves Chibon 73d120
    print(
Pierre-Yves Chibon 73d120
        "User created: {} <{}>, {}".format(
Pierre-Yves Chibon 73d120
            item.user, item.default_email, "testing123"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
    )
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # pagure_group
skrzepto 779aa7
    item = pagure.lib.model.PagureGroup(
Pierre-Yves Chibon 73d120
        group_name="admin",
Pierre-Yves Chibon 73d120
        group_type="admin",
Alex Gleason 633b65
        user_id=pingou.id,
Pierre-Yves Chibon 73d120
        display_name="admin",
Pierre-Yves Chibon 73d120
        description="Admin Group",
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
Alex Gleason 6656f7
    print('Created "admin" group. Pingou is a member.')
skrzepto 779aa7
skrzepto 779aa7
    # Add a couple of groups so that we can list them
skrzepto 779aa7
    item = pagure.lib.model.PagureGroup(
Pierre-Yves Chibon 73d120
        group_name="group",
Pierre-Yves Chibon 73d120
        group_type="user",
Alex Gleason 633b65
        user_id=pingou.id,
Pierre-Yves Chibon 73d120
        display_name="group group",
Pierre-Yves Chibon 73d120
        description="this is a group group",
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
Alex Gleason 6656f7
    print('Created "group" group. Pingou is a member.')
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.PagureGroup(
Pierre-Yves Chibon 73d120
        group_name="rel-eng",
Pierre-Yves Chibon 73d120
        group_type="user",
Alex Gleason 633b65
        user_id=pingou.id,
Pierre-Yves Chibon 73d120
        display_name="Release Engineering",
Pierre-Yves Chibon 73d120
        description="The group of release engineers",
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
Alex Gleason 6656f7
    print('Created "rel-eng" group. Pingou is a member.')
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # projects
skrzepto 779aa7
skrzepto 779aa7
    import shutil
Pierre-Yves Chibon 73d120
skrzepto 779aa7
    # delete folder from local instance to start from a clean slate
Pierre-Yves Chibon 73d120
    if os.path.exists(_config["GIT_FOLDER"]):
Pierre-Yves Chibon 73d120
        shutil.rmtree(_config["GIT_FOLDER"])
skrzepto 779aa7
Alex Gleason 633b65
    # Create projects
Alex Gleason 633b65
    item = project1 = pagure.lib.model.Project(
Alex Gleason 633b65
        user_id=pingou.id,
Pierre-Yves Chibon 73d120
        name="test",
Alex Gleason 633b65
        is_fork=False,
Alex Gleason 633b65
        parent_id=None,
Pierre-Yves Chibon 73d120
        description="test project #1",
Pierre-Yves Chibon 73d120
        hook_token="aaabbbccc",
Alex Gleason 633b65
    )
Pierre-Yves Chibon 73d120
    item.close_status = ["Invalid", "Insufficient data", "Fixed", "Duplicate"]
Alex Gleason 633b65
    session.add(item)
Alex Gleason 633b65
    session.flush()
Alex Gleason 633b65
    tests.create_locks(session, item)
Alex Gleason 633b65
Alex Gleason 633b65
    item = project2 = pagure.lib.model.Project(
Alex Gleason 633b65
        user_id=pingou.id,
Pierre-Yves Chibon 73d120
        name="test2",
Alex Gleason 633b65
        is_fork=False,
Alex Gleason 633b65
        parent_id=None,
Pierre-Yves Chibon 73d120
        description="test project #2",
Pierre-Yves Chibon 73d120
        hook_token="aaabbbddd",
Alex Gleason 633b65
    )
Pierre-Yves Chibon 73d120
    item.close_status = ["Invalid", "Insufficient data", "Fixed", "Duplicate"]
Alex Gleason 633b65
    session.add(item)
Alex Gleason 633b65
Alex Gleason 633b65
    item = project3 = pagure.lib.model.Project(
Alex Gleason 633b65
        user_id=pingou.id,
Pierre-Yves Chibon 73d120
        name="test3",
Alex Gleason 633b65
        is_fork=False,
Alex Gleason 633b65
        parent_id=None,
Pierre-Yves Chibon 73d120
        description="namespaced test project",
Pierre-Yves Chibon 73d120
        hook_token="aaabbbeee",
Pierre-Yves Chibon 73d120
        namespace="somenamespace",
Alex Gleason 633b65
    )
Pierre-Yves Chibon 73d120
    item.close_status = ["Invalid", "Insufficient data", "Fixed", "Duplicate"]
Alex Gleason 633b65
    session.add(item)
Alex Gleason 633b65
Alex Gleason 633b65
    session.commit()
Alex Gleason 633b65
Pierre-Yves Chibon 73d120
    tests.create_projects_git(_config["GIT_FOLDER"], bare=True)
Pierre-Yves Chibon 73d120
    add_content_git_repo(os.path.join(_config["GIT_FOLDER"], "test.git"))
Pierre-Yves Chibon 73d120
    tests.add_readme_git_repo(os.path.join(_config["GIT_FOLDER"], "test.git"))
skrzepto 779aa7
skrzepto 779aa7
    # Add some content to the git repo
Alex Gleason 633b65
    add_content_git_repo(
Pierre-Yves Chibon 73d120
        os.path.join(_config["GIT_FOLDER"], "forks", "pingou", "test.git")
Pierre-Yves Chibon 73d120
    )
skrzepto 779aa7
    tests.add_readme_git_repo(
Pierre-Yves Chibon 73d120
        os.path.join(_config["GIT_FOLDER"], "forks", "pingou", "test.git")
Pierre-Yves Chibon 73d120
    )
skrzepto 779aa7
    tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
        os.path.join(_config["GIT_FOLDER"], "forks", "pingou", "test.git"),
Pierre-Yves Chibon 73d120
        ncommits=10,
Pierre-Yves Chibon 73d120
    )
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # user_emails
skrzepto 779aa7
    item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon 73d120
        user_id=pingou.id, email="bar@pingou.com"
Pierre-Yves Chibon 73d120
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon 73d120
        user_id=pingou.id, email="foo@pingou.com"
Pierre-Yves Chibon 73d120
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
Pierre-Yves Chibon 73d120
    item = pagure.lib.model.UserEmail(user_id=foo.id, email="foo@bar.com")
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
Pierre-Yves Chibon 73d120
    item = pagure.lib.model.UserEmail(user_id=you.id, email=user_email)
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # user_emails_pending
skrzepto 779aa7
    email_pend = pagure.lib.model.UserEmailPending(
Pierre-Yves Chibon 73d120
        user_id=pingou.id, email="foo@fp.o", token="abcdef"
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(email_pend)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # issues
skrzepto 779aa7
    # Add an issue and tag it so that we can list them
skrzepto 779aa7
    item = pagure.lib.model.Issue(
Alex Gleason 633b65
        id=1001,
Pierre-Yves Chibon 73d120
        uid="foobar",
Alex Gleason 633b65
        project_id=project1.id,
Pierre-Yves Chibon 73d120
        title="Problem with jenkins build",
Pierre-Yves Chibon 73d120
        content="For some reason the tests fail at line:24",
Alex Gleason 633b65
        user_id=pingou.id,
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.Issue(
Alex Gleason 633b65
        id=1002,
Pierre-Yves Chibon 73d120
        uid="foobar2",
Alex Gleason 633b65
        project_id=project1.id,
Pierre-Yves Chibon 73d120
        title="Unit tests failing",
Pierre-Yves Chibon 73d120
        content="Need to fix code for the unit tests to "
Pierre-Yves Chibon 73d120
        "pass so jenkins build can complete.",
Alex Gleason 633b65
        user_id=pingou.id,
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.Issue(
Alex Gleason 633b65
        id=1003,
Pierre-Yves Chibon 73d120
        uid="foobar3",
Alex Gleason 633b65
        project_id=project1.id,
Pierre-Yves Chibon 73d120
        title="Segfault during execution",
Pierre-Yves Chibon 73d120
        content="Index out of bounds for variable i?",
Alex Gleason 633b65
        user_id=you.id,
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # pagure_user_group
Pierre-Yves Chibon 73d120
    group = pagure.lib.query.search_groups(
Pierre-Yves Chibon 73d120
        session, pattern=None, group_name="rel-eng", group_type=None
Pierre-Yves Chibon 73d120
    )
skrzepto 779aa7
    item = pagure.lib.model.PagureUserGroup(
Pierre-Yves Chibon 73d120
        user_id=pingou.id, group_id=group.id
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
Pierre-Yves Chibon 73d120
    group = pagure.lib.query.search_groups(
Pierre-Yves Chibon 73d120
        session, pattern=None, group_name="admin", group_type=None
skrzepto 779aa7
    )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
    item = pagure.lib.model.PagureUserGroup(user_id=you.id, group_id=group.id)
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
Pierre-Yves Chibon 73d120
    group = pagure.lib.query.search_groups(
Pierre-Yves Chibon 73d120
        session, pattern=None, group_name="group", group_type=None
skrzepto 779aa7
    )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
    item = pagure.lib.model.PagureUserGroup(user_id=foo.id, group_id=group.id)
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # projects_groups
Pierre-Yves Chibon 73d120
    group = pagure.lib.query.search_groups(
Pierre-Yves Chibon 73d120
        session, pattern=None, group_name="rel-eng", group_type=None
Pierre-Yves Chibon 73d120
    )
Pierre-Yves Chibon 73d120
    repo = pagure.lib.query.get_authorized_project(session, "test")
skrzepto 779aa7
    item = pagure.lib.model.ProjectGroup(
Pierre-Yves Chibon 73d120
        project_id=repo.id, group_id=group.id, access="commit"
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
Pierre-Yves Chibon 73d120
    group = pagure.lib.query.search_groups(
Pierre-Yves Chibon 73d120
        session, pattern=None, group_name="admin", group_type=None
Pierre-Yves Chibon 73d120
    )
Pierre-Yves Chibon 73d120
    repo = pagure.lib.query.get_authorized_project(session, "test2")
skrzepto 779aa7
    item = pagure.lib.model.ProjectGroup(
Pierre-Yves Chibon 73d120
        project_id=repo.id, group_id=group.id, access="admin"
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # pull_requests
Pierre-Yves Chibon 73d120
    repo = pagure.lib.query.get_authorized_project(session, "test")
Pierre-Yves Chibon 73d120
    forked_repo = pagure.lib.query.get_authorized_project(session, "test")
Pierre-Yves Chibon 930073
    req = pagure.lib.query.new_pull_request(
skrzepto 779aa7
        session=session,
skrzepto 779aa7
        repo_from=forked_repo,
Pierre-Yves Chibon 73d120
        branch_from="master",
skrzepto 779aa7
        repo_to=repo,
Pierre-Yves Chibon 73d120
        branch_to="master",
Pierre-Yves Chibon 73d120
        title="Fixing code for unittest",
Lenka Segura 0e09f5
        user=username,
Pierre-Yves Chibon 73d120
        status="Open",
skrzepto 779aa7
    )
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
Pierre-Yves Chibon 73d120
    repo = pagure.lib.query.get_authorized_project(session, "test")
Pierre-Yves Chibon 73d120
    forked_repo = pagure.lib.query.get_authorized_project(session, "test")
Lenka Segura 0e09f5
    req = pagure.lib.query.new_pull_request(
Lenka Segura 0e09f5
        session=session,
Lenka Segura 0e09f5
        repo_from=forked_repo,
Pierre-Yves Chibon 73d120
        branch_from="master",
Lenka Segura 0e09f5
        repo_to=repo,
Pierre-Yves Chibon 73d120
        branch_to="master",
Pierre-Yves Chibon 73d120
        title="add very nice README",
Lenka Segura 0e09f5
        user=username,
Pierre-Yves Chibon 73d120
        status="Open",
Lenka Segura 0e09f5
    )
Lenka Segura 0e09f5
    session.commit()
Lenka Segura 0e09f5
Pierre-Yves Chibon 73d120
    repo = pagure.lib.query.get_authorized_project(session, "test")
Pierre-Yves Chibon 73d120
    forked_repo = pagure.lib.query.get_authorized_project(session, "test")
Lenka Segura 0e09f5
    req = pagure.lib.query.new_pull_request(
Lenka Segura 0e09f5
        session=session,
Lenka Segura 0e09f5
        repo_from=forked_repo,
Pierre-Yves Chibon 73d120
        branch_from="master",
Lenka Segura 0e09f5
        repo_to=repo,
Pierre-Yves Chibon 73d120
        branch_to="master",
Pierre-Yves Chibon 73d120
        title="Add README",
Lenka Segura 0e09f5
        user=username,
Pierre-Yves Chibon 73d120
        status="Closed",
Lenka Segura 0e09f5
    )
Lenka Segura 0e09f5
    session.commit()
Lenka Segura 0e09f5
Pierre-Yves Chibon 73d120
    repo = pagure.lib.query.get_authorized_project(session, "test")
Pierre-Yves Chibon 73d120
    forked_repo = pagure.lib.query.get_authorized_project(session, "test")
Lenka Segura 0e09f5
    req = pagure.lib.query.new_pull_request(
Lenka Segura 0e09f5
        session=session,
Lenka Segura 0e09f5
        repo_from=forked_repo,
Pierre-Yves Chibon 73d120
        branch_from="master",
Lenka Segura 0e09f5
        repo_to=repo,
Pierre-Yves Chibon 73d120
        branch_to="master",
Pierre-Yves Chibon 73d120
        title="Fix some containers",
Lenka Segura 0e09f5
        user=username,
Pierre-Yves Chibon 73d120
        status="Merged",
Lenka Segura 0e09f5
    )
Lenka Segura 0e09f5
    session.commit()
Lenka Segura 0e09f5
Pierre-Yves Chibon 73d120
    repo = pagure.lib.query.get_authorized_project(session, "test")
Pierre-Yves Chibon 73d120
    forked_repo = pagure.lib.query.get_authorized_project(session, "test")
Lenka Segura 0e09f5
    req = pagure.lib.query.new_pull_request(
Lenka Segura 0e09f5
        session=session,
Lenka Segura 0e09f5
        repo_from=forked_repo,
Pierre-Yves Chibon 73d120
        branch_from="master",
Lenka Segura 0e09f5
        repo_to=repo,
Pierre-Yves Chibon 73d120
        branch_to="master",
Pierre-Yves Chibon 73d120
        title="Fix pull request statuses",
Lenka Segura 0e09f5
        user=username,
Pierre-Yves Chibon 73d120
        status="Closed",
Lenka Segura 0e09f5
    )
Lenka Segura 0e09f5
    session.commit()
Lenka Segura 0e09f5
Pierre-Yves Chibon 73d120
    repo = pagure.lib.query.get_authorized_project(session, "test")
Pierre-Yves Chibon 73d120
    forked_repo = pagure.lib.query.get_authorized_project(session, "test")
Lenka Segura 0e09f5
    req = pagure.lib.query.new_pull_request(
Lenka Segura 0e09f5
        session=session,
Lenka Segura 0e09f5
        repo_from=forked_repo,
Pierre-Yves Chibon 73d120
        branch_from="master",
Lenka Segura 0e09f5
        repo_to=repo,
Pierre-Yves Chibon 73d120
        branch_to="master",
Pierre-Yves Chibon 73d120
        title="Fixing UI of issue",
Lenka Segura 0e09f5
        user=username,
Pierre-Yves Chibon 73d120
        status="Merged",
Lenka Segura 0e09f5
    )
Lenka Segura 0e09f5
    session.commit()
Lenka Segura 0e09f5
Pierre-Yves Chibon 067853
    #####################################
skrzepto 779aa7
    # tokens
Alex Gleason 633b65
    tests.create_tokens(session, user_id=pingou.id, project_id=project1.id)
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # user_projects
Pierre-Yves Chibon 73d120
    repo = pagure.lib.query.get_authorized_project(session, "test")
skrzepto 779aa7
    item = pagure.lib.model.ProjectUser(
Pierre-Yves Chibon 73d120
        project_id=repo.id, user_id=foo.id, access="commit"
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
Pierre-Yves Chibon 73d120
    repo = pagure.lib.query.get_authorized_project(session, "test2")
skrzepto 779aa7
    item = pagure.lib.model.ProjectUser(
Pierre-Yves Chibon 73d120
        project_id=repo.id, user_id=you.id, access="commit"
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # issue_comments
skrzepto 779aa7
    item = pagure.lib.model.IssueComment(
Alex Gleason 633b65
        user_id=pingou.id,
Pierre-Yves Chibon 73d120
        issue_uid="foobar",
Pierre-Yves Chibon 73d120
        comment="We may need to adjust the unittests instead of the code.",
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # issue_to_issue
Pierre-Yves Chibon 73d120
    repo = pagure.lib.query.get_authorized_project(session, "test")
Pierre-Yves Chibon 930073
    all_issues = pagure.lib.query.search_issues(session, repo)
Pierre-Yves Chibon 73d120
    pagure.lib.query.add_issue_dependency(
Pierre-Yves Chibon 73d120
        session, all_issues[0], all_issues[1], "pingou"
Pierre-Yves Chibon 73d120
    )
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # pull_request_comments
Pierre-Yves Chibon 73d120
    user = pagure.lib.query.search_user(session, username="pingou")
skrzepto 779aa7
    # only 1 pull request available atm
Pierre-Yves Chibon 930073
    pr = pagure.lib.query.get_pull_request_of_user(session, "pingou")[0]
skrzepto 779aa7
    item = pagure.lib.model.PullRequestComment(
skrzepto 779aa7
        pull_request_uid=pr.uid,
skrzepto 779aa7
        user_id=user.id,
skrzepto 779aa7
        comment="+1 for me. Btw, could you rebase before you merge?",
Pierre-Yves Chibon 73d120
        notification=0,
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # pull_request_flags
skrzepto 779aa7
    # only 1 pull request available atm
Pierre-Yves Chibon 930073
    pr = pagure.lib.query.get_pull_request_of_user(session, "pingou")[0]
skrzepto 779aa7
    item = pagure.lib.model.PullRequestFlag(
skrzepto 779aa7
        uid="random_pr_flag_uid",
skrzepto 779aa7
        pull_request_uid=pr.uid,
Alex Gleason 633b65
        user_id=pingou.id,
Alex Gleason 633b65
        username=pingou.user,
skrzepto 779aa7
        percent=80,
skrzepto 779aa7
        comment="Jenkins build passes",
Alex Gleason 6656f7
        url=str(pr.id),
Pierre-Yves Chibon 73d120
        status="success",
Lenka Segura 0e09f5
    )
Lenka Segura 0e09f5
    session.add(item)
Lenka Segura 0e09f5
    session.commit()
Lenka Segura 0e09f5
Lenka Segura 0e09f5
    pr = pagure.lib.query.get_pull_request_of_user(session, "foo")[1]
Lenka Segura 0e09f5
    item = pagure.lib.model.PullRequestFlag(
Lenka Segura 0e09f5
        uid="oink oink uid",
Lenka Segura 0e09f5
        pull_request_uid=pr.uid,
Lenka Segura 0e09f5
        user_id=pingou.id,
Lenka Segura 0e09f5
        username=pingou.user,
Lenka Segura 0e09f5
        percent=80,
Lenka Segura 0e09f5
        comment="Jenkins does not pass",
Lenka Segura 0e09f5
        url=str(pr.id),
Pierre-Yves Chibon 73d120
        status="failure",
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
Lenka Segura 0e09f5
    # pull_request_assignee
Pierre-Yves Chibon 73d120
    pr = pagure.lib.query.search_pull_requests(session, requestid="1006")
Lenka Segura 0e09f5
    pr.assignee_id = pingou.id
Lenka Segura 0e09f5
    session.commit()
Lenka Segura 0e09f5
Pierre-Yves Chibon 73d120
    pr = pagure.lib.query.search_pull_requests(session, requestid="1007")
Lenka Segura 0e09f5
    pr.assignee_id = you.id
Lenka Segura 0e09f5
    session.commit()
Lenka Segura 0e09f5
Pierre-Yves Chibon 73d120
    pr = pagure.lib.query.search_pull_requests(session, requestid="1004")
Lenka Segura 0e09f5
    pr.assignee_id = foo.id
Lenka Segura 0e09f5
    session.commit()
Lenka Segura 0e09f5
Lenka Segura 0e09f5
    ######################################
skrzepto 779aa7
    # tags_issues
Pierre-Yves Chibon 73d120
    repo = pagure.lib.query.get_authorized_project(session, "test")
Pierre-Yves Chibon 930073
    issues = pagure.lib.query.search_issues(session, repo)
Pierre-Yves Chibon 73d120
    item = pagure.lib.model.TagIssue(issue_uid=issues[0].uid, tag="tag1")
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # tokens_acls
skrzepto 779aa7
    tests.create_tokens_acl(session)
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # Fork a project
skrzepto 779aa7
    # delete fork data
skrzepto 779aa7
    fork_proj_location = "forks/foo/test.git"
skrzepto 779aa7
    try:
Pierre-Yves Chibon 73d120
        shutil.rmtree(os.path.join(_config["GIT_FOLDER"], fork_proj_location))
skrzepto 779aa7
    except:
Pierre-Yves Chibon 73d120
        print("git folder already deleted")
skrzepto 779aa7
skrzepto 779aa7
    try:
Pierre-Yves Chibon 73d120
        shutil.rmtree(os.path.join(_config["DOCS_FOLDER"], fork_proj_location))
skrzepto 779aa7
    except:
Pierre-Yves Chibon 73d120
        print("docs folder already deleted")
skrzepto 779aa7
skrzepto 779aa7
    try:
Pierre-Yves Chibon 73d120
        shutil.rmtree(
Pierre-Yves Chibon 73d120
            os.path.join(_config["TICKETS_FOLDER"], fork_proj_location)
Pierre-Yves Chibon 73d120
        )
skrzepto 779aa7
    except:
Pierre-Yves Chibon 73d120
        print("tickets folder already deleted")
skrzepto 779aa7
skrzepto 779aa7
    try:
Pierre-Yves Chibon 73d120
        shutil.rmtree(
Pierre-Yves Chibon 73d120
            os.path.join(_config["REQUESTS_FOLDER"], fork_proj_location)
Pierre-Yves Chibon 73d120
        )
skrzepto 779aa7
    except:
Pierre-Yves Chibon 73d120
        print("requests folder already deleted")
skrzepto 779aa7
Pierre-Yves Chibon 73d120
    repo = pagure.lib.query.get_authorized_project(session, "test")
Pierre-Yves Chibon 73d120
    result = pagure.lib.query.fork_project(session, "foo", repo)
skrzepto 779aa7
    if result == 'Repo "test" cloned to "foo/test"':
skrzepto 779aa7
        session.commit()
skrzepto 779aa7
skrzepto 779aa7
Pierre-Yves Chibon 73d120
def add_content_git_repo(folder, branch="master"):
Alex Gleason 633b65
    """ Create some content for the specified git repo. """
Alex Gleason 633b65
    if not os.path.exists(folder):
Alex Gleason 633b65
        os.makedirs(folder)
Alex Gleason 633b65
    brepo = pygit2.init_repository(folder, bare=True)
Alex Gleason 633b65
Pierre-Yves Chibon 73d120
    newfolder = tempfile.mkdtemp(prefix="pagure-tests")
Alex Gleason 633b65
    repo = pygit2.clone_repository(folder, newfolder)
Alex Gleason 633b65
Alex Gleason 633b65
    # Create a file in that git repo
Pierre-Yves Chibon 73d120
    with open(os.path.join(newfolder, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
        stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
    repo.index.add("sources")
Alex Gleason 633b65
    repo.index.write()
Alex Gleason 633b65
Alex Gleason 633b65
    parents = []
Alex Gleason 633b65
    commit = None
Alex Gleason 633b65
    try:
Pierre-Yves Chibon 73d120
        commit = repo.revparse_single("HEAD" if branch == "master" else branch)
Alex Gleason 633b65
    except KeyError:
Alex Gleason 633b65
        pass
Alex Gleason 633b65
    if commit:
Alex Gleason 633b65
        parents = [commit.oid.hex]
Alex Gleason 633b65
Alex Gleason 633b65
    # Commits the files added
Alex Gleason 633b65
    tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
    author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
    committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Alex Gleason 633b65
    repo.create_commit(
Pierre-Yves Chibon 73d120
        "refs/heads/%s" % branch,  # the name of the reference to update
Alex Gleason 633b65
        author,
Alex Gleason 633b65
        committer,
Pierre-Yves Chibon 73d120
        "Add sources file for testing",
Alex Gleason 633b65
        # binary string representing the tree object ID
Alex Gleason 633b65
        tree,
Alex Gleason 633b65
        # list of binary strings representing parents of the new commit
Alex Gleason 633b65
        parents,
Alex Gleason 633b65
    )
Alex Gleason 633b65
Alex Gleason 633b65
    parents = []
Alex Gleason 633b65
    commit = None
Alex Gleason 633b65
    try:
Pierre-Yves Chibon 73d120
        commit = repo.revparse_single("HEAD" if branch == "master" else branch)
Alex Gleason 633b65
    except KeyError:
Alex Gleason 633b65
        pass
Alex Gleason 633b65
    if commit:
Alex Gleason 633b65
        parents = [commit.oid.hex]
Alex Gleason 633b65
Pierre-Yves Chibon 73d120
    subfolder = os.path.join("folder1", "folder2")
Alex Gleason 633b65
    if not os.path.exists(os.path.join(newfolder, subfolder)):
Alex Gleason 633b65
        os.makedirs(os.path.join(newfolder, subfolder))
Alex Gleason 633b65
    # Create a file in that git repo
Pierre-Yves Chibon 73d120
    with open(os.path.join(newfolder, subfolder, "file"), "w") as stream:
Pierre-Yves Chibon 73d120
        stream.write("foo\n bar\nbaz")
Pierre-Yves Chibon 73d120
    repo.index.add(os.path.join(subfolder, "file"))
Alex Gleason 633b65
    repo.index.write()
Alex Gleason 633b65
Alex Gleason 633b65
    # Commits the files added
Alex Gleason 633b65
    tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
    author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
    committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Alex Gleason 633b65
    repo.create_commit(
Pierre-Yves Chibon 73d120
        "refs/heads/%s" % branch,  # the name of the reference to update
Alex Gleason 633b65
        author,
Alex Gleason 633b65
        committer,
Pierre-Yves Chibon 73d120
        "Add some directory and a file for more testing",
Alex Gleason 633b65
        # binary string representing the tree object ID
Alex Gleason 633b65
        tree,
Alex Gleason 633b65
        # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
        parents,
Alex Gleason 633b65
    )
Alex Gleason 633b65
Alex Gleason 633b65
    # Push to origin
Alex Gleason 633b65
    ori_remote = repo.remotes[0]
Alex Gleason 633b65
    master_ref = repo.lookup_reference(
Pierre-Yves Chibon 73d120
        "HEAD" if branch == "master" else "refs/heads/%s" % branch
Pierre-Yves Chibon 73d120
    ).resolve()
Pierre-Yves Chibon 73d120
    refname = "%s:%s" % (master_ref.name, master_ref.name)
Alex Gleason 633b65
Alex Gleason 633b65
    PagureRepo.push(ori_remote, refname)
Alex Gleason 633b65
Alex Gleason 633b65
    shutil.rmtree(newfolder)
Alex Gleason 633b65
Alex Gleason 633b65
Pierre-Yves Chibon 067853
def _get_username():
Pierre-Yves Chibon 73d120
    invalid_option = ["pingou", "foo"]
Pierre-Yves Chibon 067853
    user_name = os.environ.get("USER_NAME")
Pierre-Yves Chibon 067853
    if not user_name:
Pierre-Yves Chibon 067853
        print("")
Pierre-Yves Chibon 067853
        user_name = six.moves.input(
Pierre-Yves Chibon 73d120
            "Enter your username so we can add you into the test data:  "
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 067853
    cnt = 0
Pierre-Yves Chibon 067853
    while not user_name.strip() or user_name in invalid_option:
Pierre-Yves Chibon 067853
        print("Reserved names: " + str(invalid_option))
Pierre-Yves Chibon 067853
        user_name = six.moves.input(
Pierre-Yves Chibon 73d120
            "Enter your username so we can add you into the " "test data:  "
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 067853
        cnt += 1
Pierre-Yves Chibon 067853
        if cnt == 4:
Pierre-Yves Chibon 067853
            print("We asked too many times, bailing")
Pierre-Yves Chibon 067853
            sys.exit(1)
Pierre-Yves Chibon 067853
Pierre-Yves Chibon 067853
    return user_name
Pierre-Yves Chibon 067853
Pierre-Yves Chibon 067853
Pierre-Yves Chibon 067853
def _get_user_email():
Pierre-Yves Chibon 73d120
    invalid_option = ["bar@pingou.com", "foo@bar.com"]
Pierre-Yves Chibon 067853
    user_email = os.environ.get("USER_EMAIL")
Pierre-Yves Chibon 067853
    if not user_email:
Pierre-Yves Chibon 067853
        print("")
Pierre-Yves Chibon 067853
        user_email = six.moves.input("Enter your user email:  ")
Pierre-Yves Chibon 067853
Pierre-Yves Chibon 067853
    cnt = 0
Pierre-Yves Chibon 067853
    while not user_email.strip() or user_email in invalid_option:
Pierre-Yves Chibon 067853
        print("Reserved names: " + str(invalid_option))
Pierre-Yves Chibon 067853
        user_email = six.moves.input("Enter your user email:  ")
Pierre-Yves Chibon 067853
        cnt += 1
Pierre-Yves Chibon 067853
        if cnt == 4:
Pierre-Yves Chibon 067853
            print("We asked too many times, bailing")
Pierre-Yves Chibon 067853
            sys.exit(1)
Pierre-Yves Chibon 067853
Pierre-Yves Chibon 067853
    return user_email
Pierre-Yves Chibon 067853
Pierre-Yves Chibon 067853
skrzepto 779aa7
if __name__ == "__main__":
Pierre-Yves Chibon 73d120
    desc = (
Pierre-Yves Chibon 73d120
        "Run the dev database initialization/insertion/deletion "
Pierre-Yves Chibon 73d120
        "script for db located  " + str(_config["DB_URL"])
Pierre-Yves Chibon 73d120
    )
skrzepto 779aa7
    parser = argparse.ArgumentParser(prog="dev-data", description=desc)
Pierre-Yves Chibon 73d120
    parser.add_argument(
Pierre-Yves Chibon 73d120
        "-i", "--init", action="store_true", help="Create the dev db"
Pierre-Yves Chibon 73d120
    )
Pierre-Yves Chibon 73d120
    parser.add_argument(
Pierre-Yves Chibon 73d120
        "-p", "--populate", action="store_true", help="Add test data to the db"
Pierre-Yves Chibon 73d120
    )
Pierre-Yves Chibon 73d120
    parser.add_argument(
Pierre-Yves Chibon 73d120
        "-d", "--delete", action="store_true", help="Wipe the dev db"
Pierre-Yves Chibon 73d120
    )
Pierre-Yves Chibon 73d120
    parser.add_argument(
Pierre-Yves Chibon 73d120
        "-a",
Pierre-Yves Chibon 73d120
        "--all",
Pierre-Yves Chibon 73d120
        action="store_true",
Pierre-Yves Chibon 73d120
        help="Create, Populate then Wipe the dev db",
Pierre-Yves Chibon 73d120
    )
skrzepto 779aa7
skrzepto 779aa7
    args = parser.parse_args()
skrzepto 779aa7
skrzepto 779aa7
    # forcing the user to choose
skrzepto 779aa7
    if not any(vars(args).values()):
Pierre-Yves Chibon 73d120
        parser.error("No arguments provided.")
skrzepto 779aa7
Pierre-Yves Chibon 067853
    session = None
skrzepto 779aa7
Pierre-Yves Chibon 067853
    if args.init or args.all:
Pierre-Yves Chibon 067853
        session = pagure.lib.model.create_tables(
Pierre-Yves Chibon 067853
            db_url=_config["DB_URL"],
Pierre-Yves Chibon 067853
            alembic_ini=None,
Pierre-Yves Chibon 067853
            acls=_config["ACLS"],
Pierre-Yves Chibon 73d120
            debug=False,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 067853
        print("Database created")
skrzepto 779aa7
skrzepto 779aa7
    if args.populate or args.all:
Pierre-Yves Chibon 067853
        if not session:
Pierre-Yves Chibon 73d120
            session = pagure.lib.query.create_session(_config["DB_URL"])
skrzepto 779aa7
Pierre-Yves Chibon 067853
        user_name = _get_username()
Pierre-Yves Chibon 067853
        user_email = _get_user_email()
skrzepto 779aa7
        insert_data(session, user_name, user_email)
Pierre-Yves Chibon 067853
Pierre-Yves Chibon 067853
    if args.delete or args.all:
Pierre-Yves Chibon 067853
        empty_dev_db(session)