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
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
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 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 930073
import pagure.lib.query  # 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 73d120
    @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 73d120
            name="test",
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbccc",
Pierre-Yves Chibon c2560f
        )
Pierre-Yves Chibon c2560f
        item.close_status = [
Pierre-Yves Chibon 73d120
            "Invalid",
Pierre-Yves Chibon 73d120
            "Insufficient data",
Pierre-Yves Chibon 73d120
            "Fixed",
Pierre-Yves Chibon 73d120
            "Duplicate",
Pierre-Yves Chibon 73d120
        ]
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 73d120
            name="test",
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            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 73d120
            "Invalid",
Pierre-Yves Chibon 73d120
            "Insufficient data",
Pierre-Yves Chibon 73d120
            "Fixed",
Pierre-Yves Chibon 73d120
            "Duplicate",
Pierre-Yves Chibon 73d120
        ]
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 73d120
        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 73d120
            self.path, "repos", "forks", "pingou", "test.git"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 839f14
        pygit2.init_repository(gitrepo2, bare=True)
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 73d120
        newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
Pierre-Yves Chibon 73d120
        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 73d120
            with open(os.path.join(repopath, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
                stream.write("foo%s\n bar%s\n" % (i, i))
Pierre-Yves Chibon 73d120
            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 73d120
                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 73d120
            author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 839f14
            committer = pygit2.Signature(
Pierre-Yves Chibon 73d120
                "Cecil Committer", "cecil@committers.tld"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 839f14
            clone_repo.create_commit(
Pierre-Yves Chibon 73d120
                "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 839f14
                author,
Pierre-Yves Chibon 839f14
                committer,
Pierre-Yves Chibon 73d120
                "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 73d120
                parents,
Pierre-Yves Chibon 839f14
            )
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 839f14
        # Push to the main repo
Pierre-Yves Chibon 73d120
        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 73d120
        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 73d120
            with open(os.path.join(repopath, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
                stream.write("foo%s\n bar%s\n" % (i, i))
Pierre-Yves Chibon 73d120
            clone_repo.index.add("sources")
Pierre-Yves Chibon 839f14
            clone_repo.index.write()
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 73d120
            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 73d120
            author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 839f14
            committer = pygit2.Signature(
Pierre-Yves Chibon 73d120
                "Cecil Committer", "cecil@committers.tld"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 839f14
            clone_repo.create_commit(
Pierre-Yves Chibon 73d120
                "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 839f14
                author,
Pierre-Yves Chibon 839f14
                committer,
Pierre-Yves Chibon 73d120
                "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 73d120
                [last_commit.oid.hex],
Pierre-Yves Chibon 839f14
            )
Pierre-Yves Chibon 839f14
        # Push to the main repo
Pierre-Yves Chibon 73d120
        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 73d120
        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 73d120
        with open(os.path.join(repopath, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar\n")
Pierre-Yves Chibon 73d120
        clone_repo.index.add("sources")
Pierre-Yves Chibon 839f14
        clone_repo.index.write()
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon cd4dad
        last_commit = clone_repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/feature_foo",  # the name of the reference to update
Pierre-Yves Chibon 839f14
            author,
Pierre-Yves Chibon 839f14
            committer,
Pierre-Yves Chibon 73d120
            "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 73d120
            [last_commit.oid.hex],
Pierre-Yves Chibon 839f14
        )
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 73d120
        with open(os.path.join(repopath, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar\nbaz\n")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon cd4dad
        last_commit = clone_repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/feature_foo",  # the name of the reference to update
Pierre-Yves Chibon cd4dad
            author,
Pierre-Yves Chibon cd4dad
            committer,
Pierre-Yves Chibon 73d120
            "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 73d120
            [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 73d120
        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 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test", user="pingou"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon c2560f
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon c2560f
            session=self.session,
Pierre-Yves Chibon c2560f
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="feature_foo",
Pierre-Yves Chibon c2560f
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon c2560f
        )
Pierre-Yves Chibon c2560f
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        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 73d120
        gitrepo = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 839f14
        gitrepo2 = os.path.join(
Pierre-Yves Chibon 73d120
            self.path, "repos", "forks", "pingou", "test.git"
Pierre-Yves Chibon 73d120
        )
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 73d120
            branch_from="feature_foo",
Pierre-Yves Chibon 73d120
            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 73d120
            "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 73d120
            "New edition on side branch of the file sources for testing",
Pierre-Yves Chibon 839f14
        )
Pierre-Yves Chibon 839f14
        self.assertEqual(
Pierre-Yves Chibon 73d120
            orig_commit.message, "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 73d120
        gitrepo = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon c2560f
        gitrepo2 = os.path.join(
Pierre-Yves Chibon 73d120
            self.path, "repos", "forks", "pingou", "test.git"
Pierre-Yves Chibon 73d120
        )
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 73d120
            branch_from="feature",
Pierre-Yves Chibon 73d120
            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 73d120
            branch_from="feature_foo",
Pierre-Yves Chibon 73d120
            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 73d120
        gitrepo = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon c2560f
        gitrepo2 = os.path.join(
Pierre-Yves Chibon 73d120
            self.path, "repos", "forks", "pingou", "test.git"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, requestid=1, project_id=1
Pierre-Yves Chibon 73d120
        )
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 73d120
            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 73d120
            "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 73d120
            "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 73d120
        self.assertIn("refs/pull/1/head", list(repo.listall_references()))
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        self.assertTrue(cnt < 60)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon 73d120
        pr_ref = repo.lookup_reference("refs/pull/1/head")
Pierre-Yves Chibon 29ff0a
        commit = pr_ref.peel()
Pierre-Yves Chibon 73d120
        self.assertEqual(commit.oid.hex, diff_commits[0].oid.hex)
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 73d120
        gitrepo = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon cd4dad
        gitrepo2 = os.path.join(
Pierre-Yves Chibon 73d120
            self.path, "repos", "forks", "pingou", "test.git"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, requestid=1, project_id=1
Pierre-Yves Chibon 73d120
        )
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 73d120
            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 73d120
        self.assertIn("refs/pull/1/head", list(repo.listall_references()))
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        self.assertTrue(cnt < 60)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon 73d120
        pr_ref = repo.lookup_reference("refs/pull/1/head")
Pierre-Yves Chibon 29ff0a
        commit = pr_ref.peel()
Pierre-Yves Chibon 73d120
        self.assertEqual(commit.oid.hex, diff_commits[0].oid.hex)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        # Add a new commit on the fork
Pierre-Yves Chibon 73d120
        repopath = os.path.join(self.path, "pingou_test2")
Pierre-Yves Chibon cd4dad
        clone_repo = pygit2.clone_repository(
Pierre-Yves Chibon 73d120
            gitrepo2, repopath, checkout_branch="feature_foo"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon 73d120
        with open(os.path.join(repopath, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar\nbaz\nhey there\n")
Pierre-Yves Chibon 73d120
        clone_repo.index.add("sources")
Pierre-Yves Chibon cd4dad
        clone_repo.index.write()
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon 73d120
        last_commit = clone_repo.lookup_branch("feature_foo").peel()
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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon cd4dad
        last_commit = clone_repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/feature_foo",  # the name of the reference to update
Pierre-Yves Chibon cd4dad
            author,
Pierre-Yves Chibon cd4dad
            committer,
Pierre-Yves Chibon 73d120
            "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 73d120
            [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 73d120
        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 73d120
            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 73d120
        self.assertIn("refs/pull/1/head", list(repo.listall_references()))
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        self.assertTrue(cnt < 60)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon 73d120
        pr_ref = repo.lookup_reference("refs/pull/1/head")
Pierre-Yves Chibon 29ff0a
        commit2 = pr_ref.peel()
Pierre-Yves Chibon 73d120
        self.assertEqual(commit2.oid.hex, diff_commits[0].oid.hex)
Pierre-Yves Chibon 73d120
        self.assertNotEqual(commit.oid.hex, commit2.oid.hex)
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 73d120
        gitrepo = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon cd4dad
        gitrepo2 = os.path.join(
Pierre-Yves Chibon 73d120
            self.path, "repos", "forks", "pingou", "test.git"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, requestid=1, project_id=1
Pierre-Yves Chibon 73d120
        )
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 73d120
            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 73d120
        self.assertIn("refs/pull/1/head", list(repo.listall_references()))
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        self.assertTrue(cnt < 60)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon 73d120
        pr_ref = repo.lookup_reference("refs/pull/1/head")
Pierre-Yves Chibon 29ff0a
        commit = pr_ref.peel()
Pierre-Yves Chibon 73d120
        self.assertEqual(commit.oid.hex, diff_commits[0].oid.hex)
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 73d120
            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 73d120
            [d.oid.hex for d in diff_commits],
Pierre-Yves Chibon 73d120
        )
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 73d120
        self.assertIn("refs/pull/1/head", list(repo.listall_references()))
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon cd4dad
        self.assertTrue(cnt < 60)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon 73d120
        pr_ref = repo.lookup_reference("refs/pull/1/head")
Pierre-Yves Chibon 29ff0a
        commit2 = pr_ref.peel()
Pierre-Yves Chibon 73d120
        self.assertEqual(commit2.oid.hex, diff_commits[0].oid.hex)
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
        self.assertEqual(commit.oid.hex, commit2.oid.hex)
Pierre-Yves Chibon cd4dad
Pierre-Yves Chibon 839f14
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 839f14
    unittest.main(verbosity=2)