From 5da86e07e7536db79890040778c7be6fc3b1ac04 Mon Sep 17 00:00:00 2001 From: Heath Stewart Date: Sun, 8 Dec 2024 17:52:58 -0800 Subject: [PATCH] Merge changes from #10004 Merges changes from @williammartin including acceptance tests and word changes. Co-authored-by: William Martin --- .../pr-create-from-issue-develop-base.txtar | 37 +++++++++++++++++++ .../pr/pr-create-from-manual-merge-base.txtar | 34 +++++++++++++++++ git/client.go | 2 +- pkg/cmd/issue/develop/develop.go | 7 ++++ pkg/cmd/pr/create/create.go | 4 ++ 5 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 acceptance/testdata/pr/pr-create-from-issue-develop-base.txtar create mode 100644 acceptance/testdata/pr/pr-create-from-manual-merge-base.txtar diff --git a/acceptance/testdata/pr/pr-create-from-issue-develop-base.txtar b/acceptance/testdata/pr/pr-create-from-issue-develop-base.txtar new file mode 100644 index 00000000000..f0619940e06 --- /dev/null +++ b/acceptance/testdata/pr/pr-create-from-issue-develop-base.txtar @@ -0,0 +1,37 @@ +# Set up env vars +env REPO=${SCRIPT_NAME}-${RANDOM_STRING} + +# Use gh as a credential helper +exec gh auth setup-git + +# Create a repository with a file so it has a default branch +exec gh repo create ${ORG}/${REPO} --add-readme --private + +# Defer repo cleanup +defer gh repo delete --yes ${ORG}/${REPO} + +# Clone the repo +exec gh repo clone ${ORG}/${REPO} + +# Create a branch to act as the merge base branch +cd ${REPO} +exec git checkout -b long-lived-feature-branch +exec git push -u origin long-lived-feature-branch + +# Create an issue to develop against +exec gh issue create --title 'Feature Request' --body 'Request Body' +stdout2env ISSUE_URL + +# Create a new branch using issue develop with the long lived branch as the base +exec gh issue develop --name 'feature-branch' --base 'long-lived-feature-branch' --checkout ${ISSUE_URL} + +# Prepare a PR on the develop branch +exec git commit --allow-empty -m 'Empty Commit' +exec git push -u origin feature-branch + +# Create the PR +exec gh pr create --title 'Feature Title' --body 'Feature Body' + +# Check the PR is created against the base branch we specified +exec gh pr view --json 'baseRefName' --jq '.baseRefName' +stdout 'long-lived-feature-branch' diff --git a/acceptance/testdata/pr/pr-create-from-manual-merge-base.txtar b/acceptance/testdata/pr/pr-create-from-manual-merge-base.txtar new file mode 100644 index 00000000000..97ae168f539 --- /dev/null +++ b/acceptance/testdata/pr/pr-create-from-manual-merge-base.txtar @@ -0,0 +1,34 @@ +# Set up env vars +env REPO=${SCRIPT_NAME}-${RANDOM_STRING} + +# Use gh as a credential helper +exec gh auth setup-git + +# Create a repository with a file so it has a default branch +exec gh repo create ${ORG}/${REPO} --add-readme --private + +# Defer repo cleanup +defer gh repo delete --yes ${ORG}/${REPO} + +# Clone the repo +exec gh repo clone ${ORG}/${REPO} + +# Create a branch to act as the merge base branch +cd ${REPO} +exec git checkout -b long-lived-feature-branch +exec git push -u origin long-lived-feature-branch + +# Prepare a branch from the merge base to PR +exec git checkout -b feature-branch +exec git commit --allow-empty -m 'Empty Commit' +exec git push -u origin feature-branch + +# Set the merge-base branch config +exec git config 'branch.feature-branch.gh-merge-base' 'long-lived-feature-branch' + +# Create the PR +exec gh pr create --title 'Feature Title' --body 'Feature Body' + +# Check the PR is created against the merge base branch +exec gh pr view --json 'baseRefName' --jq '.baseRefName' +stdout 'long-lived-feature-branch' diff --git a/git/client.go b/git/client.go index 43568839afa..35b9cf16cdc 100644 --- a/git/client.go +++ b/git/client.go @@ -376,7 +376,7 @@ func (c *Client) lookupCommit(ctx context.Context, sha, format string) ([]byte, return out, nil } -// ReadBranchConfig parses the `branch.BRANCH.(remote|merge)` part of git config. +// ReadBranchConfig parses the `branch.BRANCH.(remote|merge|gh-merge-base)` part of git config. func (c *Client) ReadBranchConfig(ctx context.Context, branch string) (cfg BranchConfig) { prefix := regexp.QuoteMeta(fmt.Sprintf("branch.%s.", branch)) args := []string{"config", "--get-regexp", fmt.Sprintf("^%s(remote|merge|%s)$", prefix, MergeBaseConfig)} diff --git a/pkg/cmd/issue/develop/develop.go b/pkg/cmd/issue/develop/develop.go index bbffa60acea..1536800f072 100644 --- a/pkg/cmd/issue/develop/develop.go +++ b/pkg/cmd/issue/develop/develop.go @@ -44,6 +44,13 @@ func NewCmdDevelop(f *cmdutil.Factory, runF func(*DevelopOptions) error) *cobra. cmd := &cobra.Command{ Use: "develop { | }", Short: "Manage linked branches for an issue", + Long: heredoc.Docf(` + Manage linked branches for an issue. + + When using the %[1]s--base%[1]s flag, the new development branch will be created from the specified + remote branch. The new branch will be configured as the base branch for pull requests created using + %[1]sgh pr create%[1]s. + `, "`"), Example: heredoc.Doc(` # List branches for issue 123 $ gh issue develop --list 123 diff --git a/pkg/cmd/pr/create/create.go b/pkg/cmd/pr/create/create.go index 2074a6f59c1..f3bd1287025 100644 --- a/pkg/cmd/pr/create/create.go +++ b/pkg/cmd/pr/create/create.go @@ -119,6 +119,10 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co alongside %[1]s--fill%[1]s, the values specified by %[1]s--title%[1]s and/or %[1]s--body%[1]s will take precedence and overwrite any autofilled content. + The base branch for the created PR can be specified using the %[1]s--base%[1]s flag. If not provided, + the value of %[1]sgh-merge-base%[1]s git branch config will be used. If not configured, the repository's + default branch will be used. + Link an issue to the pull request by referencing the issue in the body of the pull request. If the body text mentions %[1]sFixes #123%[1]s or %[1]sCloses #123%[1]s, the referenced issue will automatically get closed when the pull request gets merged.