GitLabgitlab-org
cli
main
1package importcmd
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "io"
9 "net/http"
10 "os"
11 "strings"
13 "github.com/MakeNowJust/heredoc/v2"
14 "github.com/spf13/cobra"
16 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
18 "gitlab.com/gitlab-org/cli/internal/api"
19 "gitlab.com/gitlab-org/cli/internal/cmdutils"
20 "gitlab.com/gitlab-org/cli/internal/commands/variable/variableutils"
21 "gitlab.com/gitlab-org/cli/internal/glrepo"
22 "gitlab.com/gitlab-org/cli/internal/iostreams"
23 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
24)
26// importedVariable holds the fields shared by project and group variables, as
27// emitted by `glab variable export --output json`. Importing reads this back.
28type importedVariable struct {
29 Key string `json:"key"`
30 Value string `json:"value"`
31 VariableType string `json:"variable_type"`
32 Protected bool `json:"protected"`
33 Masked bool `json:"masked"`
34 Hidden bool `json:"hidden"`
35 Raw bool `json:"raw"`
36 EnvironmentScope string `json:"environment_scope"`
37 Description string `json:"description"`
38}
40type options struct {
41 apiClient func(repoHost string) (*api.Client, error)
42 io *iostreams.IOStreams
43 baseRepo func() (glrepo.Interface, error)
45 group string
46 inputFile string
47 skipExisting bool
48}
50func NewCmd(f cmdutils.Factory, runE func(opts *options) error) *cobra.Command {
51 opts := &options{
52 io: f.IO(),
53 apiClient: f.ApiClient,
54 baseRepo: f.BaseRepo,
55 }
57 cmd := &cobra.Command{
58 Use: "import",
59 Short: "Import variables from a JSON file or standard input.",
60 Long: heredoc.Docf(`
61 The inverse of %[1]sglab variable export%[1]s. Reads a JSON array of
62 variable objects, in the same shape %[1]sexport --output json%[1]s emits.
64 The command:
66 - Reads from standard input, or from a file with %[1]s--input-file%[1]s.
67 - Imports into the current project by default. Use %[1]s--group%[1]s to
68 import into a group instead.
69 - Stops with an error if a variable already exists. Pass
70 %[1]s--skip-existing%[1]s to skip those and continue.
72 Hidden variables' values aren't included in %[1]sexport%[1]s output,
73 so re-importing one is skipped with a warning. Set its value again
74 with %[1]sglab variable set --hidden%[1]s.
75 `, "`"),
76 Aliases: []string{"im"},
77 Args: cobra.NoArgs,
78 ValidArgsFunction: cobra.NoFileCompletions,
79 Example: heredoc.Doc(`
80 # Pipe an export straight into an import, to restore the same project
81 glab variable export | glab variable import
83 # Import variables from a saved file instead of standard input
84 glab variable import --input-file variables.json
86 # Import into a group instead of the current project
87 glab variable export --group gitlab-org | glab variable import --group gitlab-org
89 # Skip variables that already exist instead of failing
90 glab variable import --input-file variables.json --skip-existing`),
91 Annotations: map[string]string{
92 mcpannotations.Destructive: "true",
93 },
94 RunE: func(cmd *cobra.Command, args []string) error {
95 if err := opts.complete(cmd); err != nil {
96 return err
97 }
99 if runE != nil {
100 return runE(opts)
101 }
103 return opts.run(cmd.Context())
104 },
105 }
107 cmdutils.EnableRepoOverride(cmd, f)
108 cmd.PersistentFlags().StringP("group", "g", "", "Select a group or subgroup. Ignored if a repository argument is set.")
110 fl := cmd.Flags()
111 fl.StringVarP(&opts.inputFile, "input-file", "i", "", "Read the variables JSON from this file instead of standard input.")
112 fl.BoolVar(&opts.skipExisting, "skip-existing", false, "Skip variables that already exist instead of failing.")
113 return cmd
114}
116func (o *options) complete(cmd *cobra.Command) error {
117 group, err := cmdutils.GroupOverride(cmd)
118 if err != nil {
119 return err
120 }
121 o.group = group
123 return nil
124}
126func (o *options) read() ([]importedVariable, error) {
127 var (
128 data []byte
129 err error
130 )
131 if o.inputFile != "" {
132 data, err = os.ReadFile(o.inputFile)
133 } else {
134 data, err = io.ReadAll(o.io.In)
135 }
136 if err != nil {
137 return nil, err
138 }
140 var variables []importedVariable
141 if err := json.Unmarshal(data, &variables); err != nil {
142 return nil, fmt.Errorf("parsing variables JSON: %w", err)
143 }
144 return variables, nil
145}
147// validate checks every key up front and defaults an empty EnvironmentScope to
148// "*", so a bad entry is rejected before any variable has been created —
149// otherwise a later invalid key would leave a partial import behind.
150func validate(variables []importedVariable) error {
151 for i, v := range variables {
152 if !variableutils.IsValidKey(v.Key) {
153 return fmt.Errorf("invalid key %q: %s", v.Key, variableutils.ValidKeyMsg)
154 }
155 if v.EnvironmentScope == "" {
156 variables[i].EnvironmentScope = "*"
157 }
158 }
159 return nil
160}
162func (o *options) run(ctx context.Context) error {
163 c := o.io.Color()
165 variables, err := o.read()
166 if err != nil {
167 return err
168 }
169 if len(variables) == 0 {
170 o.io.LogInfo("No variables to import.")
171 return nil
172 }
173 if err := validate(variables); err != nil {
174 return err
175 }
177 // NOTE: this command can not only be used for projects, so we have to
178 // manually check for the base repo. If it doesn't exist, we bootstrap the
179 // client with the default hostname.
180 var repoHost string
181 if baseRepo, err := o.baseRepo(); err == nil {
182 repoHost = baseRepo.RepoHost()
183 }
184 apiClient, err := o.apiClient(repoHost)
185 if err != nil {
186 return err
187 }
188 client := apiClient.Lab()
190 target := o.group
191 if target == "" {
192 baseRepo, err := o.baseRepo()
193 if err != nil {
194 return err
195 }
196 target = baseRepo.FullName()
197 }
199 created, skipped := 0, 0
200 for _, v := range variables {
201 if v.Hidden && v.Value == "" {
202 o.io.LogInfof("%s Skipped %s: hidden variables' values aren't included in `variable export`; set it again with `glab variable set --hidden`.\n", c.WarnIcon(), v.Key)
203 skipped++
204 continue
205 }
207 if err := o.create(ctx, client, target, v); err != nil {
208 if o.skipExisting && isAlreadyExists(err) {
209 o.io.LogInfof("%s Skipped existing variable %s.\n", c.WarnIcon(), v.Key)
210 skipped++
211 continue
212 }
213 return fmt.Errorf("importing variable %s: %w", v.Key, err)
214 }
215 o.io.LogInfof("%s Imported variable %s.\n", c.GreenCheck(), v.Key)
216 created++
217 }
219 summary := fmt.Sprintf("Imported %d variables into %s", created, target)
220 if skipped > 0 {
221 summary += fmt.Sprintf(" (%d skipped)", skipped)
222 }
223 o.io.LogInfo(summary + ".")
224 return nil
225}
227func (o *options) create(ctx context.Context, client *gitlab.Client, target string, v importedVariable) error {
228 if o.group != "" {
229 groupOpts := &gitlab.CreateGroupVariableOptions{
230 Key: new(v.Key),
231 Value: new(v.Value),
232 EnvironmentScope: new(v.EnvironmentScope),
233 Masked: new(v.Masked),
234 Protected: new(v.Protected),
235 VariableType: new(gitlab.VariableTypeValue(v.VariableType)),
236 Raw: new(v.Raw),
237 Description: new(v.Description),
238 }
239 if v.Hidden {
240 groupOpts.MaskedAndHidden = new(true)
241 }
242 _, _, err := client.GroupVariables.CreateVariable(target, groupOpts, gitlab.WithContext(ctx))
243 return err
244 }
246 projectOpts := &gitlab.CreateProjectVariableOptions{
247 Key: new(v.Key),
248 Value: new(v.Value),
249 EnvironmentScope: new(v.EnvironmentScope),
250 Masked: new(v.Masked),
251 Protected: new(v.Protected),
252 VariableType: new(gitlab.VariableTypeValue(v.VariableType)),
253 Raw: new(v.Raw),
254 Description: new(v.Description),
255 }
256 if v.Hidden {
257 projectOpts.MaskedAndHidden = new(true)
258 }
259 _, _, err := client.ProjectVariables.CreateVariable(target, projectOpts, gitlab.WithContext(ctx))
260 return err
261}
263// isAlreadyExists reports whether err is GitLab's "key has already been
264// taken" validation error, returned when a variable with the same key and
265// scope exists. It inspects the structured API error (status + message)
266// rather than matching on err.Error(), which also bakes in the request
267// method and URL.
268func isAlreadyExists(err error) bool {
269 var errResp *gitlab.ErrorResponse
270 if !errors.As(err, &errResp) || !errResp.HasStatusCode(http.StatusBadRequest) {
271 return false
272 }
273 return strings.Contains(strings.ToLower(errResp.Message), "has already been taken")
274}
Diagnostics8 git calls · 278 ms
CallStatusSizeTime0278 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:02001.8 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1830 git.pack.trailer=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200375 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=375 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=210e8ccdc9766a94adae919c6922ea88d77a8fb1 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=375 git.pkt.payload.max=348 git.sideband.band1.bytes=348 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=348 git.pack.trailer=8c3972a32424b06b4c572b8aa869b28eca201985Open pack map
fetchtree:0200137 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=261 git.response.bytes=137 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=51e6d5d34052c13671ee0c8708ecaea0ab5e66fa git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=137 git.pkt.payload.max=110 git.sideband.band1.bytes=110 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=110 git.pack.trailer=64e6b6c2411ed206e42401749e85c55d827ddf40Open pack map
fetch2002.9 KiB261 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3001 git.response.cache=MISS git.response.duration_ms=261 git.fetch.wants=eb700a63e90a04b7394af262e82b9b44dc2212cd git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=3001 git.pkt.payload.max=2974 git.sideband.band1.bytes=2974 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5ea93315cbca87d
- Request
- e5251b69…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format