OpenClaw 自动化实战:如何用 AI 帮你写周报(附完整代码)

作者:飞来城 🍵
难度:⭐⭐
预计耗时:20 分钟


🎯 为什么需要 AI 写周报?

相信每个打工人都经历过这样的时刻:

周五下午 5 点:“完了,还没写周报!这个星期干了啥来着?”

翻聊天记录:“上周三开的会说了啥…”

查 Git 提交:“哦,好像修了三个 bug”

凑字数中…:“下周继续努力!” 😅

今天教你用 OpenClaw 搭建一个自动周报生成器——只要给它一周的工作记录,它就能帮你整理成一份漂亮的周报。

效果预览:

输入:Git 日志 + 会议记录 + 任务列表
    ↓
输出:结构清晰、重点突出、数据详实的周报

📚 目录

  1. 需求分析
  2. 工具准备
  3. 核心脚本开发
  4. OpenClaw 集成
  5. 实际使用案例
  6. 扩展玩法

一、需求分析

1.1 周报标准结构

根据我的观察,一份合格的周报应该包含:

板块 内容 占比
本周工作总结 完成了什么项目/任务 40%
问题与解决 遇到了什么困难、怎么解决的 20%
下周计划 接下来要做什么 25%
思考与建议 个人成长、团队建议 15%

1.2 数据来源

我们需要的原始素材:

📁 数据源清单:
├── git log             → 代码提交记录
├── 会议纪要.txt        → 周会/评审记录
├── todo-list.md        → 任务跟踪表
└── 聊天导出.json       → 工作沟通记录(可选)

1.3 期望输出

# 第 XX 周工作汇报

## 一、本周完成

### 1.1 项目 A - 功能模块开发
- ✅ 完成用户认证模块(commit: a1b2c3d)
- ✅ 修复登录超时问题(issue #42)
- 📊 性能提升:响应时间从 500ms 降至 200ms

### 1.2 项目 B - Bug 修复
...

## 二、问题与思考
...

## 三、下周计划
...

二、工具准备

2.1 环境要求

# Node.js (OpenClaw 必需)
node --version  # >= 18

# Python (用于数据处理,可选)
python --version  # >= 3.8

2.2 项目结构

创建周报助手目录:

mkdir -p ~/workspace/weebly-report-tool
cd ~/workspace/weebly-report-tool

# 初始化结构
mkdir -p scripts templates data output
touch scripts/generate-report.js scripts/parse-git-log.js
touch templates/report-template.md .env

2.3 依赖安装

# JavaScript 方案(推荐,与 OpenClaw 原生兼容)
npm init -y
npm install dayjs commander chalk

三、核心脚本开发

3.1 Git 日志解析器

创建 scripts/parse-git-log.js

#!/usr/bin/env node
/**
 * Git 日志解析器
 * 将 git log 转换为结构化数据
 */

const { execSync } = require('child_process');
const dayjs = require('dayjs');

/**
 * 获取最近一周的 git 提交记录
 * @param {string} repoPath - 仓库路径
 * @param {number} days - 获取多少天前的记录
 * @returns {Object[]}
 */
function parseGitLog(repoPath = '.', days = 7) {
  const since = dayjs().subtract(days, 'day').format('YYYY-MM-DD');
  
  const command = `git --git-dir=${repoPath}/.log --work-tree=${repoPath} log \
    --since="${since}" \
    --pretty=format:"%H|%h|%an|%ad|%s" \
    --date=short`;
  
  try {
    const output = execSync(command, { encoding: 'utf-8' });
    if (!output.trim()) return [];
    
    return output.split('\n').map(line => {
      const [hash, shortHash, author, date, message] = line.split('|');
      return { hash, shortHash, author, date, message };
    });
  } catch (error) {
    console.error('❌ Git 日志读取失败:', error.message);
    return [];
  }
}

/**
 * 按类型分类 commit
 * @param {Object[]} commits 
 * @returns {Object}
 */
function classifyCommits(commits) {
  const categories = {
    feature: [],    // 新功能
    fix: [],        // Bug 修复
    refactor: [],   // 重构
    docs: [],       // 文档
    chore: []       // 其他
  };
  
  commits.forEach(commit => {
    const msg = commit.message.toLowerCase();
    
    if (msg.startsWith('feat') || msg.includes('新增') || msg.includes('feature')) {
      categories.feature.push(commit);
    } else if (msg.startsWith('fix') || msg.includes('修复') || msg.includes('bug')) {
      categories.fix.push(commit);
    } else if (msg.startsWith('refactor') || msg.includes('重构')) {
      categories.refactor.push(commit);
    } else if (msg.startsWith('docs') || msg.includes('文档')) {
      categories.docs.push(commit);
    } else {
      categories.chore.push(commit);
    }
  });
  
  return categories;
}

module.exports = { parseGitLog, classifyCommits };

3.2 周报生成器

创建 scripts/generate-report.js

#!/usr/bin/env node
/**
 * 周报生成器主程序
 */

const fs = require('fs');
const path = require('path');
const dayjs = require('dayjs');
const { parseGitLog, classifyCommits } = require('./parse-git-log');
const chalk = require('chalk');

// 配置
const CONFIG = {
  repoPath: process.env.GIT_REPO_PATH || '.',
  outputPath: './output',
  templatePath: './templates/report-template.md'
};

/**
 * 获取当前周范围
 * @returns {{start: string, end: string, weekNum: number}}
 */
function getWeekRange() {
  const now = dayjs();
  const startOfWeek = now.startOf('week').add(1, 'day'); // 周一为起始
  const weekNum = now.week();
  
  return {
    start: startOfWeek.format('YYYY-MM-DD'),
    end: now.format('YYYY-MM-DD'),
    weekNum
  };
}

/**
 * 总结 commit 到自然语言
 * @param {Object[]} commits 
 * @returns {string}
 */
function summarizeCommits(commits) {
  if (!commits.length) return '- 暂无代码提交记录';
  
  return commits.map(c => {
    // 简化消息,去掉前缀
    let msg = c.message.replace(/^(feat|fix|docs|refactor|chore)(\([^)]+\))?:\s*/, '');
    return `- ${msg} \`(${c.shortHash})\``;
  }).join('\n');
}

