<commit_before>package main

import (
	"fmt"
	"strings"

	"github.com/rainforestapp/rainforest-cli/rainforest"
	"github.com/urfave/cli"
)

type branchAPI interface {
	GetBranches(...string) ([]rainforest.Branch, error)
	CreateBranch(*rainforest.Branch) error
	MergeBranch(int) error
	DeleteBranch(int) error
}

func newBranch(c cliContext, api branchAPI) error {
	name := c.Args().First()
	name = strings.TrimSpace(name)

	if name == "" {
		return cli.NewExitError("Branch name cannot be blank", 1)
	}

	branch := rainforest.Branch{
		Name: name,
	}

	err := api.CreateBranch(&branch)

	if err != nil {
		return cli.NewExitError(err.Error(), 1)
	}

	fmt.Printf("Created branch %q.\n", name)
	return nil
}

func mergeBranch(c cliContext, api branchAPI) error {
	name := c.Args().First()
	name = strings.TrimSpace(name)

	if name == "" {
		return cli.NewExitError("Branch name cannot be blank", 1)
	}

	branches, err := api.GetBranches(name)

	if err != nil {
		return cli.NewExitError(err.Error(), 1)
	}

	if len(branches) == 0 {
		return cli.NewExitError("Cannot find branch", 1)
	}

	branch := branches[0]

	err = api.MergeBranch(branch.ID)

	if err != nil {
		return cli.NewExitError(err.Error(), 1)
	}

	fmt.Printf("Merged branch %q into main.\n", name)
	return nil
}

func deleteBranch(c cliContext, api branchAPI) error {
	name := c.Args().First()
	name = strings.TrimSpace(name)

	if name == "" {
		return cli.NewExitError("Branch name cannot be blank", 1)
	}

	branches, err := api.GetBranches(name)

	if err != nil {
		return cli.NewExitError(err.Error(), 1)
	}

	if len(branches) == 0 {
		return cli.NewExitError("Cannot find branch", 1)
	}

	branch := branches[0]

	err = api.DeleteBranch(branch.ID)

	if err != nil {
		return cli.NewExitError(err.Error(), 1)
	}

	fmt.Printf("Deleted branch %q.\n", name)
	return nil
}<commit_msg>Extract single function to get branchID from name<commit_after>package main

import (
	"errors"
	"fmt"
	"strings"

	"github.com/rainforestapp/rainforest-cli/rainforest"
	"github.com/urfave/cli"
)

type branchAPI interface {
	GetBranches(...string) ([]rainforest.Branch, error)
	CreateBranch(*rainforest.Branch) error
	MergeBranch(int) error
	DeleteBranch(int) error
}

func newBranch(c cliContext, api branchAPI) error {
	name := c.Args().First()
	name = strings.TrimSpace(name)

	if name == "" {
		return cli.NewExitError("Branch name cannot be blank", 1)
	}

	branch := rainforest.Branch{
		Name: name,
	}

	err := api.CreateBranch(&branch)

	if err != nil {
		return cli.NewExitError(err.Error(), 1)
	}

	fmt.Printf("Created branch %q.\n", name)
	return nil
}

func mergeBranch(c cliContext, api branchAPI) error {
	name := c.Args().First()
	name = strings.TrimSpace(name)

	if name == "" {
		return cli.NewExitError("Branch name cannot be blank", 1)
	}

	branchID, err := getBranchID(name, api)

	if err != nil {
		return cli.NewExitError(err.Error(), 1)
	}

	err = api.MergeBranch(branchID)

	if err != nil {
		return cli.NewExitError(err.Error(), 1)
	}

	fmt.Printf("Merged branch %q into main.\n", name)
	return nil
}

func deleteBranch(c cliContext, api branchAPI) error {
	name := c.Args().First()
	name = strings.TrimSpace(name)

	if name == "" {
		return cli.NewExitError("Branch name cannot be blank", 1)
	}

	branchID, err := getBranchID(name, api)

	if err != nil {
		return cli.NewExitError(err.Error(), 1)
	}

	err = api.DeleteBranch(branchID)

	if err != nil {
		return cli.NewExitError(err.Error(), 1)
	}

	fmt.Printf("Deleted branch %q.\n", name)
	return nil
}

// getBranchID gets branchID by using the branch name to query the API
func getBranchID(name string, api branchAPI) (int, error) {
	branches, err := api.GetBranches(name)

	if err != nil {
		return 0, err
	}

	if len(branches) == 0 {
		return 0, errors.New("Cannot find branch")
	}

	branch := branches[0]

	return branch.ID, nil
}
{'fraction_non_alphanumeric': 0.14611990656631196, 'fraction_numerical': 0.006747988580327018, 'mean_word_length': 3.9473684210526314, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3565415', 'n_tokens_mistral': 1500, 'n_tokens_neox': 1426, 'n_words': 361}