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

Function NewCreateContext

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

Source from the content-addressed store, hash-verified

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

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