/**
 * 生成周报内容
 * @param {Object} gitData 
 * @returns {string}
 */
function generateReport(gitData) {
  const { start, end, weekNum } = getWeekRange();
  const classified = classifyCommits(gitData.commits);
  
  const totalCommits = gitData.commits.length;
  const featureCount = classified.feature.length;
  const fixCount = classified.fix.length;
  
  return `# 第${weekNum}周工作汇报

**汇报人**:[你的名字]
**周期**:${start} ~ ${end}

---

## 一、本周工作总结

### 📊 总体数据
| 指标 | 数量 |
|------|------|
| 代码提交 | ${totalCommits} 次 |
| 新功能开发 | ${featureCount} 项 |
| Bug 修复 | ${fixCount} 个 |
| 文档更新 | ${classified.docs.length} 处 |

### 💻 主要产出

#### 1. 新功能开发
${summarizeCommits(classified.feature)}

#### 2. Bug 修复
${summarizeCommits(classified.fix)}

#### 3. 其他改进
${summarizeCommits([...classified.refactor, ...classified.docs])}

---

## 二、问题与思考

> ℹ️ 以下内容需要你手动补充

1. **遇到的主要问题**:
   - [ ] 待补充

2. **解决方案与收获**:
   - [ ] 待补充

3. **需要支持的事项**:
   - [ ] 待补充

---

## 三、下周工作计划

> ℹ️ 根据实际情况调整

### Priority P0(必须完成)
- [ ] 

### Priority P1(重要)
- [ ] 

### Priority P2(优化)
- [ ] 

---

## 四、个人成长与建议

### 📚 学习收获
- [ ] 新技术/新知识

### 💡 团队建议
- [ ] 流程/工具/协作方面

---

*报告生成时间:${dayjs().format('YYYY-MM-DD HH:mm:ss')}*  
*自动生成,部分字段需人工完善*
`;
}

/**
 * 主函数
 */
async function main() {
  console.log(chalk.cyan('🚀 开始生成周报...'));
  
  // 1. 读取 Git 日志
  const commits = parseGitLog(CONFIG.repoPath, 7);
  console.log(chalk.green(`✅ 读取到 ${commits.length} 条提交记录`));
  
  // 2. 生成报告
  const report = generateReport({ commits });
  
  // 3. 保存输出
  const weekRange = getWeekRange();
  const outputPath = path.join(CONFIG.outputPath, `周报_第${weekRange.weekNum}周_${dayjs().format('MMDD')}.md`);
  
  fs.mkdirSync(CONFIG.outputPath, { recursive: true });
  fs.writeFileSync(outputPath, report, 'utf-8');
  
  console.log(chalk.green(`✅ 周报已保存至:${outputPath}`));
  console.log(chalk.yellow('⚠️  请记得填写"问题与思考"和"下周计划"部分'));
}

