Skip to content

安装

shell
npm install @gitbeaker/node -S

使用

注意

目前范例使用的GitLab是这个地址,版本是V4。

js
import { Gitlab } from '@gitbeaker/node';
const gitlabApi = new Gitlab({
  host: 'http://example.com',
  token: 'personaltoken',
});

1.获取全部用户

js
// const users = await gitlabApi.Users.all({ page: 从第几页查询, perPage: 每页展示的条数});
// 如果.all方法不传递参数,就会默认返回全部的用户列表
const users = await gitlabApi.Users.all({ page: 1, perPage: 10});

输出结果如下:

json
[
    {
        "id": 4,
        "username": "demo",
        "name": "demo",
        "state": "active",
        "avatar_url": "https://secure.gravatar.com/avatar/6c4108e85410a1d5bb9413848207d641?s=80&d=identicon",
        "web_url": "https://gitlab.cghbh.com/demo",
        "created_at": "2023-02-18T18:30:56.280+05:30",
        "bio": "",
        "location": null,
        "public_email": null,
        "skype": "",
        "linkedin": "",
        "twitter": "",
        "website_url": "",
        "organization": null,
        "job_title": "",
        "pronouns": null,
        "bot": false,
        "work_information": null,
        "followers": 0,
        "following": 0,
        "is_followed": false,
        "local_time": null,
        "last_sign_in_at": "2023-02-18T18:31:38.097+05:30",
        "confirmed_at": "2023-02-18T18:30:56.093+05:30",
        "last_activity_on": "2023-02-18",
        "email": "914539467@qq.com",
        "theme_id": 2,
        "color_scheme_id": 1,
        "projects_limit": 1000,
        "current_sign_in_at": "2023-02-18T18:31:55.585+05:30",
        "identities": [],
        "can_create_group": true,
        "can_create_project": true,
        "two_factor_enabled": false,
        "external": false,
        "private_profile": false,
        "commit_email": "914539467@qq.com",
        "is_admin": false,
        "note": "",
        "namespace_id": 32
    },
    {
        "id": 1,
        "username": "root",
        "name": "Administrator",
        "state": "active",
        "avatar_url": "https://secure.gravatar.com/avatar/5aebfc15d76a494412f2940cbfd00de5?s=80&d=identicon",
        "web_url": "https://gitlab.cghbh.com/root",
        "created_at": "2022-10-30T15:25:48.902+05:30",
        "bio": "",
        "location": null,
        "public_email": null,
        "skype": "",
        "linkedin": "",
        "twitter": "",
        "website_url": "",
        "organization": null,
        "job_title": "",
        "pronouns": null,
        "bot": false,
        "work_information": null,
        "followers": 0,
        "following": 0,
        "is_followed": false,
        "local_time": null,
        "last_sign_in_at": "2023-02-14T07:14:07.659+05:30",
        "confirmed_at": "2022-10-30T15:25:48.259+05:30",
        "last_activity_on": "2023-02-18",
        "email": "gchent54@163.com",
        "theme_id": 2,
        "color_scheme_id": 1,
        "projects_limit": 100,
        "current_sign_in_at": "2023-02-18T18:29:59.823+05:30",
        "identities": [],
        "can_create_group": true,
        "can_create_project": true,
        "two_factor_enabled": false,
        "external": false,
        "private_profile": false,
        "commit_email": "gchent54@163.com",
        "is_admin": true,
        "note": null,
        "namespace_id": 1
    }
]

2.创建用户

js
const result = await gitlabApi.Users.create({
   // 是否是管理员
   admin: false,
   // 用户名称,以此为登录的账号
   username: 'demo12xx',
   // 昵称
   name: 'demo12xx',
   // 邮箱,必传
   email: 'chengang113xx@qq.com',
   // 密码
   password: '12345678',
   // 用户可以创建的项目数量
   projects_limit: 10,
   // 是否强制跳过邮箱确认的操作
   skip_confirmation: true,
 });

输出结果如下:

json
{
    "id": 9,
    "username": "demo12xx",
    "name": "demo12xx",
    "state": "active",
    "avatar_url": "https://secure.gravatar.com/avatar/dced61bd6327ee3e7f0be8444099db7d?s=80&d=identicon",
    "web_url": "https://gitlab.cghbh.com/demo12xx",
    "created_at": "2023-02-19T11:57:47.880+05:30",
    "bio": "",
    "location": null,
    "public_email": null,
    "skype": "",
    "linkedin": "",
    "twitter": "",
    "website_url": "",
    "organization": null,
    "job_title": "",
    "pronouns": null,
    "bot": false,
    "work_information": null,
    "followers": 0,
    "following": 0,
    "is_followed": false,
    "local_time": null,
    "last_sign_in_at": null,
    "confirmed_at": "2023-02-19T11:57:47.856+05:30",
    "last_activity_on": null,
    "email": "chengang113xx@qq.com",
    "theme_id": 2,
    "color_scheme_id": 1,
    "projects_limit": 10,
    "current_sign_in_at": null,
    "identities": [],
    "can_create_group": true,
    "can_create_project": true,
    "two_factor_enabled": false,
    "external": false,
    "private_profile": false,
    "commit_email": "chengang113xx@qq.com",
    "is_admin": false,
    "note": null,
    "namespace_id": 42
}

3.删除用户

js
// const result = await gitlabApi.Users.remove(用户ID);
const result = await gitlabApi.Users.remove(9);

输出结果如下:

json
{
    "type": "Buffer",
    "data": []
}

4.获取所有的代码组列表

js
// const groups = await gitlabApi.Groups.all({ page: 从第几页查询, perPage: 每页展示的条数});
// 如果.all方法不传递参数,就会默认返回全部的代码组列表
const groups = await gitlabApi.Groups.all({ page: 1, perPage: 10});

输出结果如下:

