This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
project_mirror.go
170 lines (150 loc) · 4.5 KB
/
project_mirror.go
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
161
162
163
164
165
166
167
168
169
170
package mirror
import (
"errors"
"fmt"
"strings"
"github.com/profclems/glab/api"
"github.com/profclems/glab/commands/cmdutils"
"github.com/profclems/glab/internal/glrepo"
"github.com/profclems/glab/pkg/iostreams"
"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
)
type MirrorOptions struct {
URL string
Direction string
Enabled bool
ProtectedBranchesOnly bool
AllowDivergence bool
ProjectID int
IO *iostreams.IOStreams
BaseRepo glrepo.Interface
APIClient func() (*gitlab.Client, error)
httpClient *gitlab.Client
}
func NewCmdMirror(f *cmdutils.Factory) *cobra.Command {
opts := MirrorOptions{
IO: f.IO,
}
var projectMirrorCmd = &cobra.Command{
Use: "mirror [ID | URL | PATH] [flags]",
Short: "Mirror a project/repository",
Long: `Mirrors a project/repository to the specified location using pull or push method.`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
opts.APIClient = f.HttpClient
if len(args) > 0 {
opts.BaseRepo, err = glrepo.FromFullName(args[0])
if err != nil {
return err
}
opts.APIClient = func() (*gitlab.Client, error) {
if opts.httpClient != nil {
return opts.httpClient, nil
}
cfg, err := f.Config()
if err != nil {
return nil, err
}
c, err := api.NewClientWithCfg(opts.BaseRepo.RepoHost(), cfg, false)
if err != nil {
return nil, err
}
opts.httpClient = c.Lab()
return opts.httpClient, nil
}
} else {
opts.BaseRepo, err = f.BaseRepo()
if err != nil {
return err
}
}
if opts.Direction != "pull" && opts.Direction != "push" {
return cmdutils.WrapError(
errors.New("invalid choice for --direction"),
"argument direction value should be pull or push",
)
}
if opts.Direction == "pull" && opts.AllowDivergence {
fmt.Fprintf(
f.IO.StdOut,
"[Warning] allow-divergence flag has no effect for pull mirror and is ignored.\n",
)
}
opts.URL = strings.TrimSpace(opts.URL)
opts.httpClient, err = opts.APIClient()
if err != nil {
return err
}
project, err := opts.BaseRepo.Project(opts.httpClient)
if err != nil {
return err
}
opts.ProjectID = project.ID
return runProjectMirror(&opts)
},
}
projectMirrorCmd.Flags().StringVar(&opts.URL, "url", "", "The target URL to which the repository is mirrored.")
projectMirrorCmd.Flags().StringVar(&opts.Direction, "direction", "pull", "Mirror direction")
projectMirrorCmd.Flags().BoolVar(&opts.Enabled, "enabled", true, "Determines if the mirror is enabled.")
projectMirrorCmd.Flags().BoolVar(&opts.ProtectedBranchesOnly, "protected-branches-only", false, "Determines if only protected branches are mirrored.")
projectMirrorCmd.Flags().BoolVar(&opts.AllowDivergence, "allow-divergence", false, "Determines if divergent refs are skipped.")
_ = projectMirrorCmd.MarkFlagRequired("url")
_ = projectMirrorCmd.MarkFlagRequired("direction")
return projectMirrorCmd
}
func runProjectMirror(opts *MirrorOptions) error {
if opts.Direction == "push" {
return createPushMirror(opts)
} else {
return createPullMirror(opts)
}
}
func createPushMirror(opts *MirrorOptions) error {
var pm *gitlab.ProjectMirror
var err error
var pushOptions = api.CreatePushMirrorOptions{
Url: opts.URL,
Enabled: opts.Enabled,
OnlyProtectedBranches: opts.ProtectedBranchesOnly,
KeepDivergentRefs: opts.AllowDivergence,
}
pm, err = api.CreatePushMirror(
opts.httpClient,
opts.ProjectID,
&pushOptions,
)
if err != nil {
return cmdutils.WrapError(err, "Failed to create Push Mirror")
}
greenCheck := opts.IO.Color().Green("✓")
fmt.Fprintf(
opts.IO.StdOut,
"%s Created Push Mirror for %s (%d) on GitLab at %s (%d)\n",
greenCheck, pm.URL, pm.ID, opts.BaseRepo.FullName(), opts.ProjectID,
)
return err
}
func createPullMirror(opts *MirrorOptions) error {
var pullOptions = api.CreatePullMirrorOptions{
Url: opts.URL,
Enabled: opts.Enabled,
OnlyProtectedBranches: opts.ProtectedBranchesOnly,
}
err := api.CreatePullMirror(
opts.httpClient,
opts.ProjectID,
&pullOptions,
)
if err != nil {
return cmdutils.WrapError(err, "Failed to create Pull Mirror")
}
greenCheck := opts.IO.Color().Green("✓")
fmt.Fprintf(
opts.IO.StdOut,
"%s Created Pull Mirror for %s on GitLab at %s (%d)\n",
greenCheck, opts.URL, opts.BaseRepo.FullName(), opts.ProjectID,
)
return err
}