Skip to content

Commit ec9f23d

Browse files
author
Viktor Nagy
committed
Introduce projectPath argument for GitLab bootstrap
Deprecates the `owner` and `repository` arguments. Closes: #3817 Signed-off-by: Viktor Nagy <[email protected]>
1 parent 31d160b commit ec9f23d

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

cmd/flux/bootstrap_gitlab.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@ const (
7878
glDefaultPermission = "maintain"
7979
glDefaultDomain = "gitlab.com"
8080
glTokenEnvVar = "GITLAB_TOKEN"
81-
gitlabProjectRegex = `\A[[:alnum:]\x{00A9}-\x{1f9ff}_][[:alnum:]\p{Pd}\x{00A9}-\x{1f9ff}_\.]*\z`
81+
gitlabProjectRegex = `^([[:alnum:]\x{00A9}-\x{1f9ff}_][[:alnum:]\p{Pd}\x{00A9}-\x{1f9ff}_\.]*)/([[:alnum:]\x{00A9}-\x{1f9ff}_][[:alnum:]\p{Pd}\x{00A9}-\x{1f9ff}_\.]*)((/[[:alnum:]\x{00A9}-\x{1f9ff}_][[:alnum:]\p{Pd}\x{00A9}-\x{1f9ff}_\.]*)*)$`
8282
)
8383

8484
type gitlabFlags struct {
8585
owner string
8686
repository string
87+
projectPath string
8788
interval time.Duration
8889
personal bool
8990
visibility flags.GitLabVisibility
@@ -105,10 +106,14 @@ func NewGitlabFlags() gitlabFlags {
105106
var gitlabArgs = NewGitlabFlags()
106107

107108
func init() {
109+
bootstrapGitLabCmd.Flags().StringVar(&gitlabArgs.projectPath, "project-path", "", "GitLab full project path")
108110
bootstrapGitLabCmd.Flags().StringVar(&gitlabArgs.owner, "owner", "", "GitLab user or group name")
111+
bootstrapGitLabCmd.Flags().MarkDeprecated("owner", "use --project-path instead")
109112
bootstrapGitLabCmd.Flags().StringVar(&gitlabArgs.repository, "repository", "", "GitLab repository name")
113+
bootstrapGitLabCmd.Flags().MarkDeprecated("repository", "use --project-path instead")
110114
bootstrapGitLabCmd.Flags().StringSliceVar(&gitlabArgs.teams, "team", []string{}, "GitLab teams to be given maintainer access (also accepts comma-separated values)")
111115
bootstrapGitLabCmd.Flags().BoolVar(&gitlabArgs.personal, "personal", false, "if true, the owner is assumed to be a GitLab user; otherwise a group")
116+
bootstrapGitLabCmd.Flags().MarkDeprecated("personal", "not used")
112117
bootstrapGitLabCmd.Flags().BoolVar(&gitlabArgs.private, "private", true, "if true, the repository is setup or configured as private")
113118
bootstrapGitLabCmd.Flags().MarkDeprecated("private", "use --visibility instead")
114119
bootstrapGitLabCmd.Flags().Var(&gitlabArgs.visibility, "visibility", gitlabArgs.visibility.Description())
@@ -132,9 +137,15 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
132137
}
133138
}
134139

135-
if projectNameIsValid, err := regexp.MatchString(gitlabProjectRegex, gitlabArgs.repository); err != nil || !projectNameIsValid {
140+
if gitlabArgs.owner != "" || gitlabArgs.repository != "" {
141+
gitlabArgs.projectPath = fmt.Sprintf("%s/%s", gitlabArgs.owner, gitlabArgs.repository)
142+
}
143+
// Split project path to owner and repository
144+
owner, group, project := splitGroupAndProject(gitlabArgs.projectPath)
145+
146+
if projectNameIsValid, err := regexp.MatchString(gitlabProjectRegex, gitlabArgs.projectPath); err != nil || !projectNameIsValid {
136147
if err == nil {
137-
err = fmt.Errorf("%s is an invalid project name for gitlab.\nIt can contain only letters, digits, emojis, '_', '.', dash, space. It must start with letter, digit, emoji or '_'.", gitlabArgs.repository)
148+
err = fmt.Errorf("%s is an invalid full project path for gitlab.\nIt can contain only letters, digits, emojis, '_', '.', dash, space. It must start with letter, digit, emoji or '_'.", gitlabArgs.repository)
138149
}
139150
return err
140151
}
@@ -216,7 +227,7 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
216227
clientOpts := []gogit.ClientOption{gogit.WithDiskStorage(), gogit.WithFallbackToDefaultKnownHosts()}
217228
gitClient, err := gogit.NewClient(tmpDir, &git.AuthOptions{
218229
Transport: git.HTTPS,
219-
Username: gitlabArgs.owner,
230+
Username: owner,
220231
Password: glToken,
221232
CAFile: caBundle,
222233
}, clientOpts...)
@@ -296,7 +307,7 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
296307

297308
// Bootstrap config
298309
bootstrapOpts := []bootstrap.GitProviderOption{
299-
bootstrap.WithProviderRepository(gitlabArgs.owner, gitlabArgs.repository, gitlabArgs.personal),
310+
bootstrap.WithProviderRepository(group, project, false),
300311
bootstrap.WithProviderVisibility(gitlabArgs.visibility.String()),
301312
bootstrap.WithBranch(bootstrapArgs.branch),
302313
bootstrap.WithBootstrapTransportType("https"),
@@ -330,3 +341,18 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
330341
// Run
331342
return bootstrap.Run(ctx, b, manifestsBase, installOptions, secretOpts, syncOpts, rootArgs.pollInterval, rootArgs.timeout)
332343
}
344+
345+
func splitGroupAndProject(s string) (owner, group, project string) {
346+
parts := strings.Split(s, "/")
347+
348+
if len(parts) <= 1 {
349+
// No slashes found, treat the whole string as the project
350+
return s, "", s
351+
}
352+
353+
project = parts[len(parts)-1]
354+
owner = parts[1]
355+
group = strings.Join(parts[:len(parts)-1], "/")
356+
357+
return owner, group, project
358+
}

0 commit comments

Comments
 (0)