json
[
    {
        "id": 4,
        "web_url": "https://gitlab.cghbh.com/groups/open-source",
        "name": "open-source",
        "path": "open-source",
        "description": "开源项目组",
        "visibility": "public",
        "share_with_group_lock": false,
        "require_two_factor_authentication": false,
        "two_factor_grace_period": 48,
        "project_creation_level": "developer",
        "auto_devops_enabled": null,
        "subgroup_creation_level": "maintainer",
        "emails_disabled": null,
        "mentions_disabled": null,
        "lfs_enabled": true,
        "default_branch_protection": 2,
        "avatar_url": null,
        "request_access_enabled": true,
        "full_name": "open-source",
        "full_path": "open-source",
        "created_at": "2022-10-30T15:30:14.786+05:30",
        "parent_id": null
    }
]

5.查询代码组下的代码库列表

js
// const codeLibs = await gitlabApi.Groups.projects(代码组的ID);,请参考第3个api的返回结果
const codeLibs = await gitlabApi.Groups.projects(4);

输出结果如下:

json
[
  {
      "id": 20,
      "description": "devops核心项目",
      "name": "devops-web",
      "name_with_namespace": "open-source / devops-web",
      "path": "devops-web",
      "path_with_namespace": "open-source/devops-web",
      "created_at": "2023-02-14T08:06:24.029+05:30",
      "default_branch": "master",
      "tag_list": [],
      "topics": [],
      "ssh_url_to_repo": "git@gitlab.cghbh.com:open-source/devops-web.git",
      "http_url_to_repo": "https://gitlab.cghbh.com/open-source/devops-web.git",
      "web_url": "https://gitlab.cghbh.com/open-source/devops-web",
      "readme_url": "https://gitlab.cghbh.com/open-source/devops-web/-/blob/master/README.md",
      "avatar_url": null,
      "forks_count": 0,
      "star_count": 0,
      "last_activity_at": "2023-02-14T08:06:24.029+05:30",
      "namespace": {
          "id": 4,
          "name": "open-source",
          "path": "open-source",
          "kind": "group",
          "full_path": "open-source",
          "parent_id": null,
          "avatar_url": null,
          "web_url": "https://gitlab.cghbh.com/groups/open-source"
      },
      "_links": {
          "self": "https://gitlab.cghbh.com/api/v4/projects/20",
          "issues": "https://gitlab.cghbh.com/api/v4/projects/20/issues",
          "merge_requests": "https://gitlab.cghbh.com/api/v4/projects/20/merge_requests",
          "repo_branches": "https://gitlab.cghbh.com/api/v4/projects/20/repository/branches",
          "labels": "https://gitlab.cghbh.com/api/v4/projects/20/labels",
          "events": "https://gitlab.cghbh.com/api/v4/projects/20/events",
          "members": "https://gitlab.cghbh.com/api/v4/projects/20/members",
          "cluster_agents": "https://gitlab.cghbh.com/api/v4/projects/20/cluster_agents"
      },
      "packages_enabled": true,
      "empty_repo": false,
      "archived": false,
      "visibility": "public",
      "resolve_outdated_diff_discussions": false,
      "container_expiration_policy": {
          "cadence": "1d",
          "enabled": false,
          "keep_n": 10,
          "older_than": "90d",
          "name_regex": ".*",
          "name_regex_keep": null,
          "next_run_at": "2023-02-15T08:06:24.084+05:30"
      },
      "issues_enabled": true,
      "merge_requests_enabled": true,
      "wiki_enabled": true,
      "jobs_enabled": true,
      "snippets_enabled": true,
      "container_registry_enabled": true,
      "service_desk_enabled": false,
      "service_desk_address": null,
      "can_create_merge_request_in": true,
      "issues_access_level": "enabled",
      "repository_access_level": "enabled",
      "merge_requests_access_level": "enabled",
      "forking_access_level": "enabled",
      "wiki_access_level": "enabled",
      "builds_access_level": "enabled",
      "snippets_access_level": "enabled",
      "pages_access_level": "enabled",
      "operations_access_level": "enabled",
      "analytics_access_level": "enabled",
      "container_registry_access_level": "enabled",
      "security_and_compliance_access_level": "private",
      "emails_disabled": null,
      "shared_runners_enabled": true,
      "lfs_enabled": true,
      "creator_id": 1,
      "import_url": null,
      "import_type": null,
      "import_status": "none",
      "open_issues_count": 0,
      "ci_default_git_depth": 20,
      "ci_forward_deployment_enabled": true,
      "ci_job_token_scope_enabled": false,
      "ci_separated_caches": true,
      "public_jobs": true,
      "build_timeout": 3600,
      "auto_cancel_pending_pipelines": "enabled",
      "build_coverage_regex": null,
      "ci_config_path": null,
      "shared_with_groups": [],
      "only_allow_merge_if_pipeline_succeeds": false,
      "allow_merge_on_skipped_pipeline": null,
      "restrict_user_defined_variables": false,
      "request_access_enabled": true,
      "only_allow_merge_if_all_discussions_are_resolved": false,
      "remove_source_branch_after_merge": true,
      "printing_merge_request_link_enabled": true,
      "merge_method": "merge",
      "squash_option": "default_off",
      "enforce_auth_checks_on_uploads": true,
      "suggestion_commit_message": null,
      "merge_commit_template": null,
      "squash_commit_template": null,
      "auto_devops_enabled": true,
      "auto_devops_deploy_strategy": "continuous",
      "autoclose_referenced_issues": true,
      "repository_storage": "default",
      "keep_latest_artifact": true,
      "runner_token_expiration_interval": null
  }
]

6.创建代码组

js
// 代码组的路径
// 1.如果gitlab的地址是https://gitlab.cghbh.com,那么如果写的代码组的路径是demo1
// 则以https://gitlab.cghbh.com/groups/demo1访问,最好代码组名称和路径保持一致
// const result = await gitlabApi.Groups.create('代码组的名称', '代码组的路径名称');
const result = await gitlabApi.Groups.create('demo1', 'demo1');

