github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/runbits/checker/checker.go (about)

     1  package checker
     2  
     3  import (
     4  	"errors"
     5  	"strconv"
     6  
     7  	"github.com/ActiveState/cli/internal/errs"
     8  	"github.com/ActiveState/cli/internal/locale"
     9  	"github.com/ActiveState/cli/internal/logging"
    10  	"github.com/ActiveState/cli/internal/output"
    11  	"github.com/ActiveState/cli/pkg/localcommit"
    12  	"github.com/ActiveState/cli/pkg/platform/authentication"
    13  	"github.com/ActiveState/cli/pkg/platform/model"
    14  	"github.com/ActiveState/cli/pkg/project"
    15  )
    16  
    17  // RunCommitsBehindNotifier checks for the commits behind count based on the
    18  // provided project and displays the results to the user in a helpful manner.
    19  func RunCommitsBehindNotifier(p *project.Project, out output.Outputer, auth *authentication.Auth) {
    20  	count, err := CommitsBehind(p, auth)
    21  	if err != nil {
    22  		if errors.Is(err, model.ErrCommitCountUnknowable) {
    23  			out.Notice(output.Title(locale.Tr("runtime_update_notice_unknown_count")))
    24  			out.Notice(locale.Tr("runtime_update_help", p.Owner(), p.Name()))
    25  			return
    26  		}
    27  
    28  		logging.Warning(locale.T("err_could_not_get_commit_behind_count"))
    29  		return
    30  	}
    31  	if count > 0 {
    32  		ct := strconv.Itoa(count)
    33  		out.Notice(output.Title(locale.Tr("runtime_update_notice_known_count", ct)))
    34  		out.Notice(locale.Tr("runtime_update_help", p.Owner(), p.Name()))
    35  	}
    36  }
    37  
    38  func CommitsBehind(p *project.Project, auth *authentication.Auth) (int, error) {
    39  	if p.IsHeadless() {
    40  		return 0, nil
    41  	}
    42  
    43  	latestCommitID, err := model.BranchCommitID(p.Owner(), p.Name(), p.BranchName())
    44  	if err != nil {
    45  		return 0, locale.WrapError(err, "Could not get branch information for {{.V0}}/{{.V1}}", p.Owner(), p.Name())
    46  	}
    47  
    48  	if latestCommitID == nil {
    49  		return 0, locale.NewError("err_latest_commit", "Latest commit ID is nil")
    50  	}
    51  
    52  	commitID, err := localcommit.Get(p.Dir())
    53  	if err != nil {
    54  		return 0, errs.Wrap(err, "Unable to get local commit")
    55  	}
    56  
    57  	return model.CommitsBehind(*latestCommitID, commitID, auth)
    58  }