为新贡献者将项目分叉到组中 (API)

Fork project into group for a new contributor (API)

由于我正在考虑可能有更好的方法,我想先解释一下我的要求(问题从分隔线之后开始)。

鉴于我有以下 GitLab 组结构:

- main-group
  - sub-group-project-templates
    - project-template-1
  - sub-group-projects

main-group/sub-group-project-templates中有多个项目作为任务的启动项目。 如果有人想做这样的任务,我想在组sub-group-projects中有一个相应项目模板(例如project-template-1)的fork,并以该人的名字作为项目名称。

鉴于此人的名字是 John Doe,他想要开始 project-template-1 的任务,因此应该有一个新项目 main-group/sub-group-projects/john_doe_project-template-1

John Doe 应该只能看到(读/写)存储库 john_doe_project-template-1 而不是项目的其余部分(其他分支,...)。


我的解决方案是将模板项目分叉到子组,然后添加新贡献者。

但我已经在第一步失败了(将项目分叉到具有新名称的子组中)。

我第一次看到它是在看:

POST http://gitlab.com/api/v4/projects/project_id/fork

但我不知道如何定义目标目录和设置新分支的名称。 以下无效:

POST http://gitlab.com/api/v4/projects/project_id/fork?namespace=main-group%2Fsub-group-projects%2Fjohn_doe_project-template-1

"message": "404 Target Namespace Not Found"

这样的事情是否可能或者我该如何实现?

假设子组 sub-group-project-templates 中的项目 project-template-1 具有以下配置:

可以在多个 API 请求中完成,但以下功能还有一些正在进行的工作:

但是这 2 个问题有解决方法,例如只需执行附加请求以获取命名空间 ID (1) 并在分叉后执行编辑项目请求 (2)

步骤如下:

  • get namespace id 子组 sub-group-projects
  • fork project main-group/sub-group-project-templates/project-template-1 到 ID 为 $namespace_id 的命名空间 & 获取创建项目的项目 ID
  • rename project id $project_idproject-template-1 到 : johndoe_project-template-1
  • get user id 用户 johndoe
  • add project member 项目 johndoe_project-template-1 : 添加 ID 为 $user_id
  • 的用户

请注意,要添加会员,您需要用户 ID,我假设您要在第 4 步中搜索该用户 ID,但如果您已经拥有它,则可能不需要此步骤

这是执行所有这些步骤的 bash 脚本,它使用 & :

#!/bin/bash
set -e

gitlab_host=gitlab.your.host
access_token=YOUR_ACCESS_TOKEN

username=johndoe
project=project-template-1
user_access=30

group=main-group
namespace_src=sub-group-project-templates
namespace_dest=sub-group-projects
new_project_name=${username}_${project}

project_src=$group/$namespace_src/$project

encoded_project=$(echo $project_src | sed 's/\//%2F/g')

echo "1 - get namespace id for $namespace_dest"
#https://docs.gitlab.com/ce/api/namespaces.html
namespace_id=$(curl -s "https://$gitlab_host/api/v4/namespaces?search=$namespace_dest" \
    -H "Private-Token: $access_token" | jq -r '.[0].id')


echo "2 - fork project $project_src to namespace with id $namespace_id"
#https://docs.gitlab.com/ce/api/projects.html#fork-project
project_id=$(curl -s -X POST "https://$gitlab_host/api/v4/projects/$encoded_project/fork?namespace=$namespace_id" \
    -H "Private-Token: $access_token" | jq '.id')

if [ -z $project_id ]; then
    echo "fork failed"
    exit 1
fi

echo "3 - rename project with id $project_id from $project to : $new_project_name"
#https://docs.gitlab.com/ce/api/projects.html#edit-project
curl -s -X PUT "https://$gitlab_host/api/v4/projects/$project_id" \
    -H "Content-Type: application/json" \
    -d "{\"path\": \"$new_project_name\",\"name\": \"$new_project_name\" }" \
    -H "Private-Token: $access_token" > /dev/null

echo "4 - get user id for : $username"
#https://docs.gitlab.com/ce/api/users.html
user_id=$(curl -s "https://$gitlab_host/api/v4/users?username=johndoe" \
    -H "Private-Token: $access_token" | \
    jq -r '.[] | select(.name == "johndoe") | .id')

echo "5 - edit project member : add user with id $user_id"
#https://docs.gitlab.com/ce/api/members.html#add-a-member-to-a-group-or-project
curl -s "https://$gitlab_host/api/v4/projects/$project_id/members" \
    -H "Private-Token: $access_token" \
    -d "user_id=$user_id&access_level=$user_access" > /dev/null

在第 2 步(fork 项目)中,项目源命名空间是 URL 编码的,因此您可能希望使用适当的方式对其进行编码(在这种情况下,我们只需要替换 / %2F)