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

Function NewCreateContext

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

Source from the content-addressed store, hash-verified

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

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