package com.atlassian.bitbucket.mesh.git.exception;

import com.atlassian.bitbucket.mesh.rpc.v1.RpcBranchNotMerged;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcGitError;
import com.google.protobuf.ByteString;
import io.grpc.Status;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

public class BranchNotMergedException extends AbstractGitException {

    private final String branchName;

    public BranchNotMergedException(@Nonnull String branchName) {
        super("Branch " + branchName + " is not merged", Status.INVALID_ARGUMENT);

        this.branchName = requireNonNull(branchName, "branchName");
    }

    @Nonnull
    public String getBranchName() {
        return branchName;
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setBranchNotMerged(RpcBranchNotMerged.newBuilder()
                        .setBranchName(ByteString.copyFromUtf8(branchName)))
                .build();
    }
}