输出结果如下:

json
{
    "id": 30,
    "web_url": "https://gitlab.cghbh.com/groups/demo2",
    "name": "demo2",
    "path": "demo2",
    "description": "",
    "visibility": "private",
    "share_with_group_lock": false,
    "require_two_factor_authentication": false,
    "two_factor_grace_period": 48,
    "project_creation_level": "developer",
    "auto_devops_enabled": null,
    "subgroup_creation_level": "maintainer",
    "emails_disabled": null,
    "mentions_disabled": null,
    "lfs_enabled": true,
    "default_branch_protection": 2,
    "avatar_url": null,
    "request_access_enabled": true,
    "full_name": "demo2",
    "full_path": "demo2",
    "created_at": "2023-02-16T13:56:08.078+05:30",
    "parent_id": null,
    "shared_with_groups": [],
    "prevent_sharing_groups_outside_hierarchy": false,
    "projects": [],
    "shared_projects": []
}

7.删除代码组

js
// const result = await gitlabApi.Groups.remove(代码组ID);
const result = await gitlabApi.Groups.remove(30);

输出结果如下:

json
{
    "message": "202 Accepted"
}

8.获取所有的代码库列表

js
// const groups = await gitlabApi.Projects.all({ page: 从第几页查询, perPage: 每页展示的条数});
// 如果.all方法不传递参数,就会默认返回全部的代码库列表
const allCodeLibs = await gitlabApi.Projects.all({ page: 1, perPage: 10});

输出结果如下:

json
[
  {
      "id": 2,
      "description": "我的开源文档",
      "name": "open-docs",
      "name_with_namespace": "open-source / open-docs",
      "path": "open-docs",
      "path_with_namespace": "open-source/open-docs",
      "created_at": "2022-10-30T15:30:40.949+05:30",
      "default_branch": "main",
      "tag_list": [],
      "topics": [],
      "ssh_url_to_repo": "git@gitlab.cghbh.com:open-source/open-docs.git",
      "http_url_to_repo": "https://gitlab.cghbh.com/open-source/open-docs.git",
      "web_url": "https://gitlab.cghbh.com/open-source/open-docs",
      "readme_url": "https://gitlab.cghbh.com/open-source/open-docs/-/blob/main/README.md",
      "avatar_url": null,
      "forks_count": 0,
      "star_count": 0,
      "last_activity_at": "2023-02-13T15:24:55.520+05:30",
      "namespace": {
          "id": 4,
          "name": "open-source",
          "path": "open-source",
          "kind": "group",
          "full_path": "open-source",
          "parent_id": null,
          "avatar_url": null,
          "web_url": "https://gitlab.cghbh.com/groups/open-source"
      },
      "_links": {
          "self": "https://gitlab.cghbh.com/api/v4/projects/2",
          "issues": "https://gitlab.cghbh.com/api/v4/projects/2/issues",
          "merge_requests": "https://gitlab.cghbh.com/api/v4/projects/2/merge_requests",
          "repo_branches": "https://gitlab.cghbh.com/api/v4/projects/2/repository/branches",
          "labels": "https://gitlab.cghbh.com/api/v4/projects/2/labels",
          "events": "https://gitlab.cghbh.com/api/v4/projects/2/events",
          "members": "https://gitlab.cghbh.com/api/v4/projects/2/members",
          "cluster_agents": "https://gitlab.cghbh.com/api/v4/projects/2/cluster_agents"
      },
      "packages_enabled": true,
      "empty_repo": false,
      "archived": false,
      "visibility": "public",
      "resolve_outdated_diff_discussions": false,
      "container_expiration_policy": {
          "cadence": "1d",
          "enabled": false,
          "keep_n": 10,
          "older_than": "90d",
          "name_regex": ".*",
          "name_regex_keep": null,
          "next_run_at": "2022-10-31T15:30:41.025+05:30"
      },
      "issues_enabled": true,
      "merge_requests_enabled": true,
      "wiki_enabled": true,
      "jobs_enabled": true,
      "snippets_enabled": true,
      "container_registry_enabled": true,
      "service_desk_enabled": false,
      "service_desk_address": null,
      "can_create_merge_request_in": true,
      "issues_access_level": "enabled",
      "repository_access_level": "enabled",
      "merge_requests_access_level": "enabled",
      "forking_access_level": "enabled",
      "wiki_access_level": "enabled",
      "builds_access_level": "enabled",
      "snippets_access_level": "enabled",
      "pages_access_level": "enabled",
      "operations_access_level": "enabled",
      "analytics_access_level": "enabled",
      "container_registry_access_level": "enabled",
      "security_and_compliance_access_level": "private",
      "emails_disabled": null,
      "shared_runners_enabled": true,
      "lfs_enabled": true,
      "creator_id": 1,
      "import_url": null,
      "import_type": null,
      "import_status": "none",
      "open_issues_count": 0,
      "ci_default_git_depth": 20,
      "ci_forward_deployment_enabled": true,
      "ci_job_token_scope_enabled": false,
      "ci_separated_caches": true,
      "public_jobs": true,
      "build_timeout": 3600,
      "auto_cancel_pending_pipelines": "enabled",
      "build_coverage_regex": null,
      "ci_config_path": null,
      "shared_with_groups": [],
      "only_allow_merge_if_pipeline_succeeds": false,
      "allow_merge_on_skipped_pipeline": null,
      "restrict_user_defined_variables": false,
      "request_access_enabled": true,
      "only_allow_merge_if_all_discussions_are_resolved": false,
      "remove_source_branch_after_merge": true,
      "printing_merge_request_link_enabled": true,
      "merge_method": "merge",
      "squash_option": "default_off",
      "enforce_auth_checks_on_uploads": true,
      "suggestion_commit_message": null,
      "merge_commit_template": null,
      "squash_commit_template": null,
      "auto_devops_enabled": false,
      "auto_devops_deploy_strategy": "continuous",
      "autoclose_referenced_issues": true,
      "repository_storage": "default",
      "keep_latest_artifact": true,
      "runner_token_expiration_interval": null,
      "permissions": {
          "project_access": null,
          "group_access": {
              "access_level": 50,
              "notification_level": 3
          }
      }
  }
]

