Skip to content

Commit

Permalink
Merge pull request #18 from k-kinzal/support-assignee-command
Browse files Browse the repository at this point in the history
support assignee manipuration
  • Loading branch information
k-kinzal authored Jan 21, 2020
2 parents d164116 + b2f372c commit 1e73666
Show file tree
Hide file tree
Showing 6 changed files with 641 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ $ pr label [owner]/[repo] -l 'state == `"open"`' --action "replace" --label "foo
`--action "remove"` removes the label specified for the PR that matched the rule.
`--action "replace"` replaces all labels on PR that match the rule with the specified label.
### Assignee
Append/Remove/Replace assignees to PRs that match the rule.
```bash
$ pr assignees [owner]/[repo] -l 'state == `"open"`' --action "append" --assignee "foo"
...
$ pr assignees [owner]/[repo] -l 'state == `"open"`' --action "remove" --assignee "foo"
...
$ pr assignees [owner]/[repo] -l 'state == `"open"`' --action "replace" --assignee "foo"
...
```
`--action "append"` appends the specified label to the PR that matches the rule.
`--action "remove"` removes the label specified for the PR that matched the rule.
`--action "replace"` replaces all labels on PR that match the rule with the specified label.
### Check
When the PR CLI is run on the CI, the rule status is displayed separately from the CI.
Expand Down
61 changes: 61 additions & 0 deletions cmd/assignee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cmd

import (
"encoding/json"
"fmt"
"os"

"github.com/k-kinzal/pr/pkg/pr"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
)

func AssigneeRun(cmd *cobra.Command, args []string) error {
assigneeOption.Action = pr.AssigneeAction(assigneeAction)

pulls, err := pr.Assignee(owner, repo, assigneeOption)
if err != nil {
if _, ok := err.(*pr.NoMatchError); ok {
fmt.Fprintln(os.Stderr, err.Error())
fmt.Fprintln(os.Stdout, "[]")
if exitCode {
os.Exit(127)
}
return nil
}
return err
}

out, err := json.Marshal(pulls)
if err != nil {
return xerrors.Errorf("merge: %s", err)
}
fmt.Fprintln(os.Stdout, string(out))

return nil
}

var (
assigneeOption *pr.AssigneeOption
assigneeCmd = &cobra.Command{
Use: "assignee owner/repo",
Short: "Manipulate assignees that match a rule",
RunE: AssigneeRun,
SilenceErrors: true,
SilenceUsage: true,
}
assigneeAction string
)

func setAssigneeFrags(cmd *cobra.Command) *pr.AssigneeOption {
opt := &pr.AssigneeOption{}
cmd.Flags().StringArrayVarP(&opt.Assignees, "assignee", "", nil, "Manipulate assignee")
cmd.Flags().StringVar(&assigneeAction, "action", "append", "Manipulation of `append`, `remove`, `replace` for assignees")
return opt
}

func init() {
assigneeOption = setAssigneeFrags(assigneeCmd)
assigneeOption.ListOption = setListFrags(assigneeCmd)
rootCmd.AddCommand(assigneeCmd)
}
108 changes: 108 additions & 0 deletions pkg/api/assignee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package api

import (
"context"
"fmt"

"golang.org/x/sync/errgroup"
)

type AssigneeOption struct {
Assignees []string
}

func (c *Client) AddAssignees(ctx context.Context, pulls []*PullRequest, opt *AssigneeOption) ([]*PullRequest, error) {
eg, ctx := errgroup.WithContext(ctx)

for _, pull := range pulls {
eg.Go(func(pull *PullRequest) func() error {
return func() error {
issue, _, err := c.github.Issues.AddAssignees(ctx, pull.Owner, pull.Repo, int(pull.Number), opt.Assignees)
if err != nil {
return err
}

users := make([]*User, len(issue.Assignees))
for i, assignee := range issue.Assignees {
users[i] = newUser(assignee)
}
pull.Assignees = users
pull.UpdatedAt = newTimestamp(issue.UpdatedAt)

return nil
}
}(pull))
}
if err := eg.Wait(); err != nil {
return nil, err
}

return pulls, nil
}

func (c *Client) RemoveAssignees(ctx context.Context, pulls []*PullRequest, opt *AssigneeOption) ([]*PullRequest, error) {
eg, ctx := errgroup.WithContext(ctx)

for _, pull := range pulls {
eg.Go(func(pull *PullRequest) func() error {
return func() error {
issue, _, err := c.github.Issues.RemoveAssignees(ctx, pull.Owner, pull.Repo, int(pull.Number), opt.Assignees)
if err != nil {
return err
}

users := make([]*User, len(issue.Assignees))
for i, assignee := range issue.Assignees {
users[i] = newUser(assignee)
}
pull.Assignees = users
pull.UpdatedAt = newTimestamp(issue.UpdatedAt)

return nil
}
}(pull))
}
if err := eg.Wait(); err != nil {
return nil, err
}

return pulls, nil
}

func (c *Client) ReplaceAssignees(ctx context.Context, pulls []*PullRequest, opt *AssigneeOption) ([]*PullRequest, error) {
eg, ctx := errgroup.WithContext(ctx)

for _, pull := range pulls {
eg.Go(func(pull *PullRequest) func() error {
return func() error {
assignees := make([]string, len(pull.Assignees))
for i, a := range pull.Assignees {
assignees[i] = a.Login
}
_, _, err := c.github.Issues.RemoveAssignees(ctx, pull.Owner, pull.Repo, int(pull.Number), assignees)
if err != nil {
return err
}
issue, resp, err := c.github.Issues.AddAssignees(ctx, pull.Owner, pull.Repo, int(pull.Number), opt.Assignees)
if err != nil {
return err
}
fmt.Println(resp)

users := make([]*User, len(issue.Assignees))
for i, assignee := range issue.Assignees {
users[i] = newUser(assignee)
}
pull.Assignees = users
pull.UpdatedAt = newTimestamp(issue.UpdatedAt)

return nil
}
}(pull))
}
if err := eg.Wait(); err != nil {
return nil, err
}

return pulls, nil
}
Loading

0 comments on commit 1e73666

Please sign in to comment.