Blame tests/test_pagure_lib_git_diff_pr.py

Pierre-Yves Chibon 839f14
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
"""
Pierre-Yves Chibon 839f14
 (c) 2017 - Copyright Red Hat Inc
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
 Authors:
Pierre-Yves Chibon 839f14
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
"""
Pierre-Yves Chibon 839f14
Aurélien Bompard dcf6f6
from __future__ import unicode_literals
Aurélien Bompard dcf6f6
Pierre-Yves Chibon 839f14
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 839f14
import pkg_resources  # noqa
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
import json  # noqa
Pierre-Yves Chibon 839f14
import unittest  # noqa
Pierre-Yves Chibon 839f14
import shutil  # noqa
Pierre-Yves Chibon 839f14
import sys  # noqa
Pierre-Yves Chibon 839f14
import tempfile  # noqa
Pierre-Yves Chibon cd4dad
import time  # noqa
Pierre-Yves Chibon 839f14
import os  # noqa
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
import pygit2  # noqa
Pierre-Yves Chibon c2560f
from mock import patch, MagicMock  # noqa
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 839f14
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
import pagure.lib  # noqa
Pierre-Yves Chibon 839f14
import tests  # noqa
Pierre-Yves Chibon 839f14
from pagure.lib.repo import PagureRepo  # noqa
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
class PagureFlaskForkPrtests(tests.Modeltests):
Pierre-Yves Chibon 839f14
    """ Tests for flask fork controller of pagure regarding diffing PRs """
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon c2560f
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 839f14
    def setUp(self):
Pierre-Yves Chibon 839f14
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 839f14
        super(PagureFlaskForkPrtests, self).setUp()
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon c2560f
        # Create the main project in the DB
Pierre-Yves Chibon c2560f
        item = pagure.lib.model.Project(
Pierre-Yves Chibon c2560f
            user_id=1,  # pingou
Pierre-Yves Chibon c2560f
            name='test',
Pierre-Yves Chibon c2560f
            description='test project #1',
Pierre-Yves Chibon c2560f
            hook_token='aaabbbccc',
Pierre-Yves Chibon c2560f
        )
