如何删除所有Gitlab存储库?

时间:2018-01-02 06:48:05

标签: git gitlab gitlab-api

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

3 个答案:

答案 0 :(得分:8)

首先你list all projects,获取一个ID列表并循环遍历列表:对于每个项目ID remove the project

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

答案 1 :(得分:0)

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

  

免责声明:请务必谨慎使用以下代码。仔细阅读代码。您一个人对结果负全部责任。

话虽这么说,这里是:

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()函数以删除项目的操作,同样,我将不对上述代码承担任何责任。

答案 2 :(得分:-1)

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