mirror of
				https://gitea.com/gitea/tea.git
				synced 2025-10-30 16:55:25 +01:00 
			
		
		
		
	tea pr create: make local repo optional (#393)
this is a partial fix to #378, making the command available outside of a local repo. new behaviour: - when run interactively without local repo context, the head repo prompt is not pre-populated - when run with flags without local repo context, it will complain unless `--head` is specified refactor: - pass TeaContext down to task.CreatePull Co-authored-by: Norwin <git@nroo.de> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/393 Reviewed-by: Alexey 〒erentyev <axifive@noreply.gitea.io> Reviewed-by: 6543 <6543@obermui.de> Co-authored-by: Norwin <noerw@noreply.gitea.io> Co-committed-by: Norwin <noerw@noreply.gitea.io>
This commit is contained in:
		| @@ -35,11 +35,10 @@ var CmdPullsCreate = cli.Command{ | |||||||
|  |  | ||||||
| func runPullsCreate(cmd *cli.Context) error { | func runPullsCreate(cmd *cli.Context) error { | ||||||
| 	ctx := context.InitCommand(cmd) | 	ctx := context.InitCommand(cmd) | ||||||
| 	ctx.Ensure(context.CtxRequirement{LocalRepo: true}) |  | ||||||
|  |  | ||||||
| 	// no args -> interactive mode | 	// no args -> interactive mode | ||||||
| 	if ctx.NumFlags() == 0 { | 	if ctx.NumFlags() == 0 { | ||||||
| 		return interact.CreatePull(ctx.Login, ctx.Owner, ctx.Repo) | 		return interact.CreatePull(ctx) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// else use args to create PR | 	// else use args to create PR | ||||||
| @@ -49,9 +48,7 @@ func runPullsCreate(cmd *cli.Context) error { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return task.CreatePull( | 	return task.CreatePull( | ||||||
| 		ctx.Login, | 		ctx, | ||||||
| 		ctx.Owner, |  | ||||||
| 		ctx.Repo, |  | ||||||
| 		ctx.String("base"), | 		ctx.String("base"), | ||||||
| 		ctx.String("head"), | 		ctx.String("head"), | ||||||
| 		opts, | 		opts, | ||||||
|   | |||||||
| @@ -6,26 +6,23 @@ package interact | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"code.gitea.io/sdk/gitea" | 	"code.gitea.io/sdk/gitea" | ||||||
| 	"code.gitea.io/tea/modules/config" | 	"code.gitea.io/tea/modules/context" | ||||||
| 	"code.gitea.io/tea/modules/git" |  | ||||||
| 	"code.gitea.io/tea/modules/task" | 	"code.gitea.io/tea/modules/task" | ||||||
|  |  | ||||||
| 	"github.com/AlecAivazis/survey/v2" | 	"github.com/AlecAivazis/survey/v2" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // CreatePull interactively creates a PR | // CreatePull interactively creates a PR | ||||||
| func CreatePull(login *config.Login, owner, repo string) error { | func CreatePull(ctx *context.TeaContext) (err error) { | ||||||
| 	var base, head string | 	var base, head string | ||||||
|  |  | ||||||
| 	// owner, repo | 	// owner, repo | ||||||
| 	owner, repo, err := promptRepoSlug(owner, repo) | 	if ctx.Owner, ctx.Repo, err = promptRepoSlug(ctx.Owner, ctx.Repo); err != nil { | ||||||
| 	if err != nil { |  | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// base | 	// base | ||||||
| 	base, err = task.GetDefaultPRBase(login, owner, repo) | 	if base, err = task.GetDefaultPRBase(ctx.Login, ctx.Owner, ctx.Repo); err != nil { | ||||||
| 	if err != nil { |  | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	promptI := &survey.Input{Message: "Target branch:", Default: base} | 	promptI := &survey.Input{Message: "Target branch:", Default: base} | ||||||
| @@ -34,14 +31,14 @@ func CreatePull(login *config.Login, owner, repo string) error { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// head | 	// head | ||||||
| 	localRepo, err := git.RepoForWorkdir() | 	var headOwner, headBranch string | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	promptOpts := survey.WithValidator(survey.Required) | 	promptOpts := survey.WithValidator(survey.Required) | ||||||
| 	headOwner, headBranch, err := task.GetDefaultPRHead(localRepo) |  | ||||||
| 	if err == nil { | 	if ctx.LocalRepo != nil { | ||||||
| 		promptOpts = nil | 		headOwner, headBranch, err = task.GetDefaultPRHead(ctx.LocalRepo) | ||||||
|  | 		if err == nil { | ||||||
|  | 			promptOpts = nil | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 	promptI = &survey.Input{Message: "Source repo owner:", Default: headOwner} | 	promptI = &survey.Input{Message: "Source repo owner:", Default: headOwner} | ||||||
| 	if err := survey.AskOne(promptI, &headOwner); err != nil { | 	if err := survey.AskOne(promptI, &headOwner); err != nil { | ||||||
| @@ -52,17 +49,15 @@ func CreatePull(login *config.Login, owner, repo string) error { | |||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	head = task.GetHeadSpec(headOwner, headBranch, owner) | 	head = task.GetHeadSpec(headOwner, headBranch, ctx.Owner) | ||||||
|  |  | ||||||
| 	opts := gitea.CreateIssueOption{Title: task.GetDefaultPRTitle(head)} | 	opts := gitea.CreateIssueOption{Title: task.GetDefaultPRTitle(head)} | ||||||
| 	if err = promptIssueProperties(login, owner, repo, &opts); err != nil { | 	if err = promptIssueProperties(ctx.Login, ctx.Owner, ctx.Repo, &opts); err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return task.CreatePull( | 	return task.CreatePull( | ||||||
| 		login, | 		ctx, | ||||||
| 		owner, |  | ||||||
| 		repo, |  | ||||||
| 		base, | 		base, | ||||||
| 		head, | 		head, | ||||||
| 		&opts) | 		&opts) | ||||||
|   | |||||||
| @@ -10,22 +10,17 @@ import ( | |||||||
|  |  | ||||||
| 	"code.gitea.io/sdk/gitea" | 	"code.gitea.io/sdk/gitea" | ||||||
| 	"code.gitea.io/tea/modules/config" | 	"code.gitea.io/tea/modules/config" | ||||||
|  | 	"code.gitea.io/tea/modules/context" | ||||||
| 	local_git "code.gitea.io/tea/modules/git" | 	local_git "code.gitea.io/tea/modules/git" | ||||||
| 	"code.gitea.io/tea/modules/print" | 	"code.gitea.io/tea/modules/print" | ||||||
| 	"code.gitea.io/tea/modules/utils" | 	"code.gitea.io/tea/modules/utils" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // CreatePull creates a PR in the given repo and prints the result | // CreatePull creates a PR in the given repo and prints the result | ||||||
| func CreatePull(login *config.Login, repoOwner, repoName, base, head string, opts *gitea.CreateIssueOption) error { | func CreatePull(ctx *context.TeaContext, base, head string, opts *gitea.CreateIssueOption) (err error) { | ||||||
| 	// open local git repo |  | ||||||
| 	localRepo, err := local_git.RepoForWorkdir() |  | ||||||
| 	if err != nil { |  | ||||||
| 		return fmt.Errorf("Could not open local repo: %s", err) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	// default is default branch | 	// default is default branch | ||||||
| 	if len(base) == 0 { | 	if len(base) == 0 { | ||||||
| 		base, err = GetDefaultPRBase(login, repoOwner, repoName) | 		base, err = GetDefaultPRBase(ctx.Login, ctx.Owner, ctx.Repo) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return err | 			return err | ||||||
| 		} | 		} | ||||||
| @@ -33,12 +28,15 @@ func CreatePull(login *config.Login, repoOwner, repoName, base, head string, opt | |||||||
|  |  | ||||||
| 	// default is current one | 	// default is current one | ||||||
| 	if len(head) == 0 { | 	if len(head) == 0 { | ||||||
| 		headOwner, headBranch, err := GetDefaultPRHead(localRepo) | 		if ctx.LocalRepo == nil { | ||||||
|  | 			return fmt.Errorf("no local git repo detected, please specify head branch") | ||||||
|  | 		} | ||||||
|  | 		headOwner, headBranch, err := GetDefaultPRHead(ctx.LocalRepo) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return err | 			return err | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		head = GetHeadSpec(headOwner, headBranch, repoOwner) | 		head = GetHeadSpec(headOwner, headBranch, ctx.Owner) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// head & base may not be the same | 	// head & base may not be the same | ||||||
| @@ -52,10 +50,10 @@ func CreatePull(login *config.Login, repoOwner, repoName, base, head string, opt | |||||||
| 	} | 	} | ||||||
| 	// title is required | 	// title is required | ||||||
| 	if len(opts.Title) == 0 { | 	if len(opts.Title) == 0 { | ||||||
| 		return fmt.Errorf("Title is required") | 		return fmt.Errorf("title is required") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	pr, _, err := login.Client().CreatePullRequest(repoOwner, repoName, gitea.CreatePullRequestOption{ | 	pr, _, err := ctx.Login.Client().CreatePullRequest(ctx.Owner, ctx.Repo, gitea.CreatePullRequestOption{ | ||||||
| 		Head:      head, | 		Head:      head, | ||||||
| 		Base:      base, | 		Base:      base, | ||||||
| 		Title:     opts.Title, | 		Title:     opts.Title, | ||||||
| @@ -67,7 +65,7 @@ func CreatePull(login *config.Login, repoOwner, repoName, base, head string, opt | |||||||
| 	}) | 	}) | ||||||
|  |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return fmt.Errorf("Could not create PR from %s to %s:%s: %s", head, repoOwner, base, err) | 		return fmt.Errorf("could not create PR from %s to %s:%s: %s", head, ctx.Owner, base, err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	print.PullDetails(pr, nil, nil) | 	print.PullDetails(pr, nil, nil) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Norwin
					Norwin