RULE_NAME.groovy
RULE_NAME should be substitute by your unique rule name.
A file which was created is in fact a definition of custom groovy class which looks like:
import re.hub.commithub.rest.commit.Commit import re.hub.commithub.rest.commit.CommitCheckRequest import re.hub.commithub.ruleScript.annotation.RuleParameter import re.hub.commithub.ruleScript.annotation.RuleScript import re.hub.commithub.ruleScript.annotation.RuleParameterValue import re.hub.commithub.ruleScript.CommonRuleScript @RuleScript(ruleName = "Test rule with custom params desc", ruleDescription = "Put a string here: {{aString}}, choose a field: {{field}}, provide an <b>important</b> number: {{anInt}}, check if really important: {{aBoolean}} <br> in new line date here: {{aDate}}, finally statuses: {{statuses}}") class SampleRule implements CommonRuleScript { @RuleParameter(type=RuleParameter.Type.STRING, name="aString", required=true) String aString @RuleParameter(type=RuleParameter.Type.FIELD, name="field", required=true) String field @RuleParameter(type=RuleParameter.Type.INT, name="anInt", required=true) int anInt @RuleParameter(type=RuleParameter.Type.BOOL, name="aBoolean", required=true) boolean aBoolean @RuleParameter(type=RuleParameter.Type.DATE, name="aDate", required=true) String aDate @RuleParameter(type=RuleParameter.Type.STATUSES, name="statuses", required=true) long[] statuses boolean check(CommitCheckRequest commitCheckRequest, OutputStream outputCommitMessage) { boolean commitCheckResult = true String resultMessage = "" for(Commit commit : commitCheckRequest.getCommits()){ if(!(commit.message =~ aString)){ commitCheckResult = false resultMessage+="[commit ID: "+commit.id+"] Commit message '"+commit.message+"' does not match the text: '" + aString + "' \n" } } outputCommitMessage.write(resultMessage.getBytes()) return commitCheckResult } }
The rule above checks if the every commit message matches the text provided in aString parameter. Now let's break it down into parts:
import re.hub.commithub.rest.commit.Commit import re.hub.commithub.rest.commit.CommitCheckRequest import re.hub.commithub.ruleScript.annotation.RuleParameter import re.hub.commithub.ruleScript.annotation.RuleScript import re.hub.commithub.ruleScript.annotation.RuleParameterValue import re.hub.commithub.ruleScript.CommonRuleScript
This set of imports let us work with rules but of course we can import here any Java or one of the Atlassian JIRA class supported by Commit Hub which is accessible in classloader to be used in rule body.
Used Commit Hub classes are described in section: Commit Hub classes used in custom Rule.
The list of the Atlassian JIRA classes is placed in section: List of Atlassian JIRA classes supported in custom Rule scripts.
@RuleScript(ruleName = "Test rule with custom params desc", ruleDescription = "Put a string here: {{aString}}, choose a field: {{field}}, provide an <b>important</b> number: {{anInt}}, check if really important: {{aBoolean}} <br> in new line date here: {{aDate}}, finally statuses: {{statuses}}") class SampleRule { }
In this section we are defining @RuleScript using Java Annotation with passing there attributes:
- ruleName - which will be presentent on UI as your visible Rule name
- ruleDescription - is a phrase used for parametrization which will be visible on UI. The Rule parameter names are placed in double curly braces and are automatically replaced with the correct type of parameter input fields while rendering in Ruleset configuration. Note that every variable in double curly braces needs to be defined in your groovy class.
Next we should define standard groovy class with unique name using standard groovy naming convension which is covered in groovy lang: http://groovy-lang.org/style-guide.html
@RuleParameter(type=RuleParameter.Type.STRING, name="aString", required=true) String aString @RuleParameter(type=RuleParameter.Type.FIELD, name="field", required=true) String field @RuleParameter(type=RuleParameter.Type.INT, name="anInt", required=true) int anInt @RuleParameter(type=RuleParameter.Type.BOOL, name="aBoolean", required=true) boolean aBoolean @RuleParameter(type=RuleParameter.Type.DATE, name="aDate", required=true) String aDate @RuleParameter(type=RuleParameter.Type.STATUSES, name="statuses", required=true) long[] statuses
Next, there is a declaration of class fields. If a class field is also a Rule parameter, it is annotated with @RuleParameter with three attributes:
- type
- name - used in double curly braces in Rule description
- required - true value makes the field required in Ruleset configuration form
More information about Rule parameterization and parameter type is described in Custom Rule parameterization.
boolean check(CommitCheckRequest commitCheckRequest, OutputStream outputCommitMessage) { boolean commitCheckResult = true String resultMessage = "" for(Commit commit : commitCheckRequest.getCommits()){ if(!(commit.message =~ aString)){ commitCheckResult = false resultMessage+="[commit ID: "+commit.id+"] Commit message '"+commit.message+"' does not match the text: '" + aString + "' \n" } } outputCommitMessage.write(resultMessage.getBytes()) return commitCheckResult }
The file contains a class which implements one of two Rule interfaces:
CommonRuleScript
SearchingRuleScript
Both of the interfaces require implementing check() method which is executed when rule is triggered by commit from repository. The first one states for a typical Rule and the second one for a Rule in which occurs a JQL search.
The method receives arguments:
- commitCheckRequest - an object which holds a data about commits in the current transaction/push
- outputCommitMessage - an object type of OutputStream which you can write to any messages to the committer e.g. error messages
- searchUser - is given only in SearchingRuleScript and is type of JIRA's ApplicationUser; it is the user configured in Commit Hub configuration
Method check() simply returns:
- true when rule has passed and checked commit is accepted
- false when the checked commit is rejected