// 运行
main();

3.3 配置文件

创建 .env

# Git 仓库路径(默认当前目录)
GIT_REPO_PATH=/path/to/your/project

# 输出路径
OUTPUT_DIR=./output

四、OpenClaw 集成

4.1 注册为 OpenClaw 技能

如果你想让 OpenClaw 帮你自动生成周报,可以创建一个技能:

# weekly-report-generator

自动根据 Git 日志和本周记录生成周报初稿。

## 🎯 触发条件

当用户提到以下关键词时调用:
- "写周报" / "生成周报" / "周报"
- "这周的工作总结"

## 💬 使用方法

清安,帮我写份周报
生成本周工作汇报


## 🔧 配置说明

需要在 TOOLS.md 中配置:

weekly-report:
gitRepo: /path/to/repo
outputDir: ~/reports
customTemplate: optional/path/to/template.md


## 📊 输出格式

Markdown 格式的周报,包含:
- 本周完成(基于 Git 日志自动填充)
- 问题与思考(需手动补充)
- 下周计划(需手动补充)

4.2 OpenClaw 调用示例

在聊天中说:

清安,帮我生成周报

它会:

  1. 执行上面的脚本
  2. 读取你的 Git 日志
  3. 生成周报草稿
  4. 把文件路径告诉你

五、实际使用案例

5.1 真实场景演示

假设你这一周的 Git 提交如下:

$ git log --oneline -10
a1b2c3d feat(user): 完成登录功能
e4f5g6h fix(auth): 修复 token 过期问题
i7j8k9l docs(readme): 更新安装说明
m0n1o2p feat(api): 添加用户查询接口
q3r4s5t refactor(db): 优化数据库连接池

运行脚本后,周报自动生成:

## 一、本周工作总结

### 📊 总体数据
| 指标 | 数量 |
|------|------|
| 代码提交 | 5 次 |
| 新功能开发 | 2 项 |
| Bug 修复 | 1 个 |
| 文档更新 | 1 处 |

### 💻 主要产出

#### 1. 新功能开发
- 完成登录功能 `(a1b2c3d)`
- 添加用户查询接口 `(m0n1o2p)`

#### 2. Bug 修复
- 修复 token 过期问题 `(e4f5g6h)`

#### 3. 其他改进
- 优化数据库连接池 `(q3r4s5t)`
- 更新安装说明 `(i7j8k9l)`

然后你只需要:

  1. 补充"问题与思考"部分
  2. 填写"下周计划"
  3. 发给领导 ✉️

省时对比:

方式 耗时
传统手动写 30-60 分钟
本方案 10-15 分钟

六、扩展玩法

6.1 对接更多数据源

加入会议纪要
// 解析 Markdown 格式的会议纪要
function parseMeetingNotes(filePath) {
  const content = fs.readFileSync(filePath, 'utf-8');
  // 提取行动项、决策点等
  return {
    decisions: [],
    actionItems: [],
    discussions: []
  };
}
接入任务管理系统
// Jira/Gitea/Tapd API 集成
async function fetchTaskStatus(taskId) {
  // 获取任务状态、工时等
}

6.2 定时自动发送

用 cron 每天下午 5 点提醒你:

// cron 配置
{
  schedule: { expr: "0 17 * * 5", tz: "Asia/Shanghai" }, // 每周五 17:00
  payload: { kind: "systemEvent", text: "💼 周五啦,要不要生成周报?" }
}

6.3 自定义模板

# 个性化模板示例

## 团队风格适配

| 团队类型 | 风格建议 |
|----------|----------|
| 技术团队 | 多放数据和代码片段 |
| 产品团队 | 强调用户反馈和数据 |
| 管理层 | 突出成果和风险预警 |

🎉 结语

恭喜你!现在已经掌握了:

  1. ✅ 如何解析 Git 日志
  2. ✅ 自动生成周报框架
  3. ✅ 与 OpenClaw 集成的方法
  4. ✅ 扩展更多数据源的技巧

下一步建议:

  • 根据自己团队风格调整模板
  • 增加更多数据源(Jira/禅道等)
  • 分享出来帮到其他人

记住:最好的工具是你愿意用的工具。先跑通再迭代!


我是飞来城,专注分享实用的 AI 自动化工具。如果觉得有用,欢迎点赞收藏~ 🍵


📖 相关阅读


觉得有用?

  • 👍 点个赞支持原创
  • 💬 评论区分享你的周报痛点
  • 🔔 关注我不迷路
  • ➕ 收藏以防找不到
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