package com.atlassian.bitbucket.repository;

import com.atlassian.bitbucket.project.Project;
import com.atlassian.bitbucket.user.SecurityService;
import com.atlassian.bitbucket.util.UncheckedOperation;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * An operation that finds a repository by its {@link Project#getKey() project key} and
 * {@link Repository#getSlug() slug}, for use with {@link SecurityService SecurityService}.
 */
public class FindRepositoryOperation implements UncheckedOperation<Repository> {

    private final RepositoryService repositoryService;
    private final String projectKey;
    private final String repositorySlug;

    public FindRepositoryOperation(@Nonnull RepositoryService repositoryService,
                                   @Nonnull String projectKey,
                                   @Nonnull String repositorySlug) {
        this.repositoryService = repositoryService;
        this.projectKey = projectKey;
        this.repositorySlug = repositorySlug;
    }

    @Nullable
    @Override
    public Repository perform() {
        return repositoryService.getBySlug(projectKey, repositorySlug);
    }
}

