Skip to content

Commit

Permalink
Add FX to manage DI
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz committed Apr 3, 2023
1 parent de06a20 commit 754c2a7
Show file tree
Hide file tree
Showing 21 changed files with 380 additions and 266 deletions.
24 changes: 18 additions & 6 deletions cmd/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ package cmd

import "github.com/spf13/cobra"

var prCmd = &cobra.Command{
Use: "pr",
Short: "Manage pull requests",
Long: `Manage pull requests`,
type PrCmd struct {
cmd *cobra.Command
}

func init() {
rootCmd.AddCommand(prCmd)
func (p PrCmd) GetCommand() *cobra.Command {
return p.cmd
}

func NewPrCmd(subcommands []Command) PrCmd {
cmd := &cobra.Command{
Use: "pr",
Short: "Manage pull requests",
Long: `Manage pull requests`,
}

for _, subcommand := range subcommands {
cmd.AddCommand(GetCommand(subcommand))
}

return PrCmd{cmd: cmd}
}
217 changes: 125 additions & 92 deletions cmd/pr_merge.go
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
}
Loading

0 comments on commit 754c2a7

Please sign in to comment.