From 8fe644f8d76b563701dfa055a775d3ce2dd695e0 Mon Sep 17 00:00:00 2001
From: Bauke
Date: Wed, 30 Jan 2019 16:48:45 +0100
Subject: [PATCH] feat: add lines changed and contributor count statistics
---
gulpfile.js | 14 +++++++++-----
statistics.js | 40 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 49 insertions(+), 5 deletions(-)
diff --git a/gulpfile.js b/gulpfile.js
index 9f9dd10..3e44b9c 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -14,7 +14,7 @@ const
sync = require('browser-sync')
// Require statistic functions
-const { avgTime, freqUsers, labelsAlphabet } = require('./statistics')
+const { avgTime, freqUsers, labelsAlphabet, changedLines, uniqueContributors } = require('./statistics')
// Define paths that are gonna be used commonly
const paths = {
@@ -172,7 +172,7 @@ function download() {
log('Downloading commits...')
})
.then(() => {
- api.Commits.all(project.id, { ref_name: 'master' })
+ api.Commits.all(project.id, { ref_name: 'master', with_stats: true })
.catch((error) => reject(new Error('There was an error downloading the commits:', error)))
.then((commits) => {
log(`Downloaded commits, saving commits from ${months[wantedMonth]} ${wantedYear} to file...`)
@@ -321,8 +321,12 @@ function createStatistics() {
let statistics = '\n'
statistics += ' Statistics
\n'
- statistics += ` In the month of ${months[wantedMonth]} `
- statistics += `${commits.length} commits were made to the Tildes code, `
+ const commitStats = changedLines(commits)
+ const contributors = uniqueContributors(commits)
+
+ statistics += `
In the month of ${months[wantedMonth]}, `
+ statistics += `${commits.length} commits were made by ${contributors.length} contributors, `
+ statistics += `changing a total of ${Math.abs(commitStats.total)} (+${commitStats.added}|-${commitStats.deleted}) lines. `
statistics += `${opened.length} issues were opened and `
statistics += `${closed.length} issues were closed.
\n`
@@ -333,7 +337,7 @@ function createStatistics() {
statistics += `or ${avgTime(closed, 'hours')} hours.
\n`
const topUsers = freqUsers(opened, 3)
- statistics += ' Top 3 issue creators:
\n'
+ statistics += ' Top 3 issue creators:
\n'
statistics += ' \n'
for (const user in topUsers) {
statistics += ' - \n'
diff --git a/statistics.js b/statistics.js
index f6f8c69..13827b3 100644
--- a/statistics.js
+++ b/statistics.js
@@ -72,6 +72,46 @@ function labelsAlphabet(data, checkNull) {
return labelsOrdered
}
+/**
+ * @function changedLines
+ * @description Returns the number of added, deleted and total lines changed
+ * @param {Array} data Array with paths leading to GitLab Commit .json files (with stats)
+ * @returns {Object}
+ */
+function changedLines(data) {
+ const stats = {
+ added: 0,
+ deleted: 0,
+ total: 0,
+ }
+ for (const file of data) {
+ const commit = require(file.path)
+ stats.added += commit.stats.additions
+ stats.deleted += commit.stats.deletions
+ stats.total += commit.stats.additions - commit.stats.deletions
+ }
+ return stats
+}
+
+/**
+ * @function uniqueContributors
+ * @description Returns the names of all contributors
+ * @param {Array} data Array with paths leading to GitLab Commit .json files (with stats)
+ * @returns {Array}
+ */
+function uniqueContributors(data) {
+ const contributors = []
+ for (const file of data) {
+ const commit = require(file.path)
+ if (!contributors.includes(commit.author_name)) {
+ contributors.push(commit.author_name)
+ }
+ }
+ return contributors
+}
+
exports.avgTime = avgTime
exports.freqUsers = freqUsers
exports.labelsAlphabet = labelsAlphabet
+exports.changedLines = changedLines
+exports.uniqueContributors = uniqueContributors