1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
package label
import (
"context"
"errors"
"fmt"
"net/http"
"sync/atomic"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/text"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)
type cloneOptions struct {
BaseRepo func() (ghrepo.Interface, error)
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
SourceRepo ghrepo.Interface
Force bool
}
func newCmdClone(f *cmdutil.Factory, runF func(*cloneOptions) error) *cobra.Command {
opts := cloneOptions{
HttpClient: f.HttpClient,
IO: f.IOStreams,
}
cmd := &cobra.Command{
Use: "clone <source-repository>",
Short: "Clones labels from one repository to another",
Long: heredoc.Docf(`
Clones labels from a source repository to a destination repository on GitHub.
By default, the destination repository is the current repository.
All labels from the source repository will be copied to the destination
repository. Labels in the destination repository that are not in the source
repository will not be deleted or modified.
Labels from the source repository that already exist in the destination
repository will be skipped. You can overwrite existing labels in the
destination repository using the %[1]s--force%[1]s flag.
`, "`"),
Example: heredoc.Doc(`
# clone and overwrite labels from cli/cli repository into the current repository
$ gh label clone cli/cli --force
# clone labels from cli/cli repository into a octocat/cli repository
$ gh label clone cli/cli --repo octocat/cli
`),
Args: cmdutil.ExactArgs(1, "cannot clone labels: source-repository argument required"),
RunE: func(c *cobra.Command, args []string) error {
var err error
opts.BaseRepo = f.BaseRepo
opts.SourceRepo, err = ghrepo.FromFullName(args[0])
if err != nil {
return err
}
if runF != nil {
return runF(&opts)
}
return cloneRun(&opts)
},
}
cmd.Flags().BoolVarP(&opts.Force, "force", "f", false, "Overwrite labels in the destination repository")
return cmd
}
func cloneRun(opts *cloneOptions) error {
httpClient, err := opts.HttpClient()
if err != nil {
return err
}
baseRepo, err := opts.BaseRepo()
if err != nil {
return err
}
opts.IO.StartProgressIndicator()
successCount, totalCount, err := cloneLabels(httpClient, baseRepo, opts)
opts.IO.StopProgressIndicator()
if err != nil {
return err
}
if opts.IO.IsStdoutTTY() {
cs := opts.IO.ColorScheme()
pluralize := func(num int) string {
return text.Pluralize(num, "label")
}
successCount := int(successCount)
switch {
case successCount == totalCount:
fmt.Fprintf(opts.IO.Out, "%s Cloned %s from %s to %s\n", cs.SuccessIcon(), pluralize(successCount), ghrepo.FullName(opts.SourceRepo), ghrepo.FullName(baseRepo))
default:
fmt.Fprintf(opts.IO.Out, "%s Cloned %s of %d from %s to %s\n", cs.WarningIcon(), pluralize(successCount), totalCount, ghrepo.FullName(opts.SourceRepo), ghrepo.FullName(baseRepo))
}
}
return nil
}
func cloneLabels(client *http.Client, destination ghrepo.Interface, opts *cloneOptions) (successCount uint32, totalCount int, err error) {
successCount = 0
labels, totalCount, err := listLabels(client, opts.SourceRepo, listQueryOptions{Limit: -1})
if err != nil {
return
}
workers := 10
toCreate := make(chan createOptions)
wg, ctx := errgroup.WithContext(context.Background())
for i := 0; i < workers; i++ {
wg.Go(func() error {
for {
select {
case <-ctx.Done():
return nil
case l, ok := <-toCreate:
if !ok {
return nil
}
err := createLabel(client, destination, &l)
if err != nil {
if !errors.Is(err, errLabelAlreadyExists) {
return err
}
} else {
atomic.AddUint32(&successCount, 1)
}
}
}
})
}
for _, label := range labels {
createOpts := createOptions{
Name: label.Name,
Description: label.Description,
Color: label.Color,
Force: opts.Force,
}
toCreate <- createOpts
}
close(toCreate)
err = wg.Wait()
return
}
|