Skip to content

Commit 059eb8a

Browse files
askripeCopilot
andauthored
Gitlab add groups support (#1041)
* add groups secret for gitlab Signed-off-by: Alexander Kulikov <askripe@gmail.com> * Update GitLab secrets section in README Clarified GitLab secrets section with examples for project and group variables. Signed-off-by: Alexander Kulikov <askripe@gmail.com> * Update pkg/providers/gitlab/gitlab.go Signed-off-by: Alexander Kulikov <askripe@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Signed-off-by: Alexander Kulikov <askripe@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 27f1576 commit 059eb8a

2 files changed

Lines changed: 54 additions & 9 deletions

File tree

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,12 +864,26 @@ Examples:
864864
865865
For this provider to work you require an [access token](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html) exported as the environment variable `GITLAB_TOKEN`.
866866
867+
- `ref+gitlab://my-gitlab-server.com/[projects/|groups/]id/secret_name?[ssl_verify=false&scheme=https&api_version=v4]`
867868
868-
- `ref+gitlab://my-gitlab-server.com/project_id/secret_name?[ssl_verify=false&scheme=https&api_version=v4]`
869+
* `Project variables`
870+
871+
Fetches a CI/CD variable `password` from a `project`. Both forms are equivalent:
872+
873+
- `ref+gitlab://gitlab.com/11111/password`
874+
- `ref+gitlab://gitlab.com/projects/11111/password`
875+
876+
* `Group variables`
877+
878+
Fetches a CI/CD variable `password` from a `group`:
879+
880+
- `ref+gitlab://gitlab.com/groups/2222/password`
869881
870882
Examples:
871883
872884
- `ref+gitlab://gitlab.com/11111/password`
885+
- `ref+gitlab://gitlab.com/projects/11111/password`
886+
- `ref+gitlab://gitlab.com/groups/2222/password`
873887
- `ref+gitlab://my-gitlab.org/11111/password?ssl_verify=true&scheme=https`
874888
875889
### 1Password

pkg/providers/gitlab/gitlab.go

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,51 @@ func New(cfg api.StaticConfig) *provider {
5151
return p
5252
}
5353

54+
// buildURL constructs the GitLab API URL based on the key path.
55+
//
56+
// Supported formats:
57+
// - host/id/varname → projects (legacy, 2-component path after host)
58+
// - host/projects/id/varname → projects (explicit)
59+
// - host/groups/id/varname → groups
60+
func (p *provider) buildURL(key string) (string, error) {
61+
splits := strings.SplitN(key, "/", 4)
62+
63+
switch len(splits) {
64+
case 3:
65+
// legacy: host/project_id/varname → treated as projects
66+
host, id, varName := splits[0], splits[1], splits[2]
67+
return fmt.Sprintf("%s://%s/api/%s/projects/%s/variables/%s",
68+
p.Scheme, host, p.APIVersion, id, varName), nil
69+
70+
case 4:
71+
host, kind, id, varName := splits[0], splits[1], splits[2], splits[3]
72+
switch kind {
73+
case "projects":
74+
return fmt.Sprintf("%s://%s/api/%s/projects/%s/variables/%s",
75+
p.Scheme, host, p.APIVersion, id, varName), nil
76+
case "groups":
77+
return fmt.Sprintf("%s://%s/api/%s/groups/%s/variables/%s",
78+
p.Scheme, host, p.APIVersion, id, varName), nil
79+
default:
80+
return "", fmt.Errorf("unsupported resource type %q: must be 'projects' or 'groups'", kind)
81+
}
82+
83+
default:
84+
return "", fmt.Errorf("invalid key format %q: expected host/id/var or host/projects|groups/id/var", key)
85+
}
86+
}
87+
5488
// Get gets secret from GitLab API
5589
func (p *provider) GetString(key string) (string, error) {
56-
splits := strings.Split(key, "/")
5790
gitlabToken, ok := os.LookupEnv("GITLAB_TOKEN")
5891
if !ok {
59-
return "", errors.New("Missing GITLAB_TOKEN environment variable")
92+
return "", errors.New("missing GITLAB_TOKEN environment variable")
6093
}
6194

62-
url := fmt.Sprintf("%s://%s/api/%s/projects/%s/variables/%s",
63-
p.Scheme,
64-
splits[0],
65-
p.APIVersion,
66-
splits[1],
67-
splits[2])
95+
url, err := p.buildURL(key)
96+
if err != nil {
97+
return "", err
98+
}
6899

69100
tr := &http.Transport{
70101
TLSClientConfig: &tls.Config{InsecureSkipVerify: p.SSLVerify},

0 commit comments

Comments
 (0)