class TestGerritAPI(unittest.TestCase):
    def setUp(self):
        self.gerrit = GerritAPI("gerrit_url", "project_name", "username", "password")

    def test_create_change_successful(self):
        with patch("requests.post") as patched_post:
            patched_post.return_value = Response(201)
            rv = self.gerrit.create_change("change_subject")
            assert patched_post.called
            self.assertIsInstance(rv, tuple)

    def test_create_change_failed(self):
        with patch("requests.post") as patched_post:
            patched_post.return_value = Response(500)
            rv = self.gerrit.create_change("change_subject")
            assert patched_post.called
            self.assertIsInstance(rv, tuple)
 def setUp(self):
     self.gerrit = GerritAPI("gerrit_url", "project_name", "username", "password")
示例#3
0
def process_update(args, config):

        # Rebase the current branch
        subprocess.call(['git', 'checkout', 'master'])
        subprocess.call(['git', 'pull', '--rebase'])

        # Parse in the document mappings
        mapping_dir = '{}/mappings'.format(args.repopath)

        # Article directory
        article_dir = '{}/{}'.format(
            args.repopath,
            args.articlepath
        )

        if not os.path.exists(article_dir):
            os.makedirs(article_dir)

        # Documentation map between directory/files and
        # Categories/Folders/Articles
        docmap = FreshDeskDocumentMap(
            mapping_dir,
            article_dir,
            config['freshdesk_config']['api_url'],
            config['freshdesk_config']['api_token']
        )

        # Set up gerrit interface
        gerrit = GerritAPI(
            config['gerrit_config']['gerrit_url'],
            config['gerrit_config']['project_name'],
            config['gerrit_config']['web_username'],
            config['gerrit_config']['web_password']
        )

        # Reparse the filesystem
        docmap.update_articles()

        # Push the changes into Freshdesk
        docmap.synchronize_freshdesk()

        # Write out the updated information
        docmap.save_categories()
        docmap.save_folders()
        docmap.save_articles()
        docmap.save_counters()

        # Check if we need to make a new change
        log.debug('Checking if we need a change: {}'.format(
            docmap.require_change
        ))
        if docmap.require_change:
            # Assumes we are in the repo directory
            # Create a branch
            change_title = 'brokerupdate-{}'.format(
                datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
            )

            # Create a branch
            subprocess.call([
                'git',
                'checkout',
                '-b',
                change_title
            ])

            # Get a new change ID through the REST API
            change_id, long_id = gerrit.create_change(change_title)
            log.info('Change ID: {}'.format(change_id))
            log.info('Long ID: {}'.format(long_id))

            # Add all changes to be sent
            subprocess.call([
                'git',
                'add',
                '--all',
                args.repopath
            ])
            subprocess.call([
                'git',
                'commit',
                '-m',
                '{change_title}\n\nChange-Id: {change_id}'.format(
                    change_title=change_title,
                    change_id=change_id
                ),
                args.repopath
            ])

            # Push change to gerrit
            push_url = re.sub(
                'https://',
                'https://{username}:{password}@'.format(
                    username=config['gerrit_config']['web_username'],
                    password=config['gerrit_config']['web_password']
                ),
                config['gerrit_config']['gerrit_url']
            )
            subprocess.call([
                'git',
                'push',
                push_url + '/' + config['gerrit_config']['project_name'],
                'HEAD:refs/for/master'
            ])

            # Wait for verified state
            verfied = False

            while not gerrit.verified(long_id):
                # Wait for jenkins...
                time.sleep(5)

            # Self approve
            gerrit.self_approve_change(long_id)
示例#4
0
def process_update(args, config):

        # Rebase the current branch
        subprocess.call(['git', 'checkout', 'master'])
        subprocess.call(['git', 'pull', '--rebase'])

        # Parse in the document mappings
        mapping_dir = '{}/mappings'.format(args.repopath)

        # Article directory
        article_dir = '{}/{}'.format(
            args.repopath,
            args.articlepath
        )

        if not os.path.exists(article_dir):
            os.makedirs(article_dir)

        # Documentation map between directory/files and
        # Categories/Folders/Articles
        docmap = FreshDeskDocumentMap(
            mapping_dir,
            article_dir,
            config['freshdesk_config']['api_url'],
            config['freshdesk_config']['api_token']
        )

        # Set up gerrit interface
        gerrit = GerritAPI(
            config['gerrit_config']['gerrit_url'],
            config['gerrit_config']['project_name'],
            config['gerrit_config']['web_username'],
            config['gerrit_config']['web_password']
        )

        # Reparse the filesystem
        try:
            docmap.update_articles()
        except Exception as e:
            log.error("Article(s) cannot be updated. Please verify what missed")
            log.error(e)

        # Push the changes into Freshdesk
        docmap.synchronize_freshdesk()

        # Write out the updated information
        docmap.save_categories()
        docmap.save_folders()
        docmap.save_articles()
        docmap.save_counters()

        # Check if we need to make a new change
        log.debug('Checking if we need a change: {}'.format(
            docmap.require_change
        ))
        if docmap.require_change:
            # Assumes we are in the repo directory
            # Create a branch
            change_title = 'brokerupdate-{}'.format(
                datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
            )

            # Create a branch
            subprocess.call([
                'git',
                'checkout',
                '-b',
                change_title
            ])

            # Get a new change ID through the REST API
            change_id, long_id = gerrit.create_change(change_title)
            log.info('Change ID: {}'.format(change_id))
            log.info('Long ID: {}'.format(long_id))

            # Add all changes to be sent
            subprocess.call([
                'git',
                'add',
                '--all',
                args.repopath
            ])
            subprocess.call([
                'git',
                'commit',
                '-m',
                '{change_title}\n\nChange-Id: {change_id}'.format(
                    change_title=change_title,
                    change_id=change_id
                ),
                args.repopath
            ])

            # Push change to gerrit
            push_url = re.sub(
                'https://',
                'https://{username}:{password}@'.format(
                    username=config['gerrit_config']['web_username'],
                    password=config['gerrit_config']['web_password']
                ),
                config['gerrit_config']['gerrit_url']
            )
            subprocess.call([
                'git',
                'push',
                push_url + '/' + config['gerrit_config']['project_name'],
                'HEAD:refs/for/master'
            ])

            # Wait for verified state
            verfied = False

            while not gerrit.verified(long_id):
                # Wait for jenkins...
                time.sleep(5)

            # Self approve
            gerrit.self_approve_change(long_id)