Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Checking if security level can be set (Scriptrunner)

Henrik Mikkelsen April 5, 2017

Hi,

In a workflow post function I'm using Scriptrunner "Set security level to ...". Works fine under the conditions that the user has the right to set security level and the project has the right security scheme. Problem being it ignores the function without warnings to the user if the conditions are not met

I've tried to find a way to validate that with a Script Validator. So that you can't do the transition if this condition is not met. 

I'm not a great coder (!) and normally I find a snippet on the net and try to make it work for my needs. I haven't been able to find anything for this.

Anyone out there with an idea?

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Henning Tietgens
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 5, 2017

Hi,

this is a method that could be used for checking if a security level id is valid for an issue.

Boolean isSecurityLevelValid(Issue issue, Long securityLevelId, Logger log = null) {
    def issueSecuritySchemeManager = ComponentAccessor.getComponent(IssueSecuritySchemeManager)
    def issueSecurityLevelManager = ComponentAccessor.issueSecurityLevelManager

 def srcProject = issue.projectObject
 def issueSecurityScheme = issueSecuritySchemeManager.getSchemeFor(srcProject)

 if (issueSecurityScheme) {
  def validLevels = issueSecurityLevelManager.getIssueSecurityLevels(issueSecurityScheme.id)
  log?.debug "..validLevels-IDs: ${validLevels*.id?.dump()}"
  return validLevels*.id.contains(securityLevelId)
 } else {
  log?.debug "..Doesn't find any IssueSecurityScheme for project ${srcProject.key}"
  return false
 }
}

Henning

Henrik Mikkelsen April 5, 2017

Thank you,

I guess I'm going to have to have some imports on top of that?!

This code assumes that an Issue already exists? But if I'm going to use it in a validator in the create statement - that's probably not true?!

Henning Tietgens
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 5, 2017

Yes, you need some imports.

The projectObject of the issue is already defined for the create validator and the only thing used. So it should work.

Try this as a scripted validator after adapting SECURITY_LEVEL_ID to the correct id.

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.opensymphony.workflow.InvalidInputException

Long SECURITY_LEVEL_ID = 12345

if (!isSecurityLevelValid(issue, SECURITY_LEVEL_ID, log)) {
    invalidInputException = new InvalidInputException("Security level would not be valid.")
}

Boolean isSecurityLevelValid(Issue issue, Long securityLevelId, Logger log = null) {
    def issueSecuritySchemeManager = ComponentAccessor.getComponent(IssueSecuritySchemeManager)
    def issueSecurityLevelManager = ComponentAccessor.issueSecurityLevelManager

    def srcProject = issue.projectObject
    def issueSecurityScheme = issueSecuritySchemeManager.getSchemeFor(srcProject)

    if (issueSecurityScheme) {
        def validLevels = issueSecurityLevelManager.getIssueSecurityLevels(issueSecurityScheme.id)
        log?.debug "..validLevels-IDs: ${validLevels*.id?.dump()}"
        return validLevels*.id.contains(securityLevelId)
    } else {
        log?.debug "..Doesn't find any IssueSecurityScheme for project ${srcProject.key}"
        return false
    }
}

Henning

Henrik Mikkelsen April 5, 2017

Thank you!

It works

I put it in a simple scripted validator - so i just discarded your initial if-sentence so it returns true/false.

 

0 votes
Daniel Yelamos [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 5, 2017

Hello Henrik:

Could you please provide the code that you are using so that we have something to base our development on?

Cheers!

Henrik Mikkelsen April 5, 2017

Hi DYelamos,

Thank you for responding. But I'm afraid I don't understand your question. What i'm trying to do is finding some code that validates if a security scheme/level is available. I need to run it in the validator part of create issue, I think I can use Script Validator for this.

TAGS
AUG Leaders

Atlassian Community Events