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

Function NewCreateContext

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

Source from the content-addressed store, hash-verified

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

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