Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: analyze team contributions per file #32

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/changes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { execSync } from 'child_process';
import fs from 'fs';

// Define the mapping of authors to teams
const authorToTeam = {
// Add list from Mobile Planning teams.json file
};

// Get the date 6 months ago
const sixMonthsAgo = new Date();
sixMonthsAgo.setMonth(sixMonthsAgo.getMonth() - 6);
const sinceDate = sixMonthsAgo.toISOString().split('T')[0];

// Get the list of commits in the past 6 months
const commits = execSync(`git log --since=${sinceDate} --pretty=format:%H`, { encoding: 'utf-8' }).split('\n');

// Dictionary to track file updates and unknown authors
const fileUpdates = {};
const unknownAuthors = {};

// Loop over each commit and get the files changed and the author
commits.forEach(commit => {
const commitInfo = execSync(`git show --pretty=format:%an --name-only ${commit}`, { encoding: 'utf-8' }).split('\n');
const author = commitInfo[0];
const filesChanged = commitInfo.slice(1);

let team = 'unknown';
if (authorToTeam[author]) {

Check failure on line 28 in src/changes.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / lint

Type 'undefined' cannot be used as an index type.
team = authorToTeam[author];

Check failure on line 29 in src/changes.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / lint

Type 'undefined' cannot be used as an index type.
} else {
// Track unknown authors
if (!unknownAuthors[author]) {

Check failure on line 32 in src/changes.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / lint

Type 'undefined' cannot be used as an index type.
unknownAuthors[author] = 0;

Check failure on line 33 in src/changes.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / lint

Type 'undefined' cannot be used as an index type.
}
unknownAuthors[author]++;

Check failure on line 35 in src/changes.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / lint

Type 'undefined' cannot be used as an index type.
}

filesChanged.forEach(file => {
if (!fileUpdates[file]) {

Check failure on line 39 in src/changes.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / lint

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
fileUpdates[file] = {};

Check failure on line 40 in src/changes.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / lint

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
}
if (!fileUpdates[file][team]) {

Check failure on line 42 in src/changes.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / lint

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
fileUpdates[file][team] = 0;

Check failure on line 43 in src/changes.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / lint

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
}
fileUpdates[file][team]++;

Check failure on line 45 in src/changes.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / lint

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
});
});

// Calculate entropy
const calculateEntropy = (teamUpdates) => {
const totalUpdates = Object.values(teamUpdates).reduce((sum, count) => sum + count, 0);
return -Object.values(teamUpdates).reduce((entropy, count) => {
const p = count / totalUpdates;
return entropy + (p * Math.log(p));
}, 0);
};

// Identify files that require architecture decoupling
const filesForDecoupling = [];

Object.entries(fileUpdates).forEach(([file, teamUpdates]) => {
if (Object.keys(teamUpdates).length > 1) {
const totalUpdates = Object.values(teamUpdates).reduce((sum, count) => sum + count, 0);
const entropy = calculateEntropy(teamUpdates);
const score = entropy * totalUpdates;
filesForDecoupling.push({ file, teamUpdates, score });
}
});

// Sort files by score in descending order
filesForDecoupling.sort((a, b) => b.score - a.score);

// Define the header row for the decoupling CSV
const headers = 'File,Score,Team,Updates\n';

// Convert `filesForDecoupling` data to CSV format
const csvRows = filesForDecoupling.flatMap(({ file, teamUpdates, score }) =>
Object.entries(teamUpdates).map(([team, count]) =>
`${file},${score.toFixed(2)},${team},${count}`
)
);

// Write the decoupling report to a CSV file
const decouplingCsvContent = headers + csvRows.join('\n');
fs.writeFileSync('decoupling_report.csv', decouplingCsvContent, 'utf8');
console.log('Decoupling CSV file has been created');

// Write the unknown authors to a separate CSV file
const unknownAuthorsCsvContent = 'Author,Commits\n' +
Object.entries(unknownAuthors)
.map(([author, commits]) => `${author},${commits}`)
.join('\n');
fs.writeFileSync('unknown_authors.csv', unknownAuthorsCsvContent, 'utf8');
console.log('Unknown authors CSV file has been created');
Loading