Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nate smith committed Oct 4, 2019
0 parents commit 8dd0314
Show file tree
Hide file tree
Showing 21 changed files with 3,572 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Github Cli

The #ce-cli team is working on a CLI tool to reduce the friction between GitHub and your local machine for people who use the command line primarily to interact with Git and GitHub. https://github.com/github/releases/issues/659

## Process

1. For code we want to keep (production ready) create PRs and merge them into `master`. For prototype code we will merge them into the `prototype` branch because the code will most likely be garbage. We could have used one production and one prototype repo but that would have made communication more difficult and dispersed.

2. Each week we’ll have a tracking issue to coordinate plans and distribute the workload. They look like this https://github.com/github/github-cli-prototype/labels/tracking%20issue. This issue can also be used for handoff messages like this https://github.slack.com/archives/C17LP0XU3/p1569378288317500. Using issues and a project board seems like overkill right now, so start lo-fi.

3. We zoom as a team fortnight (Tuesday)
120 changes: 120 additions & 0 deletions command/pr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package command

import (
"fmt"

"github.com/github/gh/git"
"github.com/github/gh/github"
"github.com/spf13/cobra"
)

func init() {
RootCmd.AddCommand(prCmd)
prCmd.AddCommand(prListCmd)
}

var prCmd = &cobra.Command{
Use: "pr",
Short: "Work with pull requests",
Long: `This command allows you to
work with pull requests.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("pr")
},
}

var prListCmd = &cobra.Command{
Use: "list",
Short: "List pull requests",
Run: func(cmd *cobra.Command, args []string) {
ExecutePr()
},
}

type prFilter int

const (
createdByViewer prFilter = iota
reviewRequested
)

func ExecutePr() {
// prsForCurrentBranch := pullRequestsForCurrentBranch()
prsCreatedByViewer := pullRequests(createdByViewer)
// prsRequestingReview := pullRequests(reviewRequested)

fmt.Printf("🌭 count! %d\n", len(prsCreatedByViewer))
}

type searchBody struct {
Items []github.PullRequest `json:"items"`
}

func pullRequestsForCurrentBranch() []github.PullRequest {
project := project()
client := github.NewClient(project.Host)
currentBranch, error := git.Head()
if error != nil {
panic(error)
}

headWithOwner := fmt.Sprintf("%s:%s", project.Owner, currentBranch)
filterParams := map[string]interface{}{"headWithOwner": headWithOwner}
prs, error := client.FetchPullRequests(&project, filterParams, 10, nil)
if error != nil {
panic(error)
}

return prs
}

func pullRequests(filter prFilter) []github.PullRequest {
project := project()
client := github.NewClient(project.Host)
owner := project.Owner
name := project.Name
user, error := client.CurrentUser()
if error != nil {
panic(error)
}

var headers map[string]string
var q string
if filter == createdByViewer {
q = fmt.Sprintf("user:%s repo:%s state:open is:pr author:%s", owner, name, user.Login)
} else if filter == reviewRequested {
q = fmt.Sprintf("user:%s repo:%s state:open review-requested:%s", owner, name, user.Login)
} else {
panic("This is not a fitler")
}

data := map[string]interface{}{"q": q}

response, error := client.GenericAPIRequest("GET", "search/issues", data, headers, 60)
if error != nil {
panic(fmt.Sprintf("GenericAPIRequest failed %+v", error))
}
searchBody := searchBody{}
error = response.Unmarshal(&searchBody)
if error != nil {
panic(fmt.Sprintf("Unmarshal failed %+v", error))
}

return searchBody.Items
}

func project() github.Project {
remotes, error := github.Remotes()
if error != nil {
panic(error)
}

for _, remote := range remotes {
if project, error := remote.Project(); error == nil {
return *project
}
}

panic("Could not get the project. What is a project? I don't know, it's kind of like a git repository I think?")
}
17 changes: 17 additions & 0 deletions command/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package command

import (
"fmt"

"github.com/spf13/cobra"
)

var RootCmd = &cobra.Command{
Use: "gh",
Short: "GitHub CLI",
Long: `Do things with GitHub from your terminal`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("root")
},
}
Loading

0 comments on commit 8dd0314

Please sign in to comment.