9.创建代码库

js
const result = await gitlabApi.Projects.create({
   // 代码库的名称
   name: 'create-code-demo4',
   // 需要创建在哪个用户下,如果不写userId的话,默认在root管理员也就是userId为1的账户下
   userId: 4,
});

输出结果如下:

js
{
    "id": 27,
    "description": null,
    "name": "create-code-demo4",
    "name_with_namespace": "Administrator / create-code-demo4",
    "path": "create-code-demo4",
    "path_with_namespace": "root/create-code-demo4",
    "created_at": "2023-02-18T18:44:02.405+05:30",
    "default_branch": "main",
    "tag_list": [],
    "topics": [],
    "ssh_url_to_repo": "git@gitlab.cghbh.com:root/create-code-demo4.git",
    "http_url_to_repo": "https://gitlab.cghbh.com/root/create-code-demo4.git",
    "web_url": "https://gitlab.cghbh.com/root/create-code-demo4",
    "readme_url": null,
    "avatar_url": null,
    "forks_count": 0,
    "star_count": 0,
    "last_activity_at": "2023-02-18T18:44:02.405+05:30",
    "namespace": {
        "id": 1,
        "name": "Administrator",
        "path": "root",
        "kind": "user",
        "full_path": "root",
        "parent_id": null,
        "avatar_url": "https://secure.gravatar.com/avatar/5aebfc15d76a494412f2940cbfd00de5?s=80&d=identicon",
        "web_url": "https://gitlab.cghbh.com/root"
    },
    "_links": {
        "self": "https://gitlab.cghbh.com/api/v4/projects/27",
        "issues": "https://gitlab.cghbh.com/api/v4/projects/27/issues",
        "merge_requests": "https://gitlab.cghbh.com/api/v4/projects/27/merge_requests",
        "repo_branches": "https://gitlab.cghbh.com/api/v4/projects/27/repository/branches",
        "labels": "https://gitlab.cghbh.com/api/v4/projects/27/labels",
        "events": "https://gitlab.cghbh.com/api/v4/projects/27/events",
        "members": "https://gitlab.cghbh.com/api/v4/projects/27/members",
        "cluster_agents": "https://gitlab.cghbh.com/api/v4/projects/27/cluster_agents"
    },
    "packages_enabled": true,
    "empty_repo": true,
    "archived": false,
    "visibility": "private",
    "owner": {
        "id": 1,
        "username": "root",
        "name": "Administrator",
        "state": "active",
        "avatar_url": "https://secure.gravatar.com/avatar/5aebfc15d76a494412f2940cbfd00de5?s=80&d=identicon",
        "web_url": "https://gitlab.cghbh.com/root"
    },
    "resolve_outdated_diff_discussions": false,
    "container_expiration_policy": {
        "cadence": "1d",
        "enabled": false,
        "keep_n": 10,
        "older_than": "90d",
        "name_regex": ".*",
        "name_regex_keep": null,
        "next_run_at": "2023-02-19T18:44:02.414+05:30"
    },
    "issues_enabled": true,
    "merge_requests_enabled": true,
    "wiki_enabled": true,
    "jobs_enabled": true,
    "snippets_enabled": true,
    "container_registry_enabled": true,
    "service_desk_enabled": false,
    "service_desk_address": null,
    "can_create_merge_request_in": true,
    "issues_access_level": "enabled",
    "repository_access_level": "enabled",
    "merge_requests_access_level": "enabled",
    "forking_access_level": "enabled",
    "wiki_access_level": "enabled",
    "builds_access_level": "enabled",
    "snippets_access_level": "enabled",
    "pages_access_level": "private",
    "operations_access_level": "enabled",
    "analytics_access_level": "enabled",
    "container_registry_access_level": "enabled",
    "security_and_compliance_access_level": "private",
    "emails_disabled": null,
    "shared_runners_enabled": true,
    "lfs_enabled": true,
    "creator_id": 1,
    "import_url": null,
    "import_type": null,
    "import_status": "none",
    "import_error": null,
    "open_issues_count": 0,
    "runners_token": "GR13489411jZTzm8Ck9n7U3Cw6_83",
    "ci_default_git_depth": 20,
    "ci_forward_deployment_enabled": true,
    "ci_job_token_scope_enabled": false,
    "ci_separated_caches": true,
    "public_jobs": true,
    "build_git_strategy": "fetch",
    "build_timeout": 3600,
    "auto_cancel_pending_pipelines": "enabled",
    "build_coverage_regex": null,
    "ci_config_path": null,
    "shared_with_groups": [],
    "only_allow_merge_if_pipeline_succeeds": false,
    "allow_merge_on_skipped_pipeline": null,
    "restrict_user_defined_variables": false,
    "request_access_enabled": true,
    "only_allow_merge_if_all_discussions_are_resolved": false,
    "remove_source_branch_after_merge": true,
    "printing_merge_request_link_enabled": true,
    "merge_method": "merge",
    "squash_option": "default_off",
    "enforce_auth_checks_on_uploads": true,
    "suggestion_commit_message": null,
    "merge_commit_template": null,
    "squash_commit_template": null,
    "auto_devops_enabled": true,
    "auto_devops_deploy_strategy": "continuous",
    "autoclose_referenced_issues": true,
    "repository_storage": "default",
    "keep_latest_artifact": true,
    "runner_token_expiration_interval": null
}

10.删除代码库

js
// const users = await gitlabApi.Projects.remove(代码库的ID);
const result = await gitlabApi.Projects.remove(27);

输出结果如下:

json
{
    "message": "202 Accepted"
}

