Пример #1
0
def get_or_create_repository(name: str = None,
                             url: str = None,
                             path: str = None,
                             test_commands: str = None):
    '''
    Returns a repository, either by retrieving it from the database or creating
    it and then returning it.  This method is complicated because there are
    various states a repository could be in:

        1) Not created locally or in db
        2) Created locally but not in db
        3) Not created locally but stored in db
        4) Created locally and in db

    '''
    # if the path is not specified, then assume it is not created and that we
    # need to create a local copy
    if not path:
        # if not the name or url or path were specified, then we don't have enough
        # data to get the repository
        if not name and not url:
            msg = (
                'You did not specify a name, url or path when trying to get '
                'a repository.  Unable to create a local copy or determine '
                'which repository you are referring to')
            raise BugBuddyError(msg)

        # if the user specifies only a name, that's enough if it's in the database.
        # We are assuming that the project does not exist locally, so we will create
        # it
        if name and not url:
            repository = get(Repository, name=name)

            if not repository:
                msg = ('The name "{}" does not exist in the database for a '
                       'repository.'.format(name))
                raise BugBuddyError(msg)

            repository.create_local_copy()
            return repository

        if url:
            repository = get(Repository, url=url)
            if not repository:
                msg = ('The url "{}" does not exist in the database for a '
                       'repository.'.format(url))
                raise BugBuddyError(msg)

            repository.create_local_copy()
            return repository

    # we have a path, see if it's located in the database.
    else:
        pass
Пример #2
0
def get_synthetic_children_commits(commit: Commit):
    '''
    Get all children commits for a synthetic commit
    '''
    if not commit.is_synthetic:
        msg = ('Tried to get children commits for the non-synthetic commit: {}'
               .format(commit))
        raise BugBuddyError(msg)

    session = Session.object_session(commit)

    children_commits = []
    for diff_set in powerset(commit.diffs):
        # an empty set in the powerset is ignored
        if not diff_set:
            continue

        # there is only one diff_set that is the same length as the commit's
        # diff, which means they're the exact same diff set, so it's not really
        # a child of the commit
        if len(diff_set) == len(commit.diffs):
            break

        base_synthetic_ids = [diff.base_synthetic_diff_id for diff in diff_set]
        diff_hash = get_hash_given_base_synthetic_ids(base_synthetic_ids)
        child_commit = (session.query(Commit).filter(
            Commit.synthetic_diff_hash == diff_hash).one())
        children_commits.append(child_commit)

    return children_commits
Пример #3
0
def _apply_diff(diff: Diff, revert=False):
    '''
    Either reverts or applies a diff
    '''
    temp_output = None
    try:
        file_name = 'bugbuddy_diff_id_{}'.format(diff.id)
        suffix = '.patch'
        temp_output = tempfile.NamedTemporaryFile(
            prefix=file_name, suffix=suffix, dir=diff.commit.repository.path)
        temp_output.write(str.encode(diff.patch + '\n\n'))
        temp_output.flush()

        command = ('git apply {revert}{file_path}'.format(
            revert='-R ' if revert else '', file_path=temp_output.name))

        stdout, stderr = run_cmd(diff.commit.repository, command)
        if stderr:
            msg = (
                'Error trying to {revert_or_add} diff {diff} with patch:\n{patch}\n\n'
                'stderr: {stderr}'.format(
                    revert_or_add='revert' if revert else 'add',
                    diff=diff,
                    patch=diff.patch,
                    stderr=stderr))
            raise BugBuddyError(msg)
    except Exception as e:
        import pdb
        pdb.set_trace()
        logger.error('Hit error trying to apply diff: {}'.format(e))
    finally:
        if temp_output:
            temp_output.close()
Пример #4
0
    def test_result_prediction(self):
        '''
        Returns test result prediction data
        '''
        if self.test_result_prediction_data is None:
            msg = 'Requested prediction data but it does not exist'
            raise BugBuddyError(msg)

        return dict(zip(self.sorted_tests, self.test_result_prediction_data))
Пример #5
0
    def blamed_function_prediction_dict(self):
        '''
        Returns test result prediction data
        '''
        if self.blamed_function_prediction is None:
            msg = 'Requested prediction data but it does not exist'
            raise BugBuddyError(msg)

        return dict(
            zip(self.test_run.commit.functions,
                self.blamed_function_prediction))
Пример #6
0
    def get_matching_test_result(self, test_result):
        '''
        Retuns the corresponding test output
        '''
        test_run = self.test_runs[0]
        matching_test_results = [
            commit_test_result for commit_test_result in test_run.test_results
            if commit_test_result.test.id == test_result.test.id
        ]
        if not matching_test_results:
            import pdb
            pdb.set_trace()
            msg = ('Could not find a matching test result for {} at {}'.format(
                test_result, self))
            raise BugBuddyError(msg)

        if len(matching_test_results) > 1:
            import pdb
            pdb.set_trace()
            msg = ('Found multiple matching test_results for {} at {}'.format(
                test_result, self))
            raise BugBuddyError(msg)

        return matching_test_results[0]
Пример #7
0
    def __init__(
        self,
        repository: Repository,
        commit_id: str,
        branch: str,
        commit_type: str = SYNTHETIC_CHANGE,
    ):  # Commit
        '''
        Creates a new TestResults instance
        '''
        if not commit_id:
            msg = 'Tried creating commit without a commit_id'
            raise BugBuddyError(msg)

        self.repository = repository
        self.commit_id = commit_id
        self.branch = branch
        self.commit_type = commit_type