|
| 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 | +} |
0 commit comments