MCPcopy Create free account
hub / github.com/cli/cli / NewCreateContext

Function NewCreateContext

pkg/cmd/pr/create/create.go:771–1080  ·  view source on GitHub ↗
(opts *CreateOptions)

Source from the content-addressed store, hash-verified

769}
770
771func NewCreateContext(opts *CreateOptions) (*CreateContext, error) {
772 httpClient, err := opts.HttpClient()
773 if err != nil {
774 return nil, err
775 }
776 client := api.NewClientFromHTTP(httpClient)
777
778 remotes, err := getRemotes(opts)
779 if err != nil {
780 return nil, err
781 }
782
783 resolvedRemotes, err := ghContext.ResolveRemotesToRepos(remotes, client, opts.RepoOverride)
784 if err != nil {
785 return nil, err
786 }
787
788 var baseRepo *api.Repository
789 if br, err := resolvedRemotes.BaseRepo(opts.IO); err == nil {
790 if r, ok := br.(*api.Repository); ok {
791 baseRepo = r
792 } else {
793 // TODO: if RepoNetwork is going to be requested anyway in `repoContext.HeadRepos()`,
794 // consider piggybacking on that result instead of performing a separate lookup
795 baseRepo, err = api.GitHubRepo(client, br)
796 if err != nil {
797 return nil, err
798 }
799 }
800 } else {
801 return nil, err
802 }
803
804 // This closure provides an easy way to instantiate a CreateContext with everything other than
805 // the refs. This probably indicates that CreateContext could do with some rework, but the refactor
806 // to introduce PRRefs is already large enough.
807 var newCreateContext = func(refs creationRefs) *CreateContext {
808 baseTrackingBranch := refs.BaseRef()
809
810 // The baseTrackingBranch is used later for a command like:
811 // `git commit upstream/main feature` in order to create a PR message showing the commits
812 // between these two refs. I'm not really sure what is expected to happen if we don't have a remote,
813 // which seems like it would be possible with a command `gh pr create --repo owner/repo-that-is-not-a-remote`.
814 // In that case, we might just have a mess? In any case, this is what the old code did, so I don't want to change
815 // it as part of an already large refactor.
816 baseRemote, _ := resolvedRemotes.RemoteForRepo(baseRepo)
817 if baseRemote != nil {
818 baseTrackingBranch = fmt.Sprintf("%s/%s", baseRemote.Name, baseTrackingBranch)
819 }
820
821 return &CreateContext{
822 ResolvedRemotes: resolvedRemotes,
823 Client: client,
824 GitClient: opts.GitClient,
825 PRRefs: refs,
826 BaseTrackingBranch: baseTrackingBranch,
827 }
828 }

Callers 1

createRunFunction · 0.85

Calls 15

RepoOwnerMethod · 0.95
RepoHostMethod · 0.95
NewClientFromHTTPFunction · 0.92
GitHubRepoFunction · 0.92
ParseQualifiedHeadRefFunction · 0.92
PluralizeFunction · 0.92
NewRemoteToRepoResolverFunction · 0.92
NewQualifiedHeadRefFunction · 0.92
ParseRemoteTrackingRefFunction · 0.92
RepoFindForksFunction · 0.92

Tested by

no test coverage detected