admin 2f5a9f7035 简化定时任务说明,去掉过度设计的双文件方案
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-27 15:19:42 +08:00

161 lines
4.7 KiB
Markdown
Raw 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.

# weekly-report
统计项目过去 7 天的 commit 和 PR为每位协作者生成个人工作日志并生成项目整体进度报告通过 SMTP 发送到指定邮箱。
> 适用于自建 Gitea 仓库,不依赖 `gh` CLI。
## 安全限制
本技能通过 `allowed-tools` 限定了 Claude 可使用的工具范围:
| 允许 | 说明 |
|------|------|
| `Read` | 读取配置文件和报告内容 |
| `Glob` | 查找脚本路径 |
| `Grep` | 搜索文件内容 |
| `Bash(git pull*)` | 仅允许执行 `git pull` |
| `Bash(python*)` | 仅允许执行 Python 脚本 |
明确**禁止**的操作:`Write`(写文件)、`Edit`(编辑文件)、任意 Bash 命令(如 `rm``curl``cat >` 等)。
临时文件(数据 JSON 和 HTML 报告)由 Python 脚本自身写入 `/tmp/`,不经过 Claude 的写工具。
## 前置要求
- Python 3.8+
- 项目使用 Git`origin` 指向 Gitea 实例
- 有可用的 SMTP 邮件账号
## 第一步:在项目根目录创建配置文件
在**需要生成周报的项目**根目录下创建 `report-config.json`(可提交到 git
```json
{
"recipients": ["boss@company.com", "team@company.com"],
"project_name": "你的项目名称",
"smtp_host": "smtphz.qiye.163.com",
"smtp_port": 994
}
```
| 字段 | 必填 | 说明 |
|------|------|------|
| `recipients` | 是 | 收件人邮箱列表 |
| `project_name` | 是 | 项目名称,显示在邮件标题中 |
| `smtp_host` | 否 | SMTP 服务器地址,内置服务商可省略(见下表) |
| `smtp_port` | 否 | SMTP 端口,内置服务商可省略 |
### 内置服务商smtp_host/smtp_port 可省略)
| 发件邮箱后缀 | 自动使用的服务器 | 端口 |
|-------------|----------------|------|
| `@gmail.com` | smtp.gmail.com | 587 |
| `@qq.com` | smtp.qq.com | 465 |
| `@163.com` | smtp.163.com | 465 |
| `@126.com` | smtp.126.com | 465 |
| `@outlook.com` / `@hotmail.com` | smtp.office365.com | 587 |
企业自定义域名(如 `@yourcompany.com`)需手动填写 `smtp_host``smtp_port`
## 第二步:设置环境变量
账号密码**不要写入任何文件**,在终端中临时设置:
```bash
export SMTP_USER=your@email.com
export SMTP_PASSWORD=your-smtp-password-or-auth-code
```
> **说明**`export` 仅对当前终端会话有效,关闭后自动失效。
> 如需持久化,可写入 `~/.bashrc`(注意文件权限 `chmod 600 ~/.bashrc`)。
### 私有 Gitea 仓库
如果仓库是私有的,还需要额外设置 Gitea 访问令牌:
```bash
export GITEA_TOKEN=your-gitea-token
```
在 Gitea 中生成令牌:`头像 → 设置 → 应用 → 管理 Access Token`,权限勾选 `repository: Read`
公开仓库无需配置此项。
## 第三步:启用插件
在项目的 `.claude/settings.json` 中添加插件路径:
```json
{
"plugins": ["/path/to/ieslab_skills"]
}
```
## 第四步:调用技能
在 Claude Code 中输入:
```
/weekly-report
```
Claude 会自动执行以下流程:
1. 读取 `report-config.json`
2. 拉取 `origin/main` 最新代码
3. 通过 Gitea API 获取过去 7 天的 commits 和已合并 PR
4. 生成每位协作者的个人工作日志
5. 生成项目整体进度报告
6. 发送 HTML 格式邮件到 `recipients` 列表
## 定时自动发送Cron
通过 `claude -p` 非交互模式配合定时任务实现每周自动发送。
新建 `~/weekly_report.sh`,把密码直接写在脚本里,设置好权限确保只有自己可读:
```bash
#!/bin/bash
export SMTP_USER=your@email.com
export SMTP_PASSWORD=your-auth-code
export GITEA_TOKEN=your-gitea-token # 私有仓库才需要
cd /path/to/your-project
claude -p "/weekly-report"
```
```bash
chmod 600 ~/weekly_report.sh
```
### Windows任务计划程序
1. 开始菜单搜索「任务计划程序」→「创建基本任务」
2. 触发器:每周,设置具体时间(如每周五 18:00
3. 操作:启动程序
- 程序:`C:\Program Files\Git\bin\bash.exe`
- 参数:`-l -c "/c/Users/你的用户名/weekly_report.sh"`
### Linux / Maccrontab
```bash
crontab -e
# 添加(每周五 18:00
0 18 * * 5 /bin/bash ~/weekly_report.sh >> ~/weekly_report.log 2>&1
```
> 如遇「命令未找到」,将 `claude` 替换为绝对路径(用 `which claude` 查询)。
---
## 常见问题
**Q发送失败提示认证错误**
ASMTP 密码通常需要使用"授权码"而非登录密码。QQ/163 邮箱请在邮箱设置中开启 SMTP 并生成授权码Gmail 需要开启两步验证后生成"应用专用密码"。
**Q没有 PR 数据**
A直接 `git push` 的提交不会产生 PR 记录,只有通过 Gitea 创建 Pull Request 并合并的才会被统计。
**Q私有仓库报 401 错误**
A需要设置 `GITEA_TOKEN` 环境变量,参考上方说明。