Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 42 additions & 22 deletions gitcmds/gitcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2299,26 +2299,21 @@ func GetBodyFromNotes(notes []string) string {
return b
}

func createPR(wd, parentRepoName, prBranchName string, notes []string, asDraft bool) (stdout string, stderr string, err error) {
func createPR(
wd,
parentRepoName,
prBranchName,
issueDescription string,
notes []string,
asDraft bool,
) (stdout string, stderr string, err error) {
if len(notes) == 0 {
return "", "", errors.New(ErrMsgPRNotesImpossible)
}

//ParseGitRemoteURL()
// get json notes object from dev branch
notesObj, ok := notesPkg.Deserialize(notes)
if !ok {
return "", "", errors.New("error deserializing notes")
}

var prTitle string
var isCustomBranch bool
switch {
case len(notesObj.GithubIssueURL) > 0:
prTitle, err = GetIssueDescription(notesObj.GithubIssueURL)
case len(notesObj.JiraTicketURL) > 0:
prTitle, err = jira.GetJiraIssueName(notesObj.JiraTicketURL, "")
default:
prTitle := issueDescription
if len(prTitle) == 0 {
isCustomBranch = true
fmt.Print("Enter pull request title: ")
reader := bufio.NewReader(os.Stdin)
Expand All @@ -2334,19 +2329,16 @@ func createPR(wd, parentRepoName, prBranchName string, notes []string, asDraft b
return "", "", errors.New("too short pull request title")
}
}
if err != nil {
return "", "", fmt.Errorf("error retrieving pull request title: %w", err)
}

var strNotes string
var url string
strNotes, url = GetNoteAndURL(notes)
var issueURL string
strNotes, issueURL = GetNoteAndURL(notes)
b := GetBodyFromNotes(notes)
if len(b) == 0 && !isCustomBranch {
b = strNotes
}
if len(url) > 0 {
b = b + caret + url
if len(issueURL) > 0 {
b = b + caret + issueURL
}
strBody := fmt.Sprintln(b)

Expand Down Expand Up @@ -3035,3 +3027,31 @@ func printLn(stdout string) {
fmt.Println(stdout)
}
}

func getIssueDescription(notes []string) (string, error) {
var (
description string
err error
)

if len(notes) == 0 {
return "", err
}

notesObj, ok := notesPkg.Deserialize(notes)
if !ok {
return "", nil
}

switch {
case len(notesObj.GithubIssueURL) > 0:
description, err = GetIssueDescription(notesObj.GithubIssueURL)
case len(notesObj.JiraTicketURL) > 0:
description, err = jira.GetJiraIssueName(notesObj.JiraTicketURL, "")
}
if err != nil {
return "", fmt.Errorf("error retrieving issue description: %w", err)
}

return description, nil
}
60 changes: 34 additions & 26 deletions gitcmds/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/untillpro/goutils/exec"
"github.com/untillpro/goutils/logger"
"github.com/untillpro/qs/internal/helper"
"github.com/untillpro/qs/internal/jira"
notesPkg "github.com/untillpro/qs/internal/notes"
)

Expand Down Expand Up @@ -41,18 +40,28 @@ func Pr(wd string, needDraft bool) error {
return err
}

// Fetch notes from origin before checking if they exist
_, _, err = new(exec.PipedExec).
Command(git, fetch, origin, "--force", refsNotes).
WorkingDir(wd).
RunToStrings()
if err != nil {
logger.Verbose(fmt.Sprintf("Failed to fetch notes: %v", err))
// Continue anyway, as notes might exist locally
}

notes, revCount, err := GetNotes(wd, currentBranchName)
if err != nil {
return err
}

issueDescription, err := getIssueDescription(notes)
if err != nil {
return err
}

// If we are on dev branch than we need to create pr branch
if branchType == notesPkg.BranchTypeDev {
// Fetch notes from origin before checking if they exist
_, _, err := new(exec.PipedExec).
Command(git, fetch, origin, "--force", refsNotes).
WorkingDir(wd).
RunToStrings()
if err != nil {
logger.Verbose(fmt.Sprintf("Failed to fetch notes: %v", err))
// Continue anyway, as notes might exist locally
}

var response string
if len(parentRepoName) > 0 && UpstreamNotExist(wd) {
fmt.Print("Upstream not found.\nRepository " + parentRepoName + " will be added as upstream. Agree[y/n]?")
Expand All @@ -77,7 +86,7 @@ func Pr(wd string, needDraft bool) error {
}

// Create a new branch for the PR
prBranchName, err := createPRBranch(wd, currentBranchName)
prBranchName, err := createPRBranch(wd, currentBranchName, issueDescription)
if err != nil {
return fmt.Errorf("failed to create PR branch: %w", err)
}
Expand Down Expand Up @@ -108,7 +117,7 @@ func Pr(wd string, needDraft bool) error {
}

// Extract notes before any operations
notes, revCount, err := GetNotes(wd, currentBranchName)
notes, revCount, err = GetNotes(wd, currentBranchName)
if err != nil {
return err
}
Expand All @@ -118,7 +127,14 @@ func Pr(wd string, needDraft bool) error {
}

// Create PR
stdout, stderr, err := createPR(wd, parentRepoName, currentBranchName, notes, needDraft)
stdout, stderr, err := createPR(
wd,
parentRepoName,
currentBranchName,
issueDescription,
notes,
needDraft,
)
if err != nil {
logger.Verbose(stdout)
logger.Verbose(stderr)
Expand Down Expand Up @@ -304,7 +320,7 @@ func RemoveBranch(wd, branchName string) error {
// Returns:
// - name of the PR branch
// - error if any operation fails
func createPRBranch(wd, devBranchName string) (string, error) {
func createPRBranch(wd, devBranchName, issueDescription string) (string, error) {
// Step 1: Get issue description from notes for a commit message
// extract notes from the dev branch before any operations
notes, revCount, err := GetNotes(wd, devBranchName)
Expand All @@ -319,17 +335,9 @@ func createPRBranch(wd, devBranchName string) (string, error) {
// update branch type in notes object
notesObj.BranchType = notesPkg.BranchTypePr

var description string
switch {
case len(notesObj.GithubIssueURL) > 0:
description, err = GetIssueDescription(notesObj.GithubIssueURL)
case len(notesObj.JiraTicketURL) > 0:
description, err = jira.GetJiraIssueName(notesObj.JiraTicketURL, "")
default:
description = DefaultCommitMessage
}
if err != nil {
return "", fmt.Errorf("error retrieving issue description: %w", err)
description := DefaultCommitMessage
if len(issueDescription) > 0 {
description = issueDescription
}

// Step 2: Generate a name for the PR branch
Expand Down
Loading