Pierre-Yves Chibon c2560f
        item.close_status = [
Pierre-Yves Chibon c2560f
            'Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Pierre-Yves Chibon c2560f
        self.session.add(item)
Pierre-Yves Chibon c2560f
        self.session.commit()
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon c2560f
        # Create the fork
Pierre-Yves Chibon c2560f
        item = pagure.lib.model.Project(
Pierre-Yves Chibon c2560f
            user_id=1,  # pingou
Pierre-Yves Chibon c2560f
            name='test',
Pierre-Yves Chibon c2560f
            description='test project #1',
Pierre-Yves Chibon c2560f
            hook_token='aaabbbcccdd',
Pierre-Yves Chibon c2560f
            parent_id=1,
Pierre-Yves Chibon c2560f
            is_fork=True,
Pierre-Yves Chibon c2560f
        )
Pierre-Yves Chibon c2560f
        item.close_status = [
Pierre-Yves Chibon c2560f
            'Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Pierre-Yves Chibon c2560f
        self.session.add(item)
Pierre-Yves Chibon c2560f
        self.session.commit()
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon 839f14
        # Create two git repos, one has 6 commits, the other 4 of which only
Pierre-Yves Chibon 839f14
        # 1 isn't present in the first repo
Pierre-Yves Chibon 839f14
        gitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon 839f14
        pygit2.init_repository(gitrepo, bare=True)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        gitrepo2 = os.path.join(
Pierre-Yves Chibon 839f14
            self.path, 'repos', 'forks', 'pingou', 'test.git')
Pierre-Yves Chibon 839f14
        pygit2.init_repository(gitrepo2, bare=True)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
Pierre-Yves Chibon 839f14
        repopath = os.path.join(newpath, 'test')
Pierre-Yves Chibon 839f14
        clone_repo = pygit2.clone_repository(gitrepo, repopath)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        # Do 3 commits to the main repo
Pierre-Yves Chibon 839f14
        for i in range(3):
Pierre-Yves Chibon 839f14
            with open(os.path.join(repopath, 'sources'), 'w') as stream:
Pierre-Yves Chibon 839f14
                stream.write('foo%s\n bar%s\n' % (i, i))
Pierre-Yves Chibon 839f14
            clone_repo.index.add('sources')
Pierre-Yves Chibon 839f14
            clone_repo.index.write()
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
            parents = []
Pierre-Yves Chibon 839f14
            try:
Pierre-Yves Chibon 839f14
                last_commit = clone_repo.revparse_single('HEAD')
Pierre-Yves Chibon 839f14
                parents = [last_commit.oid.hex]
Pierre-Yves Chibon 839f14
            except KeyError:
Pierre-Yves Chibon 839f14
                pass
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
            # Commits the files added
Pierre-Yves Chibon 839f14
            tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 839f14
            author = pygit2.Signature(
Pierre-Yves Chibon 839f14
                'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 839f14
            committer = pygit2.Signature(
Pierre-Yves Chibon 839f14
                'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 839f14
            clone_repo.create_commit(
Pierre-Yves Chibon 839f14
                'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 839f14
                author,
Pierre-Yves Chibon 839f14
                committer,
Pierre-Yves Chibon 839f14
                'Editing the file sources for testing #%s' % i,
Pierre-Yves Chibon 839f14
                # binary string representing the tree object ID
Pierre-Yves Chibon 839f14
                tree,
Pierre-Yves Chibon 839f14
                # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 839f14
                parents
Pierre-Yves Chibon 839f14
            )
Pierre-Yves Chibon 20a73c
            time.sleep(1)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        # Push to the main repo
Pierre-Yves Chibon 839f14
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon 839f14
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 839f14
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        # Push to the fork repo
Pierre-Yves Chibon 839f14
        remote = clone_repo.create_remote('pingou_fork', gitrepo2)
Pierre-Yves Chibon 839f14
        PagureRepo.push(remote, refname)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        # Do another 3 commits to the main repo
Pierre-Yves Chibon 839f14
        for i in range(3, 6):
Pierre-Yves Chibon 839f14
            with open(os.path.join(repopath, 'sources'), 'w') as stream:
Pierre-Yves Chibon 839f14
                stream.write('foo%s\n bar%s\n' % (i, i))
Pierre-Yves Chibon 839f14
            clone_repo.index.add('sources')
Pierre-Yves Chibon 839f14
            clone_repo.index.write()
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
            last_commit = clone_repo.revparse_single('HEAD')
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
            # Commits the files added
Pierre-Yves Chibon 839f14
            tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 839f14
            author = pygit2.Signature(
Pierre-Yves Chibon 839f14
                'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 839f14
            committer = pygit2.Signature(
Pierre-Yves Chibon 839f14
                'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 839f14
            clone_repo.create_commit(
Pierre-Yves Chibon 839f14
                'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 839f14
                author,
Pierre-Yves Chibon 839f14
                committer,
Pierre-Yves Chibon 839f14
                'Editing the file sources for testing #%s' % i,
Pierre-Yves Chibon 839f14
                # binary string representing the tree object ID
Pierre-Yves Chibon 839f14
                tree,
Pierre-Yves Chibon 839f14
                # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 839f14
                [last_commit.oid.hex]
Pierre-Yves Chibon 839f14
            )
Pierre-Yves Chibon 20a73c
            time.sleep(1)
Pierre-Yves Chibon 839f14
        # Push to the main repo
Pierre-Yves Chibon 839f14
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon 839f14
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 839f14
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon cd4dad
        # Add two commits to the fork repo
Pierre-Yves Chibon 839f14
        repopath = os.path.join(newpath, 'pingou_test')
Pierre-Yves Chibon 839f14
        clone_repo = pygit2.clone_repository(gitrepo2, repopath)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        with open(os.path.join(repopath, 'sources'), 'w') as stream:
Pierre-Yves Chibon 839f14
                stream.write('foo\n bar\n')
Pierre-Yves Chibon 839f14
        clone_repo.index.add('sources')
Pierre-Yves Chibon 839f14
        clone_repo.index.write()
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        last_commit = clone_repo.revparse_single('HEAD')
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        # Commits the files added
Pierre-Yves Chibon 839f14
        tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 839f14
        author = pygit2.Signature(
Pierre-Yves Chibon 839f14
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 839f14
        committer = pygit2.Signature(
Pierre-Yves Chibon 839f14
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon cd4dad
        last_commit = clone_repo.create_commit(
Pierre-Yves Chibon 839f14
            'refs/heads/feature_foo',  # the name of the reference to update
Pierre-Yves Chibon 839f14
            author,
Pierre-Yves Chibon 839f14
            committer,
Pierre-Yves Chibon 839f14
            'New edition on side branch of the file sources for testing',
Pierre-Yves Chibon 839f14
            # binary string representing the tree object ID
Pierre-Yves Chibon 839f14
            tree,
Pierre-Yves Chibon 839f14
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 839f14
            [last_commit.oid.hex]
Pierre-Yves Chibon 839f14
        )
Pierre-Yves Chibon 20a73c
        time.sleep(1)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon cd4dad
        with open(os.path.join(repopath, 'sources'), 'w') as stream:
Pierre-Yves Chibon cd4dad
                stream.write('foo\n bar\nbaz\n')
Pierre-Yves Chibon cd4dad
        clone_repo.index.add('sources')
Pierre-Yves Chibon cd4dad
        clone_repo.index.write()
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        # Commits the files added
Pierre-Yves Chibon cd4dad
        tree = clone_repo.index.write_tree()
Pierre-Yves Chibon cd4dad
        author = pygit2.Signature(
Pierre-Yves Chibon cd4dad
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon cd4dad
        committer = pygit2.Signature(
Pierre-Yves Chibon cd4dad
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon cd4dad
        last_commit = clone_repo.create_commit(
Pierre-Yves Chibon cd4dad
            'refs/heads/feature_foo',  # the name of the reference to update
Pierre-Yves Chibon cd4dad
            author,
Pierre-Yves Chibon cd4dad
            committer,
Pierre-Yves Chibon cd4dad
            'Second edit on side branch of the file sources for testing',
Pierre-Yves Chibon cd4dad
            # binary string representing the tree object ID
Pierre-Yves Chibon cd4dad
            tree,
Pierre-Yves Chibon cd4dad
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon cd4dad
            [last_commit.hex]
Pierre-Yves Chibon cd4dad
        )
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon 839f14
        # Push to the fork repo
Pierre-Yves Chibon 839f14
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 839f14
        refname = 'refs/heads/feature_foo:refs/heads/feature_foo'
Pierre-Yves Chibon 839f14
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 875a08
        shutil.rmtree(newpath)
Pierre-Yves Chibon 875a08
Pierre-Yves Chibon c2560f
        # Create the PR between the two repos
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon b130e5
        forked_repo = pagure.lib.get_authorized_project(
Pierre-Yves Chibon c2560f
            self.session, 'test', user='pingou')
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon c2560f
        req = pagure.lib.new_pull_request(
Pierre-Yves Chibon c2560f
            session=self.session,
Pierre-Yves Chibon c2560f
            repo_from=forked_repo,
Pierre-Yves Chibon c2560f
            branch_from='feature_foo',
Pierre-Yves Chibon c2560f
            repo_to=repo,
Pierre-Yves Chibon c2560f
            branch_to='master',
Pierre-Yves Chibon c2560f
            title='test pull-request',
Pierre-Yves Chibon c2560f
            user='pingou',
Pierre-Yves Chibon c2560f
            requestfolder=None,
Pierre-Yves Chibon c2560f
        )
Pierre-Yves Chibon c2560f
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon c2560f
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon 839f14
    def test_get_pr_info(self):
Pierre-Yves Chibon 839f14
        """ Test pagure.ui.fork._get_pr_info """
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        gitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon 839f14
        gitrepo2 = os.path.join(
Pierre-Yves Chibon 839f14
            self.path, 'repos', 'forks', 'pingou', 'test.git')
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 875a08
        diff, diff_commits, orig_commit = pagure.lib.git.get_diff_info(
Pierre-Yves Chibon 839f14
            repo_obj=PagureRepo(gitrepo2),
Pierre-Yves Chibon 839f14
            orig_repo=PagureRepo(gitrepo),
Pierre-Yves Chibon 839f14
            branch_from='feature_foo',
Pierre-Yves Chibon 839f14
            branch_to='master'
Pierre-Yves Chibon 839f14
        )
Pierre-Yves Chibon cd4dad
        self.assertEqual(len(diff_commits), 2)
Pierre-Yves Chibon 839f14
        self.assertEqual(
Pierre-Yves Chibon 839f14
            diff_commits[0].message,
Pierre-Yves Chibon cd4dad
            'Second edit on side branch of the file sources for testing'
Pierre-Yves Chibon cd4dad
        )
Pierre-Yves Chibon cd4dad
        self.assertEqual(
Pierre-Yves Chibon cd4dad
            diff_commits[1].message,
Pierre-Yves Chibon 839f14
            'New edition on side branch of the file sources for testing'
Pierre-Yves Chibon 839f14
        )
Pierre-Yves Chibon 839f14
        self.assertEqual(
Pierre-Yves Chibon 839f14
            orig_commit.message,
Pierre-Yves Chibon 839f14
            'Editing the file sources for testing #5'
Pierre-Yves Chibon 839f14
        )
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon c2560f
    def test_get_pr_info_raises(self):
Pierre-Yves Chibon c2560f
        """ Test pagure.ui.fork._get_pr_info """
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon c2560f
        gitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon c2560f
        gitrepo2 = os.path.join(
Pierre-Yves Chibon c2560f
            self.path, 'repos', 'forks', 'pingou', 'test.git')
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon c2560f
        self.assertRaises(
Pierre-Yves Chibon c2560f
            pagure.exceptions.BranchNotFoundException,
Pierre-Yves Chibon c2560f
            pagure.lib.git.get_diff_info,
Pierre-Yves Chibon c2560f
            repo_obj=PagureRepo(gitrepo2),
Pierre-Yves Chibon c2560f
            orig_repo=PagureRepo(gitrepo),
Pierre-Yves Chibon c2560f
            branch_from='feature',
Pierre-Yves Chibon c2560f
            branch_to='master'
Pierre-Yves Chibon c2560f
        )
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon c2560f
        self.assertRaises(
Pierre-Yves Chibon c2560f
            pagure.exceptions.BranchNotFoundException,
Pierre-Yves Chibon c2560f
            pagure.lib.git.get_diff_info,
Pierre-Yves Chibon c2560f
            repo_obj=PagureRepo(gitrepo2),
Pierre-Yves Chibon c2560f
            orig_repo=PagureRepo(gitrepo),
Pierre-Yves Chibon c2560f
            branch_from='feature_foo',
Pierre-Yves Chibon c2560f
            branch_to='bar'
Pierre-Yves Chibon c2560f
        )
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon c2560f
    def test_diff_pull_request(self):
Pierre-Yves Chibon c2560f
        """ Test pagure.lib.git.diff_pull_request """
Pierre-Yves Chibon c2560f
        gitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon c2560f
        gitrepo2 = os.path.join(
Pierre-Yves Chibon c2560f
            self.path, 'repos', 'forks', 'pingou', 'test.git')
Pierre-Yves Chibon c2560f
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon c2560f
            self.session, requestid=1, project_id=1)
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon c2560f
        diff_commits, diff = pagure.lib.git.diff_pull_request(
Pierre-Yves Chibon c2560f
            self.session,
Pierre-Yves Chibon c2560f
            request=request,
Pierre-Yves Chibon c2560f
            repo_obj=PagureRepo(gitrepo2),
Pierre-Yves Chibon c2560f
            orig_repo=PagureRepo(gitrepo),
Pierre-Yves Chibon c2560f
            requestfolder=None,
Pierre-Yves Chibon c2560f
            with_diff=True
Pierre-Yves Chibon c2560f
        )
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon cd4dad
        self.assertEqual(len(diff_commits), 2)
Pierre-Yves Chibon c2560f
        self.assertEqual(
Pierre-Yves Chibon c2560f
            diff_commits[0].message,
Pierre-Yves Chibon cd4dad
            'Second edit on side branch of the file sources for testing'
Pierre-Yves Chibon cd4dad
        )
Pierre-Yves Chibon cd4dad
        self.assertEqual(
Pierre-Yves Chibon cd4dad
            diff_commits[1].message,
Pierre-Yves Chibon c2560f
            'New edition on side branch of the file sources for testing'
Pierre-Yves Chibon c2560f
        )
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon cd4dad
        # Check that the PR has its PR refs
Pierre-Yves Chibon cd4dad
        # we don't know the task id but we'll give it 30 sec to finish
Pierre-Yves Chibon cd4dad
        cnt = 0
Pierre-Yves Chibon cd4dad
        repo = PagureRepo(gitrepo)
Pierre-Yves Chibon cd4dad
        while 1:
Pierre-Yves Chibon cd4dad
            if 'refs/pull/1/head' in list(repo.listall_references()):
Pierre-Yves Chibon cd4dad
                break
Pierre-Yves Chibon cd4dad
            cnt += 1
Pierre-Yves Chibon cd4dad
            if cnt == 60:
Pierre-Yves Chibon cd4dad
                break
Pierre-Yves Chibon cd4dad
            time.sleep(0.5)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        self.assertTrue(cnt < 60)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        pr_ref = repo.lookup_reference('refs/pull/1/head')
Pierre-Yves Chibon cd4dad
        commit = pr_ref.get_object()
Pierre-Yves Chibon cd4dad
        self.assertEqual(
Pierre-Yves Chibon cd4dad
            commit.oid.hex,
Pierre-Yves Chibon cd4dad
            diff_commits[0].oid.hex
Pierre-Yves Chibon cd4dad
        )
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
    def test_diff_pull_request_updated(self):
Pierre-Yves Chibon cd4dad
        """ Test that calling pagure.lib.git.diff_pull_request on an updated
Pierre-Yves Chibon cd4dad
        PR updates the PR reference
Pierre-Yves Chibon cd4dad
        """
Pierre-Yves Chibon cd4dad
        gitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon cd4dad
        gitrepo2 = os.path.join(
Pierre-Yves Chibon cd4dad
            self.path, 'repos', 'forks', 'pingou', 'test.git')
Pierre-Yves Chibon cd4dad
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon cd4dad
            self.session, requestid=1, project_id=1)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        # Get the diff corresponding to the PR and check its ref
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        diff_commits, diff = pagure.lib.git.diff_pull_request(
Pierre-Yves Chibon cd4dad
            self.session,
Pierre-Yves Chibon cd4dad
            request=request,
Pierre-Yves Chibon cd4dad
            repo_obj=PagureRepo(gitrepo2),
Pierre-Yves Chibon cd4dad
            orig_repo=PagureRepo(gitrepo),
Pierre-Yves Chibon cd4dad
            requestfolder=None,
Pierre-Yves Chibon cd4dad
            with_diff=True
Pierre-Yves Chibon cd4dad
        )
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        self.assertEqual(len(diff_commits), 2)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        # Check that the PR has its PR refs
Pierre-Yves Chibon cd4dad
        # we don't know the task id but we'll give it 30 sec to finish
Pierre-Yves Chibon cd4dad
        cnt = 0
Pierre-Yves Chibon cd4dad
        repo = PagureRepo(gitrepo)
Pierre-Yves Chibon cd4dad
        while 1:
Pierre-Yves Chibon cd4dad
            if 'refs/pull/1/head' in list(repo.listall_references()):
Pierre-Yves Chibon cd4dad
                break
Pierre-Yves Chibon cd4dad
            cnt += 1
Pierre-Yves Chibon cd4dad
            if cnt == 60:
Pierre-Yves Chibon cd4dad
                break
Pierre-Yves Chibon cd4dad
            time.sleep(0.5)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        self.assertTrue(cnt < 60)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        pr_ref = repo.lookup_reference('refs/pull/1/head')
Pierre-Yves Chibon cd4dad
        commit = pr_ref.get_object()
Pierre-Yves Chibon cd4dad
        self.assertEqual(
Pierre-Yves Chibon cd4dad
            commit.oid.hex,
Pierre-Yves Chibon cd4dad
            diff_commits[0].oid.hex
Pierre-Yves Chibon cd4dad
        )
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        # Add a new commit on the fork
Pierre-Yves Chibon cd4dad
        repopath = os.path.join(self.path, 'pingou_test2')
Pierre-Yves Chibon cd4dad
        clone_repo = pygit2.clone_repository(
Pierre-Yves Chibon cd4dad
            gitrepo2, repopath, checkout_branch='feature_foo')
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        with open(os.path.join(repopath, 'sources'), 'w') as stream:
Pierre-Yves Chibon cd4dad
                stream.write('foo\n bar\nbaz\nhey there\n')
Pierre-Yves Chibon cd4dad
        clone_repo.index.add('sources')
Pierre-Yves Chibon cd4dad
        clone_repo.index.write()
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        last_commit = clone_repo.lookup_branch('feature_foo').get_object()
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        # Commits the files added
Pierre-Yves Chibon cd4dad
        tree = clone_repo.index.write_tree()
Pierre-Yves Chibon cd4dad
        author = pygit2.Signature(
Pierre-Yves Chibon cd4dad
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon cd4dad
        committer = pygit2.Signature(
Pierre-Yves Chibon cd4dad
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon cd4dad
        last_commit = clone_repo.create_commit(
Pierre-Yves Chibon cd4dad
            'refs/heads/feature_foo',  # the name of the reference to update
Pierre-Yves Chibon cd4dad
            author,
Pierre-Yves Chibon cd4dad
            committer,
Pierre-Yves Chibon cd4dad
            'Third edit on side branch of the file sources for testing',
Pierre-Yves Chibon cd4dad
            # binary string representing the tree object ID
Pierre-Yves Chibon cd4dad
            tree,
Pierre-Yves Chibon cd4dad
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon cd4dad
            [last_commit.oid.hex]
Pierre-Yves Chibon cd4dad
        )
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        # Push to the fork repo
Pierre-Yves Chibon cd4dad
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon cd4dad
        refname = 'refs/heads/feature_foo:refs/heads/feature_foo'
Pierre-Yves Chibon cd4dad
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        # Get the new diff for that PR and check its new ref
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        diff_commits, diff = pagure.lib.git.diff_pull_request(
Pierre-Yves Chibon cd4dad
            self.session,
Pierre-Yves Chibon cd4dad
            request=request,
Pierre-Yves Chibon cd4dad
            repo_obj=PagureRepo(gitrepo2),
Pierre-Yves Chibon cd4dad
            orig_repo=PagureRepo(gitrepo),
Pierre-Yves Chibon cd4dad
            requestfolder=None,
Pierre-Yves Chibon cd4dad
            with_diff=True
Pierre-Yves Chibon cd4dad
        )
Pierre-Yves Chibon cd4dad
        self.assertEqual(len(diff_commits), 3)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        # Check that the PR has its PR refs
Pierre-Yves Chibon cd4dad
        # we don't know the task id but we'll give it 30 sec to finish
Pierre-Yves Chibon cd4dad
        cnt = 0
Pierre-Yves Chibon cd4dad
        repo = PagureRepo(gitrepo)
Pierre-Yves Chibon cd4dad
        while 1:
Pierre-Yves Chibon cd4dad
            if 'refs/pull/1/head' in list(repo.listall_references()):
Pierre-Yves Chibon cd4dad
                break
Pierre-Yves Chibon cd4dad
            cnt += 1
Pierre-Yves Chibon cd4dad
            if cnt == 60:
Pierre-Yves Chibon cd4dad
                break
Pierre-Yves Chibon cd4dad
            time.sleep(0.5)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        self.assertTrue(cnt < 60)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        pr_ref = repo.lookup_reference('refs/pull/1/head')
Pierre-Yves Chibon cd4dad
        commit2 = pr_ref.get_object()
Pierre-Yves Chibon cd4dad
        self.assertEqual(
Pierre-Yves Chibon cd4dad
            commit2.oid.hex,
Pierre-Yves Chibon cd4dad
            diff_commits[0].oid.hex
Pierre-Yves Chibon cd4dad
        )
Pierre-Yves Chibon cd4dad
        self.assertNotEqual(
Pierre-Yves Chibon cd4dad
            commit.oid.hex,
Pierre-Yves Chibon cd4dad
            commit2.oid.hex,
Pierre-Yves Chibon cd4dad
        )
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
    def test_two_diff_pull_request_sequentially(self):
Pierre-Yves Chibon cd4dad
        """ Test calling pagure.lib.git.diff_pull_request twice returns
Pierre-Yves Chibon cd4dad
        the same data
Pierre-Yves Chibon cd4dad
        """
Pierre-Yves Chibon cd4dad
        gitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon cd4dad
        gitrepo2 = os.path.join(
Pierre-Yves Chibon cd4dad
            self.path, 'repos', 'forks', 'pingou', 'test.git')
Pierre-Yves Chibon cd4dad
        request = pagure.lib.search_pull_requests(
Pierre-Yves Chibon cd4dad
            self.session, requestid=1, project_id=1)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        # Get the diff corresponding to the PR and check its ref
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        diff_commits, diff = pagure.lib.git.diff_pull_request(
Pierre-Yves Chibon cd4dad
            self.session,
Pierre-Yves Chibon cd4dad
            request=request,
Pierre-Yves Chibon cd4dad
            repo_obj=PagureRepo(gitrepo2),
Pierre-Yves Chibon cd4dad
            orig_repo=PagureRepo(gitrepo),
Pierre-Yves Chibon cd4dad
            requestfolder=None,
Pierre-Yves Chibon cd4dad
            with_diff=True
Pierre-Yves Chibon cd4dad
        )
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        self.assertEqual(len(diff_commits), 2)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        # Check that the PR has its PR refs
Pierre-Yves Chibon cd4dad
        # we don't know the task id but we'll give it 30 sec to finish
Pierre-Yves Chibon cd4dad
        cnt = 0
Pierre-Yves Chibon cd4dad
        repo = PagureRepo(gitrepo)
Pierre-Yves Chibon cd4dad
        while 1:
Pierre-Yves Chibon cd4dad
            if 'refs/pull/1/head' in list(repo.listall_references()):
Pierre-Yves Chibon cd4dad
                break
Pierre-Yves Chibon cd4dad
            cnt += 1
Pierre-Yves Chibon cd4dad
            if cnt == 60:
Pierre-Yves Chibon cd4dad
                break
Pierre-Yves Chibon cd4dad
            time.sleep(0.5)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        self.assertTrue(cnt < 60)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        pr_ref = repo.lookup_reference('refs/pull/1/head')
Pierre-Yves Chibon cd4dad
        commit = pr_ref.get_object()
Pierre-Yves Chibon cd4dad
        self.assertEqual(
Pierre-Yves Chibon cd4dad
            commit.oid.hex,
Pierre-Yves Chibon cd4dad
            diff_commits[0].oid.hex
Pierre-Yves Chibon cd4dad
        )
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        # Run diff_pull_request a second time
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        diff_commits2, diff = pagure.lib.git.diff_pull_request(
Pierre-Yves Chibon cd4dad
            self.session,
Pierre-Yves Chibon cd4dad
            request=request,
Pierre-Yves Chibon cd4dad
            repo_obj=PagureRepo(gitrepo2),
Pierre-Yves Chibon cd4dad
            orig_repo=PagureRepo(gitrepo),
Pierre-Yves Chibon cd4dad
            requestfolder=None,
Pierre-Yves Chibon cd4dad
            with_diff=True
Pierre-Yves Chibon cd4dad
        )
Pierre-Yves Chibon cd4dad
        self.assertEqual(len(diff_commits2), 2)
Pierre-Yves Chibon cd4dad
        self.assertEqual(
Pierre-Yves Chibon cd4dad
            [d.oid.hex for d in diff_commits2],
Pierre-Yves Chibon cd4dad
            [d.oid.hex for d in diff_commits])
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        # Check that the PR has its PR refs
Pierre-Yves Chibon cd4dad
        # we don't know the task id but we'll give it 30 sec to finish
Pierre-Yves Chibon cd4dad
        cnt = 0
Pierre-Yves Chibon cd4dad
        repo = PagureRepo(gitrepo)
Pierre-Yves Chibon cd4dad
        while 1:
Pierre-Yves Chibon cd4dad
            if 'refs/pull/1/head' in list(repo.listall_references()):
Pierre-Yves Chibon cd4dad
                break
Pierre-Yves Chibon cd4dad
            cnt += 1
Pierre-Yves Chibon cd4dad
            if cnt == 60:
Pierre-Yves Chibon cd4dad
                break
Pierre-Yves Chibon cd4dad
            time.sleep(0.5)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        self.assertTrue(cnt < 60)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        pr_ref = repo.lookup_reference('refs/pull/1/head')
Pierre-Yves Chibon cd4dad
        commit2 = pr_ref.get_object()
Pierre-Yves Chibon cd4dad
        self.assertEqual(
Pierre-Yves Chibon cd4dad
            commit2.oid.hex,
Pierre-Yves Chibon cd4dad
            diff_commits[0].oid.hex
Pierre-Yves Chibon cd4dad
        )
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        self.assertEqual(
Pierre-Yves Chibon cd4dad
            commit.oid.hex,
Pierre-Yves Chibon cd4dad
            commit2.oid.hex
Pierre-Yves Chibon cd4dad
        )
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
if __name__ == '__main__':
Pierre-Yves Chibon 839f14
    unittest.main(verbosity=2)