I want to update the package version automatically when I create a pull request from release branch to master and after that I want whenever I merge it, the pre-merge git hook will be executed to launch another script.
pre-merge-commit:
cd my_app
node ./hooks/post-commit-version
RETVAL=$?
if [ $RETVAL -ne 0 ]
then
exit 1
fi
hooks/post-commit-version:
#!/usr/bin/env node
const exec = require('child_process').exec;
const path = require('path');
const moment = require('moment');
const fs = require('fs');
function getBranch(){
return new Promise((resolve, reject) =>{
exec(
"git branch | grep '*'",
function (err, stdout, stderr) {
if(err)reject(err)
const name = stdout.replace('* ','').replace('\n','');
resolve(name)
}
)
});
}
getBranch()
.then((branch) => {
if(branch === 'release') {
const currentDate = moment().format('YY.MM.DD')
var pathToFile = path.join(__dirname, "../package.json");
if (fs.existsSync(pathToFile)) {
const data = fs.readFileSync(pathToFile, 'utf-8')
const content = JSON.parse(data);
content.version = currentDate;
fs.writeFileSync(pathToFile, JSON.stringify(content, null, 2), 'utf8');
exec(`git add ${pathToFile}`, (err, stdout, stderr) => {
if(err) console.log(err)
console.log(stdout)
})
} else {
console.log("Cannot find file : " + pathToFile);
return;
}
}
return;
})
.catch(error => {
console.log(error)
})
When I try this locally, with pre-commit hook and execute the git commands manually, it works successfully and update the repository in github as the one I want it to be. But I'm not sure that git hooks are executed in Github server when I click the merge request button.