Skip to content

Commit 8dd0314

Browse files
author
nate smith
committed
initial commit
0 parents  commit 8dd0314

File tree

21 files changed

+3572
-0
lines changed

21 files changed

+3572
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Github Cli
2+
3+
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
4+
5+
## Process
6+
7+
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.
8+
9+
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.
10+
11+
3. We zoom as a team fortnight (Tuesday)

command/pr.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package command
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/github/gh/git"
7+
"github.com/github/gh/github"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
func init() {
12+
RootCmd.AddCommand(prCmd)
13+
prCmd.AddCommand(prListCmd)
14+
}
15+
16+
var prCmd = &cobra.Command{
17+
Use: "pr",
18+
Short: "Work with pull requests",
19+
Long: `This command allows you to
20+
work with pull requests.`,
21+
Args: cobra.MinimumNArgs(1),
22+
Run: func(cmd *cobra.Command, args []string) {
23+
fmt.Println("pr")
24+
},
25+
}
26+
27+
var prListCmd = &cobra.Command{
28+
Use: "list",
29+
Short: "List pull requests",
30+
Run: func(cmd *cobra.Command, args []string) {
31+
ExecutePr()
32+
},
33+
}
34+
35+
type prFilter int
36+
37+
const (
38+
createdByViewer prFilter = iota
39+
reviewRequested
40+
)
41+
42+
func ExecutePr() {
43+
// prsForCurrentBranch := pullRequestsForCurrentBranch()
44+
prsCreatedByViewer := pullRequests(createdByViewer)
45+
// prsRequestingReview := pullRequests(reviewRequested)
46+
47+
fmt.Printf("🌭 count! %d\n", len(prsCreatedByViewer))
48+
}
49+
50+
type searchBody struct {
51+
Items []github.PullRequest `json:"items"`
52+
}
53+
54+
func pullRequestsForCurrentBranch() []github.PullRequest {
55+
project := project()
56+
client := github.NewClient(project.Host)
57+
currentBranch, error := git.Head()
58+
if error != nil {
59+
panic(error)
60+
}
61+
62+
headWithOwner := fmt.Sprintf("%s:%s", project.Owner, currentBranch)
63+
filterParams := map[string]interface{}{"headWithOwner": headWithOwner}
64+
prs, error := client.FetchPullRequests(&project, filterParams, 10, nil)
65+
if error != nil {
66+
panic(error)
67+
}
68+
69+
return prs
70+
}
71+
72+
func pullRequests(filter prFilter) []github.PullRequest {
73+
project := project()
74+
client := github.NewClient(project.Host)
75+
owner := project.Owner
76+
name := project.Name
77+
user, error := client.CurrentUser()
78+
if error != nil {
79+
panic(error)
80+
}
81+
82+
var headers map[string]string
83+
var q string
84+
if filter == createdByViewer {
85+
q = fmt.Sprintf("user:%s repo:%s state:open is:pr author:%s", owner, name, user.Login)
86+
} else if filter == reviewRequested {
87+
q = fmt.Sprintf("user:%s repo:%s state:open review-requested:%s", owner, name, user.Login)
88+
} else {
89+
panic("This is not a fitler")
90+
}
91+
92+
data := map[string]interface{}{"q": q}
93+
94+
response, error := client.GenericAPIRequest("GET", "search/issues", data, headers, 60)
95+
if error != nil {
96+
panic(fmt.Sprintf("GenericAPIRequest failed %+v", error))
97+
}
98+
searchBody := searchBody{}
99+
error = response.Unmarshal(&searchBody)
100+
if error != nil {
101+
panic(fmt.Sprintf("Unmarshal failed %+v", error))
102+
}
103+
104+
return searchBody.Items
105+
}
106+
107+
func project() github.Project {
108+
remotes, error := github.Remotes()
109+
if error != nil {
110+
panic(error)
111+
}
112+
113+
for _, remote := range remotes {
114+
if project, error := remote.Project(); error == nil {
115+
return *project
116+
}
117+
}
118+
119+
panic("Could not get the project. What is a project? I don't know, it's kind of like a git repository I think?")
120+
}

command/root.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package command
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var RootCmd = &cobra.Command{
10+
Use: "gh",
11+
Short: "GitHub CLI",
12+
Long: `Do things with GitHub from your terminal`,
13+
Args: cobra.MinimumNArgs(1),
14+
Run: func(cmd *cobra.Command, args []string) {
15+
fmt.Println("root")
16+
},
17+
}

0 commit comments

Comments
 (0)