11.给指定ID的代码库加star

js
// const result = await gitlabApi.Projects.star(代码库的ID);
const result = await gitlabApi.Projects.star(23);

输出结果如下:

json
{
    "id": 23,
    "description": null,
    "name": "create-code-demo",
    "name_with_namespace": "demo / create-code-demo",
    "path": "create-code-demo",
    "path_with_namespace": "demo/create-code-demo",
    "created_at": "2023-02-18T18:34:28.975+05:30",
    "default_branch": "main",
    "tag_list": [],
    "topics": [],
    "ssh_url_to_repo": "git@gitlab.cghbh.com:demo/create-code-demo.git",
    "http_url_to_repo": "https://gitlab.cghbh.com/demo/create-code-demo.git",
    "web_url": "https://gitlab.cghbh.com/demo/create-code-demo",
    "readme_url": null,
    "avatar_url": null,
    "forks_count": 0,
    "star_count": 1,
    "last_activity_at": "2023-02-18T18:34:28.975+05:30",
    "namespace": {
        "id": 32,
        "name": "demo",
        "path": "demo",
        "kind": "user",
        "full_path": "demo",
        "parent_id": null,
        "avatar_url": "https://secure.gravatar.com/avatar/6c4108e85410a1d5bb9413848207d641?s=80&d=identicon",
        "web_url": "https://gitlab.cghbh.com/demo"
    },
    "_links": {
        "self": "https://gitlab.cghbh.com/api/v4/projects/23",
        "issues": "https://gitlab.cghbh.com/api/v4/projects/23/issues",
        "merge_requests": "https://gitlab.cghbh.com/api/v4/projects/23/merge_requests",
        "repo_branches": "https://gitlab.cghbh.com/api/v4/projects/23/repository/branches",
        "labels": "https://gitlab.cghbh.com/api/v4/projects/23/labels",
        "events": "https://gitlab.cghbh.com/api/v4/projects/23/events",
        "members": "https://gitlab.cghbh.com/api/v4/projects/23/members",
        "cluster_agents": "https://gitlab.cghbh.com/api/v4/projects/23/cluster_agents"
    },
    "packages_enabled": true,
    "empty_repo": true,
    "archived": false,
    "visibility": "private",
    "owner": {
        "id": 4,
        "username": "demo",
        "name": "demo",
        "state": "active",
        "avatar_url": "https://secure.gravatar.com/avatar/6c4108e85410a1d5bb9413848207d641?s=80&d=identicon",
        "web_url": "https://gitlab.cghbh.com/demo"
    },
    "resolve_outdated_diff_discussions": false,
    "container_expiration_policy": {
        "cadence": "1d",
        "enabled": false,
        "keep_n": 10,
        "older_than": "90d",
        "name_regex": ".*",
        "name_regex_keep": null,
        "next_run_at": "2023-02-19T18:34:29.039+05:30"
    },
    "issues_enabled": true,
    "merge_requests_enabled": true,
    "wiki_enabled": true,
    "jobs_enabled": true,
    "snippets_enabled": true,
    "container_registry_enabled": true,
    "service_desk_enabled": false,
    "service_desk_address": null,
    "can_create_merge_request_in": true,
    "issues_access_level": "enabled",
    "repository_access_level": "enabled",
    "merge_requests_access_level": "enabled",
    "forking_access_level": "enabled",
    "wiki_access_level": "enabled",
    "builds_access_level": "enabled",
    "snippets_access_level": "enabled",
    "pages_access_level": "private",
    "operations_access_level": "enabled",
    "analytics_access_level": "enabled",
    "container_registry_access_level": "enabled",
    "security_and_compliance_access_level": "private",
    "emails_disabled": null,
    "shared_runners_enabled": true,
    "lfs_enabled": true,
    "creator_id": 4,
    "import_url": null,
    "import_type": null,
    "import_status": "none",
    "open_issues_count": 0,
    "ci_default_git_depth": 20,
    "ci_forward_deployment_enabled": true,
    "ci_job_token_scope_enabled": false,
    "ci_separated_caches": true,
    "public_jobs": true,
    "build_timeout": 3600,
    "auto_cancel_pending_pipelines": "enabled",
    "build_coverage_regex": null,
    "ci_config_path": null,
    "shared_with_groups": [],
    "only_allow_merge_if_pipeline_succeeds": false,
    "allow_merge_on_skipped_pipeline": null,
    "restrict_user_defined_variables": false,
    "request_access_enabled": true,
    "only_allow_merge_if_all_discussions_are_resolved": false,
    "remove_source_branch_after_merge": true,
    "printing_merge_request_link_enabled": true,
    "merge_method": "merge",
    "squash_option": "default_off",
    "enforce_auth_checks_on_uploads": true,
    "suggestion_commit_message": null,
    "merge_commit_template": null,
    "squash_commit_template": null,
    "auto_devops_enabled": true,
    "auto_devops_deploy_strategy": "continuous",
    "autoclose_referenced_issues": true,
    "repository_storage": "default",
    "keep_latest_artifact": true,
    "runner_token_expiration_interval": null
}

12.给指定ID的代码库取消star

js
// const result = await gitlabApi.Projects.unstar(代码库的ID);
const result = await gitlabApi.Projects.unstar(23);

输出结果如下:

