Skip to content

Commit 31d160b

Browse files
authored
Merge pull request #4866 from nagyv/Introduce-visibility-flag-for-bootstrap-gitlab
Introduce visibility flag for bootstrap gitlab
2 parents a901723 + fabdbaa commit 31d160b

File tree

4 files changed

+140
-8
lines changed

4 files changed

+140
-8
lines changed

cmd/flux/bootstrap_gitlab.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525
"time"
2626

27+
"github.com/fluxcd/go-git-providers/gitprovider"
2728
"github.com/fluxcd/pkg/git"
2829
"github.com/fluxcd/pkg/git/gogit"
2930
"github.com/spf13/cobra"
@@ -58,14 +59,14 @@ the bootstrap command will perform an upgrade if needed.`,
5859
# Run bootstrap for a repository path
5960
flux bootstrap gitlab --owner=<group> --repository=<repository name> --path=dev-cluster
6061
61-
# Run bootstrap for a public repository on a personal account
62-
flux bootstrap gitlab --owner=<user> --repository=<repository name> --private=false --personal --token-auth
62+
# Run bootstrap for a public repository
63+
flux bootstrap gitlab --owner=<group> --repository=<repository name> --visibility=public --token-auth
6364
6465
# Run bootstrap for a private repository hosted on a GitLab server
65-
flux bootstrap gitlab --owner=<group> --repository=<repository name> --hostname=<domain> --token-auth
66+
flux bootstrap gitlab --owner=<group> --repository=<repository name> --hostname=<gitlab_url> --token-auth
6667
6768
# Run bootstrap for an existing repository with a branch named main
68-
flux bootstrap gitlab --owner=<organization> --repository=<repository name> --branch=main --token-auth
69+
flux bootstrap gitlab --owner=<group> --repository=<repository name> --branch=main --token-auth
6970
7071
# Run bootstrap for a private repository using Deploy Token authentication
7172
flux bootstrap gitlab --owner=<group> --repository=<repository name> --deploy-token-auth
@@ -85,6 +86,7 @@ type gitlabFlags struct {
8586
repository string
8687
interval time.Duration
8788
personal bool
89+
visibility flags.GitLabVisibility
8890
private bool
8991
hostname string
9092
path flags.SafeRelativePath
@@ -94,14 +96,22 @@ type gitlabFlags struct {
9496
deployTokenAuth bool
9597
}
9698

97-
var gitlabArgs gitlabFlags
99+
func NewGitlabFlags() gitlabFlags {
100+
return gitlabFlags{
101+
visibility: flags.GitLabVisibility(gitprovider.RepositoryVisibilityPrivate),
102+
}
103+
}
104+
105+
var gitlabArgs = NewGitlabFlags()
98106

99107
func init() {
100108
bootstrapGitLabCmd.Flags().StringVar(&gitlabArgs.owner, "owner", "", "GitLab user or group name")
101109
bootstrapGitLabCmd.Flags().StringVar(&gitlabArgs.repository, "repository", "", "GitLab repository name")
102110
bootstrapGitLabCmd.Flags().StringSliceVar(&gitlabArgs.teams, "team", []string{}, "GitLab teams to be given maintainer access (also accepts comma-separated values)")
103111
bootstrapGitLabCmd.Flags().BoolVar(&gitlabArgs.personal, "personal", false, "if true, the owner is assumed to be a GitLab user; otherwise a group")
104112
bootstrapGitLabCmd.Flags().BoolVar(&gitlabArgs.private, "private", true, "if true, the repository is setup or configured as private")
113+
bootstrapGitLabCmd.Flags().MarkDeprecated("private", "use --visibility instead")
114+
bootstrapGitLabCmd.Flags().Var(&gitlabArgs.visibility, "visibility", gitlabArgs.visibility.Description())
105115
bootstrapGitLabCmd.Flags().DurationVar(&gitlabArgs.interval, "interval", time.Minute, "sync interval")
106116
bootstrapGitLabCmd.Flags().StringVar(&gitlabArgs.hostname, "hostname", glDefaultDomain, "GitLab hostname")
107117
bootstrapGitLabCmd.Flags().Var(&gitlabArgs.path, "path", "path relative to the repository root, when specified the cluster sync will be scoped to this path")
@@ -133,6 +143,11 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
133143
return fmt.Errorf("--token-auth and --deploy-token-auth cannot be set both.")
134144
}
135145

146+
if !gitlabArgs.private {
147+
gitlabArgs.visibility.Set(string(gitprovider.RepositoryVisibilityPublic))
148+
cmd.Println("Using visibility public as --private=false")
149+
}
150+
136151
if err := bootstrapValidate(); err != nil {
137152
return err
138153
}
@@ -282,6 +297,7 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
282297
// Bootstrap config
283298
bootstrapOpts := []bootstrap.GitProviderOption{
284299
bootstrap.WithProviderRepository(gitlabArgs.owner, gitlabArgs.repository, gitlabArgs.personal),
300+
bootstrap.WithProviderVisibility(gitlabArgs.visibility.String()),
285301
bootstrap.WithBranch(bootstrapArgs.branch),
286302
bootstrap.WithBootstrapTransportType("https"),
287303
bootstrap.WithSignature(bootstrapArgs.authorName, bootstrapArgs.authorEmail),
@@ -301,9 +317,6 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
301317
if gitlabArgs.deployTokenAuth {
302318
bootstrapOpts = append(bootstrapOpts, bootstrap.WithDeployTokenAuth())
303319
}
304-
if !gitlabArgs.private {
305-
bootstrapOpts = append(bootstrapOpts, bootstrap.WithProviderRepositoryConfig("", "", "public"))
306-
}
307320
if gitlabArgs.reconcile {
308321
bootstrapOpts = append(bootstrapOpts, bootstrap.WithReconcile())
309322
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2024 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package flags
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
23+
"github.com/fluxcd/go-git-providers/gitprovider"
24+
"github.com/fluxcd/go-git-providers/validation"
25+
)
26+
27+
var supportedGitLabVisibilities = map[gitprovider.RepositoryVisibility]struct{}{
28+
gitprovider.RepositoryVisibilityPublic: {},
29+
gitprovider.RepositoryVisibilityInternal: {},
30+
gitprovider.RepositoryVisibilityPrivate: {},
31+
}
32+
33+
// ValidateRepositoryVisibility validates a given RepositoryVisibility.
34+
func ValidateRepositoryVisibility(r gitprovider.RepositoryVisibility) error {
35+
_, ok := supportedGitLabVisibilities[r]
36+
if !ok {
37+
return validation.ErrFieldEnumInvalid
38+
}
39+
return nil
40+
}
41+
42+
type GitLabVisibility gitprovider.RepositoryVisibility
43+
44+
func (d *GitLabVisibility) String() string {
45+
return string(*d)
46+
}
47+
48+
func (d *GitLabVisibility) Set(str string) error {
49+
if strings.TrimSpace(str) == "" {
50+
str = string(gitprovider.RepositoryVisibilityPrivate)
51+
}
52+
var visibility = gitprovider.RepositoryVisibility(str)
53+
if ValidateRepositoryVisibility(visibility) != nil {
54+
return fmt.Errorf("unsupported visibility '%s'", str)
55+
}
56+
*d = GitLabVisibility(visibility)
57+
return nil
58+
}
59+
60+
func (d *GitLabVisibility) Type() string {
61+
return "gitLabVisibility"
62+
}
63+
64+
func (d *GitLabVisibility) Description() string {
65+
return fmt.Sprintf("specifies the visibility of the repository. Valid values are public, private, internal")
66+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2024 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package flags
18+
19+
import (
20+
"testing"
21+
)
22+
23+
func TestGitLabVisibility_Set(t *testing.T) {
24+
tests := []struct {
25+
name string
26+
str string
27+
expect string
28+
expectErr bool
29+
}{
30+
{"private", "private", "private", false},
31+
{"internal", "internal", "internal", false},
32+
{"public", "public", "public", false},
33+
{"unsupported", "unsupported", "", true},
34+
{"default", "", "private", false},
35+
}
36+
for _, tt := range tests {
37+
t.Run(tt.name, func(t *testing.T) {
38+
var p GitLabVisibility
39+
if err := p.Set(tt.str); (err != nil) != tt.expectErr {
40+
t.Errorf("Set() error = %v, expectErr %v", err, tt.expectErr)
41+
}
42+
if str := p.String(); str != tt.expect {
43+
t.Errorf("Set() = %v, expect %v", str, tt.expect)
44+
}
45+
})
46+
}
47+
}

pkg/bootstrap/bootstrap_provider.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ func WithProviderRepository(owner, repositoryName string, personal bool) GitProv
9494
}
9595
}
9696

97+
func WithProviderVisibility(visibility string) GitProviderOption {
98+
return providerRepositoryConfigOption{
99+
visibility: visibility,
100+
}
101+
}
102+
97103
type providerRepositoryOption struct {
98104
owner string
99105
repositoryName string

0 commit comments

Comments
 (0)