如何删除所有 Gitlab 存储库?

How to remove all Gitlab repository?

我在 GitLab.Now 中创建了多个存储库我想一次删除或删除所有存储库。我怎样才能做到这一点?有API可用吗?

  1. 进入项目页面
  2. Select“设置”
  3. 如果您在页面底部有足够的权限,那么将有一个按钮 “危险设置”(即可能导致数据丢失的项目设置)或 “删除项目”(在较新的 GitLab 版本中)
  4. 按此按钮并按照说明进行操作

首先你list all projects, get a list of IDs and loop over the list: for every project id you remove the project.

您可以使用 GitLab client (an API wrapper),它们几乎适用于任何语言。

我使用 Gitlab 的 API 列出并删除了我错误迁移的大量项目,我为此制作了一个小 python 脚本。

DISCLAIMER: BE VERY CAREFUL USING THE FOLLOWING CODE. Read the code thoroughly. you alone are solely and personally responsible for your results.

话虽这么说,这里是:

import requests
import json

def get_project_ids():

    url = "https://gitlab.example.com/api/v4/users/{yourUserId}/projects"

    querystring = {"owned":"true","simple":"true","per_page":"50"}

    payload = ""
    headers = {'authorization': 'Bearer {yourToken}'}

    response = requests.request("GET", url, data=payload, headers=headers, params=querystring)

    projects = json.loads(response.text)
    projects_ids = list(map(lambda project: project.get('id'), projects))

    return projects_ids


def remove_project(project_id):
    url_temp = "https://gitlab.example.com/api/v4/projects/{project}"
    headers = {'authorization': 'Bearer {yourToken}'}
    querystring = ""
    payload = ""

    url = url_temp.format(project=project_id)

    response = requests.request("DELETE", url, data=payload, headers=headers, params=querystring)
    project = json.loads(response.text)
    print(project)


def main():
    projects_ids = get_project_ids()

    url_temp = "https://gitlab.example.com/api/v4/projects/{project}"
    headers = {'authorization': 'Bearer {yourToken}'}
    querystring = ""
    payload = ""

    for project_id in projects_ids:

        url = url_temp.format(project=project_id)

        response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
        project = json.loads(response.text)
        print(str(project.get('id')) + " " + project.get('name'))
        print("Removing")
        # remove_project(project_id)


if __name__ == "__main__":
    main()

{yourUserId}{yourToken}替换为对应的info。取消注释 remove_project() 函数以删除项目,同样,我不会对上述代码以任何方式承担任何责任。

首先您需要创建一个个人访问令牌:

Go to profile/preferences/access tokens or just click here

在以下代码中的 token 变量中替换您的令牌:

const axios = require("axios");

// Your authorization token here
const token = "YOUR_ACCESS_TOKEN";

// fetch all projects
axios
  .get("https://gitlab.com/api/v4/projects?visibility=private", {
    headers: {
      Authorization: `Bearer ${token}`,
    },
  })
  .then(async function (response) {
    // get all projects IDs
    let ids = response.data.map((e) => e.id);
    // delete all
    for (let el of ids) {
      await axios.delete(`https://gitlab.com/api/v4/projects/${el}/`, {
        headers: {
          Authorization: `Bearer ${token}`,
        },
      });
    }
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  });

您可以更改可见性 ?visibility=private?visibility=public