gpt4 book ai didi

Gitlab:一次更改用户在多个项目中的角色

转载 作者:行者123 更新时间:2023-12-01 13:33:40 32 4
gpt4 key购买 nike

我可以一次更改特定用户在多个项目中的角色吗?如果是,如何?

我有一个用户在超过 75 个项目中担任“记者”角色。我想在所有项目中将角色更改为“开发人员”。我可以用最小的努力实现吗?

最佳答案

您可以使用以下 Gitlab API 编辑项目现有成员的访问权限:

PUT /projects/:id/members/:user_id?access_level=XX .

我没有在 User API 中找到任何 API 来为特定用户请求项目.但是您可以请求所有项目的列表,提取 ID 并调用 edit member API

你可以做的是:

  • 请求用户 API 以获取目标用户名的用户 ID:

    https://gitlab.com/api/v4/users?private_token=YOUR_TOKEN&username=someuser
  • 请求项目列表并提取列表中的 ID:

    https://gitlab.com/api/v4/projects?private_token=YOUR_TOKEN&page=1&per_page=1000
  • 使用您选择的访问级别为每个项目调用编辑成员 API:

    https://gitlab.com/api/v4/projects/<project_id>/members/<user_id>?private_token=YOUR_TOKEN&access_level=10

access_level 字段可以是 these 中的任意一个:

10 => Guest access
20 => Reporter access
30 => Developer access
40 => Master access
50 => Owner access # Only valid for groups

这是一个 Bash 脚本,它将为 username johndoe 的用户更新访问级别为 30(开发人员访问权限)的所有项目,该脚本还使用 jq JSON 解析器:

#!/bin/bash

GITLAB_DOMAIN=gitlab.com
GITLAB_PORT=443
GITLAB_BASE_URL=https://$GITLAB_DOMAIN:$GITLAB_PORT
PER_PAGE=1000

PRIVATE_TOKEN=YOUR_TOKEN
ACCESS_LEVEL=30
USERNAME=johndoe

user_id=$(curl -s "$GITLAB_BASE_URL/api/v4/users?private_token=$PRIVATE_TOKEN&username=$USERNAME" | jq '.[].id')

echo "user_id : $user_id"

projects=$(curl -s "$GITLAB_BASE_URL/api/v4/projects?private_token=$PRIVATE_TOKEN&page=1&per_page=$PER_PAGE" | jq '.[].id')

while read -r project; do

status=$(curl --write-out '%{http_code}' -s -o /dev/null -X PUT "$GITLAB_BASE_URL/api/v4/projects/$project/members/$user_id?private_token=$PRIVATE_TOKEN&access_level=$ACCESS_LEVEL")

if [ "$status" == "200" ]; then
echo "right change to $ACCESS_LEVEL for project : $project"
fi
done <<< "$projects"

请注意,每个请求(包括指定用户不是成员的项目)都会导致 404

关于Gitlab:一次更改用户在多个项目中的角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44629986/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com