func authCmd(c *cli.Context) { config, err := gordon.LoadConfig() if err != nil { config = &gordon.Config{} } token := c.String("add") userName := c.String("user") if userName != "" { config.UserName = userName if err := gordon.SaveConfig(*config); err != nil { gordon.WriteError("%s", err) } } if token != "" { config.Token = token if err := gordon.SaveConfig(*config); err != nil { gordon.WriteError("%s", err) } } // Display token and user information if config, err := gordon.LoadConfig(); err == nil { if config.UserName != "" { fmt.Fprintf(os.Stdout, "Token: %s, UserName: %s\n", config.Token, config.UserName) } else { fmt.Fprintf(os.Stdout, "Token: %s\n", config.Token) } } else { fmt.Fprintf(os.Stderr, "No token registered\n") os.Exit(1) } }
//Take a specific issue. If it's taken, show a message with the overwrite optional flag //If the user doesn't have permissions, add a comment #volunteer func takeCmd(c *cli.Context) { if c.Args().Present() { number := c.Args()[0] issue, _, err := m.GetIssue(number, false) if err != nil { gordon.WriteError("%s", err) } user, err := m.GetGithubUser() if err != nil { gordon.WriteError("%s", err) } if issue.Assignee.Login != "" && !c.Bool("overwrite") { fmt.Fprintf(os.Stdout, "Use the flag --overwrite to take the issue from %s", issue.Assignee.Login) return } issue.Assignee = *user patchedIssue, err := m.PatchIssue(number, issue) if err != nil { gordon.WriteError("%s", err) } if patchedIssue.Assignee.Login != user.Login { m.AddComment(number, "#volunteer") fmt.Fprintf(os.Stdout, "No permission to assign. You '%s' was added as #volunteer.", user.Login) } else { fmt.Fprintf(os.Stdout, "The issue %s was assigned to %s", number, patchedIssue.Assignee.Login) } } else { fmt.Fprintf(os.Stdout, "Please enter the issue's number") } }
func mainCmd(c *cli.Context) { if !c.Args().Present() { filter := filters.GetIssueFilter(c) issues, err := filter(m.GetIssues("open", c.String("assigned"))) if err != nil { gordon.WriteError("Error getting issues: %s", err) } fmt.Printf("%c[2K\r", 27) gordon.DisplayIssues(c, issues, c.Bool("no-trunc")) return } var ( number = c.Args().Get(0) comment = c.String("comment") ) if comment != "" { addComment(number, comment) return } if c.Bool("vote") { addComment(number, "+1") fmt.Fprintf(os.Stdout, "Vote added to the issue: %s", number) return } issue, comments, err := m.GetIssue(number, true) if err != nil { gordon.WriteError("%s", err) } gordon.DisplayIssue(issue, comments) }
// Show the reviewers for this pull request func reviewersCmd(c *cli.Context) { if !c.Args().Present() { fmt.Println("Please enter a pull request number") return } number := c.Args()[0] var patch io.Reader if number == "-" { patch = os.Stdin } else { pr, _, err := m.GetPullRequest(number, false) if err != nil { gordon.WriteError("%s", err) } resp, err := http.Get(pr.DiffURL) if err != nil { gordon.WriteError("%s", err) } patch = resp.Body defer resp.Body.Close() } reviewers, err := gordon.ReviewPatch(patch, m.GetMaintainersDirMap()) if err != nil { gordon.WriteError("%s", err) } gordon.DisplayReviewers(c, reviewers) }
func checkoutCmd(c *cli.Context) { if !c.Args().Present() { fmt.Println("Please enter a pull request number") return } number := c.Args()[0] pr, _, err := m.GetPullRequest(number, false) if err != nil { gordon.WriteError("%s", err) } if err := m.Checkout(pr); err != nil { gordon.WriteError("%s", err) } }
func mergeCmd(c *cli.Context) { if !c.Args().Present() { fmt.Println("Please enter a pull request number") return } number := c.Args()[0] merge, err := m.MergePullRequest(number, c.String("m"), c.Bool("force")) if err != nil { gordon.WriteError("%s", err) } if merge.Merged { fmt.Fprintf(os.Stdout, "%s\n", brush.Green(merge.Message)) } else { gordon.WriteError("%s", err) } }
func repositoryInfoCmd(c *cli.Context) { r, err := m.Repository() if err != nil { gordon.WriteError("%s", err) } fmt.Fprintf(os.Stdout, "Name: %s\nForks: %d\nStars: %d\nIssues: %d\n", r.Name, r.Forks, r.Watchers, r.OpenIssues) }
func addComment(number, comment string) { cmt, err := m.AddComment(number, comment) if err != nil { gordon.WriteError("%s", err) } gordon.DisplayCommentAdded(cmt) }
func alruCmd(c *cli.Context) { lru, err := m.GetFirstPullRequest("open", "updated") if err != nil { gordon.WriteError("Error getting pull requests: %s", err) } fmt.Printf("%v (#%d)\n", gordon.HumanDuration(time.Since(lru.UpdatedAt)), lru.Number) }
func main() { app := cli.NewApp() app.Name = "pulls" app.Usage = "Manage github pull requests for project maintainers" app.Version = "0.0.1" client := gh.NewClient() org, name, err := gordon.GetOriginUrl() if err != nil { fmt.Fprintf(os.Stderr, "The current directory is not a valid git repository.\n") os.Exit(1) } t, err := gordon.NewMaintainerManager(client, org, name) if err != nil { gordon.WriteError("%s", err) } m = t loadCommands(app) app.Run(os.Args) }
// This is the top level command for // working with prs func mainCmd(c *cli.Context) { if !c.Args().Present() { var ( state = c.String("state") showAll = true // default to true so that we get the fast path ) switch { case c.Bool("no-merge"), c.Bool("lgtm"), c.Bool("new"), c.Bool("mine"): showAll = false } displayAllPullRequests(c, state, showAll) return } var ( number = c.Args().Get(0) comment = c.String("comment") ) if comment != "" { addComment(number, comment) return } pr, comments, err := m.GetPullRequest(number, true) if err != nil { gordon.WriteError("%s", err) } gordon.DisplayPullRequest(pr, comments) }
// Show contributors stats func contributorsCmd(c *cli.Context) { contributors, err := m.GetContributors() if err != nil { gordon.WriteError("%s", err) } gordon.DisplayContributors(c, contributors) }
func buildQuery(c *cli.Context) string { r, err := m.Repository() if err != nil { gordon.WriteError("%s", err) } // standard parameters query := fmt.Sprintf("q=%s+repo:%s", c.Args()[0], r.FullName) state := c.String("state") if state == "" { state = "open" } query += fmt.Sprintf("+state:%s", state) // optional parameters var optionalParameters = []string{ "author", "assignee", "mentions", "commenter", "involves", "labels"} for i := 0; i < len(optionalParameters); i++ { param := optionalParameters[i] value := c.String(param) if value != "" { query += fmt.Sprintf("+%s:%s", param, value) } } return query }
func displayAllPullRequests(c *cli.Context, state string, showAll bool) { filter := filters.GetPullRequestFilter(c) prs, err := filter(m.GetPullRequestsThatICareAbout(showAll, state, c.String("sort"))) if err != nil { gordon.WriteError("Error getting pull requests %s", err) } fmt.Printf("%c[2K\r", 27) gordon.DisplayPullRequests(c, prs, c.Bool("no-trunc")) }
// Show the patch in a PR func showCmd(c *cli.Context) { if !c.Args().Present() { fmt.Println("Please enter a pull request number") return } number := c.Args()[0] pr, _, err := m.GetPullRequest(number, false) if err != nil { gordon.WriteError("%s", err) } patch, err := http.Get(pr.DiffURL) if err != nil { gordon.WriteError("%s", err) } defer patch.Body.Close() if err := gordon.DisplayPatch(patch.Body); err != nil { gordon.WriteError("%s", err) } }
// Approve a pr by adding a LGTM to the comments func approveCmd(c *cli.Context) { if !c.Args().Present() { fmt.Println("Please enter a pull request number") return } number := c.Args().First() if _, err := m.AddComment(number, "LGTM"); err != nil { gordon.WriteError("%s", err) } fmt.Fprintf(os.Stdout, "Pull request %s approved\n", brush.Green(number)) }
//Search for issues. You add some restrictions to the query. such: // authors, assignee, state, etc. Check the command help for more options. func searchCmd(c *cli.Context) { if c.Args().Present() { issues, err := m.GetIssuesFound(buildQuery(c)) if err != nil { gordon.WriteError("%s", err) } fmt.Printf("%c[2K\r", 27) gordon.DisplayIssues(c, issues, c.Bool("no-trunc")) } else { fmt.Fprintf(os.Stdout, "Please enter a search term") } }