我在GitLab中创建了几个仓库。现在我想一次性删除或移除所有仓库。我该怎么做?是否有可用的API?
我在GitLab中创建了几个仓库。现在我想一次性删除或移除所有仓库。我该怎么做?是否有可用的API?
我使用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}
替换为相应的信息。取消remove_project()
函数的注释以删除项目,但是我不对上面呈现的代码负任何责任或责任,请注意。
首先,您需要创建一个个人访问令牌:
转到
profile
/preferences
/access tokens
或只需点击此处
在下面的代码中将您的令牌替换为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