Skip to content

Commit 7c5c0b1

Browse files
refs platform/board#3920: check if membership exist (#24)
1 parent 798d7f4 commit 7c5c0b1

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ provider "gitlab" {
7575

7676
**ATTENTION:** as described in the [Gitlab provider documentation](https://registry.terraform.io/providers/gitlabhq/gitlab/latest/docs), the `CI_JOB_TOKEN` could cause issues when used as `token` for the Gitlab provider. For this module in particular, the `gitlab_cluster_agent` and `gitlab_cluster_agent_token` resources require authorization to access to the `/users` Gitlab API endpoint, which is not granted by the `CI_JOB_TOKEN`. You have to use a Gitlab personal access token with the `api` scope to authenticate the provider.
7777

78+
## GitLab Agents user membership
79+
80+
The current user used by the provider is automatically added as `maintainer` to the "GitLab Agents" project. If you don't want this behavior, just set the variable `var.autoassign_current_user_as_maintainer` to `false`.
81+
82+
Adding the user as `maintainer` to the newly created project ensures they have the permissions to commit and push to it.
83+
84+
**ATTENTION:** If the current user is already added to the project but with a different role than `maintainer`, the apply will fail saying that a membership already exists
85+
7886
<!-- BEGIN_TF_DOCS -->
7987
## Providers
8088

main.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ locals {
5151
(var.gitlab_agent_variable_name_agent_id) : gitlab_cluster_agent.this.name,
5252
(var.gitlab_agent_variable_name_agent_project) : local.project_path_with_namespace,
5353
}
54+
55+
current_user_is_maintainer_of_project = length([for member in data.gitlab_project_membership.this.members : member if member.name == data.gitlab_current_user.this.name && member.access_level == "maintainer"]) > 0
5456
}
5557

5658
# Gitlab resources
@@ -85,14 +87,19 @@ data "gitlab_project" "enabled_projects" {
8587
path_with_namespace = each.value
8688
}
8789

90+
# Data source to get all the memberships for the project
91+
data "gitlab_project_membership" "this" {
92+
project_id = local.project_id
93+
}
94+
8895
resource "gitlab_project" "project" {
8996
count = local.use_existing_project == 0 ? 1 : 0
9097
name = var.gitlab_project_name
9198
namespace_id = var.operate_at_root_group_level ? data.gitlab_group.root_namespace.group_id : data.gitlab_group.parent_group[0].group_id
9299
}
93100

94101
resource "gitlab_project_membership" "project" {
95-
count = var.autoassign_current_user_as_maintainer ? 1 : 0
102+
count = var.autoassign_current_user_as_maintainer && !local.current_user_is_maintainer_of_project ? 1 : 0
96103
project = local.project_id
97104
user_id = data.gitlab_current_user.this.id
98105
access_level = "maintainer"

outputs.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,3 @@ output "gitlab_parent_group_auto_detected" {
4343
description = "Whether the parent group was automatically detected."
4444
value = local.auto_detect_parent
4545
}
46-

0 commit comments

Comments
 (0)