json
{
    "id": 23,
    "description": null,
    "name": "create-code-demo",
    "name_with_namespace": "demo / create-code-demo",
    "path": "create-code-demo",
    "path_with_namespace": "demo/create-code-demo",
    "created_at": "2023-02-18T18:34:28.975+05:30",
    "default_branch": "main",
    "tag_list": [],
    "topics": [],
    "ssh_url_to_repo": "git@gitlab.cghbh.com:demo/create-code-demo.git",
    "http_url_to_repo": "https://gitlab.cghbh.com/demo/create-code-demo.git",
    "web_url": "https://gitlab.cghbh.com/demo/create-code-demo",
    "readme_url": null,
    "avatar_url": null,
    "forks_count": 0,
    "star_count": 0,
    "last_activity_at": "2023-02-18T18:34:28.975+05:30",
    "namespace": {
        "id": 32,
        "name": "demo",
        "path": "demo",
        "kind": "user",
        "full_path": "demo",
        "parent_id": null,
        "avatar_url": "https://secure.gravatar.com/avatar/6c4108e85410a1d5bb9413848207d641?s=80&d=identicon",
        "web_url": "https://gitlab.cghbh.com/demo"
    },
    "_links": {
        "self": "https://gitlab.cghbh.com/api/v4/projects/23",
        "issues": "https://gitlab.cghbh.com/api/v4/projects/23/issues",
        "merge_requests": "https://gitlab.cghbh.com/api/v4/projects/23/merge_requests",
        "repo_branches": "https://gitlab.cghbh.com/api/v4/projects/23/repository/branches",
        "labels": "https://gitlab.cghbh.com/api/v4/projects/23/labels",
        "events": "https://gitlab.cghbh.com/api/v4/projects/23/events",
        "members": "https://gitlab.cghbh.com/api/v4/projects/23/members",
        "cluster_agents": "https://gitlab.cghbh.com/api/v4/projects/23/cluster_agents"
    },
    "packages_enabled": true,
    "empty_repo": true,
    "archived": false,
    "visibility": "private",
    "owner": {
        "id": 4,
        "username": "demo",
        "name": "demo",
        "state": "active",
        "avatar_url": "https://secure.gravatar.com/avatar/6c4108e85410a1d5bb9413848207d641?s=80&d=identicon",
        "web_url": "https://gitlab.cghbh.com/demo"
    },
    "resolve_outdated_diff_discussions": false,
    "container_expiration_policy": {
        "cadence": "1d",
        "enabled": false,
        "keep_n": 10,
        "older_than": "90d",
        "name_regex": ".*",
        "name_regex_keep": null,
        "next_run_at": "2023-02-19T18:34:29.039+05:30"
    },
    "issues_enabled": true,
    "merge_requests_enabled": true,
    "wiki_enabled": true,
    "jobs_enabled": true,
    "snippets_enabled": true,
    "container_registry_enabled": true,
    "service_desk_enabled": false,
    "service_desk_address": null,
    "can_create_merge_request_in": true,
    "issues_access_level": "enabled",
    "repository_access_level": "enabled",
    "merge_requests_access_level": "enabled",
    "forking_access_level": "enabled",
    "wiki_access_level": "enabled",
    "builds_access_level": "enabled",
    "snippets_access_level": "enabled",
    "pages_access_level": "private",
    "operations_access_level": "enabled",
    "analytics_access_level": "enabled",
    "container_registry_access_level": "enabled",
    "security_and_compliance_access_level": "private",
    "emails_disabled": null,
    "shared_runners_enabled": true,
    "lfs_enabled": true,
    "creator_id": 4,
    "import_url": null,
    "import_type": null,
    "import_status": "none",
    "open_issues_count": 0,
    "ci_default_git_depth": 20,
    "ci_forward_deployment_enabled": true,
    "ci_job_token_scope_enabled": false,
    "ci_separated_caches": true,
    "public_jobs": true,
    "build_timeout": 3600,
    "auto_cancel_pending_pipelines": "enabled",
    "build_coverage_regex": null,
    "ci_config_path": null,
    "shared_with_groups": [],
    "only_allow_merge_if_pipeline_succeeds": false,
    "allow_merge_on_skipped_pipeline": null,
    "restrict_user_defined_variables": false,
    "request_access_enabled": true,
    "only_allow_merge_if_all_discussions_are_resolved": false,
    "remove_source_branch_after_merge": true,
    "printing_merge_request_link_enabled": true,
    "merge_method": "merge",
    "squash_option": "default_off",
    "enforce_auth_checks_on_uploads": true,
    "suggestion_commit_message": null,
    "merge_commit_template": null,
    "squash_commit_template": null,
    "auto_devops_enabled": true,
    "auto_devops_deploy_strategy": "continuous",
    "autoclose_referenced_issues": true,
    "repository_storage": "default",
    "keep_latest_artifact": true,
    "runner_token_expiration_interval": null
}

13.获取指定ID的代码库下的所有分支

js
// const list = await gitlabApi.Branches.all(代码库ID);
const list = await gitlabApi.Branches.all(18);

输出结果如下:

