Sample Snippets

This page is a work in progress. If you don't see an example that you feel should be here, please email us at support@wittified.com.

Block commits that are single word commit messages

var commits = stash.fetchCommits( project.key, repository.slug, changeSet[0].fromHash, changeSet[0].toHash);
var hasBadMessage = false;
var theBadMessage = [];
for(var i=0;i<commits.length;i++)
{
	if( commits[i].message.indexOf(' ')==-1)
    {
    	hasBadMessage = true;
        theBadMessage.push( commits[i].message );
    }
}
if( hasBadMessage)
{
	stash.sendFeedback( "Single word commits not accepted","The following commit message is rejected:\n"
    + theBadMessage.join("\n"), true);
}

 On This Page

 

 

 

Execute a command if a specific file is being changed

var commits = stash.fetchCommits( project.key, repository.slug, changeSet[0].fromHash, changeSet[0].toHash);
var executeCommand = false;

for(var i=0;i<commits.length;i++)
{
	for(var x=0;x<commits[i].filesAdded.length;x++)
	{
		if( commits[i].filesAdded[x].path=='my-special-file')
		{
			executeCommand = true;
		}
	}
	for(var x=0;x<commits[i].filesModified.length;x++)
	{
		if( commits[i].filesModified[x].path=='my-special-file')
		{
			executeCommand = true;

		}
	}
}
 
if( executeCommand )
{
	stash.executeCommand('command-to-execute.sh arg');
}
 

 

Block certain files from being pushed

function verifyFiles( files)
{
	for(var i=0;i<files.length;i++)
    {
    	if(files[i].path().endsWith('.doc'))
        {
        
        	stash.sendFeedback('Invalid file extension', files[i].path+' was in the commit. Please remove', true);
        }
    }
}
for(var c=0;c<changeSet.length;c++)
{
	var commits = stash.fetchCommits( project.key, repository.slug, changeSet[c].fromHash , changeSet[c].toHash);
    for(var commitCounter=0;commitCounter<commits.length;commitCounter++)
    {
    	verifyFiles( commits[commitCounter].filesAdded);
    	verifyFiles( commits[commitCounter].filesModified);
    	verifyFiles( commits[commitCounter].filesMoved);
    	verifyFiles( commits[commitCounter].filesCopied);
    	verifyFiles( commits[commitCounter].filesUnknown);
    }
}