gitea_user_cleaning/README.md

101 lines
2.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Gitea 用户清理工具
一个用于清理 Gitea 实例中不在指定组织内的用户的 Python 脚本。
## 功能特性
- 获取 Gitea 实例中的所有用户
- 获取指定组织的所有成员
- 自动删除不在组织内的用户及其所有仓库
- 使用进度条显示删除进度
## 环境要求
- Python 3.11
- Gitea 实例访问权限
- Gitea API Token管理员权限
## 安装
1. 克隆或下载此项目
2. 安装依赖:
```bash
pip install -r requirements.txt
```
## 配置
在项目根目录创建 `.env` 文件,配置以下环境变量:
```env
GITEA_URL=https://your-gitea-instance.com
GITEA_API_TOKEN=your_api_token_here
```
### 获取 API Token
1. 登录 Gitea 实例
2. 进入 **设置****应用****生成新令牌**
3. 选择权限范围(需要管理员权限)
4. 复制生成的令牌到 `.env` 文件
## 使用方法
### 基本使用
运行脚本将自动:
1. 获取所有用户列表
2. 获取指定组织的成员列表(默认为 "TianMa431"
3. 删除不在组织内的用户及其所有仓库
```bash
python main.py
```
### 自定义组织名称
`main.py` 中修改 `get_org_user_names()` 函数的默认参数:
```python
org_user_names = get_org_user_names("YourOrgName")
```
### 单独删除用户
取消注释并修改以下代码来删除特定用户:
```python
delete_user("username")
```
## 主要函数
### `get_user_names()`
获取 Gitea 实例中的所有用户名列表。
### `get_org_user_names(org_name="TianMa431")`
获取指定组织的所有成员用户名列表。
### `delete_user(username)`
删除指定用户及其所有仓库。**此操作不可逆!**
## ⚠️ 警告
- **此脚本会永久删除用户及其所有仓库数据,操作不可恢复!**
- 使用前请确保已备份重要数据
- 建议先在测试环境验证
- 确保 API Token 具有足够的权限
## 依赖包
- `requests` - HTTP 请求库
- `python-dotenv` - 环境变量管理
- `tqdm` - 进度条显示
## 许可证
本项目仅供学习和内部使用。