This project is read-only.

Introduction

Police is a plugin which integrates itself with SVN and allows to define personalized rules to be applied before completing every commit on the repository. The software functioning is based on hooks, programs or script automatically executed in correspondence to the happening of specific events.

The purpose of this project is not to supply a functioning software adapting to everything; on the contrary, its aim is to prearrange a base solution from which to start for the realization of a personalized plugin which implements the necessary rules for the satisfaction of managerial needs, in terms of management and monitoring and for the monitoring of the sources.

Rule development

Rules represent processes executed before the completion of the commit. Each rule can execute any type of activity, from the tracing of the job carried out to the verification of the characteristics of the committing source code and, depending on the evaluations, the rule can decide if a commit can be executed or not.

Within Police there is a rule represented by an object C# that respects defined technical characteristics (generally it resides in the Rules fold contained within the project):
- It must implement IPreCommitRule interface, necessary for Validate, that is the main method requested for checking the following of the rule.
- It must be marked by the attribute PreCommitRuleAttribute, necessary for defining the rule name and its execution order (since it is possible to define more rules, this information is useful to define the execution order of the rules themselves).

The Validate method receives as inputs two parameters:
- Settings: it is an object containing all properties contained in the Configuration.xml file. As previously defined, the project is a base and this means that if necessary, it is possible to completely modify this entity structure to adapt the engine to self-requirements. In this case, it has been useful to define a configuration file within which to store data of common interest, but it should be possible to manage these same data starting from a remote configuration (i.e. SQL Server).
- ISvnChangeInfo: it contains detailed information on the current SVN transaction. From this, it is possible to trace info such as the author of the transaction, the code of the transaction and all the files that have been modified.

If the transaction has to be cancelled, the Validate method must generate an exception of the PreCommitValidationRuleException type . By executing this operation, Police annuls the transaction execution and gives a message back to the client SVN (moreover, potential following rules will not be executed).

In succession there is a simple rule that, by using all the information of the Configuration.xml file, verifies if the developer, author of the commit, is authorized to release the code:
namespace Police.Rules
{
    [PreCommitRule(RuleName, 0)]
    public sealed class CheckDevPermissionsRule : IPreCommitRule
    {
        public const String RuleName = "Check Dev Permissions";

        public void Validate(Configuration.Settings settings, Svn.ISvnChangeInfo changeInfo)
        {
            var dev = settings.Developers.FirstOrDefault(d => d.Username == changeInfo.Author);
            if (dev == null)
                throw new PreCommitRuleException(RuleName, "You are not authorized to execute this commit.");
        }
    }
}


By using SvnChangeInfo it is possible to check the comments inserted by the user. In this example it is shown how to analyze a comment in order to extract from it information that are considered to be important.
namespace Police.Rules
{
    [PreCommitRule(RuleName, 1)]
    public sealed class CheckCodeBlockRule : IPreCommitRule
    {
        public const String RuleName = "Check Code Block";


        public void Validate(Configuration.Settings settings, Svn.ISvnChangeInfo changeInfo)
        {
            if (String.IsNullOrEmpty(changeInfo.LogMessage))
                throw new PreCommitRuleException(RuleName, "Comment cannot be empty");

            var comment = changeInfo.LogMessage;
            var pattern = "CodeBlock:";
            var cbIndex = comment.IndexOf(pattern);
            if (cbIndex == -1)
                throw new PreCommitRuleException(RuleName, "CodeBlock prop cannot be empty");

            var cbEnd = comment.Substring(cbIndex).IndexOf('#');
            var cbValue = comment.Substring(pattern.Length, cbEnd);
            using (var log = new StreamWriter("CodeBlocks.log", true))
            {
                log.WriteLine(cbValue);
            }
        }
    }
}

Hook Configuration

In order for Police to function, it has to be configured within the server that hosts SVN. To complete this process, it is necessary to implement the following steps:
  • Position in the SVN installation folder
  • Open hooks folder
  • Create a new Police folder
  • Paste the output produced by Visual Studio through build process
  • Open visual SVN Server
  • Select a repository and click on the right button of the mouse; on the menu that appears, select the item Properties
  • In the appearing window, select Hooks tab
  • On the tab, double click on the item Pre-commit hook which is present in the grouping Commit
  • In the appearing window, depending on the specified path for Police (i.e. C:\svn\hooks\Police), insert the complete path of the file to be accomplished as in the following example: C:\svn\hooks\Police\Police.exe %1 %2
  • Press OK to confirm the closure of every open window in sequence.

Once these steps have been completed, the plugin is on. SVN, in correspondence to each new action of commit, will automatically activate Police.

*This document is a DRAFT. If you have any suggestion to improve the contents, do not hesitate to contact us.

I.T.S. S.r.l. - Informatica, Tecnologie e Servizi


Last edited Sep 26, 2013 at 9:11 AM by RoBYCoNTe, version 10