The team switched from SVN to Git a few weeks ago, and the SVN weekly report tool written before has been retired at ease. The day before yesterday I finally decided to write a weekly report tool based on Git.

My ideas for tools are as follows:

  1. Team members agreed that each commit would briefly describe the functionality of the change
  2. Function descriptions are separated by semicolons (;) or newlines
  3. If the same function is submitted again, “Update” is used to identify the submission information
  4. The weekly report contains the work information of each member under each project
  5. Generate a picture of a weekly newspaper to help me send emails

The last weekly report tool was implemented with Nodejs + SVN command, this time I do not want to use git command with. After doing some research on the Internet, NodeGit is a great library for you. Then the main theme is determined, we can start construction, the following is the flow chart.

According to the flow chart, the following overall process code is obtained:

async function init() {
    const folders = fs.readdirSync(config.dir)
    // find a git repository that does not exist.
    const emptyProjects = config.projects.filter(
        v= > folders.indexOf(v.folder) === - 1
    )
    if (emptyProjects.length) {
        Git repository does not exist locally
        await createRespository(emptyProjects)
    }
    // Get commit information
    const logs = await getRepositoryLog()
    // Generate a weekly report
    renderReport(logs)
}

Copy the code

Git repository directory (tou); Git repository directory (LAN); Git directory (tou);

There is no local Git repository. Considering that there are many projects that don’t need to be clone to the local repository, I don’t want to pull the entire Git repository to the local repository. I just want to create a link and pull Log information. So the function is like the following command:

git init
git fetch origin
git log --all
Copy the code

Get Git commit records and implement commit information for all branches of the log through nodeGit Revwalk. The internal convention is that repeated submissions are identified by the UPDATE character and are ignored by the program.

const repo = await nodeGit.Repository.open(`${temporaryFolder}/.git`)
const walker = nodeGit.Revwalk.create(repo)
// pushGlob is used to get commit records for all branches
walker.pushGlob(The '*')
// Get the commits that match the date
const commits = await walker.getCommitsUntil(c= > {
    const now = c.date()
    return now > beginDate && now < endDate
})

const selfCommits = []
Promise.all(
    commits
        .filter(x= > {
            // Filter commit information that does not need to be recorded
            const regexp = new RegExp(`${projectFolder}|update|merge`.'gi')
            return! regexp.test(x.message()) }) .map(async x => {
            // Whether to count rows
            const total = needCount ? await countLines(x) : 0
            // Build weekly information set
            selfCommits.push({
                msg: x
                    .message()
                    .split(/\n|; /g)
                    .filter(v= > v.length),
                total,
                project: projectName,
                committer: x.committer().name()
            })
        })
).then((a)= > {
    resolve(selfCommits)
})

Copy the code

Generate a weekly report. At last, use Markvis, Markdown-it, and D3-node to generate a weekly report picture. Configure the specific project path, name, account, password, and whether to count the number of rows in 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}}]Copy the code

The final result is shown below.

The weekly report tool depends more on the agreement of the team, otherwise the readability of the weekly report information is very poor, and we need to discuss a better plan with the team members later. NodeGit still has a lot to learn, so I’ll spend some time looking into it to optimize the weekly reporting tool. If you are interested or have a better idea, please leave a comment here.