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
skrzepto 779aa7
'''
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
skrzepto 779aa7
'''
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):
skrzepto 779aa7
    print('')
Pierre-Yves Chibon 067853
    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 067853
        response = six.moves.input('Do you want to continue? (yes/no)    ')
Pierre-Yves Chibon 067853
    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 b130e5
    _config['EMAIL_SEND'] = False
Pierre-Yves Chibon b130e5
    _config['TESTING'] = True
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # tags
skrzepto 779aa7
    item = pagure.lib.model.Tag(
skrzepto 779aa7
        tag='tag1',
skrzepto 779aa7
    )
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(
skrzepto 779aa7
        user='pingou',
skrzepto 779aa7
        fullname='PY C',
Alex Gleason 6656f7
        password=generate_hashed_value(u'testing123'),
Alex Gleason 6656f7
        token=None,
skrzepto 779aa7
        default_email='bar@pingou.com',
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
Alex Gleason 6656f7
    print("User created: {} <{}>, {}".format(item.user, item.default_email, 'testing123'))
skrzepto 779aa7
Alex Gleason 633b65
    foo = item = pagure.lib.model.User(
skrzepto 779aa7
        user='foo',
skrzepto 779aa7
        fullname='foo bar',
Alex Gleason 6656f7
        password=generate_hashed_value(u'testing123'),
Alex Gleason 6656f7
        token=None,
skrzepto 779aa7
        default_email='foo@bar.com',
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
Alex Gleason 6656f7
    print("User created: {} <{}>, {}".format(item.user, item.default_email, 'testing123'))
skrzepto 779aa7
Alex Gleason 633b65
    you = item = pagure.lib.model.User(
skrzepto 779aa7
        user=username,
skrzepto 779aa7
        fullname=username,
Alex Gleason 6656f7
        password=generate_hashed_value(u'testing123'),
Alex Gleason 6656f7
        token=None,
skrzepto 779aa7
        default_email=user_email,
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
Alex Gleason 6656f7
    print("User created: {} <{}>, {}".format(item.user, item.default_email, 'testing123'))
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # pagure_group
skrzepto 779aa7
    item = pagure.lib.model.PagureGroup(
skrzepto 779aa7
        group_name='admin',
Alex Gleason 6656f7
        group_type='admin',
Alex Gleason 633b65
        user_id=pingou.id,
Ryan Lerch 6aa425
        display_name='admin',
Ryan Lerch 6aa425
        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(
skrzepto 779aa7
        group_name='group',
skrzepto 779aa7
        group_type='user',
Alex Gleason 633b65
        user_id=pingou.id,
Ryan Lerch 6aa425
        display_name='group group',
Ryan Lerch 6aa425
        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(
skrzepto 779aa7
        group_name='rel-eng',
skrzepto 779aa7
        group_type='user',
Alex Gleason 633b65
        user_id=pingou.id,
Ryan Lerch 6aa425
        display_name='Release Engineering',
Ryan Lerch 6aa425
        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
skrzepto 779aa7
    # delete folder from local instance to start from a clean slate
Alex Gleason 6656f7
    if os.path.exists(_config['GIT_FOLDER']):
Pierre-Yves Chibon b130e5
        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,
Alex Gleason 633b65
        name='test',
Alex Gleason 633b65
        is_fork=False,
Alex Gleason 633b65
        parent_id=None,
Alex Gleason 633b65
        description='test project #1',
Alex Gleason 633b65
        hook_token='aaabbbccc',
Alex Gleason 633b65
    )
Alex Gleason 633b65
    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,
Alex Gleason 633b65
        name='test2',
Alex Gleason 633b65
        is_fork=False,
Alex Gleason 633b65
        parent_id=None,
Alex Gleason 633b65
        description='test project #2',
Alex Gleason 633b65
        hook_token='aaabbbddd',
Alex Gleason 633b65
    )
Alex Gleason 633b65
    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,
Alex Gleason 633b65
        name='test3',
Alex Gleason 633b65
        is_fork=False,
Alex Gleason 633b65
        parent_id=None,
Alex Gleason 633b65
        description='namespaced test project',
Alex Gleason 633b65
        hook_token='aaabbbeee',
Alex Gleason 633b65
        namespace='somenamespace',
Alex Gleason 633b65
    )
Alex Gleason 633b65
    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 b130e5
    tests.create_projects_git(_config['GIT_FOLDER'], bare=True)
Alex Gleason 633b65
    add_content_git_repo(
Pierre-Yves Chibon b130e5
        os.path.join(_config['GIT_FOLDER'], 'test.git'))
skrzepto 779aa7
    tests.add_readme_git_repo(
Pierre-Yves Chibon b130e5
        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 b130e5
        os.path.join(_config['GIT_FOLDER'], 'forks', 'pingou',
skrzepto 779aa7
                     'test.git'))
skrzepto 779aa7
    tests.add_readme_git_repo(
Pierre-Yves Chibon b130e5
        os.path.join(_config['GIT_FOLDER'], 'forks', 'pingou',
skrzepto 779aa7
                     'test.git'))
skrzepto 779aa7
    tests.add_commit_git_repo(
Pierre-Yves Chibon b130e5
        os.path.join(_config['GIT_FOLDER'], 'forks', 'pingou',
skrzepto 779aa7
                     'test.git'), ncommits=10)
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # user_emails
skrzepto 779aa7
    item = pagure.lib.model.UserEmail(
Alex Gleason 633b65
        user_id=pingou.id,
skrzepto 779aa7
        email='bar@pingou.com')
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.UserEmail(
Alex Gleason 633b65
        user_id=pingou.id,
skrzepto 779aa7
        email='foo@pingou.com')
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.UserEmail(
Alex Gleason 633b65
        user_id=foo.id,
skrzepto 779aa7
        email='foo@bar.com')
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.UserEmail(
Alex Gleason 633b65
        user_id=you.id,
skrzepto 779aa7
        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(
Alex Gleason 633b65
        user_id=pingou.id,
skrzepto 779aa7
        email='foo@fp.o',
skrzepto 779aa7
        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,
skrzepto 779aa7
        uid='foobar',
Alex Gleason 633b65
        project_id=project1.id,
skrzepto 779aa7
        title='Problem with jenkins build',
skrzepto 779aa7
        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,
skrzepto 779aa7
        uid='foobar2',
Alex Gleason 633b65
        project_id=project1.id,
skrzepto 779aa7
        title='Unit tests failing',
skrzepto 779aa7
        content='Need to fix code for the unit tests to '
skrzepto 779aa7
                '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,
skrzepto 779aa7
        uid='foobar3',
Alex Gleason 633b65
        project_id=project1.id,
skrzepto 779aa7
        title='Segfault during execution',
skrzepto 779aa7
        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 930073
    group = pagure.lib.query.search_groups(session, pattern=None,
skrzepto 779aa7
                                     group_name="rel-eng", group_type=None)
skrzepto 779aa7
    item = pagure.lib.model.PagureUserGroup(
Alex Gleason 633b65
        user_id=pingou.id,
skrzepto 779aa7
        group_id=group.id
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
Pierre-Yves Chibon 930073
    group = pagure.lib.query.search_groups(session, pattern=None,
skrzepto 779aa7
                                     group_name="admin", group_type=None)
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.PagureUserGroup(
Alex Gleason 633b65
        user_id=you.id,
skrzepto 779aa7
        group_id=group.id
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
Pierre-Yves Chibon 930073
    group = pagure.lib.query.search_groups(session, pattern=None,
skrzepto 779aa7
                                     group_name="group", group_type=None)
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.PagureUserGroup(
Alex Gleason 633b65
        user_id=foo.id,
skrzepto 779aa7
        group_id=group.id
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # projects_groups
Pierre-Yves Chibon 930073
    group = pagure.lib.query.search_groups(session, pattern=None,
skrzepto 779aa7
                                     group_name="rel-eng", group_type=None)
Pierre-Yves Chibon 930073
    repo = pagure.lib.query.get_authorized_project(session, 'test')
skrzepto 779aa7
    item = pagure.lib.model.ProjectGroup(
skrzepto 779aa7
        project_id=repo.id,
Alex Gleason 6656f7
        group_id=group.id,
Alex Gleason 6656f7
        access="commit"
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
Pierre-Yves Chibon 930073
    group = pagure.lib.query.search_groups(session, pattern=None,
skrzepto 779aa7
                                     group_name="admin", group_type=None)
Pierre-Yves Chibon 930073
    repo = pagure.lib.query.get_authorized_project(session, 'test2')
skrzepto 779aa7
    item = pagure.lib.model.ProjectGroup(
skrzepto 779aa7
        project_id=repo.id,
Alex Gleason 6656f7
        group_id=group.id,
Alex Gleason 6656f7
        access="admin"
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # pull_requests
Pierre-Yves Chibon 930073
    repo = pagure.lib.query.get_authorized_project(session, 'test')
Pierre-Yves Chibon 930073
    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,
skrzepto 779aa7
        branch_from='master',
skrzepto 779aa7
        repo_to=repo,
skrzepto 779aa7
        branch_to='master',
skrzepto 779aa7
        title='Fixing code for unittest',
Lenka Segura 0e09f5
        user=username,
Lenka Segura 0e09f5
        status="Open"
skrzepto 779aa7
    )
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
Lenka Segura 0e09f5
    repo = pagure.lib.query.get_authorized_project(session, 'test')
Lenka Segura 0e09f5
    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,
Lenka Segura 0e09f5
        branch_from='master',
Lenka Segura 0e09f5
        repo_to=repo,
Lenka Segura 0e09f5
        branch_to='master',
Lenka Segura 0e09f5
        title='add very nice README',
Lenka Segura 0e09f5
        user=username,
Lenka Segura 0e09f5
        status="Open"
Lenka Segura 0e09f5
    )
Lenka Segura 0e09f5
    session.commit()
Lenka Segura 0e09f5
Lenka Segura 0e09f5
    repo = pagure.lib.query.get_authorized_project(session, 'test')
Lenka Segura 0e09f5
    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,
Lenka Segura 0e09f5
        branch_from='master',
Lenka Segura 0e09f5
        repo_to=repo,
Lenka Segura 0e09f5
        branch_to='master',
Lenka Segura 0e09f5
        title='Add README',
Lenka Segura 0e09f5
        user=username,
Lenka Segura 0e09f5
        status="Closed"
Lenka Segura 0e09f5
    )
Lenka Segura 0e09f5
    session.commit()
Lenka Segura 0e09f5
Lenka Segura 0e09f5
    repo = pagure.lib.query.get_authorized_project(session, 'test')
Lenka Segura 0e09f5
    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,
Lenka Segura 0e09f5
        branch_from='master',
Lenka Segura 0e09f5
        repo_to=repo,
Lenka Segura 0e09f5
        branch_to='master',
Lenka Segura 0e09f5
        title='Fix some containers',
Lenka Segura 0e09f5
        user=username,
Lenka Segura 0e09f5
        status="Merged"
Lenka Segura 0e09f5
    )
Lenka Segura 0e09f5
    session.commit()
Lenka Segura 0e09f5
Lenka Segura 0e09f5
    repo = pagure.lib.query.get_authorized_project(session, 'test')
Lenka Segura 0e09f5
    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,
Lenka Segura 0e09f5
        branch_from='master',
Lenka Segura 0e09f5
        repo_to=repo,
Lenka Segura 0e09f5
        branch_to='master',
Lenka Segura 0e09f5
        title='Fix pull request statuses',
Lenka Segura 0e09f5
        user=username,
Lenka Segura 0e09f5
        status="Closed"
Lenka Segura 0e09f5
    )
Lenka Segura 0e09f5
    session.commit()
Lenka Segura 0e09f5
Lenka Segura 0e09f5
    repo = pagure.lib.query.get_authorized_project(session, 'test')
Lenka Segura 0e09f5
    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,
Lenka Segura 0e09f5
        branch_from='master',
Lenka Segura 0e09f5
        repo_to=repo,
Lenka Segura 0e09f5
        branch_to='master',
Lenka Segura 0e09f5
        title='Fixing UI of issue',
Lenka Segura 0e09f5
        user=username,
Lenka Segura 0e09f5
        status="Merged"
Lenka Segura 0e09f5
    )
Lenka Segura 0e09f5
    session.commit()
Lenka Segura 0e09f5
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 930073
    repo = pagure.lib.query.get_authorized_project(session, 'test')
skrzepto 779aa7
    item = pagure.lib.model.ProjectUser(
skrzepto 779aa7
        project_id=repo.id,
Alex Gleason 633b65
        user_id=foo.id,
Alex Gleason 6656f7
        access="commit"
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
Pierre-Yves Chibon 930073
    repo = pagure.lib.query.get_authorized_project(session, 'test2')
skrzepto 779aa7
    item = pagure.lib.model.ProjectUser(
skrzepto 779aa7
        project_id=repo.id,
Alex Gleason 633b65
        user_id=you.id,
Alex Gleason 6656f7
        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,
skrzepto 779aa7
        issue_uid='foobar',
skrzepto 779aa7
        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 930073
    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 930073
    pagure.lib.query.add_issue_dependency(session, all_issues[0],
Alex Gleason 6656f7
                                    all_issues[1], 'pingou')
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # pull_request_comments
Pierre-Yves Chibon 930073
    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?",
skrzepto 779aa7
        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),
Lenka Segura 0e09f5
        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),
Lenka Segura 0e09f5
        status="failure"
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
Lenka Segura 0e09f5
    # pull_request_assignee
Lenka Segura 0e09f5
    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
Lenka Segura 0e09f5
    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
Lenka Segura 0e09f5
    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 930073
    repo = pagure.lib.query.get_authorized_project(session, 'test')
Pierre-Yves Chibon 930073
    issues = pagure.lib.query.search_issues(session, repo)
skrzepto 779aa7
    item = pagure.lib.model.TagIssue(
skrzepto 779aa7
        issue_uid=issues[0].uid,
Alex Gleason 6656f7
        tag='tag1',
skrzepto 779aa7
    )
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 b130e5
        shutil.rmtree(os.path.join(_config['GIT_FOLDER'],
skrzepto 779aa7
                                   fork_proj_location))
skrzepto 779aa7
    except:
skrzepto 779aa7
        print('git folder already deleted')
skrzepto 779aa7
skrzepto 779aa7
    try:
Pierre-Yves Chibon b130e5
        shutil.rmtree(os.path.join(_config['DOCS_FOLDER'],
skrzepto 779aa7
                                   fork_proj_location))
skrzepto 779aa7
    except:
skrzepto 779aa7
        print('docs folder already deleted')
skrzepto 779aa7
skrzepto 779aa7
    try:
Pierre-Yves Chibon b130e5
        shutil.rmtree(os.path.join(_config['TICKETS_FOLDER'],
skrzepto 779aa7
                                   fork_proj_location))
skrzepto 779aa7
    except:
skrzepto 779aa7
        print('tickets folder already deleted')
skrzepto 779aa7
skrzepto 779aa7
    try:
Pierre-Yves Chibon b130e5
        shutil.rmtree(os.path.join(_config['REQUESTS_FOLDER'],
skrzepto 779aa7
                                   fork_proj_location))
skrzepto 779aa7
    except:
skrzepto 779aa7
        print('requests folder already deleted')
skrzepto 779aa7
Pierre-Yves Chibon 930073
    repo = pagure.lib.query.get_authorized_project(session, 'test')
Pierre-Yves Chibon 930073
    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
Alex Gleason 633b65
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
Alex Gleason 633b65
    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
Alex Gleason 633b65
    with open(os.path.join(newfolder, 'sources'), 'w') as stream:
Alex Gleason 633b65
        stream.write('foo\n bar')
Alex Gleason 633b65
    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:
Alex Gleason 633b65
        commit = repo.revparse_single(
Alex Gleason 633b65
            '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()
Alex Gleason 633b65
    author = pygit2.Signature(
Alex Gleason 633b65
        'Alice Author', 'alice@authors.tld')
Alex Gleason 633b65
    committer = pygit2.Signature(
Alex Gleason 633b65
        'Cecil Committer', 'cecil@committers.tld')
Alex Gleason 633b65
    repo.create_commit(
Alex Gleason 633b65
        'refs/heads/%s' % branch,  # the name of the reference to update
Alex Gleason 633b65
        author,
Alex Gleason 633b65
        committer,
Alex Gleason 633b65
        '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:
Alex Gleason 633b65
        commit = repo.revparse_single(
Alex Gleason 633b65
            '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
    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
Alex Gleason 633b65
    with open(os.path.join(newfolder, subfolder, 'file'), 'w') as stream:
Alex Gleason 633b65
        stream.write('foo\n bar\nbaz')
Alex Gleason 633b65
    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()
Alex Gleason 633b65
    author = pygit2.Signature(
Alex Gleason 633b65
        'Alice Author', 'alice@authors.tld')
Alex Gleason 633b65
    committer = pygit2.Signature(
Alex Gleason 633b65
        'Cecil Committer', 'cecil@committers.tld')
Alex Gleason 633b65
    repo.create_commit(
Alex Gleason 633b65
        'refs/heads/%s' % branch,  # the name of the reference to update
Alex Gleason 633b65
        author,
Alex Gleason 633b65
        committer,
Alex Gleason 633b65
        '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
Alex Gleason 633b65
        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(
Alex Gleason 633b65
        'HEAD' if branch == 'master' else 'refs/heads/%s' % branch).resolve()
Alex Gleason 633b65
    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 067853
    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 067853
            "Enter your username so we can add you into the test data:  ")
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 067853
            "Enter your username so we can add you into the "
Pierre-Yves Chibon 067853
            "test data:  ")
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 067853
    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__":
skrzepto 779aa7
    desc = "Run the dev database initialization/insertion/deletion " \
Pierre-Yves Chibon b130e5
           "script for db located  " + str(_config['DB_URL'])
skrzepto 779aa7
    parser = argparse.ArgumentParser(prog="dev-data", description=desc)
skrzepto 779aa7
    parser.add_argument('-i', '--init', action="store_true",
skrzepto 779aa7
                        help="Create the dev db")
skrzepto 779aa7
    parser.add_argument('-p', '--populate', action="store_true",
skrzepto 779aa7
                        help="Add test data to the db")
skrzepto 779aa7
    parser.add_argument('-d', '--delete', action="store_true",
skrzepto 779aa7
                        help="Wipe the dev db")
skrzepto 779aa7
    parser.add_argument('-a', '--all', action="store_true",
Pierre-Yves Chibon 067853
                        help="Create, Populate then Wipe the dev db")
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()):
skrzepto 779aa7
        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 067853
            debug=False)
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 067853
            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)