-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
641 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.