json
[
    {
        "name": "master",
        "commit": {
            "id": "0fbec64af3b518437262c382129d6ca511f0fc50",
            "short_id": "0fbec64a",
            "created_at": "2023-01-30T13:02:09.000+05:30",
            "parent_ids": null,
            "title": "feat: lock文件修改",
            "message": "feat: lock文件修改",
            "author_name": "chengang",
            "author_email": "chengang@jiayinfintech.cn",
            "authored_date": "2023-01-30T13:02:09.000+05:30",
            "committer_name": "chengang",
            "committer_email": "chengang@jiayinfintech.cn",
            "committed_date": "2023-01-30T13:02:09.000+05:30",
            "trailers": null,
            "web_url": "https://gitlab.cghbh.com/open-source/devops-system/-/commit/0fbec64af3b518437262c382129d6ca511f0fc50"
        },
        "merged": false,
        "protected": true,
        "developers_can_push": false,
        "developers_can_merge": false,
        "can_push": true,
        "default": true,
        "web_url": "https://gitlab.cghbh.com/open-source/devops-system/-/tree/master"
    },
    {
        "name": "master-feature-20230118-DevOps一期开发",
        "commit": {
            "id": "7eb26d26bcb97908a2e8231d8cf90e1197af7f29",
            "short_id": "7eb26d26",
            "created_at": "2023-02-10T11:25:54.000+05:30",
            "parent_ids": null,
            "title": "feat: 加加载进度条的功能",
            "message": "feat: 加加载进度条的功能",
            "author_name": "chengang",
            "author_email": "914539467@qq.com",
            "authored_date": "2023-02-10T11:25:54.000+05:30",
            "committer_name": "chengang",
            "committer_email": "914539467@qq.com",
            "committed_date": "2023-02-10T11:25:54.000+05:30",
            "trailers": null,
            "web_url": "https://gitlab.cghbh.com/open-source/devops-system/-/commit/7eb26d26bcb97908a2e8231d8cf90e1197af7f29"
        },
        "merged": false,
        "protected": false,
        "developers_can_push": false,
        "developers_can_merge": false,
        "can_push": true,
        "default": false,
        "web_url": "https://gitlab.cghbh.com/open-source/devops-system/-/tree/master-feature-20230118-DevOps%E4%B8%80%E6%9C%9F%E5%BC%80%E5%8F%91"
    },
    {
        "name": "master-feature-20230130-迅鸽二期开发",
        "commit": {
            "id": "0fbec64af3b518437262c382129d6ca511f0fc50",
            "short_id": "0fbec64a",
            "created_at": "2023-01-30T13:02:09.000+05:30",
            "parent_ids": null,
            "title": "feat: lock文件修改",
            "message": "feat: lock文件修改",
            "author_name": "chengang",
            "author_email": "chengang@jiayinfintech.cn",
            "authored_date": "2023-01-30T13:02:09.000+05:30",
            "committer_name": "chengang",
            "committer_email": "chengang@jiayinfintech.cn",
            "committed_date": "2023-01-30T13:02:09.000+05:30",
            "trailers": null,
            "web_url": "https://gitlab.cghbh.com/open-source/devops-system/-/commit/0fbec64af3b518437262c382129d6ca511f0fc50"
        },
        "merged": false,
        "protected": false,
        "developers_can_push": false,
        "developers_can_merge": false,
        "can_push": true,
        "default": false,
        "web_url": "https://gitlab.cghbh.com/open-source/devops-system/-/tree/master-feature-20230130-%E8%BF%85%E9%B8%BD%E4%BA%8C%E6%9C%9F%E5%BC%80%E5%8F%91"
    },
    {
        "name": "test",
        "commit": {
            "id": "8a02e928058df5fa12f9076ad27b283319410c0a",
            "short_id": "8a02e928",
            "created_at": "2023-02-10T11:26:19.000+05:30",
            "parent_ids": null,
            "title": "Merge branch 'master-feature-20230118-DevOps一期开发' into test",
            "message": "Merge branch 'master-feature-20230118-DevOps一期开发' into test",
            "author_name": "chengang",
            "author_email": "914539467@qq.com",
            "authored_date": "2023-02-10T11:26:19.000+05:30",
            "committer_name": "chengang",
            "committer_email": "914539467@qq.com",
            "committed_date": "2023-02-10T11:26:19.000+05:30",
            "trailers": null,
            "web_url": "https://gitlab.cghbh.com/open-source/devops-system/-/commit/8a02e928058df5fa12f9076ad27b283319410c0a"
        },
        "merged": false,
        "protected": false,
        "developers_can_push": false,
        "developers_can_merge": false,
        "can_push": true,
        "default": false,
        "web_url": "https://gitlab.cghbh.com/open-source/devops-system/-/tree/test"
    }
]

14.创建代码库下的分支

js
const result = await gitlabApi.Branches.create(
  // 第一个参数是代码库的ID
  18,
  // 第二个参数是分支的名称
  '测试branch分支-来源自test分支',
  // 第三个参数是以哪一个分支作为目标进行新建
  'test',
);

输出结果如下:

json
{
    "name": "测试branch分支-来源自test分支",
    "commit": {
        "id": "8a02e928058df5fa12f9076ad27b283319410c0a",
        "short_id": "8a02e928",
        "created_at": "2023-02-10T13:56:19.000+08:00",
        "parent_ids": [
            "caefef0beb27f20de49cd8f38b9605311d7c3f8c",
            "7eb26d26bcb97908a2e8231d8cf90e1197af7f29"
        ],
        "title": "Merge branch 'master-feature-20230118-DevOps一期开发' into test",
        "message": "Merge branch 'master-feature-20230118-DevOps一期开发' into test\n",
        "author_name": "chengang",
        "author_email": "914539467@qq.com",
        "authored_date": "2023-02-10T13:56:19.000+08:00",
        "committer_name": "chengang",
        "committer_email": "914539467@qq.com",
        "committed_date": "2023-02-10T13:56:19.000+08:00",
        "trailers": {},
        "web_url": "https://gitlab.cghbh.com/open-source/devops-system/-/commit/8a02e928058df5fa12f9076ad27b283319410c0a"
    },
    "merged": false,
    "protected": false,
    "developers_can_push": false,
    "developers_can_merge": false,
    "can_push": true,
    "default": false,
    "web_url": "https://gitlab.cghbh.com/open-source/devops-system/-/tree/%E6%B5%8B%E8%AF%95branch%E5%88%86%E6%94%AF-%E6%9D%A5%E6%BA%90%E8%87%AAtest%E5%88%86%E6%94%AF"
}

15.删除代码库下的指定分支

js
const result = await gitlabApi.Branches.remove(
   // 第一个参数是代码库的ID
   18,
   // 第二个参数是分支的名称
   '测试branch分支-来源自test分支',
);

输出结果如下:

{}

如果分支已被删除继续执行删除的操作,会输出如下:

json
{
    "statusCode": 500,
    "message": "Internal server error"
}

16.获取指定代码库下的所有commit

js
// const result = await gitlabApi.Commits.all(代码库的ID);
const result = await gitlabApi.Commits.all(18);

输出结果如下:

