基于 NodeGit 的周报生成工具

3,727 阅读3分钟

日子走呀走,就没了踪影,也忘了是多少周前,团队从 SVN 切换到 Git,以前写的 SVN 周报工具也算是安心退役了。前天终于下定决心写个基于 Git 的周报工具。

我对工具的构思如下:

  1. 团队成员约定每次提交都简短描述变更的功能
  2. 功能描述以「分号」或者「换行」分隔
  3. 如果再次提交相同功能则以“update”作为标识提交信息
  4. 周报内容为各个项目下各成员的工作信息
  5. 生成一张周报图片利于我发邮件

上一个周报工具是用 Nodejs + svn 命令实现的,这次就不想用 git 命令配合了。于是上网搜了一些资料后,发现 NodeGit 这个库很适合。那么主旋律确定了,就可以开始动工了,以下是流程图。

image

根据流程图得出以下的整体流程代码:

async function init() {
    const folders = fs.readdirSync(config.dir)
    //  获取到不存在的git仓库(约定文件夹都是git仓库)(其实也可以根据是否有.git 或者 nodeGit的exist)
    const emptyProjects = config.projects.filter(
        v => folders.indexOf(v.folder) === -1
    )
    if (emptyProjects.length) {
        // 创建本地不存在git仓库
        await createRespository(emptyProjects)
    }
    // 获取commit信息
    const logs = await getRepositoryLog()
    // 生成周报
    renderReport(logs)
}

读取本地 Git 仓库目录,这里取(tou)巧(lan)了,约定存在文件夹即认为存在 git 仓库,其实也可以根据是否有.git 目录或者通过 nodeGit 的 exit 来判断。

不存在与本地的 Git 仓库,考虑到有很多项目是没必要 clone 到本地的,所以我并不想把整个 Git 仓库都拉到本地,只是想创建个链接,然后拉取一下 Log 信息。所以实现的功能如同以下命令:

git init
git fetch origin
git log --all

获取 Git 提交记录,通过 nodeGit 的 Revwalk 实现 log 所有分支的 commit 信息。其中内部约定重复提交的信息以 update 字符标识,程序上会忽略这个提交信息。

const repo = await nodeGit.Repository.open(`${temporaryFolder}/.git`)
const walker = nodeGit.Revwalk.create(repo)
// 通过pushGlob来获取所有分支的提交记录
walker.pushGlob('*')
// 获取符合日期的commits
const commits = await walker.getCommitsUntil(c => {
    const now = c.date()
    return now > beginDate && now < endDate
})

const selfCommits = []
Promise.all(
    commits
        .filter(x => {
            // 过滤不需要记录的commit信息
            const regexp = new RegExp(`${projectFolder}|update|merge`, 'gi')
            return !regexp.test(x.message())
        })
        .map(async x => {
            // 是否需要统计行数
            const total = needCount ? await countLines(x) : 0
            // 构建周报信息集
            selfCommits.push({
                msg: x
                    .message()
                    .split(/\n|;/g)
                    .filter(v => v.length),
                total,
                project: projectName,
                committer: x.committer().name()
            })
        })
).then(() => {
    resolve(selfCommits)
})

生成周报,最后通过 markvis、markdown-it、d3-node 生成周报图片,具体的项目路径、名字、账号、密码、是否统计行数在 config/index.js 中配置。

// ./config/index.js
module.exports = {
    username: 'username', // Git username
    password: 'password', // Git password
    reponame: 'origin', // Repository name
    dir: 'Git directory path', // /Users/viccici/github
    reportDir: 'Report directory path', // /Users/viccici/report
    commiter: {
        'Git name': 'Real name' // Git committer name matching the real name
    },
    projects: [
        {
            name: 'Project name', // We often use chinese project name
            folder: 'Project folder', // Git folder name that based on git path.  [ PS: weekly-report-git ]
            path: 'Git path',
            count: true // Whether to count
        }
    ]
}

最终的结果如下图。

image

该周报工具更多的依赖于团队的约定,否则周报信息可读性就很差,后续还需要跟队员们商量更优的方案。NodeGit 还有很多需要深挖的知识点,后续会花点时间认真研究,从而能优化此周报工具。如有兴趣 or 更好想法,可到这里留言观看。