How to set a project property by SIL

Hello!

In this article I would like to explain how to set a project property using SIL.

SIL is a programming language for Atlassian Jira and Confluence, which can help you to automate actions in Atlassian Jira or Confluence. You can find more information here.

What is a project property?

As we know a Jira project contains such fields as Category, Project Lead, Project Name, Project URL and so on. This list is final and can not be extended easily in Jira. But what if you need to add some other field to a Jira project?

Let’s say you create an integration between Salesforce and Jira and you need to create a Jira project for each Salesforce account. Later if there is an opportunity in Salesforce for an account then you need to create an issue in the Jira project which is connected to the account of this opportunity. But how to identify which project in Jira corresponds to the required account?

We need somehow to make connection between a Jira project and a Salesforce account. We could set the name of a Jira project as the name of a Salesforce account. And then search for Jira projects where the name of a Jira project equals to the name of a Salesforce account. But what if the name of the account changed? Do we need to change the names of the projects in Jira? It is not a good solution.

Another solution to put the ID of the Salesforce account in the Project name and then search a Jira project by the ID of the Salesforce account, but in this case the name of the Jira project would be not clear. Or we could set the account id as the key of a Jira project, but in this case it would not be easy to work with such a project in Jira, because the key of a project should be easy to remember and easy to use.

What to do?

We can use project properties. Jira lets you add any number of properties to a project and then read the properties. It means that you can add a property called account id to each project and then read this property.

Sounds good! Let’s add a project property.

First of all if we have a look at the SIL language we will not find a routine to set or get project properties, that is why we need to write our own routines in Groovy.

To do it you need to install the SIL groovy connector app. It is a free app. This app lets you write groovy scripts and execute these scripts from SIL.

After you installed this app create the following files in SIL Manager:

getAccountIdForProject.groovy

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.project.property.ProjectPropertyService
import com.atlassian.jira.entity.property.EntityPropertyService
import com.atlassian.jira.entity.property.EntityProperty
import groovy.json.JsonSlurper

def jsonSlurper = new JsonSlurper()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
ProjectPropertyService projectPropertyService = ComponentAccessor.getComponent(ProjectPropertyService.class);
EntityPropertyService.PropertyResult propertyValues = projectPropertyService.getProperty(user, "${projectKey}", "sfdc"); 

if (propertyValues?.getEntityProperty().isEmpty()) {
    return ""
}else {
    def accountId = jsonSlurper.parseText(propertyValues?.getEntityProperty()?.get().getValue()).accountId
    if (accountId) {
        return accountId
    } else {
        return ""
    }
}

Here is the most important line:

EntityPropertyService.PropertyResult propertyValues = projectPropertyService.getProperty(user, "${projectKey}", "sfdc"); 

We get a project property with the key “sfdc” for the project ${projectKey}. ${projectKey} is a template. Later I will change it with a project key in my SIL script.

setAccountIdForProject.groovy

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.project.property.ProjectPropertyService
import com.atlassian.jira.entity.property.EntityPropertyService
import com.atlassian.jira.entity.property.EntityProperty
import com.atlassian.fugue.Option

def builder = new groovy.json.JsonBuilder()
def root = builder {
    accountId  ${accountId}
}
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
def project = ComponentAccessor.getProjectManager().getProjectObjByKey("${projectKey}");
ProjectPropertyService projectPropertyService = ComponentAccessor.getComponent(ProjectPropertyService.class);
EntityPropertyService.SetPropertyValidationResult result = projectPropertyService.validateSetProperty(
                user,
                project.getId(),
                new EntityPropertyService.PropertyInput(builder.toString(), "sfdc"));
  //              return result.getErrorCollection()
if (result.isValid()) {
    projectPropertyService.setProperty(user, result);
}

In this script I set a json which looks like this {“accountId” : ${accountId}} as a project property for the ${projectKey}. Later I will replace the ${accountId} and ${projectKey} with an account id and project key in my SIL script.

Now let’s create our SIL script:

test.sil

function getAccountIdForProject(string projectKey) {
    string silPath = silEnv("sil.home");
    string script = readFromTextFile(silPath + "/SFDC/groovy/getAccountIdForProject.groovy");
    script = replace(script, "${projectKey}", projectKey);
    return executeGroovyScript(script);
}

function setAccountIdForProject(string projectKey, string accountId) {
    string silPath = silEnv("sil.home");
    string script = readFromTextFile(silPath + "/SFDC/groovy/setAccountIdForProject.groovy");
    script = replace(script, "${accountId}", accountId);
    script = replace(script, "${projectKey}", projectKey);
    executeGroovyScript(script);
}

runnerLog(setAccountIdForProject("AP", "133"));
runnerLog(getAccountIdForProject("AP"));

Here is a screenshot of all created files in my SIL manager:

What s going on in test.sil?

function getAccountIdForProject(string projectKey) {
    string silPath = silEnv("sil.home");
    string script = readFromTextFile(silPath + "/SFDC/groovy/getAccountIdForProject.groovy");
    script = replace(script, "${projectKey}", projectKey);
    return executeGroovyScript(script);
}

We define a SIL function to get the account id for a Jira project:

  • we get the path to our SIL scripts (silEnv(“sil.home”))
  • we read the contents for the getAccountIdForProject.groovy file
  • we change the ${projectKey} parameter in our script with the required project key
  • we execute our script

The same way we define a SIL function to save an account id for a Jira project and then we call these two functions:

runnerLog(setAccountIdForProject("AP", "133"));
runnerLog(getAccountIdForProject("AP"));

First we set 133 as the account id for the AP project and then we read this account Id. Here is how it works:

That is it! Now you know how to set project properties in SIL.

Leave a Reply

%d bloggers like this: