2

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

鉴于我有以下 GitLab 组结构:

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

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

鉴于此人的名字是 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

但是我不知道如何定义目标目录并设置新fork的名称。以下不起作用:

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

“消息”:“未找到 404 目标命名空间”

这样的事情甚至可能吗?或者我怎样才能做到这一点?

4

1 回答 1

1

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

在此处输入图像描述

它可以在多个 API 请求中完成,但以下功能正在进行中:

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

步骤如下:

  • 获取子组的命名空间 IDsub-group-projects
  • fork project main-group/sub-group-project-templates/project-template-1 to namespace with id $namespace_id& get the project id of created project
  • 将ID$project_id为的项目重命名project-template-1为:johndoe_project-template-1
  • 获取用户 johndoe 的用户 ID
  • 为项目添加项目成员johndoe_project-template-1:添加具有 id 的用户$user_id

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

这是一个执行所有这些步骤的 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

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

于 2017-12-05T02:37:24.717 回答