[
    {
        "id": "0fbec64af3b518437262c382129d6ca511f0fc50",
        "short_id": "0fbec64a",
        "created_at": "2023-01-30T15:32:09.000+08:00",
        "parent_ids": [
            "e11cfc47fc5aca0fcdd36eaa776030234cd0cccb",
            "5000fbbb5b55e3defbe1f8ae8da62e4ae217acf4"
        ],
        "title": "feat: lock文件修改",
        "message": "feat: lock文件修改\n",
        "author_name": "chengang",
        "author_email": "chengang@jiayinfintech.cn",
        "authored_date": "2023-01-30T15:32:09.000+08:00",
        "committer_name": "chengang",
        "committer_email": "chengang@jiayinfintech.cn",
        "committed_date": "2023-01-30T15:32:09.000+08:00",
        "trailers": {},
        "web_url": "https://gitlab.cghbh.com/open-source/devops-system/-/commit/0fbec64af3b518437262c382129d6ca511f0fc50"
    },
    {
        "id": "1a92daab5134b980ddebdb75205848b52b498792",
        "short_id": "1a92daab",
        "created_at": "2023-01-18T16:41:27.000+08:00",
        "parent_ids": [
            "2f677cbda39423e529cadf98d274e865b58f5dd2"
        ],
        "title": "feat: 部署平台正式确定名称规范",
        "message": "feat: 部署平台正式确定名称规范\n",
        "author_name": "chengang",
        "author_email": "914539467@qq.com",
        "authored_date": "2023-01-18T16:41:27.000+08:00",
        "committer_name": "chengang",
        "committer_email": "914539467@qq.com",
        "committed_date": "2023-01-18T16:41:27.000+08:00",
        "trailers": {},
        "web_url": "https://gitlab.cghbh.com/open-source/devops-system/-/commit/1a92daab5134b980ddebdb75205848b52b498792"
    }
]

17.获取代码库下的项目人员列表

js
// const userList = await gitlabApi.ProjectMembers.all(代码库ID);
const userList = await gitlabApi.ProjectMembers.all(18);

输出结果如下:

json
[
    {
        "id": 4,
        "username": "demo",
        "name": "demo",
        "state": "active",
        "avatar_url": "https://secure.gravatar.com/avatar/6c4108e85410a1d5bb9413848207d641?s=80&d=identicon",
        "web_url": "https://gitlab.cghbh.com/demo",
        "access_level": 30,
        "created_at": "2023-02-18T19:24:52.698+05:30",
        "created_by": {
            "id": 1,
            "username": "root",
            "name": "Administrator",
            "state": "active",
            "avatar_url": "https://secure.gravatar.com/avatar/5aebfc15d76a494412f2940cbfd00de5?s=80&d=identicon",
            "web_url": "https://gitlab.cghbh.com/root"
        },
        "expires_at": "2023-07-28"
    }
]

18.移除代码库下的项目人员

js
// const result = await gitlabApi.ProjectMembers.add(代码库的ID, 加入代码库的用户ID, 权限的等级);
// 权限的等级如下:
// 50: Owner,可以设置项目访问权限 - Visibility Level、删除项目、迁移项目、管理组成员,开发组leader可以赋予这个权限
// 40: Master,可以创建项目、添加tag、保护分支、添加项目成员、编辑项目,核心负责人可以赋予这个权限
// 30: Developer,可以克隆代码、开发、提交、push,项目开发人员可以赋予这个权限
// 20: Reporter,可以克隆代码,不能提交,QA、PM可以赋予这个权限
// 10: Guest,可以创建issue、发表评论,不能读写版本库
const result = await gitlabApi.ProjectMembers.add(18, 5, 50);

输出结果如下:

json
{
    "id": 5,
    "username": "chengang",
    "name": "chengang",
    "state": "active",
    "avatar_url": "https://secure.gravatar.com/avatar/c0744d558ced478eb3b3cf9b03e24eec?s=80&d=identicon",
    "web_url": "https://gitlab.cghbh.com/chengang",
    "access_level": 50,
    "created_at": "2023-02-18T19:42:26.695+05:30",
    "created_by": {
        "id": 1,
        "username": "root",
        "name": "Administrator",
        "state": "active",
        "avatar_url": "https://secure.gravatar.com/avatar/5aebfc15d76a494412f2940cbfd00de5?s=80&d=identicon",
        "web_url": "https://gitlab.cghbh.com/root"
    },
    "expires_at": null
}

19.编辑代码库下人员的访问权限

js
// const result = await gitlabApi.ProjectMembers.add(代码库的ID, 加入代码库的用户ID, 需要修改的权限等级);
// 权限的等级取值请参考上面17的api使用
const result = await gitlabApi.ProjectMembers.edit(18, 5, 40);

输出结果如下:

json
{
    "id": 5,
    "username": "chengang",
    "name": "chengang",
    "state": "active",
    "avatar_url": "https://secure.gravatar.com/avatar/c0744d558ced478eb3b3cf9b03e24eec?s=80&d=identicon",
    "web_url": "https://gitlab.cghbh.com/chengang",
    "access_level": 40,
    "created_at": "2023-02-18T19:42:26.695+05:30",
    "created_by": {
        "id": 1,
        "username": "root",
        "name": "Administrator",
        "state": "active",
        "avatar_url": "https://secure.gravatar.com/avatar/5aebfc15d76a494412f2940cbfd00de5?s=80&d=identicon",
        "web_url": "https://gitlab.cghbh.com/root"
    },
    "expires_at": null
}

20.获取指定代码库下受保护的分支

js
// const result = await gitlabApi.ProtectedBranches.all(代码库的ID);
const result = await gitlabApi.ProtectedBranches.all(18);

输出结果如下:

json
[
    {
        "id": 16,
        "name": "master",
        "push_access_levels": [
            {
                "access_level": 40,
                "access_level_description": "Maintainers"
            }
        ],
        "merge_access_levels": [
            {
                "access_level": 40,
                "access_level_description": "Maintainers"
            }
        ],
        "allow_force_push": false
    }
]