-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
4 changed files
with
187 additions
and
45 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
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,68 +1,130 @@ | ||
package internal | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/ktrysmt/go-bitbucket" | ||
"github.com/logrusorgru/aurora" | ||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
func PrList(username string, password string, repoOrga string, repoSlug string) error { | ||
type ListPullRequests struct { | ||
Size int `mapstructure:"size"` | ||
Page int `mapstructure:"page"` | ||
PageLen int `mapstructure:"pagelen"` | ||
Next string `mapstructure:"next"` | ||
Previous string `mapstructure:"previous"` | ||
Values []PullRequest `mapstructure:"values"` | ||
} | ||
|
||
type PullRequest struct { | ||
ID int `mapstructure:"id"` | ||
Title string `mapstructure:"title"` | ||
State string `mapstructure:"state"` | ||
Source Resource `mapstructure:"source"` | ||
Destination Resource `mapstructure:"destination"` | ||
Type string `mapstructure:"type"` | ||
TaskCount int `mapstructure:"task_count"` | ||
Description string `mapstructure:"description"` | ||
Author User `mapstructure:"author"` | ||
CloseSourceBranch bool `mapstructure:"close_source_branch"` | ||
CommentCount int `mapstructure:"comment_count"` | ||
CreatedOn string `mapstructure:"created_on"` | ||
MergeCommit Commit `mapstructure:"merge_commit"` | ||
} | ||
|
||
type Resource struct { | ||
Branch Branch `mapstructure:"branch"` | ||
Commit Commit `mapstructure:"commit"` | ||
Repository Repository `mapstructure:"repository"` | ||
} | ||
|
||
type Branch struct { | ||
Name string `mapstructure:"name"` | ||
} | ||
|
||
type Commit struct { | ||
Hash string `mapstructure:"hash"` | ||
Type string `mapstructure:"type"` | ||
} | ||
|
||
type Repository struct { | ||
FullName string `mapstructure:"full_name"` | ||
Name string `mapstructure:"name"` | ||
Type string `mapstructure:"type"` | ||
UUID string `mapstructure:"uuid"` | ||
} | ||
|
||
type User struct { | ||
AccountID string `mapstructure:"account_id"` | ||
DisplayName string `mapstructure:"display_name"` | ||
Nickname string `mapstructure:"nickname"` | ||
Type string `mapstructure:"user"` | ||
UUID string `mapstructure:"uuid"` | ||
} | ||
|
||
func PrList(username string, password string, repoOrga string, repoSlug string) (*ListPullRequests, error) { | ||
client := bitbucket.NewBasicAuth(username, password) | ||
|
||
opt := &bitbucket.PullRequestsOptions{ | ||
Owner: repoOrga, | ||
RepoSlug: repoSlug, | ||
} | ||
|
||
response, err := client.Repositories.PullRequests.Gets(opt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var pullRequests ListPullRequests | ||
err = mapstructure.Decode(response, &pullRequests) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &pullRequests, nil | ||
} | ||
|
||
func GetPrIDBySourceBranch(username string, password string, repoOrga string, repoSlug string, sourceBranch string) (*ListPullRequests, error) { | ||
client := bitbucket.NewBasicAuth(username, password) | ||
|
||
opt := &bitbucket.PullRequestsOptions{ | ||
Owner: repoOrga, | ||
RepoSlug: repoSlug, | ||
Query: fmt.Sprintf("source.branch.name = \"%s\"", sourceBranch), | ||
} | ||
|
||
response, err := client.Repositories.PullRequests.Gets(opt) | ||
if err != nil { | ||
return err | ||
return nil, err | ||
} | ||
mapResponse, ok := response.(map[string]interface{}) | ||
if !ok { | ||
return errors.New("type assertion failed") | ||
|
||
var pullRequests ListPullRequests | ||
err = mapstructure.Decode(response, &pullRequests) | ||
if err != nil { | ||
return nil, err | ||
} | ||
values, ok := mapResponse["values"].([]interface{}) | ||
if !ok { | ||
return errors.New("type assertion failed") | ||
|
||
return &pullRequests, nil | ||
} | ||
|
||
func PrView(username string, password string, repoOrga string, repoSlug string, id string) (*PullRequest, error) { | ||
client := bitbucket.NewBasicAuth(username, password) | ||
|
||
opt := &bitbucket.PullRequestsOptions{ | ||
Owner: repoOrga, | ||
RepoSlug: repoSlug, | ||
ID: id, | ||
} | ||
|
||
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, 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) | ||
response, err := client.Repositories.PullRequests.Get(opt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return nil | ||
var pullRequest PullRequest | ||
err = mapstructure.Decode(response, &pullRequest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &pullRequest, nil | ||
} |