From af2830f3dfe7d9aa92b4c90f8ad32539d24ded5e Mon Sep 17 00:00:00 2001 From: Alisher Nurmanov Date: Tue, 9 Sep 2025 11:30:03 +0500 Subject: [PATCH] still prints my email twice on `qs pr` on Jira task --- gitcmds/gitcmds.go | 64 ++++++++++++++++++++++++++++++---------------- gitcmds/pr.go | 60 ++++++++++++++++++++++++------------------- 2 files changed, 76 insertions(+), 48 deletions(-) diff --git a/gitcmds/gitcmds.go b/gitcmds/gitcmds.go index e4d58ed..79945bb 100644 --- a/gitcmds/gitcmds.go +++ b/gitcmds/gitcmds.go @@ -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) @@ -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) @@ -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 +} diff --git a/gitcmds/pr.go b/gitcmds/pr.go index 51bfd96..bcc0665 100644 --- a/gitcmds/pr.go +++ b/gitcmds/pr.go @@ -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" ) @@ -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]?") @@ -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) } @@ -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 } @@ -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) @@ -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) @@ -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