Skip to content
This repository has been archived by the owner on Jun 14, 2019. It is now read-only.

Commit

Permalink
[github] Command pr, show by number, add reviewers (#14)
Browse files Browse the repository at this point in the history
* Command pr displays details for a github Pull Request when called with a numeric argument
* Command pr creates a Pull Request when called with no positional arguments
* version bump 0.2.3
* Display reviewers on show
* bugfix, deploy release branches, not status branches
* code review remarks
* update documentation
  • Loading branch information
dbaltas authored Oct 10, 2018
1 parent 89292b6 commit 7f8b16d
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func deployBranches(branchMap map[string]string, suffixFind, suffixReplace strin
green.Printf("Deployment will start in %s\n", d.String())
time.Sleep(d)

for i, branch := range branches {
for i, branch := range releaseBranches {
if i != 0 {
time.Sleep(intervalDuration)
t = t.Add(intervalDuration)
Expand Down Expand Up @@ -146,7 +146,7 @@ func deployBranches(branchMap map[string]string, suffixFind, suffixReplace strin
newBody := strings.Replace(*(release.Body), findText, replaceText, -1)
fmt.Println(newBody)
release.Body = &newBody
release, err = gc.EditRelease(release)
_, err = gc.EditRelease(release)
if err != nil {
return err
}
Expand Down
82 changes: 78 additions & 4 deletions cmd/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"os"
"strconv"
"strings"

"github.com/fatih/color"
Expand All @@ -13,20 +14,52 @@ import (
var compareBranch string
var title string
var description string
var number int
var reviewers string
var teamReviewers string

func init() {
rootCmd.AddCommand(prCmd)
prCmd.Flags().StringVar(&compareBranch, "compare", "", "The branch to compare with base branch. Defaults to current local branch.")
prCmd.Flags().StringVar(&title, "title", "", "The title of the PR.")
prCmd.Flags().StringVar(&reviewers, "reviewers", "", "Add reviewers.")
prCmd.Flags().StringVar(&teamReviewers, "teamReviewers", "", "Add a team as reviewers.")
prCmd.Flags().StringVar(&description, "description", "", "The description of the PR.")
prCmd.MarkFlagRequired("title")
}

var prCmd = &cobra.Command{
Use: "pr",
Short: "Create a pull request [github]",
Long: `Create a pull request on github from compare branch to base branch`,
Short: "Create or show a pull request [github]",
Long: `Create a pull request on github from compare branch to base branch
ergo pr --title "my new pull request --reviewers pespantelis,nstratos,mantzas"
Show details of a pr by pr number
ergo pr 18
Add reviewers to an existing pr
ergo pr 18 --reviewers pespantelis,nstratos,mantzas"
`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// show a PR by number
if len(args) > 0 {
number, err := strconv.Atoi(args[0])
if err != nil {
return err
}
err = getPR(number)
if err != nil {
return err
}

if reviewers != "" || teamReviewers != "" {
_, err = gc.RequestReviewersForPR(number, reviewers, teamReviewers)
if err != nil {
return err
}
}

return nil
}

return createPR()
},
}
Expand Down Expand Up @@ -59,12 +92,53 @@ func createPR() error {
}

pr, err := gc.CreatePR(baseBranch, compareBranch, title, description)

if err != nil {
return err
}

fmt.Printf("Created PR %s\n", *pr.HTMLURL)

if reviewers != "" || teamReviewers != "" {
_, err = gc.RequestReviewersForPR(pr.GetNumber(), reviewers, teamReviewers)
if err != nil {
return err
}
}

return nil
}

func getPR(number int) error {
yellow := color.New(color.FgYellow)
green := color.New(color.FgGreen)
red := color.New(color.FgRed)

prp, err := gc.GetPR(number)
if err != nil {
return err
}
pr := *prp

fmt.Println()
green.Printf("#%d: %s\n", pr.GetNumber(), pr.GetTitle())
fmt.Printf("into:%s from:%s\n", yellow.Sprint(pr.Base.GetLabel()), yellow.Sprint(pr.Head.GetLabel()))
if pr.GetBody() != "" {
yellow.Println(pr.GetBody())
}
fmt.Println()

fmt.Printf("%s: %d\n", yellow.Sprint("# Commits"), pr.GetCommits())
fmt.Printf("%s:%s, %s:%s, %s:%s\n",
yellow.Sprint("created"), pr.GetCreatedAt().Format("2006-01-02 15:04"),
yellow.Sprint("modified"), pr.GetUpdatedAt().Format("2006-01-02 15:04"),
yellow.Sprint("merged"), pr.GetMergedAt().Format("2006-01-02 15:04"),
)
fmt.Println()

a := green.Sprintf("%d", pr.GetAdditions())
d := red.Sprintf("%d", pr.GetDeletions())
c := yellow.Sprintf("%d", pr.GetChangedFiles())
fmt.Printf("%s files changed, %s additions, %s deletions\n", c, a, d)

return nil
}
22 changes: 16 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,36 @@ var rootCmd = &cobra.Command{
Long: `Ergo helps to
* compare multiple branches
* push to multiple branches with time interval (useful for multiple release environments)
Also it minimizing the browser interaction with github
* handles pull requests
* drafts a release
* updates release notes
* minimize the browser interaction with github:
* create/show a pull request
* list open pull requests
* add reviewers to a pull request
* draft a release
* update release notes
`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Hola! type `ergo help`")
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var err error

// commands not requiring a repo
noRepoCmds := make(map[string]bool)
// commands not requiring to fetch from a repo
skipFetchCmds := make(map[string]bool)

noRepoCmds["help"] = true
noRepoCmds["version"] = true
skipFetchCmds["pr"] = true
skipFetchCmds["prs"] = true

if _, ok := noRepoCmds[cmd.Name()]; ok {
return nil
}

if _, ok := skipFetchCmds[cmd.Name()]; ok {
skipFetch = true
}

err = initializeRepo()
if err != nil {
return fmt.Errorf("initialize repo: %v", err)
Expand Down Expand Up @@ -106,7 +117,6 @@ func initializeRepo() error {
} else {
r, err = repo.NewFromPath(path, viper.GetString("generic.remote"))
}

if err != nil {
return fmt.Errorf("load repo:%s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func init() {

var statusCmd = &cobra.Command{
Use: "status",
Short: "Print the status of branches compared to baseBranch",
Short: "Print the status of branches compared to base branch",
Long: `Prints the commits ahead and behind of status branches compared to a base branch`,
RunE: func(cmd *cobra.Command, args []string) error {
var diff []repo.DiffCommitBranch
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ var versionCmd = &cobra.Command{
Short: "Print the version of ergo",
Long: `Print the version of ergo`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("0.2.2")
fmt.Println("0.2.3")
},
}
28 changes: 25 additions & 3 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,39 @@ func (gc *Client) CreatePR(baseBranch, compareBranch, title, body string) (*gith
return pr, nil
}

// GetPR gets a pull request
func (gc *Client) GetPR(number int) (*github.PullRequest, error) {
pr, _, err := gc.client.PullRequests.Get(gc.ctx, gc.organization, gc.repo, number)
if err != nil {
return nil, err
}

return pr, nil
}

// RequestReviewersForPR assigns reviewers to a pull request
func (gc *Client) RequestReviewersForPR(number int, reviewers, teamReviewers string) (*github.PullRequest, error) {
payload := github.ReviewersRequest{
Reviewers: strings.Split(reviewers, ","),
TeamReviewers: strings.Split(teamReviewers, ","),
}
fmt.Println(github.Stringify(payload))
pr, _, err := gc.client.PullRequests.RequestReviewers(gc.ctx, gc.organization, gc.repo, number, payload)
if err != nil {
return nil, err
}

return pr, nil
}

// ListPRs creates a pull request
func (gc *Client) ListPRs() ([]*github.PullRequest, error) {
opt := &github.PullRequestListOptions{
Sort: "created",
Direction: "desc",
}

// pr, _, err := gc.client.PullRequests.Get(gc.ctx, gc.organization, gc.repo, 1)
// fmt.Print(pr)
pulls, _, err := gc.client.PullRequests.List(gc.ctx, gc.organization, gc.repo, opt)

if err != nil {
return nil, err
}
Expand Down
Binary file removed main
Binary file not shown.
16 changes: 14 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@ Ergo (έργο), greek name for work, is a list of utilities for the daily devel
# getting the status of repo branches compared to master
ergo status
```
![ergo sample output](ergo-status.png)
![ergo sample output](static/ergo-status.png)

```
# List open github pull requests for google/go-github repo
ergo prs
```
![go-github open prs](go-github-open-prs.png)
![go-github open prs](static/github-open-prs.png)

```
# Display details for github pull request #17
ergo pr 17
```
![go-github open prs](static/github-pr-details.png)

```
# Create a github pull request through command line
# In the screenshot the compare flag overrides the current branch
ergo pr --title 'my title' --reviewers johndoe,nstratos
```
![go-github open prs](static/github-create-pr.png)

## Installation
```
Expand Down
2 changes: 0 additions & 2 deletions repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
func (r *Repo) CurrentBranch() (string, error) {
cmd := fmt.Sprintf("cd %s && git rev-parse --abbrev-ref HEAD", r.path)
out, err := exec.Command("sh", "-c", cmd).Output()

if err != nil {
return "", errors.Wrap(err, "executing external command")
}
Expand Down Expand Up @@ -75,7 +74,6 @@ func commitsAhead(repo *git.Repository, branch string, commonAncestor string) ([
func mergeBase(branch1 string, branch2 string, directory string) (string, error) {
cmd := fmt.Sprintf("cd %s && git merge-base origin/%s origin/%s", directory, branch1, branch2)
out, err := exec.Command("sh", "-c", cmd).Output()

if err != nil {
return "", errors.Wrap(err, "executing external command")
}
Expand Down
2 changes: 0 additions & 2 deletions repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func NewClone(repoURL, path, remoteName string) (*Repo, error) {
URL: r.repoURL,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
})

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -113,7 +112,6 @@ func (r *Repo) GitRemote() *git.Remote {

func (r *Repo) setGitRemote() error {
remote, err := r.repo.Remote(r.remoteName)

if err != nil {
return fmt.Errorf("error loading remote %s:%v", r.remoteName, err)
}
Expand Down
2 changes: 0 additions & 2 deletions repo/repo_functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ func TestRepo(t *testing.T) {
path := "/tmp/ergo-functional-test-repo"
repoURL := "https://github.com/dbaltas/ergo-functional-test-repo.git"

// r := New(repoURL, path, "origin")
// cleanup after test run
defer func() {
err := os.RemoveAll(path)

if err != nil {
t.Fatalf("error cleaning up %s: %v", path, err)
}
Expand Down
File renamed without changes
Binary file added static/github-create-pr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added static/github-pr-details.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7f8b16d

Please sign in to comment.