-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de06a20
commit 754c2a7
Showing
21 changed files
with
380 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,166 @@ | ||
package cmd | ||
|
||
// | ||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/SyliusLabs/gh-kit/internal/generator" | ||
"github.com/SyliusLabs/gh-kit/internal/githubcli" | ||
"github.com/SyliusLabs/gh-kit/internal/github" | ||
"github.com/manifoldco/promptui" | ||
"github.com/spf13/cobra" | ||
"strconv" | ||
) | ||
|
||
var allowedMergeStrategies = map[string]bool{"merge": true, "squash": true, "rebase": true} | ||
|
||
var mergeCmd = &cobra.Command{ | ||
Use: "merge <pull_request_number>", | ||
Short: "Merge a pull request", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
prNumber, err := strconv.Atoi(args[0]) | ||
if nil != err { | ||
fmt.Println("π The pull request number is not a valid integer") | ||
return | ||
} | ||
pr, prErr := ghClient.GetPullRequest(prNumber) | ||
if nil != prErr { | ||
fmt.Println(prErr.Error()) | ||
return | ||
} | ||
type PrMergeCmd struct { | ||
cmd *cobra.Command | ||
ghClient *github.Client | ||
ghCli *github.Cli | ||
} | ||
|
||
func (c PrMergeCmd) GetCommand() *cobra.Command { | ||
return c.cmd | ||
} | ||
|
||
if pr.GetMerged() { | ||
fmt.Printf( | ||
"βΉοΈ The pull request #%d on %s/%s is already merged by %s\r\n", | ||
pr.GetNumber(), | ||
ghClient.Repository.Owner(), | ||
ghClient.Repository.Name(), | ||
pr.GetMergedBy().GetLogin(), | ||
) | ||
return | ||
func (c *PrMergeCmd) RunE(cmd *cobra.Command, args []string) error { | ||
prNumber, err := strconv.Atoi(args[0]) | ||
if nil != err { | ||
return errors.New("π The pull request number is not a valid integer") | ||
} | ||
|
||
pr, prErr := c.ghClient.GetPullRequest(prNumber) | ||
if nil != prErr { | ||
errMsg := fmt.Sprintf( | ||
"π The pull request #%d on %s/%s seems to not exist", | ||
prNumber, | ||
c.ghClient.Repository.Owner(), | ||
c.ghClient.Repository.Name(), | ||
) | ||
return errors.New(errMsg) | ||
} | ||
|
||
if pr.GetMerged() { | ||
errMsg := fmt.Sprintf( | ||
"βΉοΈ The pull request #%d on %s/%s is already merged by %s\r\n", | ||
pr.GetNumber(), | ||
c.ghClient.Repository.Owner(), | ||
c.ghClient.Repository.Name(), | ||
pr.GetMergedBy().GetLogin(), | ||
) | ||
return errors.New(errMsg) | ||
} | ||
|
||
mergeStrategy, _ := cmd.Flags().GetString("strategy") | ||
if "" == mergeStrategy { | ||
strategyPrompt := promptui.Select{ | ||
Label: "Select merge strategy", | ||
Items: []string{"merge", "squash", "rebase"}, | ||
} | ||
|
||
mergeStrategy, _ := cmd.Flags().GetString("strategy") | ||
_, mergeStrategy, _ = strategyPrompt.Run() | ||
} | ||
|
||
if "" == mergeStrategy { | ||
strategyPrompt := promptui.Select{ | ||
Label: "Select merge strategy", | ||
Items: []string{"merge", "squash", "rebase"}, | ||
} | ||
if !allowedMergeStrategies[mergeStrategy] || "" == mergeStrategy { | ||
return errors.New("π The merge strategy is not allowed or is empty. Allowed strategies are: merge, squash, rebase") | ||
} | ||
|
||
_, mergeStrategy, _ = strategyPrompt.Run() | ||
} | ||
mergeCategory, _ := cmd.Flags().GetString("category") | ||
|
||
if !allowedMergeStrategies[mergeStrategy] || "" == mergeStrategy { | ||
fmt.Println("π The merge strategy is not allowed or is empty. Allowed strategies are: merge, squash, rebase") | ||
return | ||
if "" == mergeCategory { | ||
categoryPrompt := promptui.Select{ | ||
Label: "Select merge category", | ||
Items: []string{"minor", "feature", "bugfix", "docs", "refactor", "test", "chore"}, | ||
} | ||
|
||
mergeCategory, _ := cmd.Flags().GetString("category") | ||
_, mergeCategory, _ = categoryPrompt.Run() | ||
} | ||
|
||
if "" == mergeCategory { | ||
categoryPrompt := promptui.Select{ | ||
Label: "Select merge category", | ||
Items: []string{"minor", "feature", "bugfix", "docs", "refactor", "test", "chore"}, | ||
} | ||
if "" == mergeCategory { | ||
return errors.New("β οΈ The Pull Request category must be provided") | ||
} | ||
|
||
_, mergeCategory, _ = categoryPrompt.Run() | ||
} | ||
mergeCategory, _ = cmd.Flags().GetString("category") | ||
|
||
if "" == mergeCategory { | ||
fmt.Println("β οΈ The Pull Request category must be provided.") | ||
return | ||
if "" == mergeCategory { | ||
categoryPrompt := promptui.Select{ | ||
Label: "Select merge category", | ||
Items: []string{"minor", "feature", "bugfix", "docs", "refactor", "test", "chore"}, | ||
} | ||
|
||
mergeSubject := generator.GenerateCommitMessage(mergeCategory, pr.GetNumber(), pr.GetTitle(), pr.GetUser().GetLogin()) | ||
_, mergeCategory, _ = categoryPrompt.Run() | ||
} | ||
|
||
commits, commitsErr := ghClient.GetCommitsInPullRequest(pr.GetNumber()) | ||
if nil != commitsErr { | ||
fmt.Println("π The pull request commits could not be fetched") | ||
return | ||
} | ||
if "" == mergeCategory { | ||
return errors.New("β οΈ The Pull Request category must be provided") | ||
} | ||
|
||
mergeBody := generator.GenerateCommitBody(pr, commits) | ||
mergeSubject := generator.GenerateCommitMessage(mergeCategory, pr.GetNumber(), pr.GetTitle(), pr.GetUser().GetLogin()) | ||
|
||
summaryMsg := `The pull request #%d on %s/%s will be merged with the following parameters: | ||
- Subject: %s | ||
- Strategy: %s | ||
- Category: %s | ||
- Body: %s` | ||
fmt.Printf(summaryMsg, pr.GetNumber(), ghClient.Repository.Owner(), ghClient.Repository.Name(), mergeSubject, mergeStrategy, mergeCategory, mergeBody) | ||
commits, commitsErr := c.ghClient.GetCommitsInPullRequest(pr.GetNumber()) | ||
if nil != commitsErr { | ||
return errors.New("π The pull request commits could not be fetched") | ||
} | ||
|
||
confirmPrompt := promptui.Select{ | ||
Label: "Are you sure you want to merge it?", | ||
Items: []string{"yes", "no"}, | ||
} | ||
mergeBody := generator.GenerateCommitBody(pr, commits) | ||
|
||
_, confirmed, _ := confirmPrompt.Run() | ||
summaryMsg := `The pull request #%d on %s/%s will be merged with the following parameters: | ||
- Subject: %s | ||
- Strategy: %s | ||
- Category: %s | ||
- Body: %s` | ||
fmt.Printf(summaryMsg, pr.GetNumber(), c.ghClient.Repository.Owner(), c.ghClient.Repository.Name(), mergeSubject, mergeStrategy, mergeCategory, mergeBody) | ||
|
||
if "" == confirmed || "no" == confirmed { | ||
fmt.Println("π The pull request has not been merged") | ||
return | ||
} | ||
confirmPrompt := promptui.Select{ | ||
Label: "Are you sure you want to merge it?", | ||
Items: []string{"yes", "no"}, | ||
} | ||
|
||
mergeErr := githubcli.Merge(ghCli, pr.GetNumber(), mergeSubject, mergeBody, mergeStrategy) | ||
_, confirmed, _ := confirmPrompt.Run() | ||
|
||
if nil != mergeErr { | ||
fmt.Printf("π The pull request #%d could not be merged. Error message: %s\r\n", pr.GetNumber(), mergeErr.Error()) | ||
return | ||
} | ||
if "" == confirmed || "no" == confirmed { | ||
return errors.New("π The pull request has not been merged") | ||
} | ||
|
||
fmt.Printf("β The pull request #%d on %s/%s has been merged\r\n", pr.GetNumber(), ghClient.Repository.Owner(), ghClient.Repository.Name()) | ||
mergeErr := c.ghCli.Merge(pr.GetNumber(), mergeSubject, mergeBody, mergeStrategy) | ||
|
||
if noThankYou, _ := cmd.Flags().GetBool("skip-thankyou"); noThankYou { | ||
fmt.Println("β© The thank you comment has not been added") | ||
return | ||
} | ||
if nil != mergeErr { | ||
errMsg := fmt.Sprintf("π The pull request #%d could not be merged. Error message: %s\r\n", pr.GetNumber(), mergeErr.Error()) | ||
return errors.New(errMsg) | ||
} | ||
|
||
commentErr := ghClient.AddCommentToIssue(pr.GetNumber(), fmt.Sprintf("Thank you, @%s!", pr.GetUser().GetLogin())) | ||
if nil != commentErr { | ||
fmt.Printf("β οΈ The comment could not be added. Error message: %s\r\n", commentErr.Error()) | ||
return | ||
} | ||
fmt.Printf("β The pull request #%d on %s/%s has been merged\r\n", pr.GetNumber(), c.ghClient.Repository.Owner(), c.ghClient.Repository.Name()) | ||
|
||
fmt.Printf("β The thank you comment has been added to the pull request #%d\r\n", pr.GetNumber()) | ||
}, | ||
if noThankYou, _ := cmd.Flags().GetBool("skip-thankyou"); noThankYou { | ||
fmt.Println("β© The thank you comment has not been added") | ||
return nil | ||
} | ||
|
||
commentErr := c.ghClient.AddCommentToIssue(pr.GetNumber(), fmt.Sprintf("Thank you, @%s!", pr.GetUser().GetLogin())) | ||
if nil != commentErr { | ||
errMsg := fmt.Sprintf("β οΈ The comment could not be added. Error message: %s\r\n", commentErr.Error()) | ||
return errors.New(errMsg) | ||
} | ||
|
||
fmt.Printf("β The thank you comment has been added to the pull request #%d\r\n", pr.GetNumber()) | ||
return nil | ||
} | ||
|
||
func init() { | ||
prCmd.AddCommand(mergeCmd) | ||
func NewPrMergeCmd(ghClient *github.Client, ghCli *github.Cli) PrMergeCmd { | ||
cmd := &cobra.Command{ | ||
Use: "merge <pull_request_number>", | ||
Short: "Merge a pull request", | ||
Args: cobra.ExactArgs(1), | ||
} | ||
|
||
cmd.PersistentFlags().StringP("category", "c", "", "Category of the pull request") | ||
cmd.PersistentFlags().StringP("strategy", "s", "", "Merge strategy to be used") | ||
cmd.PersistentFlags().Bool("skip-thankyou", false, "Do not add a thank you comment to the pull request") | ||
|
||
prMergeCmd := new(PrMergeCmd) | ||
prMergeCmd.cmd = cmd | ||
prMergeCmd.cmd.RunE = prMergeCmd.RunE | ||
prMergeCmd.ghClient = ghClient | ||
prMergeCmd.ghCli = ghCli | ||
|
||
mergeCmd.PersistentFlags().StringP("category", "c", "", "Category of the pull request") | ||
mergeCmd.PersistentFlags().StringP("strategy", "s", "", "Merge strategy to be used") | ||
mergeCmd.PersistentFlags().Bool("skip-thankyou", false, "Do not add a thank you comment to the pull request") | ||
return *prMergeCmd | ||
} |
Oops, something went wrong.