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

Function NewCreateContext

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

Source from the content-addressed store, hash-verified

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

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