JIRA workflow condition/Validator/Post Function to force PR creation if issue have commits

Hi

I need to force developer to create a pull request if they have some changes on code. I need a validator or post function to check the issues if it has any related commits and based on that ask(if validator) for PR creation or open PR creation form (if Post Function) during issue transition.
 

Are there any plugin or workarounds to achieve this?

1 answer

0 vote
Gonchik Tsymzhitov Community Champion Dec 27, 2016

Hi Ansar, 

I have written own validator learn tutorial from Atlassian Developer site. 

@JiraComponent
public class CommitValidator implements Validator {
private final ValidatorUtils validatorUtils;
private final I18nHelper i18n;
private final WorkflowActionsBean workflowActionsBean = new WorkflowActionsBean();
private static Logger log = LoggerFactory.getLogger("ru.team365.jira.plugins.jira.workflow.validator");

@Inject
public CommitValidator(ValidatorUtils validatorUtils, @ComponentImport I18nHelper i18n) {
this.validatorUtils = validatorUtils;
this.i18n = i18n;
}

public void validate(Map transientVars, Map args, PropertySet propertySet)
throws InvalidInputException, WorkflowException {
Issue issue = (Issue) transientVars.get("issue");
List<Resolution> protectedResolutions = validatorUtils.getResolutions(args);
SearchService searchService = ComponentAccessor.getComponent(SearchService.class);
User user = ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser();
String jqlQuery = String.format("issue in (%s) AND issue.property[development].commits > 0", issue.getKey());
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlQuery);
long resultsCount = 0;

if (parseResult.isValid()) {
try {
Query query = parseResult.getQuery();
resultsCount = searchService.searchCount(user, query);
if (protectedResolutions.contains(issue.getResolutionObject())) {
if (resultsCount == 0) {
throw getException(transientVars, IssueFieldConstants.RESOLUTION,
"commit-validator.missing.commits");
}
}
} catch (SearchException e) {
log.error("Error running search", e);
}
} else {
log.warn("Error parsing jqlQuery: " + parseResult.getErrors());
}
}
private InvalidInputException getException(Map transientVars, String field, String key) {
InvalidInputException invalidInputException = new InvalidInputException();
if (isFieldOnScreen(transientVars, field)) {
invalidInputException.addError(field, i18n.getText(key));
} else {
invalidInputException.addError(i18n.getText(key));
}
return invalidInputException;
}
private boolean isFieldOnScreen(Map transientVars, String field) {
if (transientVars.containsKey("descriptor") && transientVars.containsKey("actionId")) {
WorkflowDescriptor workflowDescriptor = (WorkflowDescriptor) transientVars.get("descriptor");
Integer actionId = (Integer) transientVars.get("actionId");
ActionDescriptor actionDescriptor = workflowDescriptor.getAction(actionId.intValue());
FieldScreen fieldScreen = workflowActionsBean.getFieldScreenForView(actionDescriptor);
if (fieldScreen != null) {
for (FieldScreenTab tab : fieldScreen.getTabs()) {
for (FieldScreenLayoutItem fieldScreenLayoutItem : tab.getFieldScreenLayoutItems()) {
if (field.equals(fieldScreenLayoutItem.getFieldId())) {
return true;
}
}
}
}
}
return false;
}
}

 

Now, it works. 

 

 

Hi Gonchik

Thanks for your comment, Can you provide more information about it? I'm not familiar enough with scripting in Atlassian products, so I can't resolve errors like this:

The script could not be compiled: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script6.groovy: 1: unable to resolve class Validator 
 @ line 1, column 1.
   @JiraComponent

Which Validator should I use for runnig this?

Gonchik Tsymzhitov Community Champion Dec 27, 2016

Sorry, for my miscommunication. 

This is some java plugin not groovy script. 

I have used this tutorial  http://jiradev.com/workflow-validator.html

 

 

 

 

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Posted 10 hours ago in Jira

We want to know what Jira Service Desk apps you're using!

Hi Community! My name is Amir and I’m on the Jira Service Desk product marketing team at Atlassian. Our team would love to understand how you’re leveraging our ecosystem for Jira Service Desk. Wha...

33 views 0 5
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you