Skip to content

Instantly share code, notes, and snippets.

@cooljith91112
Created July 20, 2018 10:04
Show Gist options
  • Select an option

  • Save cooljith91112/7b859aeb226b437bc402ce534677bb77 to your computer and use it in GitHub Desktop.

Select an option

Save cooljith91112/7b859aeb226b437bc402ce534677bb77 to your computer and use it in GitHub Desktop.
Dirty Code to Generate Excel Report of Git changed files and other stuffs
const Git = require('nodegit');
let shouldProceed = true;
let dataArray = [];
const authorEmail = "mymail@oalsdsdsdsd.com";
const lastCommitSHA = 'sdfsdfs'; // hardcoded latest git commit ID
const commitTillSHA = 'sdfsfdsfsdf'; // hardcoded commit id till we want the report for
Git.Repository.open('/home/myname/Documents/projects/mygitrepo') // your local repository path
.then((repository) => {
return repository.getCommit(lastCommitSHA); // get that commit man...
}, (error) => {
console.log('Error Getting Repository Details ', error);
}).then((commit) => {
let eventEmitter = commit.history();
eventEmitter.on('commit', (commit)=> {
if(commit.sha()===commitTillSHA) // nodegit will traverse till the end. We just want to stop pulling data beyond that.
{ // If anyone knows better way please help, :(
shouldProceed = false;
}
if(shouldProceed && commit.author().email()===authorEmail) { // Only get commit of your own
getCommitHistory(commit);
}
});
eventEmitter.on('end', (commits) =>{
generateExcelDoc();
});
eventEmitter.on('error', (error) =>{
});
eventEmitter.start()
},(error)=>{
console.log('Bleh... Some error ', error);
})
function getCommitHistory(commit){
let files = '';
commit.getDiff().then((arrayDiff) => { // Get the git diff of the commit
arrayDiff.forEach((diff) => {
diff.patches().then((patches) => {
patches.forEach((patch) => { // Patches contains all the data about files changed and other stuffs.If you go for path.hunks() there is more interesting data there
if (!patch.isAdded()) { // Check the current path file is an old file, ie changed files
files += patch.oldFile().path() + ' - File Changed\n';
} else { // ngha ha new file bro
files += patch.newFile().path() + ' - New File\n';
}
});
dataArray.push({ // this array will be later used to create out awesome Excel files
ticketID: commit.message().split(' ')[0],
commitMessage: commit.message(),
fileList: files
});
});
});
})
}
function generateExcelDoc(){
let xl = require('excel4node'); // A package I found on the wild to generate excel files ==> https://www.npmjs.com/package/excel4node
let wb = new xl.Workbook();
let ws = wb.addWorksheet("Sheet 1");
const headerStyle = wb.createStyle({
alignment:{
vertical: ['center']
},
font:{
bold: true,
vertAlign: 'center'
}
})
ws.cell(1,1).string('TICKET ID').style(headerStyle); // Header styles
ws.cell(1,2).string('Change Description').style(headerStyle);
ws.cell(1,3).string('Files Changed').style(headerStyle);
dataArray.forEach((value, idx) => {
ws.cell(idx+2,1).string(value.ticketID);
ws.cell(idx+2,2).string(value.commitMessage);
ws.cell(idx+2,3).string(value.fileList);
});
wb.write('report.xlsx'); // Our Report File
}
@cooljith91112
Copy link
Copy Markdown
Author

cooljith91112 commented Jul 20, 2018

Dependencies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment