package add import ( "fmt" "strconv" "github.com/MakeNowJust/heredoc/v2" "github.com/spf13/cobra" gitlab "gitlab.com/gitlab-org/api/client-go/v3" "gitlab.com/gitlab-org/cli/internal/cmdutils" "gitlab.com/gitlab-org/cli/internal/glrepo" "gitlab.com/gitlab-org/cli/internal/iostreams" "gitlab.com/gitlab-org/cli/internal/mcpannotations" ) type options struct { role string roleID int64 expiresAt string userID int username string baseRepo func() (glrepo.Interface, error) gitlabClient func() (*gitlab.Client, error) io *iostreams.IOStreams } var roles = map[string]gitlab.AccessLevelValue{ "guest": gitlab.GuestPermissions, "reporter": gitlab.ReporterPermissions, "developer": gitlab.DeveloperPermissions, "maintainer": gitlab.MaintainerPermissions, "owner": gitlab.OwnerPermissions, } func newOptions(f cmdutils.Factory) *options { return &options{ io: f.IO(), baseRepo: f.BaseRepo, gitlabClient: f.GitLabClient, } } func NewCmd(f cmdutils.Factory) *cobra.Command { opts := newOptions(f) cmd := &cobra.Command{ Use: "add [flags]", Short: `Add a member to the project.`, Long: heredoc.Docf(` Add a member to the project with the specified role. Roles: - guest (10): Can view the project. - reporter (20): Can view and create issues. - developer (30): Can push to non-protected branches. - maintainer (40): Can manage the project. - owner (50): Full access to the project. For custom roles, use %[1]s--role-id%[1]s with the ID of a custom role defined in the project or group. If the custom role does not exist, an error is returned. `, "`"), Example: heredoc.Doc(` # Add a user as a developer glab repo members add --username=john.doe --role=developer # Add a user as a maintainer with expiration date glab repo members add --username=jane.smith --role=maintainer --expires-at=2024-12-31 # Add a user by ID glab repo members add --user-id=123 --role=reporter # Add a user with a custom role glab repo members add --username=john.doe --role-id=101`), Args: cobra.NoArgs, ValidArgsFunction: cobra.NoFileCompletions, Annotations: map[string]string{ mcpannotations.Destructive: "true", }, RunE: func(cmd *cobra.Command, args []string) error { if err := opts.validate(); err != nil { return err } return opts.run() }, } cmdutils.EnableRepoOverride(cmd, f) fl := cmd.Flags() fl.StringVarP(&opts.role, "role", "r", "developer", "Role for the user (guest, reporter, developer, maintainer, owner)") fl.Int64Var(&opts.roleID, "role-id", 0, "ID of a custom role defined in the project or group.") fl.StringVarP(&opts.expiresAt, "expires-at", "e", "", "Expiration date for the membership (YYYY-MM-DD)") fl.IntVarP(&opts.userID, "user-id", "u", 0, "User ID instead of username.") fl.StringVarP(&opts.username, "username", "", "", "Username instead of user-id.") cmd.MarkFlagsMutuallyExclusive("username", "user-id") cmd.MarkFlagsMutuallyExclusive("role", "role-id") return cmd } func (o *options) validate() error { if o.username == "" && o.userID == 0 { return fmt.Errorf("either username or user-id must be specified") } if o.roleID != 0 { // Custom role: no further validation here, but API will validate if o.role != "developer" { // Reset to default if role-id is set o.role = "" } } else { if _, ok := roles[o.role]; !ok { return fmt.Errorf("invalid role: %s. Valid roles are: guest, reporter, developer, maintainer, owner", o.role) } } return nil } func (o *options) run() error { client, err := o.gitlabClient() if err != nil { return err } repo, err := o.baseRepo() if err != nil { return err } var userIDToAdd int var userIdentifier string switch { case o.userID != 0: userIDToAdd = o.userID userIdentifier = strconv.Itoa(o.userID) case o.username != "": // Get user ID from username users, _, err := client.Users.ListUsers(&gitlab.ListUsersOptions{ Username: &o.username, }) if err != nil { return fmt.Errorf("failed to find user %s: %w", o.username, err) } switch len(users) { case 0: return fmt.Errorf("user %s not found", o.username) case 1: default: return fmt.Errorf("multiple users found with username %s", o.username) } userIDToAdd = int(users[0].ID) userIdentifier = o.username } addOptions := &gitlab.AddProjectMemberOptions{} if o.roleID != 0 { addOptions.MemberRoleID = new(o.roleID) } else { role := roles[o.role] addOptions.AccessLevel = new(role) } if o.expiresAt != "" { addOptions.ExpiresAt = new(o.expiresAt) } addOptions.UserID = new(userIDToAdd) member, _, err := client.ProjectMembers.AddProjectMember(repo.FullName(), addOptions) if err != nil { return fmt.Errorf("failed to add member %s: %w", userIdentifier, err) } c := o.io.Color() if o.roleID != 0 { o.io.LogInfof("%s Successfully added %s with custom role ID %d to %s\n", c.GreenCheck(), c.Bold(member.Username), o.roleID, c.Bold(repo.FullName())) } else { o.io.LogInfof("%s Successfully added %s as %s to %s\n", c.GreenCheck(), c.Bold(member.Username), c.Bold(o.role), c.Bold(repo.FullName())) } if o.expiresAt != "" { o.io.LogInfof(" Membership expires on: %s\n", o.expiresAt) } return nil }