-
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.
Merge pull request #17 from k-kinzal/support-label-command
support label manipuration
- Loading branch information
Showing
6 changed files
with
603 additions
and
3 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 LabelRun(cmd *cobra.Command, args []string) error { | ||
labelOption.Action = pr.LabelAction(labelAction) | ||
|
||
pulls, err := pr.Label(owner, repo, labelOption) | ||
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 ( | ||
labelOption *pr.LabelOption | ||
labelCmd = &cobra.Command{ | ||
Use: "label owner/repo", | ||
Short: "Manipulate labels that match a rule", | ||
RunE: LabelRun, | ||
SilenceErrors: true, | ||
SilenceUsage: true, | ||
} | ||
labelAction string | ||
) | ||
|
||
func setLabelFrags(cmd *cobra.Command) *pr.LabelOption { | ||
opt := &pr.LabelOption{} | ||
cmd.Flags().StringArrayVarP(&opt.Labels, "label", "", nil, "Manipulate labels") | ||
cmd.Flags().StringVar(&labelAction, "action", "append", "Manipulation of `append`, `remove`, `replace` for labels") | ||
return opt | ||
} | ||
|
||
func init() { | ||
labelOption = setLabelFrags(labelCmd) | ||
labelOption.ListOption = setListFrags(labelCmd) | ||
rootCmd.AddCommand(labelCmd) | ||
} |
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,100 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
type LabelOption struct { | ||
Labels []string | ||
} | ||
|
||
func (c *Client) AppendLabel(ctx context.Context, pulls []*PullRequest, opt *LabelOption) ([]*PullRequest, error) { | ||
eg, ctx := errgroup.WithContext(ctx) | ||
|
||
for _, pull := range pulls { | ||
eg.Go(func(pull *PullRequest) func() error { | ||
return func() error { | ||
now := Timestamp(time.Now().UTC().Unix()) | ||
labels, _, err := c.github.Issues.AddLabelsToIssue(ctx, pull.Owner, pull.Repo, int(pull.Number), opt.Labels) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, label := range labels { | ||
pull.Labels = append(pull.Labels, newLabel(label)) | ||
} | ||
pull.UpdatedAt = now | ||
|
||
return nil | ||
} | ||
}(pull)) | ||
} | ||
if err := eg.Wait(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return pulls, nil | ||
} | ||
|
||
func (c *Client) RemoveLabel(ctx context.Context, pulls []*PullRequest, opt *LabelOption) ([]*PullRequest, error) { | ||
eg, ctx := errgroup.WithContext(ctx) | ||
|
||
for _, pull := range pulls { | ||
for _, label := range opt.Labels { | ||
eg.Go(func(pull *PullRequest, label string) func() error { | ||
return func() error { | ||
now := Timestamp(time.Now().UTC().Unix()) | ||
_, err := c.github.Issues.RemoveLabelForIssue(ctx, pull.Owner, pull.Repo, int(pull.Number), label) | ||
if err != nil { | ||
return err | ||
} | ||
for i, lbl := range pull.Labels { | ||
if lbl.Name == label { | ||
pull.Labels[i] = pull.Labels[len(pull.Labels)-1] | ||
pull.Labels = pull.Labels[:len(pull.Labels)-1] | ||
break | ||
} | ||
} | ||
pull.UpdatedAt = now | ||
return nil | ||
} | ||
}(pull, label)) | ||
} | ||
} | ||
if err := eg.Wait(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return pulls, nil | ||
} | ||
|
||
func (c *Client) ReplaceLabel(ctx context.Context, pulls []*PullRequest, opt *LabelOption) ([]*PullRequest, error) { | ||
eg, ctx := errgroup.WithContext(ctx) | ||
|
||
for _, pull := range pulls { | ||
eg.Go(func(pull *PullRequest) func() error { | ||
return func() error { | ||
now := Timestamp(time.Now().UTC().Unix()) | ||
labels, _, err := c.github.Issues.ReplaceLabelsForIssue(ctx, pull.Owner, pull.Repo, int(pull.Number), opt.Labels) | ||
if err != nil { | ||
return err | ||
} | ||
pull.Labels = make([]*Label, len(labels)) | ||
for i, label := range labels { | ||
pull.Labels[i] = newLabel(label) | ||
} | ||
pull.UpdatedAt = now | ||
|
||
return nil | ||
} | ||
}(pull)) | ||
} | ||
if err := eg.Wait(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return pulls, nil | ||
} |
Oops, something went wrong.