Skip to content

Commit

Permalink
modified cmd/pr and internal/pr
Browse files Browse the repository at this point in the history
  • Loading branch information
craftamap committed Oct 18, 2020
1 parent 857ee19 commit 3107b65
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
10 changes: 7 additions & 3 deletions cmd/pr.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package cmd

import (
"fmt"

"github.com/craftamap/bb/internal"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
)

var (
prCommand = cobra.Command{
Use: "pr",
Run: func(cmd *cobra.Command, args []string) {
internal.PrList(globalOpts.Username, globalOpts.Password, globalOpts.RepoOrga, globalOpts.RepoSlug)
},
}
prListCommand = cobra.Command{
Use: "list",
Expand All @@ -34,6 +34,10 @@ func init() {
}

func list(cmd *cobra.Command, args []string) {
err := internal.PrList(globalOpts.Username, globalOpts.Password, globalOpts.RepoOrga, globalOpts.RepoSlug)
if err != nil {
fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), err)
}
}

func view(cmd *cobra.Command, args []string) {
Expand Down
51 changes: 44 additions & 7 deletions internal/pr.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,68 @@
package internal

import (
"errors"
"fmt"

"github.com/ktrysmt/go-bitbucket"
"github.com/logrusorgru/aurora"
)

func PrList(username string, password string, repoOrga string, repoSlug string) {
func PrList(username string, password string, repoOrga string, repoSlug string) error {

client := bitbucket.NewBasicAuth(username, password)

opt := &bitbucket.PullRequestsOptions{
Owner: repoOrga,
RepoSlug: repoSlug,
}

response, _ := client.Repositories.PullRequests.Gets(opt)
mapResponse := response.(map[string]interface{})
values := mapResponse["values"].([]interface{})
response, err := client.Repositories.PullRequests.Gets(opt)
if err != nil {
return err
}
mapResponse, ok := response.(map[string]interface{})
if !ok {
return errors.New("type assertion failed")
}
values, ok := mapResponse["values"].([]interface{})
if !ok {
return errors.New("type assertion failed")
}

fmt.Println()
fmt.Printf("%s Showing %d of %d open pull requests in %s/%s\n", aurora.Blue(" :: "), len(values), int(mapResponse["size"].(float64)), repoOrga, repoSlug)
fmt.Println()
for _, pr := range values {
mapPr := pr.(map[string]interface{})
sourceName := mapPr["source"].(map[string]interface{})["branch"].(map[string]interface{})["name"]
destName := mapPr["destination"].(map[string]interface{})["branch"].(map[string]interface{})["name"]
mapPr, ok := pr.(map[string]interface{})
if !ok {
return errors.New("type assertion failed")
}
source, ok := mapPr["source"].(map[string]interface{})
if !ok {
return errors.New("type assertion failed")
}

sourceBranch, ok := source["branch"].(map[string]interface{})
if !ok {
return errors.New("type assertion failed")
}

sourceName := sourceBranch["name"]

dest, ok := mapPr["destination"].(map[string]interface{})
if !ok {
return errors.New("type assertion failed")
}

destBranch, ok := dest["branch"].(map[string]interface{})
if !ok {
return errors.New("type assertion failed")
}

destName := destBranch["name"]
fmt.Printf("#%03d %s %s -> %s\n", aurora.Green(int(mapPr["id"].(float64))), mapPr["title"].(string), sourceName, destName)
}

return nil
}

0 comments on commit 3107b65

Please # to comment.